Skip to content

Commit efd2a99

Browse files
committed
Tests
1 parent d897c77 commit efd2a99

File tree

5 files changed

+172
-2
lines changed

5 files changed

+172
-2
lines changed

PMKHomeKit.xcodeproj/project.pbxproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
/* Begin PBXBuildFile section */
1010
63C7FFF71D5C020D003BAE60 /* PMKHomeKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 63C7FFA71D5BEE09003BAE60 /* PMKHomeKit.framework */; };
11+
8312EA1521096DF000A47DDF /* HMAccessoryBrowserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8312EA1421096DF000A47DDF /* HMAccessoryBrowserTests.swift */; };
12+
8312EA19210974F600A47DDF /* UtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8312EA18210974F600A47DDF /* UtilsTests.swift */; };
1113
838C29D32103FB9700CE5309 /* HMAcessoryBrowser+Promise.swift in Sources */ = {isa = PBXBuildFile; fileRef = 838C29CE2103FB9600CE5309 /* HMAcessoryBrowser+Promise.swift */; };
1214
838C29D42103FB9700CE5309 /* HMHome+Promise.swift in Sources */ = {isa = PBXBuildFile; fileRef = 838C29CF2103FB9700CE5309 /* HMHome+Promise.swift */; };
1315
838C29D52103FB9700CE5309 /* HMCharacteristic+Promise.swift in Sources */ = {isa = PBXBuildFile; fileRef = 838C29D02103FB9700CE5309 /* HMCharacteristic+Promise.swift */; };
@@ -31,6 +33,8 @@
3133
63C7FFF21D5C020D003BAE60 /* PMKHKTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PMKHKTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3234
63CCF8121D5C0C4E00503216 /* Cartfile */ = {isa = PBXFileReference; lastKnownFileType = text; path = Cartfile; sourceTree = "<group>"; };
3335
63CCF8171D5C11B500503216 /* Carthage.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Carthage.xcconfig; sourceTree = "<group>"; };
36+
8312EA1421096DF000A47DDF /* HMAccessoryBrowserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HMAccessoryBrowserTests.swift; sourceTree = "<group>"; };
37+
8312EA18210974F600A47DDF /* UtilsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UtilsTests.swift; sourceTree = "<group>"; };
3438
838C29CE2103FB9600CE5309 /* HMAcessoryBrowser+Promise.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "HMAcessoryBrowser+Promise.swift"; sourceTree = "<group>"; };
3539
838C29CF2103FB9700CE5309 /* HMHome+Promise.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "HMHome+Promise.swift"; sourceTree = "<group>"; };
3640
838C29D02103FB9700CE5309 /* HMCharacteristic+Promise.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "HMCharacteristic+Promise.swift"; sourceTree = "<group>"; };
@@ -93,6 +97,8 @@
9397
63C7FFF31D5C020D003BAE60 /* Tests */ = {
9498
isa = PBXGroup;
9599
children = (
100+
8312EA1421096DF000A47DDF /* HMAccessoryBrowserTests.swift */,
101+
8312EA18210974F600A47DDF /* UtilsTests.swift */,
96102
);
97103
path = Tests;
98104
sourceTree = SOURCE_ROOT;
@@ -162,7 +168,7 @@
162168
};
163169
63C7FFF11D5C020D003BAE60 = {
164170
CreatedOnToolsVersion = 8.0;
165-
LastSwiftMigration = 0900;
171+
LastSwiftMigration = 0940;
166172
ProvisioningStyle = Automatic;
167173
};
168174
};
@@ -220,6 +226,8 @@
220226
isa = PBXSourcesBuildPhase;
221227
buildActionMask = 2147483647;
222228
files = (
229+
8312EA1521096DF000A47DDF /* HMAccessoryBrowserTests.swift in Sources */,
230+
8312EA19210974F600A47DDF /* UtilsTests.swift in Sources */,
223231
);
224232
runOnlyForDeploymentPostprocessing = 0;
225233
};

PMKHomeKit.xcodeproj/xcshareddata/xcschemes/PMKHomeKit.xcscheme

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@
4242
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
4343
shouldUseLaunchSchemeArgsEnv = "YES">
4444
<Testables>
45+
<TestableReference
46+
skipped = "NO">
47+
<BuildableReference
48+
BuildableIdentifier = "primary"
49+
BlueprintIdentifier = "63C7FFF11D5C020D003BAE60"
50+
BuildableName = "PMKHKTests.xctest"
51+
BlueprintName = "PMKHKTests"
52+
ReferencedContainer = "container:PMKHomeKit.xcodeproj">
53+
</BuildableReference>
54+
</TestableReference>
4555
</Testables>
4656
<MacroExpansion>
4757
<BuildableReference
@@ -65,6 +75,15 @@
6575
debugDocumentVersioning = "YES"
6676
debugServiceExtension = "internal"
6777
allowLocationSimulation = "YES">
78+
<MacroExpansion>
79+
<BuildableReference
80+
BuildableIdentifier = "primary"
81+
BlueprintIdentifier = "63C7FFA61D5BEE09003BAE60"
82+
BuildableName = "PMKHomeKit.framework"
83+
BlueprintName = "PMKHomeKit"
84+
ReferencedContainer = "container:PMKHomeKit.xcodeproj">
85+
</BuildableReference>
86+
</MacroExpansion>
6887
<AdditionalOptions>
6988
</AdditionalOptions>
7089
</LaunchAction>

Sources/HMAcessoryBrowser+Promise.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private class BrowserProxy: PromiseProxy<[HMAccessory]>, HMAccessoryBrowserDeleg
6666
*/
6767
func accessoryBrowser(_ browser: HMAccessoryBrowser, didFindNewAccessory accessory: HMAccessory) {
6868
if case .returnFirst = scanInterval {
69-
fulfill(browser.discoveredAccessories)
69+
fulfill([accessory])
7070
}
7171
}
7272
}

Tests/HMAccessoryBrowserTests.swift

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// HMAccessoryBrowserTests.swift
3+
// PMKHKTests
4+
//
5+
// Created by Chris Chares on 7/25/18.
6+
// Copyright © 2018 Max Howell. All rights reserved.
7+
//
8+
9+
import XCTest
10+
import PromiseKit
11+
import HomeKit
12+
@testable import PMKHomeKit
13+
14+
class HMAccessoryBrowserTests: XCTestCase {
15+
16+
func testBrowserScanReturningFirst() {
17+
swizzle(HMAccessoryBrowser.self, #selector(HMAccessoryBrowser.startSearchingForNewAccessories)) {
18+
let ex = expectation(description: "")
19+
20+
HMPromiseAccessoryBrowser().start(scanInterval: .returnFirst(timeout: 0.5))
21+
.done { accessories in
22+
XCTAssertEqual(accessories.count, 1)
23+
ex.fulfill()
24+
}
25+
26+
waitForExpectations(timeout: 1, handler: nil)
27+
}
28+
}
29+
30+
func testBrowserScanReturningTimeout() {
31+
let ex = expectation(description: "")
32+
33+
HMPromiseAccessoryBrowser().start(scanInterval: .returnFirst(timeout: 0.5))
34+
.catch { error in
35+
// Why would we have discovered anything?
36+
ex.fulfill()
37+
}
38+
39+
waitForExpectations(timeout: 1, handler: nil)
40+
}
41+
}
42+
43+
extension HMAccessoryBrowser {
44+
@objc func pmk_startSearchingForNewAccessories() {
45+
after(.milliseconds(100))
46+
.done { swag in
47+
self.delegate!.accessoryBrowser?(self, didFindNewAccessory: MockAccessory())
48+
}
49+
}
50+
}
51+
52+
/// Mocks
53+
class MockAccessory: HMAccessory {
54+
var _uniqueID: UUID = UUID()
55+
override var uniqueIdentifier: UUID { return _uniqueID }
56+
57+
override init() {
58+
super.init()
59+
}
60+
}
61+
62+
// Utilty taken from https://github.com/PromiseKit/CoreLocation/blob/master/Tests/CLLocationManagerTests.swift
63+
import ObjectiveC
64+
65+
func swizzle(_ foo: AnyClass, _ from: Selector, isClassMethod: Bool = false, body: () -> Void) {
66+
let originalMethod: Method
67+
let swizzledMethod: Method
68+
69+
if isClassMethod {
70+
originalMethod = class_getClassMethod(foo, from)!
71+
swizzledMethod = class_getClassMethod(foo, Selector("pmk_\(from)"))!
72+
} else {
73+
originalMethod = class_getInstanceMethod(foo, from)!
74+
swizzledMethod = class_getInstanceMethod(foo, Selector("pmk_\(from)"))!
75+
}
76+
77+
method_exchangeImplementations(originalMethod, swizzledMethod)
78+
body()
79+
method_exchangeImplementations(swizzledMethod, originalMethod)
80+
}

Tests/UtilsTests.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// UtilsTests.swift
3+
// PMKHKTests
4+
//
5+
// Created by Chris Chares on 7/25/18.
6+
// Copyright © 2018 Max Howell. All rights reserved.
7+
//
8+
9+
import XCTest
10+
import PromiseKit
11+
@testable import PMKHomeKit
12+
13+
class UtilsTests: XCTestCase {
14+
15+
var strongProxy: PromiseProxy<Int>? = PromiseProxy()
16+
17+
override func setUp() {
18+
strongProxy = PromiseProxy()
19+
}
20+
21+
override func tearDown() {
22+
strongProxy = nil
23+
}
24+
25+
// The proxy should create a retain cycle until the promise is resolved
26+
func testRetainCycle() {
27+
weak var weakVar = strongProxy
28+
XCTAssertNotNil(weakVar)
29+
30+
let exp = expectation(description: "")
31+
strongProxy = nil
32+
after(.milliseconds(50))
33+
.done {
34+
XCTAssertNotNil(weakVar)
35+
exp.fulfill()
36+
}
37+
waitForExpectations(timeout: 1.0, handler: nil)
38+
}
39+
40+
// Once resolved, the proxy should break the retain cycle
41+
func testRelease() {
42+
weak var weakVar = strongProxy
43+
XCTAssertNotNil(weakVar)
44+
45+
let exp = expectation(description: "")
46+
strongProxy!.fulfill(42)
47+
strongProxy = nil
48+
49+
after(.milliseconds(50))
50+
.done {
51+
XCTAssertNil(weakVar)
52+
exp.fulfill()
53+
}
54+
waitForExpectations(timeout: 1.0, handler: nil)
55+
}
56+
57+
// Cancel should reject with a PMKError
58+
func testCancel() {
59+
let proxy = strongProxy!
60+
proxy.cancel()
61+
XCTAssertNotNil(proxy.promise.error)
62+
}
63+
}

0 commit comments

Comments
 (0)