-
Notifications
You must be signed in to change notification settings - Fork 27
/
TestSQLiteBookmarks.swift
731 lines (611 loc) · 36.3 KB
/
TestSQLiteBookmarks.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import Shared
@testable import Storage
import XCTest
private func getBrowserDB(filename: String, files: FileAccessor) -> BrowserDB? {
let db = BrowserDB(filename: filename, files: files)
// BrowserTable exists only to perform create/update etc. operations -- it's not
// a queryable thing that needs to stick around.
if !db.createOrUpdate(BrowserTable()) {
return nil
}
return db
}
extension SQLiteBookmarks {
var testFactory: SQLiteBookmarksModelFactory {
return SQLiteBookmarksModelFactory(bookmarks: self, direction: .Local)
}
}
// MARK: - Tests.
class TestSQLiteBookmarks: XCTestCase {
let files = MockFiles()
private func remove(path: String) {
do {
try self.files.remove(path)
} catch {}
}
override func tearDown() {
self.remove("TSQLBtestBookmarks.db")
self.remove("TSQLBtestBufferStorage.db")
self.remove("TSQLBtestLocalAndMirror.db")
self.remove("TSQLBtestRecursiveAndURLDelete.db")
self.remove("TSQLBtestUnrooted.db")
self.remove("TSQLBtestTreeBuilding.db")
super.tearDown()
}
func testBookmarks() {
guard let db = getBrowserDB("TSQLBtestBookmarks.db", files: self.files) else {
XCTFail("Unable to create browser DB.")
return
}
let bookmarks = SQLiteBookmarks(db: db)
let factory = bookmarks.testFactory
let url = "http://url1/"
let u = url.asURL!
bookmarks.addToMobileBookmarks(u, title: "Title", favicon: nil).succeeded()
let model = factory.modelForFolder(BookmarkRoots.MobileFolderGUID).value.successValue
XCTAssertEqual((model?.current[0] as? BookmarkItem)?.url, url)
XCTAssertTrue(factory.isBookmarked(url).value.successValue ?? false)
factory.removeByURL("").succeeded()
// Grab that GUID and move it into desktop bookmarks.
let guid = (model?.current[0] as! BookmarkItem).guid
// Desktop bookmarks.
XCTAssertFalse(factory.hasDesktopBookmarks().value.successValue ?? true)
let toolbar = BookmarkRoots.ToolbarFolderGUID
XCTAssertTrue(bookmarks.db.run([
"UPDATE \(TableBookmarksLocal) SET parentid = '\(toolbar)' WHERE guid = '\(guid)'",
"UPDATE \(TableBookmarksLocalStructure) SET parent = '\(toolbar)' WHERE child = '\(guid)'",
]).value.isSuccess)
XCTAssertTrue(factory.hasDesktopBookmarks().value.successValue ?? true)
}
private func createStockMirrorTree(db: BrowserDB) {
// Set up a mirror tree.
let mirrorQuery =
"INSERT INTO \(TableBookmarksMirror) (guid, type, bmkUri, title, parentid, parentName, description, tags, keyword, is_overridden, server_modified, pos) " +
"VALUES " +
"(?, \(BookmarkNodeType.Folder.rawValue), NULL, ?, ?, '', '', '', '', 0, \(NSDate.now()), NULL), " +
"(?, \(BookmarkNodeType.Folder.rawValue), NULL, ?, ?, '', '', '', '', 0, \(NSDate.now()), NULL), " +
"(?, \(BookmarkNodeType.Folder.rawValue), NULL, ?, ?, '', '', '', '', 0, \(NSDate.now()), NULL), " +
"(?, \(BookmarkNodeType.Separator.rawValue), NULL, NULL, ?, '', '', '', '', 0, \(NSDate.now()), 0), " +
"(?, \(BookmarkNodeType.Bookmark.rawValue), ?, ?, ?, '', '', '', '', 0, \(NSDate.now()), NULL), " +
"(?, \(BookmarkNodeType.Bookmark.rawValue), ?, ?, ?, '', '', '', '', 0, \(NSDate.now()), NULL), " +
"(?, \(BookmarkNodeType.Bookmark.rawValue), ?, ?, ?, '', '', '', '', 0, \(NSDate.now()), NULL), " +
"(?, \(BookmarkNodeType.Bookmark.rawValue), ?, ?, ?, '', '', '', '', 0, \(NSDate.now()), NULL), " +
"(?, \(BookmarkNodeType.Bookmark.rawValue), ?, ?, ?, '', '', '', '', 0, \(NSDate.now()), NULL) "
let mirrorArgs: Args = [
"folderAAAAAA", "AAA", BookmarkRoots.ToolbarFolderGUID,
"folderBBBBBB", "BBB", BookmarkRoots.MenuFolderGUID,
"folderCCCCCC", "CCC", "folderBBBBBB",
"separator101", "folderAAAAAA",
"bookmark1001", "http://example.org/1", "Bookmark 1", "folderAAAAAA",
"bookmark1002", "http://example.org/1", "Bookmark 1 Again", "folderAAAAAA",
"bookmark2001", "http://example.org/2", "Bookmark 2", "folderAAAAAA",
"bookmark2002", "http://example.org/2", "Bookmark 2 Again", "folderCCCCCC",
"bookmark3001", "http://example.org/3", "Bookmark 3", "folderBBBBBB",
]
let structureQuery =
"INSERT INTO \(TableBookmarksMirrorStructure) (parent, child, idx) VALUES " +
"(?, ?, ?), " +
"(?, ?, ?), " +
"(?, ?, ?), " +
"(?, ?, ?), " +
"(?, ?, ?), " +
"(?, ?, ?), " +
"(?, ?, ?), " +
"(?, ?, ?), " +
"(?, ?, ?) "
let structureArgs: Args = [
BookmarkRoots.ToolbarFolderGUID, "folderAAAAAA", 0,
BookmarkRoots.MenuFolderGUID, "folderBBBBBB", 0,
"folderAAAAAA", "bookmark1001", 0,
"folderAAAAAA", "separator101", 1,
"folderAAAAAA", "bookmark1002", 2,
"folderAAAAAA", "bookmark2001", 3,
"folderBBBBBB", "bookmark3001", 0,
"folderBBBBBB", "folderCCCCCC", 1,
"folderCCCCCC", "bookmark2002", 0,
]
db.moveLocalToMirrorForTesting() // So we have the roots.
db.run([
(sql: mirrorQuery, args: mirrorArgs),
(sql: structureQuery, args: structureArgs),
]).succeeded()
}
private func isUnknown(folder: BookmarkTreeNode, withGUID: GUID) {
switch folder {
case .Unknown(let guid):
XCTAssertEqual(withGUID, guid)
default:
XCTFail("Not an unknown with GUID \(withGUID).")
}
}
private func isNonFolder(folder: BookmarkTreeNode, withGUID: GUID) {
switch folder {
case .NonFolder(let guid):
XCTAssertEqual(withGUID, guid)
default:
XCTFail("Not a non-folder with GUID \(withGUID).")
}
}
private func isFolder(folder: BookmarkTreeNode, withGUID: GUID) {
switch folder {
case .Folder(let record):
XCTAssertEqual(withGUID, record.guid)
default:
XCTFail("Not a folder with GUID \(withGUID).")
}
}
private func areFolders(folders: [BookmarkTreeNode], withGUIDs: [GUID]) {
folders.zip(withGUIDs).forEach { (node, guid) in
self.isFolder(node, withGUID: guid)
}
}
private func assertTreeIsEmpty(treeMaybe: Maybe<BookmarkTree>) {
guard let tree = treeMaybe.successValue else {
XCTFail("Couldn't get tree!")
return
}
XCTAssertTrue(tree.orphans.isEmpty)
XCTAssertTrue(tree.deleted.isEmpty)
XCTAssertTrue(tree.isEmpty)
}
private func assertTreeContainsOnlyRoots(treeMaybe: Maybe<BookmarkTree>) {
guard let tree = treeMaybe.successValue else {
XCTFail("Couldn't get tree!")
return
}
XCTAssertTrue(tree.orphans.isEmpty)
XCTAssertTrue(tree.deleted.isEmpty)
XCTAssertFalse(tree.isEmpty)
XCTAssertEqual(1, tree.subtrees.count)
if case let .Folder(guid, children) = tree.subtrees[0] {
XCTAssertEqual(guid, "root________")
XCTAssertEqual(4, children.count)
children.forEach { child in
guard case let .Folder(_, lower) = child where lower.isEmpty else {
XCTFail("Child \(child) wasn't empty!")
return
}
}
} else {
XCTFail("Tree didn't contain root.")
}
}
func testUnrootedBufferRowsDontAppearInTrees() {
guard let db = getBrowserDB("TSQLBtestUnrooted.db", files: self.files) else {
XCTFail("Unable to create browser DB.")
return
}
let bookmarks = SQLiteBookmarks(db: db)
self.assertTreeContainsOnlyRoots(bookmarks.treeForMirror().value)
self.assertTreeIsEmpty(bookmarks.treeForBuffer().value)
self.assertTreeContainsOnlyRoots(bookmarks.treeForLocal().value)
let args: Args = [
"unrooted0001", BookmarkNodeType.Bookmark.rawValue, 0, "somefolder01", "Some Folder", "I have no folder", "http://example.org/",
"rooted000002", BookmarkNodeType.Bookmark.rawValue, 0, "somefolder02", "Some Other Folder", "I have a folder", "http://example.org/",
"somefolder02", BookmarkNodeType.Folder.rawValue, 0, BookmarkRoots.MobileFolderGUID, "Mobile Bookmarks", "Some Other Folder",
]
let now = NSDate.now()
let bufferSQL =
"INSERT INTO \(TableBookmarksBuffer) (server_modified, guid, type, is_deleted, parentid, parentName, title, bmkUri) VALUES " +
"(\(now), ?, ?, ?, ?, ?, ?, ?), " +
"(\(now), ?, ?, ?, ?, ?, ?, ?), " +
"(\(now), ?, ?, ?, ?, ?, ?, NULL)"
let bufferStructureSQL = "INSERT INTO \(TableBookmarksBufferStructure) (parent, child, idx) VALUES ('somefolder02', 'rooted000002', 0)"
db.run(bufferSQL, withArgs: args).succeeded()
db.run(bufferStructureSQL).succeeded()
let tree = bookmarks.treeForBuffer().value.successValue!
XCTAssertFalse(tree.orphans.contains("somefolder02")) // Folders are never orphans; they appear in subtrees instead.
XCTAssertFalse(tree.orphans.contains("rooted000002")) // This tree contains its parent, so it's not an orphan.
XCTAssertTrue(tree.orphans.contains("unrooted0001"))
XCTAssertEqual(Set(tree.subtrees.map { $0.recordGUID }), Set(["somefolder02"]))
}
func testTreeBuilding() {
guard let db = getBrowserDB("TSQLBtestTreeBuilding.db", files: self.files) else {
XCTFail("Unable to create browser DB.")
return
}
let bookmarks = SQLiteBookmarks(db: db)
self.assertTreeContainsOnlyRoots(bookmarks.treeForMirror().value)
self.assertTreeIsEmpty(bookmarks.treeForBuffer().value)
self.assertTreeContainsOnlyRoots(bookmarks.treeForLocal().value)
self.createStockMirrorTree(db)
self.assertTreeIsEmpty(bookmarks.treeForBuffer().value)
// Local was emptied when we moved the roots to the mirror.
self.assertTreeIsEmpty(bookmarks.treeForLocal().value)
guard let tree = bookmarks.treeForMirror().value.successValue else {
XCTFail("Couldn't get tree!")
return
}
// Mirror is no longer empty.
XCTAssertFalse(tree.isEmpty)
// There's one root.
XCTAssertEqual(1, tree.subtrees.count)
if case let .Folder(guid, children) = tree.subtrees[0] {
XCTAssertEqual("root________", guid)
XCTAssertEqual(4, children.count)
self.areFolders(children, withGUIDs: BookmarkRoots.RootChildren)
} else {
XCTFail("Root should be a folder.")
}
// Every GUID is in the tree's nodes.
["folderAAAAAA",
"folderBBBBBB",
"folderCCCCCC"].forEach {
isFolder(tree.lookup[$0]!, withGUID: $0)
}
["separator101",
"bookmark1001",
"bookmark1002",
"bookmark2001",
"bookmark2002",
"bookmark3001"].forEach {
isNonFolder(tree.lookup[$0]!, withGUID: $0)
}
let expectedCount =
BookmarkRoots.RootChildren.count + 1 + // The roots.
6 + // Non-folders.
3 // Folders.
XCTAssertEqual(expectedCount, tree.lookup.count)
// There are no orphans and no deletions.
XCTAssertTrue(tree.orphans.isEmpty)
XCTAssertTrue(tree.deleted.isEmpty)
// root________
// menu________
// folderBBBBBB
// bookmark3001
if case let .Folder(guidR, rootChildren) = tree.subtrees[0] {
XCTAssertEqual(guidR, "root________")
if case let .Folder(guidM, menuChildren) = rootChildren[0] {
XCTAssertEqual(guidM, "menu________")
if case let .Folder(guidB, bbbChildren) = menuChildren[0] {
XCTAssertEqual(guidB, "folderBBBBBB")
// BBB contains bookmark3001.
if case let .NonFolder(guidBM) = bbbChildren[0] {
XCTAssertEqual(guidBM, "bookmark3001")
} else {
XCTFail("First child of BBB should be bookmark3001.")
}
// BBB contains folderCCCCCC.
if case let .Folder(guidBF, _) = bbbChildren[1] {
XCTAssertEqual(guidBF, "folderCCCCCC")
} else {
XCTFail("Second child of BBB should be folderCCCCCC.")
}
} else {
XCTFail("First child of menu should be BBB.")
}
} else {
XCTFail("First child of root should be menu________")
}
} else {
XCTFail("First root should be root________")
}
// Add a bookmark. It'll override the folder.
bookmarks.insertBookmark("https://foo.com/".asURL!, title: "Foo", favicon: nil, intoFolder: "folderBBBBBB", withTitle: "BBB").succeeded()
let newlyInserted = db.getRecordByURL("https://foo.com/", fromTable: TableBookmarksLocal).guid
guard let local = bookmarks.treeForLocal().value.successValue else {
XCTFail("Couldn't get local tree!")
return
}
XCTAssertFalse(local.isEmpty)
XCTAssertEqual(4, local.lookup.count) // Folder, new bookmark, original two children.
XCTAssertEqual(1, local.subtrees.count)
if case let .Folder(guid, children) = local.subtrees[0] {
XCTAssertEqual("folderBBBBBB", guid)
// We have shadows of the original two children.
XCTAssertEqual(3, children.count)
self.isUnknown(children[0], withGUID: "bookmark3001")
self.isUnknown(children[1], withGUID: "folderCCCCCC")
self.isNonFolder(children[2], withGUID: newlyInserted)
} else {
XCTFail("Root should be folderBBBBBB.")
}
// Insert partial data into the buffer.
// We insert:
let bufferArgs: Args = [
// * A folder whose parent isn't present in the structure.
"ihavenoparent", BookmarkNodeType.Folder.rawValue, 0, "myparentnoexist", "No Exist", "No Parent",
// * A folder with no children.
"ihavenochildren", BookmarkNodeType.Folder.rawValue, 0, "ihavenoparent", "No Parent", "No Children",
// * A folder that meets both criteria.
"xhavenoparent", BookmarkNodeType.Folder.rawValue, 0, "myparentnoexist", "No Exist", "No Parent And No Children",
// * A changed bookmark with no parent.
"changedbookmark", BookmarkNodeType.Bookmark.rawValue, 0, "folderCCCCCC", "CCC", "I changed", "http://change.org/",
// * A deleted record.
"iwasdeleted", BookmarkNodeType.Bookmark.rawValue,
]
let now = NSDate.now()
let bufferSQL = "INSERT INTO \(TableBookmarksBuffer) (server_modified, guid, type, is_deleted, parentid, parentName, title, bmkUri) VALUES " +
"(\(now), ?, ?, ?, ?, ?, ?, NULL), " +
"(\(now), ?, ?, ?, ?, ?, ?, NULL), " +
"(\(now), ?, ?, ?, ?, ?, ?, NULL), " +
"(\(now), ?, ?, ?, ?, ?, ?, ?), " +
"(\(now), ?, ?, 1, NULL, NULL, NULL, NULL) "
let bufferStructureSQL = "INSERT INTO \(TableBookmarksBufferStructure) (parent, child, idx) VALUES (?, ?, ?)"
let bufferStructureArgs: Args = ["ihavenoparent", "ihavenochildren", 0]
db.run([(bufferSQL, bufferArgs), (bufferStructureSQL, bufferStructureArgs)]).succeeded()
// Now build the tree.
guard let partialBuffer = bookmarks.treeForBuffer().value.successValue else {
XCTFail("Couldn't get buffer tree!")
return
}
XCTAssertEqual(partialBuffer.deleted, Set<GUID>(["iwasdeleted"]))
XCTAssertEqual(partialBuffer.orphans, Set<GUID>(["changedbookmark"]))
XCTAssertEqual(partialBuffer.subtreeGUIDs, Set<GUID>(["ihavenoparent", "xhavenoparent"]))
if case let .Folder(_, children) = partialBuffer.lookup["ihavenochildren"]! {
XCTAssertTrue(children.isEmpty)
} else {
XCTFail("Couldn't look up childless folder.")
}
}
func testRecursiveAndURLDelete() {
guard let db = getBrowserDB("TSQLBtestRecursiveAndURLDelete.db", files: self.files) else {
XCTFail("Unable to create browser DB.")
return
}
let bookmarks = SQLiteBookmarks(db: db)
self.createStockMirrorTree(db)
let menuOverridden = BookmarkRoots.MenuFolderGUID
XCTAssertFalse(db.isOverridden(menuOverridden) ?? true)
func getMenuChildren() -> [GUID] {
return db.getChildrenOfFolder(BookmarkRoots.MenuFolderGUID)
}
XCTAssertEqual(["folderBBBBBB"], getMenuChildren())
// Locally add an item to the menu. This'll override the menu folder.
bookmarks.insertBookmark(NSURL(string: "http://example.com/2")!, title: "Bookmark 2 added locally", favicon: nil, intoFolder: BookmarkRoots.MenuFolderGUID, withTitle: "Bookmarks Menu").succeeded()
XCTAssertTrue(db.isOverridden(BookmarkRoots.MenuFolderGUID) ?? false)
let menuChildrenBeforeRecursiveDelete = getMenuChildren()
XCTAssertEqual(2, menuChildrenBeforeRecursiveDelete.count)
XCTAssertEqual("folderBBBBBB", menuChildrenBeforeRecursiveDelete[0])
let locallyAddedBookmark2 = menuChildrenBeforeRecursiveDelete[1]
// It's local, so it's not overridden.
// N.B., these tests all use XCTAssertEqual instead of XCTAssert{True,False} because
// the former plays well with optionals.
XCTAssertNil(db.isOverridden(locallyAddedBookmark2))
XCTAssertEqual(false, db.isLocallyDeleted(locallyAddedBookmark2))
// Now let's delete folder B and check that things that weren't deleted now are.
XCTAssertEqual(false, db.isOverridden("folderBBBBBB"))
XCTAssertNil(db.isLocallyDeleted("folderBBBBBB"))
XCTAssertEqual(false, db.isOverridden("folderCCCCCC"))
XCTAssertNil(db.isLocallyDeleted("folderCCCCCC"))
XCTAssertEqual(false, db.isOverridden("bookmark2002"))
XCTAssertNil(db.isLocallyDeleted("bookmark2002"))
XCTAssertEqual(false, db.isOverridden("bookmark3001"))
XCTAssertNil(db.isLocallyDeleted("bookmark3001"))
bookmarks.testFactory.removeByGUID("folderBBBBBB").succeeded()
XCTAssertEqual(true, db.isOverridden("folderBBBBBB"))
XCTAssertEqual(true, db.isLocallyDeleted("folderBBBBBB"))
XCTAssertEqual(true, db.isOverridden("folderCCCCCC"))
XCTAssertEqual(true, db.isLocallyDeleted("folderCCCCCC"))
XCTAssertEqual(true, db.isOverridden("bookmark2002"))
XCTAssertEqual(true, db.isLocallyDeleted("bookmark2002"))
XCTAssertEqual(true, db.isOverridden("bookmark3001"))
XCTAssertEqual(true, db.isLocallyDeleted("bookmark3001"))
// Still there.
XCTAssertNil(db.isOverridden(locallyAddedBookmark2))
XCTAssertFalse(db.isLocallyDeleted(locallyAddedBookmark2) ?? true)
let menuChildrenAfterRecursiveDelete = getMenuChildren()
XCTAssertEqual(1, menuChildrenAfterRecursiveDelete.count)
XCTAssertEqual(locallyAddedBookmark2, menuChildrenAfterRecursiveDelete[0])
// Now let's delete by URL.
XCTAssertEqual(false, db.isOverridden("bookmark1001"))
XCTAssertNil(db.isLocallyDeleted("bookmark1001"))
XCTAssertEqual(false, db.isOverridden("bookmark1002"))
XCTAssertNil(db.isLocallyDeleted("bookmark1002"))
bookmarks.testFactory.removeByURL("http://example.org/1").succeeded()
// To conclude, check the entire hierarchy.
// Menu: overridden, only the locally-added bookmark 2.
// B, 3001, C, 2002: locally deleted.
// A: overridden, children [separator101, 2001].
// No bookmarks with URL /1.
XCTAssertEqual(true, db.isOverridden(BookmarkRoots.MenuFolderGUID))
XCTAssertEqual(true, db.isOverridden("folderBBBBBB"))
XCTAssertEqual(true, db.isLocallyDeleted("folderBBBBBB"))
XCTAssertEqual(true, db.isOverridden("folderCCCCCC"))
XCTAssertEqual(true, db.isLocallyDeleted("folderCCCCCC"))
XCTAssertEqual(true, db.isOverridden("bookmark2002"))
XCTAssertEqual(true, db.isLocallyDeleted("bookmark2002"))
XCTAssertEqual(true, db.isOverridden("bookmark3001"))
XCTAssertEqual(true, db.isLocallyDeleted("bookmark3001"))
XCTAssertEqual(true, db.isOverridden("bookmark1001"))
XCTAssertEqual(true, db.isLocallyDeleted("bookmark1001"))
XCTAssertEqual(true, db.isOverridden("bookmark1002"))
XCTAssertEqual(true, db.isLocallyDeleted("bookmark1002"))
let menuChildrenAfterURLDelete = getMenuChildren()
XCTAssertEqual(1, menuChildrenAfterURLDelete.count)
XCTAssertEqual(locallyAddedBookmark2, menuChildrenAfterURLDelete[0])
XCTAssertEqual(["separator101", "bookmark2001"], db.getChildrenOfFolder("folderAAAAAA"))
}
func testLocalAndMirror() {
guard let db = getBrowserDB("TSQLBtestLocalAndMirror.db", files: self.files) else {
XCTFail("Unable to create browser DB.")
return
}
// Preconditions.
let rootGUIDs = [
BookmarkRoots.RootGUID,
BookmarkRoots.MobileFolderGUID,
BookmarkRoots.MenuFolderGUID,
BookmarkRoots.ToolbarFolderGUID,
BookmarkRoots.UnfiledFolderGUID,
]
let positioned = [
BookmarkRoots.MenuFolderGUID,
BookmarkRoots.ToolbarFolderGUID,
BookmarkRoots.UnfiledFolderGUID,
BookmarkRoots.MobileFolderGUID,
]
XCTAssertEqual(rootGUIDs, db.getGUIDs("SELECT guid FROM \(TableBookmarksLocal) ORDER BY id"))
XCTAssertEqual(positioned, db.getGUIDs("SELECT child FROM \(TableBookmarksLocalStructure) ORDER BY idx"))
XCTAssertEqual([], db.getGUIDs("SELECT guid FROM \(TableBookmarksMirror)"))
XCTAssertEqual([], db.getGUIDs("SELECT child FROM \(TableBookmarksMirrorStructure)"))
// Add a local bookmark.
let bookmarks = SQLiteBookmarks(db: db)
bookmarks.insertBookmark("http://example.org/".asURL!, title: "Example", favicon: nil, intoFolder: BookmarkRoots.MobileFolderGUID, withTitle: "The Mobile").succeeded()
let rowA = db.getRecordByURL("http://example.org/", fromTable: TableBookmarksLocal)
XCTAssertEqual(rowA.bookmarkURI, "http://example.org/")
XCTAssertEqual(rowA.title, "Example")
XCTAssertEqual(rowA.parentName, "The Mobile")
XCTAssertEqual(rootGUIDs + [rowA.guid], db.getGUIDs("SELECT guid FROM \(TableBookmarksLocal) ORDER BY id"))
XCTAssertEqual([rowA.guid], db.getGUIDs("SELECT child FROM \(TableBookmarksLocalStructure) WHERE parent = '\(BookmarkRoots.MobileFolderGUID)' ORDER BY idx"))
XCTAssertEqual(SyncStatus.New, db.getSyncStatusForGUID(rowA.guid))
// Add another. Order should be maintained.
bookmarks.insertBookmark("https://reddit.com/".asURL!, title: "Reddit", favicon: nil, intoFolder: BookmarkRoots.MobileFolderGUID, withTitle: "Mobile").succeeded()
let rowB = db.getRecordByURL("https://reddit.com/", fromTable: TableBookmarksLocal)
XCTAssertEqual(rowB.bookmarkURI, "https://reddit.com/")
XCTAssertEqual(rowB.title, "Reddit")
XCTAssertEqual(rootGUIDs + [rowA.guid, rowB.guid], db.getGUIDs("SELECT guid FROM \(TableBookmarksLocal) ORDER BY id"))
XCTAssertEqual([rowA.guid, rowB.guid], db.getGUIDs("SELECT child FROM \(TableBookmarksLocalStructure) WHERE parent = '\(BookmarkRoots.MobileFolderGUID)' ORDER BY idx"))
XCTAssertEqual(SyncStatus.New, db.getSyncStatusForGUID(rowA.guid))
XCTAssertEqual(SyncStatus.New, db.getSyncStatusForGUID(rowB.guid))
// The indices should be 0, 1.
let positions = db.getPositionsForChildrenOfParent(BookmarkRoots.MobileFolderGUID, fromTable: TableBookmarksLocalStructure)
XCTAssertEqual(positions.count, 2)
XCTAssertEqual(positions[rowA.guid], 0)
XCTAssertEqual(positions[rowB.guid], 1)
// Delete the first. sync_status was New, so the row was immediately deleted.
bookmarks.testFactory.removeByURL("http://example.org/").succeeded()
XCTAssertEqual(rootGUIDs + [rowB.guid], db.getGUIDs("SELECT guid FROM \(TableBookmarksLocal) ORDER BY id"))
XCTAssertEqual([rowB.guid], db.getGUIDs("SELECT child FROM \(TableBookmarksLocalStructure) WHERE parent = '\(BookmarkRoots.MobileFolderGUID)' ORDER BY idx"))
let positionsAfterDelete = db.getPositionsForChildrenOfParent(BookmarkRoots.MobileFolderGUID, fromTable: TableBookmarksLocalStructure)
XCTAssertEqual(positionsAfterDelete.count, 1)
XCTAssertEqual(positionsAfterDelete[rowB.guid], 0)
// Manually shuffle all of these into the mirror, as if we were fully synchronized.
db.moveLocalToMirrorForTesting()
XCTAssertEqual([], db.getGUIDs("SELECT guid FROM \(TableBookmarksLocal) ORDER BY id"))
XCTAssertEqual([], db.getGUIDs("SELECT child FROM \(TableBookmarksLocalStructure)"))
XCTAssertEqual(rootGUIDs + [rowB.guid], db.getGUIDs("SELECT guid FROM \(TableBookmarksMirror) ORDER BY id"))
XCTAssertEqual([rowB.guid], db.getGUIDs("SELECT child FROM \(TableBookmarksMirrorStructure) WHERE parent = '\(BookmarkRoots.MobileFolderGUID)' ORDER BY idx"))
let mirrorPositions = db.getPositionsForChildrenOfParent(BookmarkRoots.MobileFolderGUID, fromTable: TableBookmarksMirrorStructure)
XCTAssertEqual(mirrorPositions.count, 1)
XCTAssertEqual(mirrorPositions[rowB.guid], 0)
// Now insert a new mobile bookmark.
bookmarks.insertBookmark("https://letsencrypt.org/".asURL!, title: "Let's Encrypt", favicon: nil, intoFolder: BookmarkRoots.MobileFolderGUID, withTitle: "Mobile").succeeded()
// The mobile bookmarks folder is overridden.
XCTAssertEqual(true, db.isOverridden(BookmarkRoots.MobileFolderGUID))
// Our previous inserted bookmark is not.
XCTAssertEqual(false, db.isOverridden(rowB.guid))
let rowC = db.getRecordByURL("https://letsencrypt.org/", fromTable: TableBookmarksLocal)
// We have the old structure in the mirror.
XCTAssertEqual(rootGUIDs + [rowB.guid], db.getGUIDs("SELECT guid FROM \(TableBookmarksMirror) ORDER BY id"))
XCTAssertEqual([rowB.guid], db.getGUIDs("SELECT child FROM \(TableBookmarksMirrorStructure) WHERE parent = '\(BookmarkRoots.MobileFolderGUID)' ORDER BY idx"))
// We have the new structure in the local table.
XCTAssertEqual(Set([BookmarkRoots.MobileFolderGUID, rowC.guid]), Set(db.getGUIDs("SELECT guid FROM \(TableBookmarksLocal) ORDER BY id")))
XCTAssertEqual([rowB.guid, rowC.guid], db.getGUIDs("SELECT child FROM \(TableBookmarksLocalStructure) WHERE parent = '\(BookmarkRoots.MobileFolderGUID)' ORDER BY idx"))
// Parent is changed. The new record is New. The unmodified and deleted records aren't present.
XCTAssertNil(db.getSyncStatusForGUID(rowA.guid))
XCTAssertNil(db.getSyncStatusForGUID(rowB.guid))
XCTAssertEqual(SyncStatus.New, db.getSyncStatusForGUID(rowC.guid))
XCTAssertEqual(SyncStatus.Changed, db.getSyncStatusForGUID(BookmarkRoots.MobileFolderGUID))
// If we delete the old record, we mark it as changed, and it's no longer in the structure.
bookmarks.testFactory.removeByGUID(rowB.guid).succeeded()
XCTAssertEqual(SyncStatus.Changed, db.getSyncStatusForGUID(rowB.guid))
XCTAssertEqual([rowC.guid], db.getGUIDs("SELECT child FROM \(TableBookmarksLocalStructure) WHERE parent = '\(BookmarkRoots.MobileFolderGUID)' ORDER BY idx"))
// Add a duplicate to test multi-deletion (unstar).
bookmarks.insertBookmark("https://letsencrypt.org/".asURL!, title: "Let's Encrypt", favicon: nil, intoFolder: BookmarkRoots.MobileFolderGUID, withTitle: "Mobile").succeeded()
let guidD = db.getGUIDs("SELECT child FROM \(TableBookmarksLocalStructure) WHERE parent = '\(BookmarkRoots.MobileFolderGUID)' ORDER BY idx").last!
XCTAssertNotEqual(rowC.guid, guidD)
XCTAssertEqual(SyncStatus.New, db.getSyncStatusForGUID(guidD))
XCTAssertEqual(SyncStatus.Changed, db.getSyncStatusForGUID(BookmarkRoots.MobileFolderGUID))
// Delete by URL.
// If we delete the new records, they just go away -- there's no server version to delete.
bookmarks.testFactory.removeByURL(rowC.bookmarkURI!).succeeded()
XCTAssertNil(db.getSyncStatusForGUID(rowC.guid))
XCTAssertNil(db.getSyncStatusForGUID(guidD))
XCTAssertEqual([], db.getGUIDs("SELECT child FROM \(TableBookmarksLocalStructure) WHERE parent = '\(BookmarkRoots.MobileFolderGUID)' ORDER BY idx"))
// The mirror structure is unchanged after all this.
XCTAssertEqual(rootGUIDs + [rowB.guid], db.getGUIDs("SELECT guid FROM \(TableBookmarksMirror) ORDER BY id"))
XCTAssertEqual([rowB.guid], db.getGUIDs("SELECT child FROM \(TableBookmarksMirrorStructure) WHERE parent = '\(BookmarkRoots.MobileFolderGUID)' ORDER BY idx"))
}
/*
// This is dead test code after we eliminated the merged view.
// Expect this to be ported to reflect post-sync state.
func testBookmarkStructure() {
guard let db = getBrowserDB("TSQLBtestBufferStorage.db", files: self.files) else {
XCTFail("Unable to create browser DB.")
return
}
let bookmarks = MergedSQLiteBookmarks(db: db)
guard let model = bookmarks.modelForRoot().value.successValue else {
XCTFail("Unable to get root.")
return
}
let root = model.current
// The root isn't useful. The merged bookmark implementation doesn't use it.
XCTAssertEqual(root.guid, BookmarkRoots.RootGUID)
XCTAssertEqual(0, root.count)
// Specifically fetching the fake desktop folder works, though.
guard let desktopModel = bookmarks.modelForFolder(BookmarkRoots.FakeDesktopFolderGUID).value.successValue else {
XCTFail("Unable to get desktop bookmarks.")
return
}
// It contains the toolbar.
let desktopFolder = desktopModel.current
XCTAssertEqual(desktopFolder.guid, BookmarkRoots.FakeDesktopFolderGUID)
XCTAssertEqual(1, desktopFolder.count)
guard let toolbarModel = desktopModel.selectFolder(BookmarkRoots.ToolbarFolderGUID).value.successValue else {
XCTFail("Unable to get toolbar.")
return
}
// The toolbar is the child, and it has the two bookmarks as entries.
let toolbarFolder = toolbarModel.current
XCTAssertEqual(toolbarFolder.guid, BookmarkRoots.ToolbarFolderGUID)
XCTAssertEqual(2, toolbarFolder.count)
guard let first = toolbarModel.current[0] else {
XCTFail("Expected to get AAA.")
return
}
guard let second = toolbarModel.current[1] else {
XCTFail("Expected to get BBB.")
return
}
XCTAssertEqual(first.guid, "aaaaaaaaaaaa")
XCTAssertEqual(first.title, "AAA")
XCTAssertEqual((first as? BookmarkItem)?.url, "http://getfirefox.com")
XCTAssertEqual(second.guid, "bbbbbbbbbbbb")
XCTAssertEqual(second.title, "BBB")
XCTAssertEqual((second as? BookmarkItem)?.url, "http://getfirefox.com")
let del: [BookmarkMirrorItem] = [BookmarkMirrorItem.deleted(BookmarkNodeType.Bookmark, guid: "aaaaaaaaaaaa", modified: NSDate.now())]
bookmarks.applyRecords(del, withMaxVars: 1)
guard let newToolbar = bookmarks.modelForFolder(BookmarkRoots.ToolbarFolderGUID).value.successValue else {
XCTFail("Unable to get toolbar.")
return
}
XCTAssertEqual(newToolbar.current.count, 1)
XCTAssertEqual(newToolbar.current[0]?.guid, "bbbbbbbbbbbb")
}
*/
func testBufferStorage() {
guard let db = getBrowserDB("TSQLBtestBufferStorage.db", files: self.files) else {
XCTFail("Unable to create browser DB.")
return
}
let bookmarks = SQLiteBookmarkBufferStorage(db: db)
let record1 = BookmarkMirrorItem.bookmark("aaaaaaaaaaaa", modified: NSDate.now(), hasDupe: false, parentID: BookmarkRoots.ToolbarFolderGUID, parentName: "Bookmarks Toolbar", title: "AAA", description: "AAA desc", URI: "http://getfirefox.com", tags: "[]", keyword: nil)
let record2 = BookmarkMirrorItem.bookmark("bbbbbbbbbbbb", modified: NSDate.now() + 10, hasDupe: false, parentID: BookmarkRoots.ToolbarFolderGUID, parentName: "Bookmarks Toolbar", title: "BBB", description: "BBB desc", URI: "http://getfirefox.com", tags: "[]", keyword: nil)
let toolbar = BookmarkMirrorItem.folder("toolbar", modified: NSDate.now(), hasDupe: false, parentID: "places", parentName: "", title: "Bookmarks Toolbar", description: "Add bookmarks to this folder to see them displayed on the Bookmarks Toolbar", children: ["aaaaaaaaaaaa", "bbbbbbbbbbbb"])
let recordsA: [BookmarkMirrorItem] = [record1, toolbar, record2]
bookmarks.applyRecords(recordsA, withMaxVars: 3).succeeded()
// Insert mobile bookmarks as produced by Firefox 40-something on desktop.
let children = ["M87np9Vfh_2s","-JxRyqNte-ue","6lIQzUtbjE8O","eOg3jPSslzXl","1WJIi9EjQErp","z5uRo45Rvfbd","EK3lcNd0sUFN","gFD3GTljgu12","eRZGsbN1ew9-","widfEdgGn9de","l7eTOR4Uf6xq","vPbxG-gpN4Rb","4dwJ8CototFe","zK-kw9Ii6ScW","eDmDU-gtEFW6","lKjqWQaL_syt","ETVDvWgGT31Q","3Z_bMIHPSZQ8","Fqu4_bJOk7fT","Uo_5K1QrA67j","gDTXNg4m1AJZ","zpds8P-9xews","87zjNtVGPtEp","ZJru8Sn3qhW7","txVnzBBBOgLP","JTnRqFaj_oNa","soaMlfmM4kjR","g8AcVBjo6IRf","uPUDaiG4q637","rfq2bUud_w4d","XBGxsiuUG2UD","-VQRnJlyAvMs","6wu7TScKdTU7","ZeFji2hLVpLj","HpCn_TVizMWX","IPR5HZwRdlwi","00JFOGuWnhWB","P1jb3qKt32Vg","D6MQJ43V1Ir5","qWSoXFteRfsq","o2avfYqEdomL","xRS0U0YnjK9G","VgOgzE_xfP4w","SwP3rMJGvoO3","Hf2jEgI_-PWa","AyhmBi7Cv598","-PaMuzTJXxVk","JMhYrg8SlY5K","SQeySEjzyplL","GTAwd2UkEQEe","x3RsZj5Ilebr","sRZWZqPi74FP","amHR50TpygA6","XSk782ceVNN6","ipiMyYQzeypI","ph2k3Nqfhau4","m5JKC3hAEQ0H","yTVerkmQbNxk","7taA6FbbbUbH","PZvpbSRuJLPs","C8atoa25U94F","KOfNJk_ISLc6","Bt74lBG9tJq6","BuHoY2rUhuKA","XTmoWKnwfIPl","ZATwa3oTD1m0","e8TczN5It6Am","6kCUYs8hQtKg","jDD8s5aiKoex","QmpmcrYwLU29","nCRcekynuJ08","resttaI4J9tu","EKSX3HV55VU3","2-yCz0EIsVls","sSeeGw3VbBY-","qfpCrU34w9y0","RKDgzPWecD6m","5SgXEKu_dICW","R143WAeB5E5r","8Ns4-NiKG62r","4AHuZDvop5XX","YCP1OsO1goFF","CYYaU1mQ_N6t","UGkzEOMK8cuU","1RzZOarkzQBa","qSW2Z3cZSI9c","ooPlKEAfQsnn","jIUScoKLiXQt","bjNTKugzRRL1","hR24ZVnHUZcs","3j2IDAZgUyYi","xnWcy-sQDJRu","UCcgJqGk3bTV","WSSRWeptH9tq","4ugv47OGD2E2","XboCZgUx-x3x","HrmWqiqsuLrm","OjdxvRJ3Jb6j"]
let mA = BookmarkMirrorItem.bookmark("jIUScoKLiXQt", modified: NSDate.now(), hasDupe: false, parentID: "mobile", parentName: "mobile", title: "Join the Engineering Leisure Class — Medium", description: nil, URI: "https://medium.com/@chrisloer/join-the-engineering-leisure-class-b3083c09a78e", tags: "[]", keyword: nil)
let mB = BookmarkMirrorItem.folder("UjAHxFOGEqU8", modified: NSDate.now(), hasDupe: false, parentID: "places", parentName: "", title: "mobile", description: nil, children: children)
bookmarks.applyRecords([mA, mB]).succeeded()
func childCount(parent: GUID) -> Int? {
let sql = "SELECT COUNT(*) AS childCount FROM \(TableBookmarksBufferStructure) WHERE parent = ?"
let args: Args = [parent]
return db.runQuery(sql, args: args, factory: { $0["childCount"] as! Int }).value.successValue?[0]
}
// We have children.
XCTAssertEqual(children.count, childCount("UjAHxFOGEqU8"))
// Insert an empty mobile bookmarks folder, so we can verify that the structure table is wiped.
let mBEmpty = BookmarkMirrorItem.folder("UjAHxFOGEqU8", modified: NSDate.now() + 1, hasDupe: false, parentID: "places", parentName: "", title: "mobile", description: nil, children: [])
bookmarks.applyRecords([mBEmpty]).succeeded()
// We no longer have children.
XCTAssertEqual(0, childCount("UjAHxFOGEqU8"))
}
}