HeartbeatLoggingTestUtils.swift 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2021 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 Foundation
  15. import XCTest
  16. import FirebaseCoreInternal
  17. @objc(FIRHeartbeatLoggingTestUtils)
  18. @objcMembers
  19. public class HeartbeatLoggingTestUtils: NSObject {
  20. /// This should mirror the `Constants` enum in the `HeartbeatLogging` module.
  21. /// See `HeartbeatLogging/Sources/StorageFactory.swift`.
  22. public enum Constants {
  23. /// The name of the file system directory where heartbeat data is stored.
  24. public static let heartbeatFileStorageDirectoryPath = "google-heartbeat-storage"
  25. /// The name of the user defaults suite where heartbeat data is stored.
  26. public static let heartbeatUserDefaultsSuiteName = "com.google.heartbeat.storage"
  27. }
  28. public static var dateFormatter: DateFormatter {
  29. HeartbeatsPayload.dateFormatter
  30. }
  31. @objc(assertEncodedPayloadString:isEqualToLiteralString:withError:)
  32. public static func assertEqualPayloadStrings(_ encoded: String, _ literal: String) throws {
  33. var encodedData = try XCTUnwrap(Data(base64URLEncoded: encoded))
  34. if encodedData.count > 0 {
  35. encodedData = try! encodedData.unzipped()
  36. }
  37. let literalData = try XCTUnwrap(literal.data(using: .utf8))
  38. let decoder = JSONDecoder()
  39. decoder.dateDecodingStrategy = .formatted(HeartbeatsPayload.dateFormatter)
  40. let payloadFromEncoded = try? decoder.decode(HeartbeatsPayload.self, from: encodedData)
  41. let payloadFromLiteral = try? decoder.decode(HeartbeatsPayload.self, from: literalData)
  42. let encoder = JSONEncoder()
  43. encoder.dateEncodingStrategy = .formatted(HeartbeatsPayload.dateFormatter)
  44. encoder.outputFormatting = .prettyPrinted
  45. let payloadDataFromEncoded = try XCTUnwrap(encoder.encode(payloadFromEncoded))
  46. let payloadDataFromLiteral = try XCTUnwrap(encoder.encode(payloadFromLiteral))
  47. XCTAssertEqual(
  48. payloadFromEncoded,
  49. payloadFromLiteral,
  50. """
  51. Mismatched payloads!
  52. Payload 1:
  53. \(String(data: payloadDataFromEncoded, encoding: .utf8) ?? "")
  54. Payload 2:
  55. \(String(data: payloadDataFromLiteral, encoding: .utf8) ?? "")
  56. """
  57. )
  58. }
  59. /// Removes all underlying storage containers used by the module.
  60. /// - Throws: An error if the storage container could not be removed.
  61. public static func removeUnderlyingHeartbeatStorageContainers() throws {
  62. #if os(tvOS)
  63. UserDefaults().removePersistentDomain(forName: Constants.heartbeatUserDefaultsSuiteName)
  64. #else
  65. let applicationSupportDirectory = try XCTUnwrap(
  66. FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
  67. )
  68. let heartbeatsDirectoryURL = applicationSupportDirectory
  69. .appendingPathComponent(
  70. Constants.heartbeatFileStorageDirectoryPath, isDirectory: true
  71. )
  72. do {
  73. try FileManager.default.removeItem(at: heartbeatsDirectoryURL)
  74. } catch CocoaError.fileNoSuchFile {
  75. // Do nothing.
  76. } catch {
  77. throw error
  78. }
  79. #endif // os(tvOS)
  80. }
  81. }