HeartbeatLoggingTestUtils.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. public static var emptyHeartbeatsPayload: _ObjC_HeartbeatsPayload {
  32. let literalData = """
  33. {
  34. "version": 2,
  35. "heartbeats": []
  36. }
  37. """
  38. .data(using: .utf8)!
  39. let decoder = JSONDecoder()
  40. decoder.dateDecodingStrategy = .formatted(HeartbeatsPayload.dateFormatter)
  41. let heartbeatsPayload = try! decoder.decode(HeartbeatsPayload.self, from: literalData)
  42. return _ObjC_HeartbeatsPayload(heartbeatsPayload)
  43. }
  44. public static var nonEmptyHeartbeatsPayload: _ObjC_HeartbeatsPayload {
  45. let literalData = """
  46. {
  47. "version": 2,
  48. "heartbeats": [
  49. {
  50. "agent": "dummy_agent_1",
  51. "dates": ["2021-11-01", "2021-11-02"]
  52. },
  53. {
  54. "agent": "dummy_agent_2",
  55. "dates": ["2021-11-03"]
  56. }
  57. ]
  58. }
  59. """
  60. .data(using: .utf8)!
  61. let decoder = JSONDecoder()
  62. decoder.dateDecodingStrategy = .formatted(HeartbeatsPayload.dateFormatter)
  63. let heartbeatsPayload = try! decoder.decode(HeartbeatsPayload.self, from: literalData)
  64. return _ObjC_HeartbeatsPayload(heartbeatsPayload)
  65. }
  66. @objc(assertEncodedPayloadString:isEqualToLiteralString:withError:)
  67. public static func assertEqualPayloadStrings(_ encoded: String, _ literal: String) throws {
  68. var encodedData = try XCTUnwrap(Data(base64URLEncoded: encoded))
  69. if encodedData.count > 0 {
  70. encodedData = try! encodedData.unzipped()
  71. }
  72. let literalData = try XCTUnwrap(literal.data(using: .utf8))
  73. let decoder = JSONDecoder()
  74. decoder.dateDecodingStrategy = .formatted(HeartbeatsPayload.dateFormatter)
  75. let payloadFromEncoded = try? decoder.decode(HeartbeatsPayload.self, from: encodedData)
  76. let payloadFromLiteral = try? decoder.decode(HeartbeatsPayload.self, from: literalData)
  77. let encoder = JSONEncoder()
  78. encoder.dateEncodingStrategy = .formatted(HeartbeatsPayload.dateFormatter)
  79. encoder.outputFormatting = .prettyPrinted
  80. let payloadDataFromEncoded = try XCTUnwrap(encoder.encode(payloadFromEncoded))
  81. let payloadDataFromLiteral = try XCTUnwrap(encoder.encode(payloadFromLiteral))
  82. XCTAssertEqual(
  83. payloadFromEncoded,
  84. payloadFromLiteral,
  85. """
  86. Mismatched payloads!
  87. Payload 1:
  88. \(String(data: payloadDataFromEncoded, encoding: .utf8) ?? "")
  89. Payload 2:
  90. \(String(data: payloadDataFromLiteral, encoding: .utf8) ?? "")
  91. """
  92. )
  93. }
  94. /// Removes all underlying storage containers used by the module.
  95. /// - Throws: An error if the storage container could not be removed.
  96. public static func removeUnderlyingHeartbeatStorageContainers() throws {
  97. #if os(tvOS)
  98. UserDefaults().removePersistentDomain(forName: Constants.heartbeatUserDefaultsSuiteName)
  99. #else
  100. let applicationSupportDirectory = try XCTUnwrap(
  101. FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
  102. )
  103. let heartbeatsDirectoryURL = applicationSupportDirectory
  104. .appendingPathComponent(
  105. Constants.heartbeatFileStorageDirectoryPath, isDirectory: true
  106. )
  107. do {
  108. try FileManager.default.removeItem(at: heartbeatsDirectoryURL)
  109. } catch CocoaError.fileNoSuchFile {
  110. // Do nothing.
  111. } catch {
  112. throw error
  113. }
  114. #endif // os(tvOS)
  115. }
  116. }