ABTTestUtilities.m 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FirebaseABTesting/Tests/Unit/Utilities/ABTTestUtilities.h"
  15. #import "FirebaseABTesting/Sources/Private/ABTExperimentPayload.h"
  16. NS_ASSUME_NONNULL_BEGIN
  17. @implementation ABTTestUtilities
  18. + (NSBundle *)getBundle {
  19. #if SWIFT_PACKAGE
  20. return SWIFTPM_MODULE_BUNDLE;
  21. #else
  22. return [NSBundle bundleForClass:[ABTTestUtilities class]];
  23. #endif
  24. }
  25. + (ABTExperimentPayload *)payloadFromTestFilename:(NSString *)filename {
  26. NSBundle *abtBundle = [self getBundle];
  27. NSString *testJsonDataFilePath = [abtBundle pathForResource:filename ofType:@"txt"];
  28. NSError *readTextError = nil;
  29. NSString *fileText = [[NSString alloc] initWithContentsOfFile:testJsonDataFilePath
  30. encoding:NSUTF8StringEncoding
  31. error:&readTextError];
  32. if (readTextError) {
  33. NSAssert(NO, readTextError.localizedDescription);
  34. return nil;
  35. }
  36. return [ABTExperimentPayload parseFromData:[fileText dataUsingEncoding:NSUTF8StringEncoding]];
  37. }
  38. + (NSData *)payloadJSONDataFromFile:(NSString *)filename
  39. modifiedStartTime:(nullable NSDate *)modifiedStartTime {
  40. NSBundle *abtBundle = [self getBundle];
  41. NSString *testJsonDataFilePath = [abtBundle pathForResource:filename ofType:@"txt"];
  42. NSError *readTextError = nil;
  43. NSString *fileText = [[NSString alloc] initWithContentsOfFile:testJsonDataFilePath
  44. encoding:NSUTF8StringEncoding
  45. error:&readTextError];
  46. NSData *fileData = [fileText dataUsingEncoding:kCFStringEncodingUTF8];
  47. NSError *jsonDictionaryError = nil;
  48. NSMutableDictionary *jsonDictionary =
  49. [[NSJSONSerialization JSONObjectWithData:fileData
  50. options:kNilOptions
  51. error:&jsonDictionaryError] mutableCopy];
  52. if (modifiedStartTime) {
  53. jsonDictionary[@"experimentStartTime"] =
  54. [[ABTTestUtilities class] dateStringForStartTime:modifiedStartTime];
  55. }
  56. NSError *jsonDataError = nil;
  57. return [NSJSONSerialization dataWithJSONObject:jsonDictionary
  58. options:kNilOptions
  59. error:&jsonDataError];
  60. }
  61. + (NSString *)dateStringForStartTime:(NSDate *)startTime {
  62. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  63. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
  64. [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
  65. // Locale needs to be hardcoded. See
  66. // https://developer.apple.com/library/ios/#qa/qa1480/_index.html for more details.
  67. [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  68. [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
  69. return [dateFormatter stringFromDate:startTime];
  70. }
  71. @end
  72. NS_ASSUME_NONNULL_END