Skip to content

Commit 1651a8c

Browse files
authored
Xcode9 unittest fix (#294)
* Fixed crashing when starting tests on Xcode 9 * Fixed failing tests * Updated Travis to Xcode9 + iOS 11
1 parent d721e72 commit 1651a8c

File tree

2 files changed

+48
-36
lines changed

2 files changed

+48
-36
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
language: objective-c
2-
osx_image: xcode8.2
2+
osx_image: xcode9
33
before_install:
44
- cd iOS_SDK/OneSignalSDK
55
script:
66
- xcodebuild -list
77
- xcodebuild build -scheme OneSignal-Dynamic
8-
- xcodebuild -scheme UnitTests -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPad Air,OS=10.2' test
8+
- xcodebuild -scheme UnitTests -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPad Air,OS=11.0' test

iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#import <CoreData/CoreData.h>
1515
#import <UserNotifications/UserNotifications.h>
1616

17+
//#import <MobileCoreServices/LSBundleProxy.h>
18+
1719

1820
#import "UncaughtExceptionHandler.h"
1921

@@ -184,12 +186,14 @@ @interface NSUserDefaultsOverrider : NSObject
184186
@implementation NSUserDefaultsOverrider
185187
+ (void)load {
186188
defaultsDictionary = [[NSMutableDictionary alloc] init];
187-
189+
190+
// Sets
188191
injectToProperClass(@selector(overrideSetObject:forKey:), @selector(setObject:forKey:), @[], [NSUserDefaultsOverrider class], [NSUserDefaults class]);
189192
injectToProperClass(@selector(overrideSetString:forKey:), @selector(setString:forKey:), @[], [NSUserDefaultsOverrider class], [NSUserDefaults class]);
190193
injectToProperClass(@selector(overrideSetDouble:forKey:), @selector(setDouble:forKey:), @[], [NSUserDefaultsOverrider class], [NSUserDefaults class]);
191194
injectToProperClass(@selector(overrideSetBool:forKey:), @selector(setBool:forKey:), @[], [NSUserDefaultsOverrider class], [NSUserDefaults class]);
192195

196+
// Gets
193197
injectToProperClass(@selector(overrideObjectForKey:), @selector(objectForKey:), @[], [NSUserDefaultsOverrider class], [NSUserDefaults class]);
194198
injectToProperClass(@selector(overrideStringForKey:), @selector(stringForKey:), @[], [NSUserDefaultsOverrider class], [NSUserDefaults class]);
195199
injectToProperClass(@selector(overrideDoubleForKey:), @selector(doubleForKey:), @[], [NSUserDefaultsOverrider class], [NSUserDefaults class]);
@@ -217,23 +221,32 @@ - (void)overrideSetBool:(BOOL)value forKey:(NSString*)key {
217221
defaultsDictionary[key] = [NSNumber numberWithBool:value];
218222
}
219223

224+
- (void)overrideSetInteger:(NSInteger)value forKey:(NSString*)key {
225+
defaultsDictionary[key] = [NSNumber numberWithInteger:value];
226+
}
227+
220228
// Gets
221-
- (id)overrideObjectForKey:(NSString*)key {
229+
- (nullable id)overrideObjectForKey:(NSString*)key {
230+
if ([key isEqualToString:@"XCTIDEConnectionTimeout"])
231+
return [NSNumber numberWithInt:60];
232+
222233
return defaultsDictionary[key];
223234
}
224235

225236
- (NSString*)overrideStringForKey:(NSString*)key {
226237
return defaultsDictionary[key];
227238
}
228239

229-
-( double)overrideDoubleForKey:(NSString*)key {
240+
-(double)overrideDoubleForKey:(NSString*)key {
241+
if ([key isEqualToString:@"XCTIDEConnectionTimeout"])
242+
return 60.0;
243+
230244
return [defaultsDictionary[key] doubleValue];
231245
}
232246

233247
- (BOOL)overrideBoolForKey:(NSString*)key {
234248
return [defaultsDictionary[key] boolValue];
235249
}
236-
237250
@end
238251

239252
@interface NSDataOverrider : NSObject
@@ -271,39 +284,25 @@ - (NSTimeInterval) overrideTimeIntervalSince1970 {
271284

272285
@end
273286

274-
275287
@interface NSBundleOverrider : NSObject
276288
@end
277289
@implementation NSBundleOverrider
278290

279291
static NSDictionary* nsbundleDictionary;
280292

281293
+ (void)load {
282-
[NSBundleOverrider sizzleBundleIdentifier];
283-
294+
injectToProperClass(@selector(overrideBundleIdentifier), @selector(bundleIdentifier), @[], [NSBundleOverrider class], [NSBundle class]);
295+
284296
injectToProperClass(@selector(overrideObjectForInfoDictionaryKey:), @selector(objectForInfoDictionaryKey:), @[], [NSBundleOverrider class], [NSBundle class]);
285297
injectToProperClass(@selector(overrideURLForResource:withExtension:), @selector(URLForResource:withExtension:), @[], [NSBundleOverrider class], [NSBundle class]);
286298

287299
// Doesn't work to swizzle for mocking. Both an NSDictionary and NSMutableDictionarys both throw odd selecotor not found errors.
288300
// injectToProperClass(@selector(overrideInfoDictionary), @selector(infoDictionary), @[], [NSBundleOverrider class], [NSBundle class]);
289301
}
290302

291-
+ (void)sizzleBundleIdentifier {
292-
injectToProperClass(@selector(overrideBundleIdentifier), @selector(bundleIdentifier), @[], [NSBundleOverrider class], [NSBundle class]);
293-
}
294-
295303
- (NSString*)overrideBundleIdentifier {
296304
return @"com.onesignal.unittest";
297305
}
298-
299-
- (NSURL*)overrideBundleURL {
300-
NSURL* url = [NSURL URLWithString:@"file:///Users/hiro/Library/Developer/CoreSimulator/Devices/63A47DBE-D6F7-4BCF-82C4-5285C91CB22C/data/Containers/Bundle/Application/D5FDD051-990C-426E-89B1-E4C51429D29D/OneSignalDevApp.app/"];
301-
302-
// NSURL* url = [self overrideBundleURL];
303-
NSLog(@"url: %@", url);
304-
305-
return url;
306-
}
307306

308307
- (nullable id)overrideObjectForInfoDictionaryKey:(NSString*)key {
309308
return nsbundleDictionary[key];
@@ -373,6 +372,10 @@ + (void)load {
373372

374373
unNotifiserialQueue = dispatch_queue_create("com.UNNotificationCenter", DISPATCH_QUEUE_SERIAL);
375374

375+
injectToProperClass(@selector(overrideInitWithBundleProxy:),
376+
@selector(initWithBundleProxy:), @[],
377+
[UNUserNotificationCenterOverrider class], [UNUserNotificationCenter class]);
378+
376379
injectToProperClass(@selector(overrideInitWithBundleIdentifier:),
377380
@selector(initWithBundleIdentifier:), @[],
378381
[UNUserNotificationCenterOverrider class], [UNUserNotificationCenter class]);
@@ -390,6 +393,11 @@ + (void)load {
390393
[UNUserNotificationCenterOverrider class], [UNUserNotificationCenter class]);
391394
}
392395

396+
// Called internally by currentNotificationCenter
397+
- (id) overrideInitWithBundleProxy:(id)arg1 {
398+
return self;
399+
}
400+
393401
- (id) overrideInitWithBundleIdentifier:(NSString*) bundle {
394402
return self;
395403
}
@@ -472,19 +480,21 @@ - (void) override_run {
472480
}
473481

474482
+ (void)helperCallDidRegisterForRemoteNotificationsWithDeviceToken {
475-
id app = [UIApplication sharedApplication];
476-
id appDelegate = [[UIApplication sharedApplication] delegate];
477-
478-
if (didFailRegistarationErrorCode) {
479-
id error = [NSError errorWithDomain:@"any" code:didFailRegistarationErrorCode userInfo:nil];
480-
[appDelegate application:app didFailToRegisterForRemoteNotificationsWithError:error];
481-
return;
482-
}
483-
484-
if (!shouldFireDeviceToken)
485-
return;
486-
487-
pendingRegiseterBlock = true;
483+
dispatch_async(dispatch_get_main_queue(), ^{
484+
id app = [UIApplication sharedApplication];
485+
id appDelegate = [[UIApplication sharedApplication] delegate];
486+
487+
if (didFailRegistarationErrorCode) {
488+
id error = [NSError errorWithDomain:@"any" code:didFailRegistarationErrorCode userInfo:nil];
489+
[appDelegate application:app didFailToRegisterForRemoteNotificationsWithError:error];
490+
return;
491+
}
492+
493+
if (!shouldFireDeviceToken)
494+
return;
495+
496+
pendingRegiseterBlock = true;
497+
});
488498
}
489499

490500
+ (void)callPendingApplicationDidRegisterForRemoteNotificaitonsWithDeviceToken {
@@ -651,7 +661,7 @@ @implementation UIAlertViewOverrider
651661

652662
+ (void)load {
653663
injectToProperClass(@selector(overrideAddButtonWithTitle:), @selector(addButtonWithTitle:), @[], [UIAlertViewOverrider class], [UIAlertView class]);
654-
664+
655665
injectToProperClass(@selector(overrideInitWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:),
656666
@selector(initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:), @[],
657667
[UIAlertViewOverrider class], [UIAlertView class]);
@@ -908,6 +918,8 @@ - (void)runBackgroundThreads {
908918

909919
NSLog(@"START runBackgroundThreads");
910920

921+
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
922+
911923
dispatch_queue_t registerUserQueue, notifSettingsQueue;
912924
for(int i = 0; i < 10; i++) {
913925
dispatch_sync(serialMockMainLooper, ^{});

0 commit comments

Comments
 (0)