forked from ChatSecure/ChatSecure-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOTRYapViewTest.swift
91 lines (75 loc) · 3.59 KB
/
OTRYapViewTest.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
//
// OTRYapViewTest.swift
// ChatSecure
//
// Created by David Chiles on 2/23/17.
// Copyright © 2017 Chris Ballinger. All rights reserved.
//
import XCTest
import ChatSecureCore
class ViewHandlerTestDelegate:NSObject {
let didSetup: () -> Void
let didReceiveObjectChanges: (_ key:String,_ collection:String) -> Void
let didReceiveViewChanges: (_ row:[YapDatabaseViewRowChange],_ section:[YapDatabaseViewSectionChange]) -> Void
init(didSetup: @escaping () -> Void, objectChanges: @escaping (_ key:String, _ collection:String) -> Void, viewChanges: @escaping (_ row:[YapDatabaseViewRowChange], _ section:[YapDatabaseViewSectionChange]) -> Void) {
self.didSetup = didSetup
self.didReceiveObjectChanges = objectChanges
self.didReceiveViewChanges = viewChanges
}
}
extension ViewHandlerTestDelegate: OTRYapViewHandlerDelegateProtocol {
func didSetupMappings(_ handler: OTRYapViewHandler) {
self.didSetup()
}
func didReceiveChanges(_ handler: OTRYapViewHandler, key: String, collection: String) {
self.didReceiveObjectChanges(key, collection)
}
func didReceiveChanges(_ handler: OTRYapViewHandler, sectionChanges: [YapDatabaseViewSectionChange], rowChanges: [YapDatabaseViewRowChange]) {
self.didReceiveViewChanges(rowChanges,sectionChanges)
}
}
class OTRYapViewTest: XCTestCase {
var databaseManager: OTRDatabaseManager?
override func setUp() {
super.setUp()
if let databaseDirectory = databaseManager?.databaseDirectory {
FileManager.default.clearDirectory(databaseDirectory)
}
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
databaseManager = nil
super.tearDown()
}
func testViewHandlerUpdate() {
let setupExpecation = self.expectation(description: "Setup Mappings")
let viewChangeExpectation = self.expectation(description: "Insert buddy")
let databaseManager = OTRDatabaseManager()
self.databaseManager = databaseManager
databaseManager.setupTestDatabase(name: #function)
let viewHandler = OTRYapViewHandler(databaseConnection: databaseManager.longLivedReadOnlyConnection!, databaseChangeNotificationName: DatabaseNotificationName.LongLivedTransactionChanges)
//For this test we'll look at the buddy view
viewHandler.setup(OTRAllBuddiesDatabaseViewExtensionName, groups: [OTRBuddyGroup])
let delegate = ViewHandlerTestDelegate(didSetup: {
setupExpecation.fulfill()
//Once our view handler is ready we need to make a change to the database that will be reflected in the view.
self.databaseManager?.writeConnection?.asyncReadWrite({ (transaction) in
guard let account = OTRXMPPAccount(username: "[email protected]", accountType: .jabber) else {
XCTFail()
return
}
let buddy = OTRXMPPBuddy()!
buddy.username = "[email protected]"
buddy.accountUniqueId = account.uniqueId
buddy.trustLevel = .roster
account.save(with: transaction)
buddy.save(with: transaction)
})
}, objectChanges: { (key, collection) in
}) { (rowChanges, sectionChanges) in
viewChangeExpectation.fulfill()
}
viewHandler.delegate = delegate
self.waitForExpectations(timeout: 5, handler: nil)
}
}