Browse Source

Quick UT.

pull/113/head
Abhishek Banthia 3 years ago
parent
commit
b9022fcab6
  1. 11
      Clocker/ClockerUnitTests/ClockerUnitTests.swift
  2. 25
      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.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"))
}
}

25
Clocker/Overall App/AppDefaults.swift

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

Loading…
Cancel
Save