HeartbeatLoggingTestUtils.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #if DEBUG
  15. import Foundation
  16. /// A utility class intended to be used only in testing contexts.
  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 = Data(base64URLEncoded: encoded)!
  69. if encodedData.count > 0 {
  70. encodedData = try! encodedData.unzipped()
  71. }
  72. let literalData = 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! encoder.encode(payloadFromEncoded)
  81. let payloadDataFromLiteral = try! encoder.encode(payloadFromLiteral)
  82. assert(
  83. payloadFromEncoded == payloadFromLiteral,
  84. """
  85. Mismatched payloads!
  86. Payload 1:
  87. \(String(data: payloadDataFromEncoded, encoding: .utf8) ?? "")
  88. Payload 2:
  89. \(String(data: payloadDataFromLiteral, encoding: .utf8) ?? "")
  90. """
  91. )
  92. }
  93. /// Removes all underlying storage containers used by the module.
  94. /// - Throws: An error if the storage container could not be removed.
  95. public static func removeUnderlyingHeartbeatStorageContainers() throws {
  96. #if os(tvOS)
  97. UserDefaults().removePersistentDomain(forName: Constants.heartbeatUserDefaultsSuiteName)
  98. #else
  99. let applicationSupportDirectory = FileManager.default
  100. .urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
  101. let heartbeatsDirectoryURL = applicationSupportDirectory
  102. .appendingPathComponent(
  103. Constants.heartbeatFileStorageDirectoryPath, isDirectory: true
  104. )
  105. do {
  106. try FileManager.default.removeItem(at: heartbeatsDirectoryURL)
  107. } catch CocoaError.fileNoSuchFile {
  108. // Do nothing.
  109. } catch {
  110. throw error
  111. }
  112. #endif // os(tvOS)
  113. }
  114. }
  115. #endif // ENABLE_FIREBASE_CORE_INTERNAL_TESTING_UTILS