FIRCoreDiagnosticsDateFileStorageTests.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 <XCTest/XCTest.h>
  17. #import "FIRCDLibrary/FIRCoreDiagnosticsDateFileStorage.h"
  18. @interface FIRCoreDiagnosticsDateFileStorageTests : XCTestCase
  19. @property(nonatomic) NSURL *fileURL;
  20. @property(nonatomic) FIRCoreDiagnosticsDateFileStorage *storage;
  21. @end
  22. @implementation FIRCoreDiagnosticsDateFileStorageTests
  23. - (void)setUp {
  24. NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(
  25. NSApplicationSupportDirectory, NSUserDomainMask, YES) firstObject];
  26. XCTAssertNotNil(documentsPath);
  27. NSURL *documentsURL = [NSURL fileURLWithPath:documentsPath];
  28. self.fileURL = [documentsURL URLByAppendingPathComponent:@"FIRDiagnosticsDateFileStorageTests"
  29. isDirectory:NO];
  30. NSError *error;
  31. if (![documentsURL checkResourceIsReachableAndReturnError:&error]) {
  32. XCTAssert([[NSFileManager defaultManager] createDirectoryAtURL:documentsURL
  33. withIntermediateDirectories:YES
  34. attributes:nil
  35. error:&error],
  36. @"Error: %@", error);
  37. }
  38. self.storage = [[FIRCoreDiagnosticsDateFileStorage alloc] initWithFileURL:self.fileURL];
  39. }
  40. - (void)tearDown {
  41. [[NSFileManager defaultManager] removeItemAtURL:self.fileURL error:nil];
  42. self.fileURL = nil;
  43. self.storage = nil;
  44. }
  45. - (void)testDateStorage {
  46. NSDate *dateToSave = [NSDate date];
  47. XCTAssertNil([self.storage date]);
  48. NSError *error;
  49. XCTAssertTrue([self.storage setDate:dateToSave error:&error]);
  50. XCTAssertEqualObjects([self.storage date], dateToSave);
  51. XCTAssertTrue([self.storage setDate:nil error:&error]);
  52. XCTAssertNil([self.storage date]);
  53. }
  54. - (void)testDateIsStoredToFileSystem {
  55. NSDate *date = [NSDate date];
  56. NSError *error;
  57. XCTAssert([self.storage setDate:date error:&error], @"Error: %@", error);
  58. FIRCoreDiagnosticsDateFileStorage *anotherStorage =
  59. [[FIRCoreDiagnosticsDateFileStorage alloc] initWithFileURL:self.fileURL];
  60. XCTAssertEqualObjects([anotherStorage date], date);
  61. }
  62. @end