RCNTestUtilities.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 application support sub-directory that the Remote Config database resides in.
  22. static NSString *const RCNRemoteConfigApplicationSupportSubDirectory = @"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. NSArray<NSString *> *dirPaths =
  36. NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  37. NSString *appSupportPath = dirPaths.firstObject;
  38. NSArray<NSString *> *components = @[
  39. appSupportPath, RCNRemoteConfigApplicationSupportSubDirectory,
  40. [NSString stringWithFormat:@"test-%f.sqlite3", [[NSDate date] timeIntervalSince1970] * 1000]
  41. ];
  42. NSString *dbPath = [NSString pathWithComponents:components];
  43. [RCNTestUtilities removeDatabaseAtPath:dbPath];
  44. return dbPath;
  45. }
  46. + (void)removeDatabaseAtPath:(NSString *)DBPath {
  47. // Remove existing database if exists.
  48. NSFileManager *fileManager = [NSFileManager defaultManager];
  49. if ([fileManager fileExistsAtPath:DBPath]) {
  50. NSError *error;
  51. [fileManager removeItemAtPath:DBPath error:&error];
  52. }
  53. }
  54. #pragma mark UserDefaults
  55. /// Remote Config database path for test version
  56. + (NSString *)userDefaultsSuiteNameForTestSuite {
  57. NSString *suiteName =
  58. [NSString stringWithFormat:@"group.%@.test-%f", [NSBundle mainBundle].bundleIdentifier,
  59. [[NSDate date] timeIntervalSince1970] * 1000];
  60. return suiteName;
  61. }
  62. @end