AlertTests.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // AlertTests.swift
  3. // LoopKitTests
  4. //
  5. // Created by Rick Pasetto on 5/1/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import XCTest
  9. @testable import LoopKit
  10. class AlertTests: XCTestCase {
  11. let identifier = Alert.Identifier(managerIdentifier: "managerIdentifier1", alertIdentifier: "alertIdentifier1")
  12. let foregroundContent = Alert.Content(title: "title1", body: "body1", acknowledgeActionButtonLabel: "acknowledgeActionButtonLabel1")
  13. let backgroundContent = Alert.Content(title: "title2", body: "body2", acknowledgeActionButtonLabel: "acknowledgeActionButtonLabel2")
  14. func testIdentifierValue() {
  15. XCTAssertEqual("managerIdentifier1.alertIdentifier1", identifier.value)
  16. }
  17. func testAlertImmediateEncodable() {
  18. let alert = Alert(identifier: identifier, foregroundContent: foregroundContent, backgroundContent: backgroundContent, trigger: .immediate)
  19. let str = try! alert.encodeToString()
  20. let decoded = try! Alert.decode(from: str)
  21. XCTAssertEqual(alert, decoded)
  22. }
  23. func testAlertDelayedEncodable() {
  24. let alert = Alert(identifier: identifier, foregroundContent: foregroundContent, backgroundContent: backgroundContent, trigger: .delayed(interval: 1.0))
  25. let str = try! alert.encodeToString()
  26. let decoded = try! Alert.decode(from: str)
  27. XCTAssertEqual(alert, decoded)
  28. }
  29. func testAlertRepeatingEncodable() {
  30. let alert = Alert(identifier: identifier, foregroundContent: foregroundContent, backgroundContent: backgroundContent, trigger: .repeating(repeatInterval: 2.0))
  31. let str = try! alert.encodeToString()
  32. let decoded = try! Alert.decode(from: str)
  33. XCTAssertEqual(alert, decoded)
  34. }
  35. func testAlertVibrateSoundEncodable() {
  36. let alert = Alert(identifier: identifier, foregroundContent: foregroundContent, backgroundContent: backgroundContent, trigger: .immediate, sound: .vibrate)
  37. let str = try! alert.encodeToString()
  38. let decoded = try! Alert.decode(from: str)
  39. XCTAssertEqual(alert, decoded)
  40. }
  41. func testAlertSoundEncodable() {
  42. let alert = Alert(identifier: identifier, foregroundContent: foregroundContent, backgroundContent: backgroundContent, trigger: .immediate, sound: .sound(name: "soundName"))
  43. let str = try! alert.encodeToString()
  44. let decoded = try! Alert.decode(from: str)
  45. XCTAssertEqual(alert, decoded)
  46. }
  47. func testAlertImmediateDecodable() {
  48. let str = "{\"trigger\":\"immediate\",\"interruptionLevel\":\"timeSensitive\",\"identifier\":{\"managerIdentifier\":\"managerIdentifier1\",\"alertIdentifier\":\"alertIdentifier1\"},\"foregroundContent\":{\"body\":\"body1\",\"title\":\"title1\",\"acknowledgeActionButtonLabel\":\"acknowledgeActionButtonLabel1\"},\"backgroundContent\":{\"body\":\"body2\",\"title\":\"title2\",\"acknowledgeActionButtonLabel\":\"acknowledgeActionButtonLabel2\"}}"
  49. let expected = Alert(identifier: identifier, foregroundContent: foregroundContent, backgroundContent: backgroundContent, trigger: .immediate)
  50. let alert = try? Alert.decode(from: str)
  51. XCTAssertEqual(expected, alert)
  52. }
  53. func testAlertDelayedDecodable() {
  54. let str = "{\"trigger\":{\"delayed\":{\"delayInterval\":1}},\"interruptionLevel\":\"timeSensitive\",\"identifier\":{\"managerIdentifier\":\"managerIdentifier1\",\"alertIdentifier\":\"alertIdentifier1\"},\"foregroundContent\":{\"body\":\"body1\",\"title\":\"title1\",\"acknowledgeActionButtonLabel\":\"acknowledgeActionButtonLabel1\"},\"backgroundContent\":{\"body\":\"body2\",\"title\":\"title2\",\"acknowledgeActionButtonLabel\":\"acknowledgeActionButtonLabel2\"}}"
  55. let expected = Alert(identifier: identifier, foregroundContent: foregroundContent, backgroundContent: backgroundContent, trigger: .delayed(interval: 1.0))
  56. let alert = try? Alert.decode(from: str)
  57. XCTAssertEqual(expected, alert)
  58. }
  59. func testAlertRepeatingDecodable() {
  60. let str = "{\"trigger\":{\"repeating\":{\"repeatInterval\":2}},\"interruptionLevel\":\"timeSensitive\",\"identifier\":{\"managerIdentifier\":\"managerIdentifier1\",\"alertIdentifier\":\"alertIdentifier1\"},\"foregroundContent\":{\"body\":\"body1\",\"title\":\"title1\",\"acknowledgeActionButtonLabel\":\"acknowledgeActionButtonLabel1\"},\"backgroundContent\":{\"body\":\"body2\",\"title\":\"title2\",\"acknowledgeActionButtonLabel\":\"acknowledgeActionButtonLabel2\"}}"
  61. let expected = Alert(identifier: identifier, foregroundContent: foregroundContent, backgroundContent: backgroundContent, trigger: .repeating(repeatInterval: 2.0))
  62. let alert = try? Alert.decode(from: str)
  63. XCTAssertEqual(expected, alert)
  64. }
  65. func testAlertVibrateSoundDecodable() {
  66. let str = "{\"trigger\":\"immediate\",\"sound\":\"vibrate\",\"interruptionLevel\":\"timeSensitive\",\"identifier\":{\"managerIdentifier\":\"managerIdentifier1\",\"alertIdentifier\":\"alertIdentifier1\"},\"foregroundContent\":{\"body\":\"body1\",\"title\":\"title1\",\"acknowledgeActionButtonLabel\":\"acknowledgeActionButtonLabel1\"},\"backgroundContent\":{\"body\":\"body2\",\"title\":\"title2\",\"acknowledgeActionButtonLabel\":\"acknowledgeActionButtonLabel2\"}}"
  67. let expected = Alert(identifier: identifier, foregroundContent: foregroundContent, backgroundContent: backgroundContent, trigger: .immediate, sound: .vibrate)
  68. let alert = try? Alert.decode(from: str)
  69. XCTAssertEqual(expected, alert)
  70. }
  71. func testAlertSoundDecodable() {
  72. let str = "{\"trigger\":\"immediate\",\"sound\":{\"sound\":{\"name\":\"soundName\"}},\"interruptionLevel\":\"timeSensitive\",\"identifier\":{\"managerIdentifier\":\"managerIdentifier1\",\"alertIdentifier\":\"alertIdentifier1\"},\"foregroundContent\":{\"body\":\"body1\",\"title\":\"title1\",\"acknowledgeActionButtonLabel\":\"acknowledgeActionButtonLabel1\"},\"backgroundContent\":{\"body\":\"body2\",\"title\":\"title2\",\"acknowledgeActionButtonLabel\":\"acknowledgeActionButtonLabel2\"}}"
  73. let expected = Alert(identifier: identifier, foregroundContent: foregroundContent, backgroundContent: backgroundContent, trigger: .immediate, sound: .sound(name: "soundName"))
  74. let alert = try? Alert.decode(from: str)
  75. XCTAssertEqual(expected, alert)
  76. }
  77. func testAlertSoundFilename() {
  78. XCTAssertNil(Alert.Sound.vibrate.filename)
  79. XCTAssertEqual("foo", Alert.Sound.sound(name: "foo").filename)
  80. }
  81. }
  82. extension Alert {
  83. enum CodableError: Swift.Error { case encodeFailed, decodeFailed }
  84. func encode() throws -> Data {
  85. let encoder = JSONEncoder()
  86. return try encoder.encode(self)
  87. }
  88. static func decode(from data: Data) throws -> Alert {
  89. let decoder = JSONDecoder()
  90. return try decoder.decode(Alert.self, from: data)
  91. }
  92. func encodeToString() throws -> String {
  93. let data = try encode()
  94. guard let result = String(data: data, encoding: .utf8) else {
  95. throw CodableError.encodeFailed
  96. }
  97. return result
  98. }
  99. static func decode(from string: String) throws -> Alert {
  100. guard let data = string.data(using: .utf8) else {
  101. throw CodableError.decodeFailed
  102. }
  103. return try decode(from: data)
  104. }
  105. }