SessionsSettingsTests.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Copyright 2022 Google LLC
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. import XCTest
  16. @testable import FirebaseSessions
  17. class SessionsSettingsTests: XCTestCase {
  18. let validSettings: [String: Any] = [
  19. "cache_duration": 10,
  20. "app_quality": [
  21. "sessions_enabled": false,
  22. "sampling_rate": 0.5,
  23. "session_timeout_seconds": 10,
  24. ] as [String: Any],
  25. ]
  26. var cache: SettingsCacheClient!
  27. var appInfo: MockApplicationInfo!
  28. var downloader: MockSettingsDownloader!
  29. var remoteSettings: RemoteSettings!
  30. var localOverrideSettings: LocalOverrideSettings!
  31. var sdkDefaultSettings: SDKDefaultSettings!
  32. var sessionSettings: SessionsSettings!
  33. override func setUp() {
  34. super.setUp()
  35. appInfo = MockApplicationInfo()
  36. cache = SettingsCache()
  37. cache.removeCache() // just reinstantiating cache isn't enough because of persistence
  38. downloader = MockSettingsDownloader(successResponse: validSettings)
  39. remoteSettings = RemoteSettings(appInfo: appInfo, downloader: downloader, cache: cache)
  40. remoteSettings.updateSettings(currentTime: Date())
  41. localOverrideSettings = LocalOverrideSettings()
  42. sdkDefaultSettings = SDKDefaultSettings()
  43. sessionSettings = SessionsSettings(
  44. appInfo: appInfo,
  45. installations: MockInstallationsProtocol(),
  46. sdkDefaults: sdkDefaultSettings,
  47. localOverrides: localOverrideSettings,
  48. remoteSettings: remoteSettings
  49. )
  50. }
  51. func test_RemoteAndDefaultsPresent_RemoteConfigsApplied() {
  52. XCTAssertFalse(sessionSettings.sessionsEnabled)
  53. XCTAssertEqual(sessionSettings.samplingRate, 0.5)
  54. XCTAssertEqual(sessionSettings.sessionTimeout, 10)
  55. }
  56. func test_NoRemoteAndDefaultsPresent_DefaultConfigsApply() {
  57. let emptySettings: [String: Any] = [
  58. "cache_duration": 10,
  59. "app_quality": [:] as [String: Any],
  60. ]
  61. cache.removeCache()
  62. downloader = MockSettingsDownloader(successResponse: emptySettings)
  63. remoteSettings = RemoteSettings(appInfo: appInfo, downloader: downloader, cache: cache)
  64. remoteSettings.updateSettings(currentTime: Date())
  65. sessionSettings = SessionsSettings(
  66. appInfo: appInfo,
  67. installations: MockInstallationsProtocol(),
  68. sdkDefaults: sdkDefaultSettings,
  69. localOverrides: localOverrideSettings,
  70. remoteSettings: remoteSettings
  71. )
  72. XCTAssertTrue(sessionSettings.sessionsEnabled)
  73. XCTAssertEqual(sessionSettings.samplingRate, 1.0)
  74. XCTAssertEqual(sessionSettings.sessionTimeout, 30 * 60)
  75. }
  76. func test_SomeRemoteAndDefaultsPresent_SomeConfigsApply() {
  77. let someSettings: [String: Any] = [
  78. "cache_duration": 10,
  79. "app_quality": [
  80. "sampling_rate": 0.8,
  81. "session_timeout_seconds": 50,
  82. ],
  83. ]
  84. cache.removeCache()
  85. downloader = MockSettingsDownloader(successResponse: someSettings)
  86. remoteSettings = RemoteSettings(appInfo: appInfo, downloader: downloader, cache: cache)
  87. remoteSettings.updateSettings(currentTime: Date())
  88. sessionSettings = SessionsSettings(
  89. appInfo: appInfo,
  90. installations: MockInstallationsProtocol(),
  91. sdkDefaults: sdkDefaultSettings,
  92. localOverrides: localOverrideSettings,
  93. remoteSettings: remoteSettings
  94. )
  95. XCTAssertTrue(sessionSettings.sessionsEnabled)
  96. XCTAssertEqual(sessionSettings.samplingRate, 0.8)
  97. XCTAssertEqual(sessionSettings.sessionTimeout, 50)
  98. }
  99. }