NotificationSettingsTests.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. "showPreviewsSetting" : "whenAuthenticated",
  26. "soundSetting" : "enabled"
  27. }
  28. """
  29. )
  30. }
  31. private func assertNotificationSettingsCodable(_ original: NotificationSettings, encodesJSON string: String) throws {
  32. let data = try encoder.encode(original)
  33. XCTAssertEqual(String(data: data, encoding: .utf8), string)
  34. let decoded = try decoder.decode(NotificationSettings.self, from: data)
  35. XCTAssertEqual(decoded, original)
  36. }
  37. private let encoder: JSONEncoder = {
  38. let encoder = JSONEncoder()
  39. encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
  40. encoder.dateEncodingStrategy = .iso8601
  41. return encoder
  42. }()
  43. private let decoder: JSONDecoder = {
  44. let decoder = JSONDecoder()
  45. decoder.dateDecodingStrategy = .iso8601
  46. return decoder
  47. }()
  48. }
  49. fileprivate extension NotificationSettings {
  50. static var test: NotificationSettings {
  51. return NotificationSettings(authorizationStatus: .authorized,
  52. soundSetting: .enabled,
  53. badgeSetting: .enabled,
  54. alertSetting: .disabled,
  55. notificationCenterSetting: .notSupported,
  56. lockScreenSetting: .disabled,
  57. carPlaySetting: .notSupported,
  58. alertStyle: .banner,
  59. showPreviewsSetting: .whenAuthenticated,
  60. criticalAlertSetting: .enabled,
  61. providesAppNotificationSettings: true,
  62. announcementSetting: .enabled)
  63. }
  64. }