|
| 1 | +/* |
| 2 | + Modified MIT License |
| 3 | + |
| 4 | + Copyright 2026 OneSignal |
| 5 | + |
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights |
| 9 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + copies of the Software, and to permit persons to whom the Software is |
| 11 | + furnished to do so, subject to the following conditions: |
| 12 | + |
| 13 | + 1. The above copyright notice and this permission notice shall be included in |
| 14 | + all copies or substantial portions of the Software. |
| 15 | + |
| 16 | + 2. All copies of substantial portions of the Software may only be used in connection |
| 17 | + with services provided by OneSignal. |
| 18 | + |
| 19 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +import Foundation |
| 29 | +import XCTest |
| 30 | +import OneSignalCore |
| 31 | +@testable import OneSignalOSCore |
| 32 | + |
| 33 | +/// Validates `OSModelStore.refresh()` — the post-unlock recovery path that the SDK's |
| 34 | +/// `start()` relies on so model stores that loaded empty during iOS prewarm re-hydrate |
| 35 | +/// from disk before Path 1 runs. |
| 36 | +final class OSModelStoreRefreshTests: XCTestCase { |
| 37 | + |
| 38 | + private let storeKey = "OSModelStoreRefreshTests_storeKey" |
| 39 | + |
| 40 | + override func setUp() { |
| 41 | + super.setUp() |
| 42 | + OneSignalUserDefaults.initShared().removeValue(forKey: storeKey) |
| 43 | + } |
| 44 | + |
| 45 | + override func tearDown() { |
| 46 | + OneSignalUserDefaults.initShared().removeValue(forKey: storeKey) |
| 47 | + super.tearDown() |
| 48 | + } |
| 49 | + |
| 50 | + /// Seed UserDefaults with a serialized models dict, the same way `OSModelStore.add` |
| 51 | + /// persists. Mimics "prior session wrote models to disk". |
| 52 | + private func seedUserDefaults(with models: [String: OSModel]) { |
| 53 | + OneSignalUserDefaults.initShared().saveCodeableData(forKey: storeKey, withValue: models) |
| 54 | + } |
| 55 | + |
| 56 | + private func makeStore() -> OSModelStore<OSModel> { |
| 57 | + return OSModelStore<OSModel>(changeSubscription: OSEventProducer(), storeKey: storeKey) |
| 58 | + } |
| 59 | + |
| 60 | + /// Simulates the prewarm case: at OSModelStore.init time UserDefaults returned nil, |
| 61 | + /// then disk became readable. refresh() must pick up what's on disk. |
| 62 | + func testRefresh_hydratesFromUserDefaults_whenStoreLoadedEmpty() { |
| 63 | + // 1. Store inits empty (UD has no entry for storeKey). |
| 64 | + let store = makeStore() |
| 65 | + XCTAssertTrue(store.getModels().isEmpty, "Precondition: store should load empty") |
| 66 | + |
| 67 | + // 2. Disk gets populated after the fact (simulates the prior session's persisted state). |
| 68 | + let model = OSModel(changeNotifier: OSEventProducer()) |
| 69 | + seedUserDefaults(with: ["key_x": model]) |
| 70 | + |
| 71 | + // 3. refresh() should hydrate. |
| 72 | + store.refresh() |
| 73 | + |
| 74 | + XCTAssertEqual(store.getModels().count, 1, "refresh should hydrate the in-memory dict") |
| 75 | + XCTAssertNotNil(store.getModel(key: "key_x")) |
| 76 | + } |
| 77 | + |
| 78 | + /// refresh() must be a no-op when the store is already populated — never clobber |
| 79 | + /// in-memory state that may have diverged from disk via writes that haven't flushed. |
| 80 | + func testRefresh_isNoOp_whenStoreAlreadyPopulated() { |
| 81 | + // 1. Seed disk with model A; store init() will load it. |
| 82 | + let modelA = OSModel(changeNotifier: OSEventProducer()) |
| 83 | + seedUserDefaults(with: ["key_x": modelA]) |
| 84 | + let store = makeStore() |
| 85 | + XCTAssertEqual(store.getModels().count, 1) |
| 86 | + let loadedAId = store.getModel(key: "key_x")?.modelId |
| 87 | + |
| 88 | + // 2. Disk gets a different model B for the same key. |
| 89 | + let modelB = OSModel(changeNotifier: OSEventProducer()) |
| 90 | + seedUserDefaults(with: ["key_x": modelB]) |
| 91 | + |
| 92 | + // 3. refresh() must NOT replace the existing in-memory entry. |
| 93 | + store.refresh() |
| 94 | + |
| 95 | + XCTAssertEqual(store.getModel(key: "key_x")?.modelId, loadedAId, |
| 96 | + "refresh must not replace already-loaded model") |
| 97 | + } |
| 98 | + |
| 99 | + /// refresh() should subscribe the store to each hydrated model's change notifier so |
| 100 | + /// downstream mutations persist via the normal onModelUpdated path. |
| 101 | + func testRefresh_subscribesStoreToHydratedModelNotifiers() { |
| 102 | + let store = makeStore() |
| 103 | + XCTAssertTrue(store.getModels().isEmpty) |
| 104 | + |
| 105 | + let model = OSModel(changeNotifier: OSEventProducer()) |
| 106 | + seedUserDefaults(with: ["key_x": model]) |
| 107 | + |
| 108 | + store.refresh() |
| 109 | + |
| 110 | + guard let loaded = store.getModel(key: "key_x") else { |
| 111 | + XCTFail("Expected model to be loaded by refresh") |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + // Mutate via set(property:) — triggers fire() on changeNotifier. If refresh() wired |
| 116 | + // the subscription, the store's onModelUpdated will receive it and persist the |
| 117 | + // dict back to UserDefaults. |
| 118 | + OneSignalUserDefaults.initShared().removeValue(forKey: storeKey) |
| 119 | + loaded.set(property: "test_prop", newValue: "test_value") |
| 120 | + |
| 121 | + // After the mutation, UD should be repopulated by onModelUpdated. |
| 122 | + let written = OneSignalUserDefaults.initShared().getSavedCodeableData(forKey: storeKey, defaultValue: nil) |
| 123 | + XCTAssertNotNil(written, "Mutating a refresh()-hydrated model should persist via the store's onModelUpdated handler") |
| 124 | + } |
| 125 | + |
| 126 | + /// Sanity: when there's nothing on disk, refresh() leaves an empty store empty. |
| 127 | + func testRefresh_doesNothing_whenDiskIsEmpty() { |
| 128 | + let store = makeStore() |
| 129 | + XCTAssertTrue(store.getModels().isEmpty) |
| 130 | + |
| 131 | + store.refresh() |
| 132 | + |
| 133 | + XCTAssertTrue(store.getModels().isEmpty) |
| 134 | + } |
| 135 | +} |
0 commit comments