Browse Source

Quick UT.

pull/113/head
Abhishek Banthia 3 years ago
parent
commit
b9022fcab6
  1. 11
      Clocker/ClockerUnitTests/ClockerUnitTests.swift
  2. 23
      Clocker/Overall App/AppDefaults.swift

11
Clocker/ClockerUnitTests/ClockerUnitTests.swift

@ -430,4 +430,15 @@ class ClockerUnitTests: XCTestCase {
XCTAssertEqual(subject.subviews.count, 2) // Two textfields XCTAssertEqual(subject.subviews.count, 2) // Two textfields
XCTAssertEqual(subject.subviews.first?.layer?.animationKeys(), ["notimezone.emoji"]) XCTAssertEqual(subject.subviews.first?.layer?.animationKeys(), ["notimezone.emoji"])
} }
func testDefaultsWiping() {
let defaultsDict: [String: Any] = ["test1" : "testString", "test2": 24]
let domainName = "com.test.clocker"
let defaults = UserDefaults(suiteName: domainName)
defaults?.setPersistentDomain(defaultsDict, forName: domainName)
defaults?.wipe(for: domainName)
XCTAssertNil(defaults?.object(forKey: "test1"))
XCTAssertNil(defaults?.object(forKey: "test2"))
}
} }

23
Clocker/Overall App/AppDefaults.swift

@ -15,9 +15,6 @@ class AppDefaults {
let timezones = dataStore.timezones() let timezones = dataStore.timezones()
let selectedCalendars = userDefaults.object(forKey: CLSelectedCalendars) let selectedCalendars = userDefaults.object(forKey: CLSelectedCalendars)
// Now delete the old preferences
userDefaults.wipeIfNeccesary()
// Register the usual suspects // Register the usual suspects
userDefaults.register(defaults: defaultsDictionary()) userDefaults.register(defaults: defaultsDictionary())
@ -25,13 +22,11 @@ class AppDefaults {
userDefaults.set(selectedCalendars, forKey: CLSelectedCalendars) userDefaults.set(selectedCalendars, forKey: CLSelectedCalendars)
// Set the theme default as Light! // Set the theme default as Light!
setDefaultTheme() setDefaultTheme(userDefaults)
} }
private class func setDefaultTheme() { private class func setDefaultTheme(_ userDefaults: UserDefaults) {
let defaults = UserDefaults.standard if userDefaults.object(forKey: CLThemeKey) == nil {
if defaults.object(forKey: CLThemeKey) == nil {
Themer.shared().set(theme: 0) Themer.shared().set(theme: 0)
} }
} }
@ -67,17 +62,7 @@ extension String {
extension UserDefaults { extension UserDefaults {
// Use this with caution. Exposing this for debugging purposes only. // Use this with caution. Exposing this for debugging purposes only.
func wipe() { func wipe(for bundleID: String) {
if let bundleID = Bundle.main.bundleIdentifier {
removePersistentDomain(forName: bundleID) removePersistentDomain(forName: bundleID)
} }
}
func wipeIfNeccesary() {
if object(forKey: "PreferencesHaveBeenWiped") == nil {
Logger.info("Wiping all user defaults")
wipe()
set(true, forKey: "PreferencesHaveBeenWiped")
}
}
} }

Loading…
Cancel
Save