NotificationSettingsTests.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // NotificationSettingsTests.swift
  3. // LoopKitTests
  4. //
  5. // Created by Darin Krauss on 9/17/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import XCTest
  9. import Foundation
  10. import LoopKit
  11. class NotificationSettingsCodableTests: XCTestCase {
  12. func testCodable() throws {
  13. try assertNotificationSettingsCodable(NotificationSettings.test, encodesJSON: """
  14. {
  15. "alertSetting" : "disabled",
  16. "alertStyle" : "banner",
  17. "announcementSetting" : "enabled",
  18. "authorizationStatus" : "authorized",
  19. "badgeSetting" : "enabled",
  20. "carPlaySetting" : "notSupported",
  21. "criticalAlertSetting" : "enabled",
  22. "lockScreenSetting" : "disabled",
  23. "notificationCenterSetting" : "notSupported",
  24. "providesAppNotificationSettings" : true,
  25. "scheduledDeliverySetting" : "disabled",
  26. "showPreviewsSetting" : "whenAuthenticated",
  27. "soundSetting" : "enabled",
  28. "timeSensitiveSetting" : "enabled"
  29. }
  30. """
  31. )
  32. }
  33. func testMigration() throws {
  34. let oldSettingsString = """
  35. {
  36. "alertSetting" : "disabled",
  37. "alertStyle" : "banner",
  38. "announcementSetting" : "enabled",
  39. "authorizationStatus" : "authorized",
  40. "badgeSetting" : "enabled",
  41. "carPlaySetting" : "notSupported",
  42. "criticalAlertSetting" : "enabled",
  43. "lockScreenSetting" : "disabled",
  44. "notificationCenterSetting" : "notSupported",
  45. "providesAppNotificationSettings" : true,
  46. "showPreviewsSetting" : "whenAuthenticated",
  47. "soundSetting" : "enabled"
  48. }
  49. """
  50. let _ = try decoder.decode(NotificationSettings.self, from: oldSettingsString.data(using: .utf8)!)
  51. }
  52. private func assertNotificationSettingsCodable(_ original: NotificationSettings, encodesJSON string: String) throws {
  53. let data = try encoder.encode(original)
  54. XCTAssertEqual(String(data: data, encoding: .utf8), string)
  55. let decoded = try decoder.decode(NotificationSettings.self, from: data)
  56. XCTAssertEqual(decoded, original)
  57. }
  58. private let encoder: JSONEncoder = {
  59. let encoder = JSONEncoder()
  60. encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
  61. encoder.dateEncodingStrategy = .iso8601
  62. return encoder
  63. }()
  64. private let decoder: JSONDecoder = {
  65. let decoder = JSONDecoder()
  66. decoder.dateDecodingStrategy = .iso8601
  67. return decoder
  68. }()
  69. }
  70. fileprivate extension NotificationSettings {
  71. static var test: NotificationSettings {
  72. return NotificationSettings(authorizationStatus: .authorized,
  73. soundSetting: .enabled,
  74. badgeSetting: .enabled,
  75. alertSetting: .disabled,
  76. notificationCenterSetting: .notSupported,
  77. lockScreenSetting: .disabled,
  78. carPlaySetting: .notSupported,
  79. alertStyle: .banner,
  80. showPreviewsSetting: .whenAuthenticated,
  81. criticalAlertSetting: .enabled,
  82. providesAppNotificationSettings: true,
  83. announcementSetting: .enabled,
  84. timeSensitiveSetting: .enabled,
  85. scheduledDeliverySetting: .disabled)
  86. }
  87. }