ScreenshotJsonImporter.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import <XCTest/XCTest.h>
  21. #import <ThreemaFramework/MyIdentityStore.h>
  22. #import "ScreenshotJsonParser.h"
  23. #import "UserSettings.h"
  24. #import "UserReminder.h"
  25. #import "AppGroup.h"
  26. #import "Colors.h"
  27. #import "LicenseStore.h"
  28. #import <CocoaLumberjack/CocoaLumberjack.h>
  29. #import "BundleUtil.h"
  30. #import <Photos/Photos.h>
  31. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  32. #define FULLDATE_COMPONENTS NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekOfMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond
  33. @interface ScreenshotJsonImporter : XCTestCase
  34. @property NSURL *buildURL;
  35. @property (nonatomic, strong) NSString *language;
  36. @end
  37. @implementation ScreenshotJsonImporter
  38. - (void)setUp {
  39. [super setUp];
  40. NSDictionary *environment = [[NSProcessInfo processInfo] environment];
  41. // get the path from where the tests were started
  42. NSString *frameworkPaths = environment[@"DYLD_FRAMEWORK_PATH"];
  43. NSString *buildPath = [frameworkPaths componentsSeparatedByString:@":"][0];
  44. _buildURL = [[NSURL URLWithString:buildPath] URLByDeletingLastPathComponent];
  45. NSString *prefix = [self pathPrefix];
  46. NSString *path = [prefix stringByAppendingPathComponent:@"language.txt"];
  47. NSCharacterSet *trimCharacterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
  48. _language = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  49. _language = [_language stringByTrimmingCharactersInSet:trimCharacterSet];
  50. }
  51. - (void)testLoadJsonFile {
  52. ScreenshotJsonParser *parser = [ScreenshotJsonParser new];
  53. [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  54. }];
  55. NSString *regionCode = _language;
  56. NSString *resetDevice = [self getResetCommand];
  57. if (resetDevice) {
  58. DDLogInfo(@"ScreenshotJsonImporter: resetting device");
  59. [[MyIdentityStore sharedMyIdentityStore] destroy];
  60. if ([LicenseStore requiresLicenseKey]) {
  61. [[LicenseStore sharedLicenseStore] deleteLicense];
  62. }
  63. }
  64. NSString *srcroot = [[[NSProcessInfo processInfo] environment] objectForKey:@"SRCROOT"];
  65. NSString *screenshotProject = [NSString stringWithFormat:@"screenshot/%@", [LicenseStore requiresLicenseKey] ? @"dataWork" : @"data"];
  66. NSURL *screenShotDataURL = [NSURL URLWithString:[srcroot stringByReplacingOccurrencesOfString:@"ios-client" withString:screenshotProject]];
  67. //** uncomment for manual testing
  68. // regionCode = @"de-DE";
  69. // _language = @"de";
  70. //**
  71. if (regionCode == nil) {
  72. XCTFail(@"no language code %@, probably not started from screenshot environment", regionCode);
  73. return;
  74. }
  75. if ([regionCode isEqualToString:@"pt_BR"]) {
  76. parser.languageCode = @"pt-BR";
  77. }
  78. else if ([regionCode isEqualToString:@"rm_CH"]) {
  79. parser.languageCode = @"rm-CH";
  80. }
  81. else if ([regionCode isEqualToString:@"cs_CZ"]) {
  82. parser.languageCode = @"cs-CZ";
  83. }
  84. else {
  85. parser.languageCode = [regionCode substringToIndex:2];
  86. }
  87. parser.referenceDate = [self referenceDateForHour:9];
  88. NSFileManager *fileManager = [NSFileManager defaultManager];
  89. BOOL isDir;
  90. if ([fileManager fileExistsAtPath:screenShotDataURL.path isDirectory:&isDir] && isDir) {
  91. [self setupUserSettings];
  92. [parser clearAll];
  93. [parser loadDataFromDirectory:screenShotDataURL.path];
  94. } else {
  95. XCTFail(@"json import data not found at: %@", screenShotDataURL.path);
  96. return;
  97. }
  98. }
  99. - (NSString *)pathPrefix {
  100. NSString *path = [[[NSProcessInfo processInfo] environment] objectForKey:@"SIMULATOR_HOST_HOME"];
  101. if (path)
  102. return [path stringByAppendingPathComponent:@"Library/Caches/tools.fastlane"];
  103. return nil;
  104. }
  105. - (NSString *)getColorTheme {
  106. return [self getCommandFromFile:@"colorTheme.txt"];
  107. }
  108. - (NSString *)getResetCommand {
  109. return [self getCommandFromFile:@"resetDeviceCommand.txt"];
  110. }
  111. - (NSString *)getCommandFromFile:(NSString *)fileName {
  112. NSURL *fileURL = [_buildURL URLByAppendingPathComponent:fileName];
  113. NSString *command = [NSString stringWithContentsOfFile:fileURL.path usedEncoding:nil error:nil];
  114. command = [command stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  115. return command;
  116. }
  117. - (void)setupUserSettings {
  118. UserSettings *settings = [UserSettings sharedUserSettings];
  119. settings.syncContacts = YES;
  120. settings.showReceivedTimestamps = NO;
  121. [settings setSortOrderFirstName:NO displayOrderFirstName:YES];
  122. // set some flags to avoid reminder popups
  123. [[AppGroup userDefaults] setObject:[NSDate date] forKey:@"PushReminderShowDate"];
  124. [[AppGroup userDefaults] setBool:YES forKey:@"LinkReminderShown"];
  125. [[AppGroup userDefaults] setBool:YES forKey:@"PublicNicknameReminderShown"];
  126. }
  127. - (NSDate *)referenceDateForHour:(NSInteger)hour {
  128. NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
  129. NSDate *now = [NSDate date];
  130. NSDateComponents *dateComponents = [calendar components:FULLDATE_COMPONENTS fromDate:now];
  131. dateComponents.second = 0;
  132. dateComponents.minute = 0;
  133. dateComponents.hour = 0;
  134. NSDate *beginningOfDay = [calendar dateFromComponents:dateComponents];
  135. return [beginningOfDay dateByAddingTimeInterval:(hour * 60 * 60)];
  136. }
  137. @end