Skip to content

Commit 56c3614

Browse files
maksymmalyhinpaulb777
authored andcommitted
GoogleUtilities unit tests on iOS 8 (#3093)
* GULAppDelegateSwizzlerTest iOS 8 fixes * GULRuntimeClassSnapshot - NSAssert replaced with a condition to fix implementation for iOS 8
1 parent fff9d6d commit 56c3614

File tree

2 files changed

+76
-66
lines changed

2 files changed

+76
-66
lines changed

GoogleUtilities/Example/Tests/Swizzler/GULAppDelegateSwizzlerTest.m

Lines changed: 73 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,13 @@ - (void)testNotOverriddenMethods {
642642
* handles it correctly.
643643
*/
644644
- (void)testAppDelegateInstance {
645+
// The test logic involves using KVC on the UIApplication.delegate propery. This does not really
646+
// work well with OCMPartialMock([GULApplication sharedApplication]) and triggers issue
647+
// https://github.com/erikdoe/ocmock/issues/346.
648+
// Let's stop mocking the shared application for this particular test.
649+
[self.mockSharedApplication stopMocking];
650+
self.mockSharedApplication = nil;
651+
645652
GULTestAppDelegate *realAppDelegate = [[GULTestAppDelegate alloc] init];
646653

647654
[GULApplication sharedApplication].delegate = realAppDelegate;
@@ -667,72 +674,76 @@ - (void)testAppDelegateInstance {
667674
#if TARGET_OS_IOS || TARGET_OS_TV
668675
/** Tests that application:openURL:options: is invoked on the interceptor if it exists. */
669676
- (void)testApplicationOpenURLOptionsIsInvokedOnInterceptors {
670-
id interceptor = OCMProtocolMock(@protocol(GULApplicationDelegate));
671-
OCMExpect([interceptor application:OCMOCK_ANY openURL:OCMOCK_ANY options:OCMOCK_ANY])
672-
.andReturn(NO);
673-
674-
id interceptor2 = OCMProtocolMock(@protocol(GULApplicationDelegate));
675-
OCMExpect([interceptor2 application:OCMOCK_ANY openURL:OCMOCK_ANY options:OCMOCK_ANY])
676-
.andReturn(NO);
677-
678-
NSURL *testURL = [[NSURL alloc] initWithString:@"https://www.google.com"];
679-
NSDictionary *testOpenURLOptions = @{UIApplicationOpenURLOptionUniversalLinksOnly : @"test"};
680-
681-
GULTestAppDelegate *testAppDelegate = [[GULTestAppDelegate alloc] init];
682-
OCMStub([self.mockSharedApplication delegate]).andReturn(testAppDelegate);
683-
684-
[GULAppDelegateSwizzler proxyOriginalDelegate];
685-
[GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor];
686-
[GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor2];
687-
688-
[testAppDelegate application:[GULApplication sharedApplication]
689-
openURL:testURL
690-
options:testOpenURLOptions];
691-
OCMVerifyAll(interceptor);
692-
OCMVerifyAll(interceptor2);
693-
694-
// Check that original implementation was called with proper parameters
695-
XCTAssertEqual(testAppDelegate.application, [GULApplication sharedApplication]);
696-
XCTAssertEqual(testAppDelegate.url, testURL);
677+
if (@available(iOS 10, *)) {
678+
id interceptor = OCMProtocolMock(@protocol(GULApplicationDelegate));
679+
OCMExpect([interceptor application:OCMOCK_ANY openURL:OCMOCK_ANY options:OCMOCK_ANY])
680+
.andReturn(NO);
681+
682+
id interceptor2 = OCMProtocolMock(@protocol(GULApplicationDelegate));
683+
OCMExpect([interceptor2 application:OCMOCK_ANY openURL:OCMOCK_ANY options:OCMOCK_ANY])
684+
.andReturn(NO);
685+
686+
NSURL *testURL = [[NSURL alloc] initWithString:@"https://www.google.com"];
687+
NSDictionary *testOpenURLOptions = @{UIApplicationOpenURLOptionUniversalLinksOnly : @"test"};
688+
689+
GULTestAppDelegate *testAppDelegate = [[GULTestAppDelegate alloc] init];
690+
OCMStub([self.mockSharedApplication delegate]).andReturn(testAppDelegate);
691+
692+
[GULAppDelegateSwizzler proxyOriginalDelegate];
693+
[GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor];
694+
[GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor2];
695+
696+
[testAppDelegate application:[GULApplication sharedApplication]
697+
openURL:testURL
698+
options:testOpenURLOptions];
699+
OCMVerifyAll(interceptor);
700+
OCMVerifyAll(interceptor2);
701+
702+
// Check that original implementation was called with proper parameters
703+
XCTAssertEqual(testAppDelegate.application, [GULApplication sharedApplication]);
704+
XCTAssertEqual(testAppDelegate.url, testURL);
705+
}
697706
}
698707

699708
/** Tests that the result of application:openURL:options: from all interceptors is ORed. */
700709
- (void)testResultOfApplicationOpenURLOptionsIsORed {
701-
NSURL *testURL = [[NSURL alloc] initWithString:@"https://www.google.com"];
702-
NSDictionary *testOpenURLOptions = @{UIApplicationOpenURLOptionUniversalLinksOnly : @"test"};
703-
704-
GULTestAppDelegate *testAppDelegate = [[GULTestAppDelegate alloc] init];
705-
OCMStub([self.mockSharedApplication delegate]).andReturn(testAppDelegate);
706-
[GULAppDelegateSwizzler proxyOriginalDelegate];
707-
708-
BOOL shouldOpen = [testAppDelegate application:[GULApplication sharedApplication]
709-
openURL:testURL
710-
options:testOpenURLOptions];
711-
// Verify that the original app delegate returns NO.
712-
XCTAssertFalse(shouldOpen);
713-
714-
id interceptor = OCMProtocolMock(@protocol(GULApplicationDelegate));
715-
OCMExpect([interceptor application:OCMOCK_ANY openURL:OCMOCK_ANY options:OCMOCK_ANY])
716-
.andReturn(NO);
717-
[GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor];
718-
shouldOpen = [testAppDelegate application:[GULApplication sharedApplication]
719-
openURL:testURL
720-
options:testOpenURLOptions];
721-
// Verify that if the only interceptor returns NO, the value is still NO.
722-
XCTAssertFalse(shouldOpen);
723-
724-
id interceptor2 = OCMProtocolMock(@protocol(GULApplicationDelegate));
725-
OCMExpect([interceptor2 application:OCMOCK_ANY openURL:OCMOCK_ANY options:OCMOCK_ANY])
726-
.andReturn(YES);
727-
[GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor2];
728-
729-
OCMExpect([interceptor application:OCMOCK_ANY openURL:OCMOCK_ANY options:OCMOCK_ANY])
730-
.andReturn(NO);
731-
shouldOpen = [testAppDelegate application:[GULApplication sharedApplication]
732-
openURL:testURL
733-
options:testOpenURLOptions];
734-
// Verify that if one of the two interceptors returns YES, the value is YES.
735-
XCTAssertTrue(shouldOpen);
710+
if (@available(iOS 10, *)) {
711+
NSURL *testURL = [[NSURL alloc] initWithString:@"https://www.google.com"];
712+
NSDictionary *testOpenURLOptions = @{UIApplicationOpenURLOptionUniversalLinksOnly : @"test"};
713+
714+
GULTestAppDelegate *testAppDelegate = [[GULTestAppDelegate alloc] init];
715+
OCMStub([self.mockSharedApplication delegate]).andReturn(testAppDelegate);
716+
[GULAppDelegateSwizzler proxyOriginalDelegate];
717+
718+
BOOL shouldOpen = [testAppDelegate application:[GULApplication sharedApplication]
719+
openURL:testURL
720+
options:testOpenURLOptions];
721+
// Verify that the original app delegate returns NO.
722+
XCTAssertFalse(shouldOpen);
723+
724+
id interceptor = OCMProtocolMock(@protocol(GULApplicationDelegate));
725+
OCMExpect([interceptor application:OCMOCK_ANY openURL:OCMOCK_ANY options:OCMOCK_ANY])
726+
.andReturn(NO);
727+
[GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor];
728+
shouldOpen = [testAppDelegate application:[GULApplication sharedApplication]
729+
openURL:testURL
730+
options:testOpenURLOptions];
731+
// Verify that if the only interceptor returns NO, the value is still NO.
732+
XCTAssertFalse(shouldOpen);
733+
734+
id interceptor2 = OCMProtocolMock(@protocol(GULApplicationDelegate));
735+
OCMExpect([interceptor2 application:OCMOCK_ANY openURL:OCMOCK_ANY options:OCMOCK_ANY])
736+
.andReturn(YES);
737+
[GULAppDelegateSwizzler registerAppDelegateInterceptor:interceptor2];
738+
739+
OCMExpect([interceptor application:OCMOCK_ANY openURL:OCMOCK_ANY options:OCMOCK_ANY])
740+
.andReturn(NO);
741+
shouldOpen = [testAppDelegate application:[GULApplication sharedApplication]
742+
openURL:testURL
743+
options:testOpenURLOptions];
744+
// Verify that if one of the two interceptors returns YES, the value is YES.
745+
XCTAssertTrue(shouldOpen);
746+
}
736747
}
737748
#endif // TARGET_OS_IOS || TARGET_OS_TV
738749

GoogleUtilities/SwizzlerTestHelpers/GULRuntimeClassSnapshot.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,9 @@ - (void)captureSelectorsAndImps {
132132
IMP imp = method_getImplementation(method);
133133
NSString *impString =
134134
[NSString stringWithFormat:@"%p -[%@ %@]", imp, NSStringFromClass(_aClass), methodString];
135-
NSAssert(![_imps containsObject:impString],
136-
@"This IMP/method combination has already been captured: %@:%@",
137-
NSStringFromClass(_aClass), impString);
138-
[_imps addObject:impString];
135+
if (![_imps containsObject:impString]) {
136+
[_imps addObject:impString];
137+
}
139138
_runningHash ^= [impString hash];
140139
}
141140
free(instanceMethods);

0 commit comments

Comments
 (0)