|
| 1 | +/** |
| 2 | + * Modified MIT License |
| 3 | + * |
| 4 | + * Copyright 2020 OneSignal |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * 1. The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * 2. All copies of substantial portions of the Software may only be used in connection |
| 17 | + * with services provided by OneSignal. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#import <XCTest/XCTest.h> |
| 29 | +#import "OSMigrationController.h" |
| 30 | +#import "OSInfluenceDataRepository.h" |
| 31 | +#import "OSIndirectInfluence.h" |
| 32 | +#import "OSIndirectNotification.h" |
| 33 | +#import "OneSignalHelper.h" |
| 34 | +#import "UnitTestCommonMethods.h" |
| 35 | +#import "OneSignalUserDefaults.h" |
| 36 | +#import "OneSignalCommonDefines.h" |
| 37 | +#import "CommonAsserts.h" |
| 38 | + |
| 39 | +@interface MigrationTests : XCTestCase |
| 40 | +@end |
| 41 | + |
| 42 | +@implementation MigrationTests { |
| 43 | + OSMigrationController *migrationController; |
| 44 | + OSInfluenceDataRepository *dataRepository; |
| 45 | +} |
| 46 | + |
| 47 | +- (void)setUp { |
| 48 | + [super setUp]; |
| 49 | + [UnitTestCommonMethods beforeEachTest:self]; |
| 50 | + migrationController = [OSMigrationController new]; |
| 51 | + dataRepository = [OSInfluenceDataRepository new]; |
| 52 | +} |
| 53 | + |
| 54 | +- (void)testNoIndirectInfluenceDataAvailableToMigrate { |
| 55 | + NSArray *lastNotificationReceived = [dataRepository lastNotificationsReceivedData]; |
| 56 | + NSInteger sdkVersion = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0]; |
| 57 | + XCTAssertNil(lastNotificationReceived); |
| 58 | + XCTAssertEqual(0, sdkVersion); |
| 59 | + [migrationController migrate]; |
| 60 | + |
| 61 | + NSArray *lastNotificationReceivedAfterMigration = [dataRepository lastNotificationsReceivedData]; |
| 62 | + XCTAssertNil(lastNotificationReceivedAfterMigration); |
| 63 | + |
| 64 | + NSInteger sdkVersionAfterMigration = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0]; |
| 65 | + XCTAssertEqual([[OneSignal sdk_version_raw] intValue], sdkVersionAfterMigration); |
| 66 | +} |
| 67 | + |
| 68 | +- (void)testIndirectNotificationToIndirectInfluenceMigration { |
| 69 | + NSString *notificationId = @"1234-5678-1234-5678-1234"; |
| 70 | + double timestamp = 10; |
| 71 | + OSIndirectNotification *indirectNotification = [[OSIndirectNotification alloc] initWithParamsNotificationId:notificationId timestamp:timestamp]; |
| 72 | + NSMutableArray *indirectNotifications = [NSMutableArray new]; |
| 73 | + [indirectNotifications addObject:indirectNotification]; |
| 74 | + |
| 75 | + [dataRepository saveNotifications:indirectNotifications]; |
| 76 | + NSArray *lastNotificationReceived = [dataRepository lastNotificationsReceivedData]; |
| 77 | + NSInteger sdkVersion = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0]; |
| 78 | + XCTAssertEqual(1, [lastNotificationReceived count]); |
| 79 | + XCTAssertEqual(0, sdkVersion); |
| 80 | + |
| 81 | + [migrationController migrate]; |
| 82 | + |
| 83 | + NSArray<OSIndirectInfluence *> *lastNotificationReceivedAfterMigration = [dataRepository lastNotificationsReceivedData]; |
| 84 | + XCTAssertEqual(1, [lastNotificationReceivedAfterMigration count]); |
| 85 | + |
| 86 | + OSIndirectInfluence *indirectInfluence = [lastNotificationReceivedAfterMigration objectAtIndex:0]; |
| 87 | + XCTAssertTrue([indirectInfluence.influenceId isEqualToString:notificationId]); |
| 88 | + XCTAssertTrue([indirectInfluence.channelIdTag isEqualToString:@"notification_id"]); |
| 89 | + XCTAssertEqual(timestamp, indirectInfluence.timestamp); |
| 90 | + |
| 91 | + NSInteger sdkVersionAfterMigration = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0]; |
| 92 | + XCTAssertEqual([[OneSignal sdk_version_raw] intValue], sdkVersionAfterMigration); |
| 93 | +} |
| 94 | + |
| 95 | +- (void)testIndirectInfluenceToIndirectInfluenceMigration { |
| 96 | + NSString *notificationId = @"1234-5678-1234-5678-1234"; |
| 97 | + NSString *channelId = @"notification_id"; |
| 98 | + double timestamp = 10; |
| 99 | + OSIndirectInfluence *indirectInfluence = [[OSIndirectInfluence alloc] initWithParamsInfluenceId:notificationId forChannel:channelId timestamp:timestamp]; |
| 100 | + NSMutableArray *indirectNotifications = [NSMutableArray new]; |
| 101 | + [indirectNotifications addObject:indirectInfluence]; |
| 102 | + |
| 103 | + [dataRepository saveNotifications:indirectNotifications]; |
| 104 | + NSArray *lastNotificationReceived = [dataRepository lastNotificationsReceivedData]; |
| 105 | + NSInteger sdkVersion = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0]; |
| 106 | + XCTAssertEqual(1, [lastNotificationReceived count]); |
| 107 | + XCTAssertEqual(0, sdkVersion); |
| 108 | + |
| 109 | + [migrationController migrate]; |
| 110 | + |
| 111 | + NSArray<OSIndirectInfluence *> *lastNotificationReceivedAfterMigration = [dataRepository lastNotificationsReceivedData]; |
| 112 | + XCTAssertEqual(1, [lastNotificationReceivedAfterMigration count]); |
| 113 | + |
| 114 | + OSIndirectInfluence *indirectInfluenceAfterMigration = [lastNotificationReceivedAfterMigration objectAtIndex:0]; |
| 115 | + XCTAssertTrue([indirectInfluenceAfterMigration.influenceId isEqualToString:notificationId]); |
| 116 | + XCTAssertTrue([indirectInfluenceAfterMigration.channelIdTag isEqualToString:channelId]); |
| 117 | + XCTAssertEqual(timestamp, indirectInfluenceAfterMigration.timestamp); |
| 118 | + |
| 119 | + NSInteger sdkVersionAfterMigration = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0]; |
| 120 | + XCTAssertEqual([[OneSignal sdk_version_raw] intValue], sdkVersionAfterMigration); |
| 121 | +} |
| 122 | + |
| 123 | +@end |
0 commit comments