diff --git a/ios/engine/KMEI/KeymanEngineTests/KeymanEngineTests.swift b/ios/engine/KMEI/KeymanEngineTests/KeymanEngineTests.swift index f90de887b2..381bd836d4 100644 --- a/ios/engine/KMEI/KeymanEngineTests/KeymanEngineTests.swift +++ b/ios/engine/KMEI/KeymanEngineTests/KeymanEngineTests.swift @@ -23,6 +23,29 @@ class KeymanEngineTests: XCTestCase { // Put teardown code here. This method is called after the invocation of each test method in the class. } + func getAssignedDictionary(for userDefaults: UserDefaults) -> [String: Any] { + var currentDictionary = userDefaults.dictionaryRepresentation() + clearSettings(of: userDefaults) + let baseDict = userDefaults.dictionaryRepresentation() + + baseDict.forEach({ pair in + currentDictionary.removeValue(forKey: pair.key) + }) + + // The return value is now properly constructed. Now to put the defaults back in place before we leave. + //userDefaults.register(defaults: currentDictionary) // adds them in an unclearable way! + currentDictionary.forEach({ pair in + userDefaults.set(pair.value, forKey: pair.key) + }) + return currentDictionary + } + + func clearSettings(of userDefaults: UserDefaults) { + let domain = Bundle.main.bundleIdentifier! + userDefaults.synchronize() + userDefaults.removePersistentDomain(forName: domain) + } + func testStorageAccess() { // Xcode unit tests cannot use capabilities, and thus we cannot access a true app group. // So, we'll use a sandboxed analogue for this instead. @@ -31,8 +54,10 @@ class KeymanEngineTests: XCTestCase { let storage = Storage.active! log.info(storage.baseDir) + // Diff-magic for determining the dictionary to be utilized for a test. let userDefaults = storage.userDefaults - let dictionary = userDefaults.dictionaryRepresentation() + userDefaults.set(4, forKey: "testEntry") + let dictCheck = getAssignedDictionary(for: userDefaults) Storage.active.erase() }