RCNTestUtilities.m 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseRemoteConfig/Tests/Unit/RCNTestUtilities.h"
  17. NSString *const RCNTestsPerfNamespace = @"fireperf";
  18. NSString *const RCNTestsFIRNamespace = @"firebase";
  19. NSString *const RCNTestsDefaultFIRAppName = @"__FIRAPP_DEFAULT";
  20. NSString *const RCNTestsSecondFIRAppName = @"secondFIRApp";
  21. /// The storage sub-directory that the Remote Config database resides in.
  22. static NSString *const RCNRemoteConfigStorageSubDirectory = @"Google/RemoteConfig";
  23. @implementation RCNTestUtilities
  24. + (NSString *)generatedTestAppNameForTest:(NSString *)testName {
  25. // Filter out any characters not valid for FIRApp's naming scheme.
  26. NSCharacterSet *invalidCharacters = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
  27. // This will result in a string with the class name, a space, and the test name. We only care
  28. // about the test name so split it into components and return the last item.
  29. NSString *friendlyTestName = [testName stringByTrimmingCharactersInSet:invalidCharacters];
  30. NSArray<NSString *> *components = [friendlyTestName componentsSeparatedByString:@" "];
  31. return [components lastObject];
  32. }
  33. /// Remote Config database path for test version
  34. + (NSString *)remoteConfigPathForTestDatabase {
  35. #if TARGET_OS_TV
  36. NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  37. #else
  38. NSArray *dirPaths =
  39. NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  40. #endif
  41. NSString *storageDirPath = dirPaths.firstObject;
  42. NSArray<NSString *> *components = @[
  43. storageDirPath, RCNRemoteConfigStorageSubDirectory,
  44. [NSString stringWithFormat:@"test-%f.sqlite3", [[NSDate date] timeIntervalSince1970] * 1000]
  45. ];
  46. NSString *dbPath = [NSString pathWithComponents:components];
  47. [RCNTestUtilities removeDatabaseAtPath:dbPath];
  48. return dbPath;
  49. }
  50. + (void)removeDatabaseAtPath:(NSString *)DBPath {
  51. // Remove existing database if exists.
  52. NSFileManager *fileManager = [NSFileManager defaultManager];
  53. if ([fileManager fileExistsAtPath:DBPath]) {
  54. NSError *error;
  55. [fileManager removeItemAtPath:DBPath error:&error];
  56. }
  57. }
  58. #pragma mark UserDefaults
  59. /// Remote Config database path for test version
  60. + (NSString *)userDefaultsSuiteNameForTestSuite {
  61. NSString *suiteName =
  62. [NSString stringWithFormat:@"group.%@.test-%f", [NSBundle mainBundle].bundleIdentifier,
  63. [[NSDate date] timeIntervalSince1970] * 1000];
  64. return suiteName;
  65. }
  66. @end