ABTTestUtilities.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 "ABTTestUtilities.h"
  15. #import "FirebaseABTesting/Sources/Private/ABTExperimentPayload.h"
  16. NS_ASSUME_NONNULL_BEGIN
  17. @implementation ABTTestUtilities
  18. + (ABTExperimentPayload *)payloadFromTestFilename:(NSString *)filename {
  19. NSString *testJsonDataFilePath =
  20. [[NSBundle bundleForClass:[ABTTestUtilities class]] pathForResource:filename ofType:@"txt"];
  21. NSError *readTextError = nil;
  22. NSString *fileText = [[NSString alloc] initWithContentsOfFile:testJsonDataFilePath
  23. encoding:NSUTF8StringEncoding
  24. error:&readTextError];
  25. if (readTextError) {
  26. NSAssert(NO, readTextError.localizedDescription);
  27. return nil;
  28. }
  29. return [ABTExperimentPayload parseFromData:[fileText dataUsingEncoding:NSUTF8StringEncoding]];
  30. }
  31. + (NSData *)payloadJSONDataFromFile:(NSString *)filename
  32. modifiedStartTime:(nullable NSDate *)modifiedStartTime {
  33. NSString *testJsonDataFilePath =
  34. [[NSBundle bundleForClass:[ABTTestUtilities class]] pathForResource:filename ofType:@"txt"];
  35. NSError *readTextError = nil;
  36. NSString *fileText = [[NSString alloc] initWithContentsOfFile:testJsonDataFilePath
  37. encoding:NSUTF8StringEncoding
  38. error:&readTextError];
  39. NSData *fileData = [fileText dataUsingEncoding:kCFStringEncodingUTF8];
  40. NSError *jsonDictionaryError = nil;
  41. NSMutableDictionary *jsonDictionary =
  42. [[NSJSONSerialization JSONObjectWithData:fileData
  43. options:kNilOptions
  44. error:&jsonDictionaryError] mutableCopy];
  45. if (modifiedStartTime) {
  46. jsonDictionary[@"experimentStartTime"] =
  47. [[ABTTestUtilities class] dateStringForStartTime:modifiedStartTime];
  48. }
  49. NSError *jsonDataError = nil;
  50. return [NSJSONSerialization dataWithJSONObject:jsonDictionary
  51. options:kNilOptions
  52. error:&jsonDataError];
  53. }
  54. + (NSString *)dateStringForStartTime:(NSDate *)startTime {
  55. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  56. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
  57. [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
  58. // Locale needs to be hardcoded. See
  59. // https://developer.apple.com/library/ios/#qa/qa1480/_index.html for more details.
  60. [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  61. [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
  62. return [dateFormatter stringFromDate:startTime];
  63. }
  64. @end
  65. NS_ASSUME_NONNULL_END