diff --git a/Clocker/ClockerUnitTests/ClockerUnitTests.swift b/Clocker/ClockerUnitTests/ClockerUnitTests.swift index 49b190c..b1a7f5e 100644 --- a/Clocker/ClockerUnitTests/ClockerUnitTests.swift +++ b/Clocker/ClockerUnitTests/ClockerUnitTests.swift @@ -125,8 +125,21 @@ class ClockerUnitTests: XCTestCase { func testHashing() { let timezoneData = TimezoneData(with: california) - let hash = timezoneData.hash - XCTAssert(hash != -1) + XCTAssert(timezoneData.hash != -1) + + timezoneData.placeID = nil + timezoneData.timezoneID = nil + XCTAssert(timezoneData.hash == -1) + } + + func testBadInputDictionaryForInitialization() { + let badInput: [String: Any] = ["customLabel": "", + "latitude": "41.2565369", + "longitude": "-95.9345034"] + let badTimezoneData = TimezoneData(with: badInput) + XCTAssertEqual(badTimezoneData.placeID, "Error") + XCTAssertEqual(badTimezoneData.timezoneID, "Error") + XCTAssertEqual(badTimezoneData.formattedAddress, "Error") } func testDeletingATimezone() { @@ -228,6 +241,10 @@ class ClockerUnitTests: XCTestCase { dataObject.setShouldOverrideGlobalTimeFormat(11) // 12-hour with preceding zero and seconds XCTAssertTrue(dataObject.timezoneFormat(DataStore.shared().timezoneFormat()) == "hh:mm:ss") + + // Wrong input + dataObject.setShouldOverrideGlobalTimeFormat(0) // 12-hour with preceding zero and seconds + XCTAssertTrue(dataObject.timezoneFormat(88) == "h:mm a") } func testTimezoneFormatWithDefaultSetAs24HourFormat() { @@ -298,26 +315,39 @@ class ClockerUnitTests: XCTestCase { func testTimezoneRetrieval() { let dataObject = TimezoneData(with: mumbai) - + let autoupdatingTimezone = TimeZone.autoupdatingCurrent.identifier XCTAssertEqual(dataObject.timezone(), "Asia/Calcutta") + // Unlikely + dataObject.timezoneID = nil + XCTAssertEqual(dataObject.timezone(), autoupdatingTimezone) + dataObject.isSystemTimezone = true - let autoupdatingTimezone = TimeZone.autoupdatingCurrent.identifier XCTAssertEqual(dataObject.timezone(), autoupdatingTimezone) } func testFormattedLabel() { let dataObject = TimezoneData(with: mumbai) - XCTAssertTrue(dataObject.formattedTimezoneLabel() == "Ghar", "Incorrect custom label returned by model.") + XCTAssertTrue(dataObject.formattedTimezoneLabel() == "Ghar", "Incorrect custom label returned by model \(dataObject.formattedTimezoneLabel())") dataObject.setLabel("") - XCTAssertTrue(dataObject.formattedTimezoneLabel() == "Mumbai", "Incorrect custom label returned by model.") + XCTAssertTrue(dataObject.formattedTimezoneLabel() == "Mumbai", "Incorrect custom label returned by model \(dataObject.formattedTimezoneLabel())") dataObject.formattedAddress = nil - XCTAssertTrue(dataObject.formattedTimezoneLabel() == "Asia", "Incorrect custom label returned by model.") + XCTAssertTrue(dataObject.formattedTimezoneLabel() == "Asia", "Incorrect custom label returned by model \(dataObject.formattedTimezoneLabel())") dataObject.setLabel("Jogeshwari") - XCTAssertTrue(dataObject.formattedTimezoneLabel() == "Jogeshwari", "Incorrect custom label returned by model.") + XCTAssertTrue(dataObject.formattedTimezoneLabel() == "Jogeshwari", "Incorrect custom label returned by model \(dataObject.formattedTimezoneLabel())") + + // Unlikely scenario + dataObject.setLabel("") + dataObject.timezoneID = "GMT" + XCTAssertTrue(dataObject.formattedTimezoneLabel() == "GMT", "Incorrect custom label returned by model \(dataObject.formattedTimezoneLabel())") + + // Another unlikely scenario + dataObject.setLabel("") + dataObject.timezoneID = nil + XCTAssertTrue(dataObject.formattedTimezoneLabel() == "Error", "Incorrect custom label returned by model \(dataObject.formattedTimezoneLabel())") } func testEquality() { diff --git a/Clocker/CoreModelKit/Sources/CoreModelKit/TimezoneData.swift b/Clocker/CoreModelKit/Sources/CoreModelKit/TimezoneData.swift index 594a983..34e6ca0 100644 --- a/Clocker/CoreModelKit/Sources/CoreModelKit/TimezoneData.swift +++ b/Clocker/CoreModelKit/Sources/CoreModelKit/TimezoneData.swift @@ -276,10 +276,6 @@ public class TimezoneData: NSObject, NSCoding { return placeIdentifier.hashValue ^ timezone.hashValue } - static func == (lhs: TimezoneData, rhs: TimezoneData) -> Bool { - return lhs.isEqual(rhs) - } - public override func isEqual(_ object: Any?) -> Bool { guard let compared = object as? TimezoneData else { return false @@ -301,17 +297,17 @@ public extension TimezoneData { private func objectDescription() -> String { let customString = """ - TimezoneID: \(timezoneID ?? "Error") + TimezoneID: \(String(describing: timezoneID)) Formatted Address: \(formattedAddress ?? "Error") Custom Label: \(customLabel ?? "Error") Latitude: \(latitude ?? -0.0) Longitude: \(longitude ?? -0.0) - Place Identifier: \(placeID ?? "Error") + Place Identifier: \(String(describing: placeID)) Is Favourite: \(isFavourite) - Sunrise Time: \(sunriseTime?.debugDescription ?? "N/A") - Sunset Time: \(sunsetTime?.debugDescription ?? "N/A") + Sunrise Time: \(String(describing: sunriseTime)) + Sunset Time: \(String(describing: sunsetTime)) Selection Type: \(selectionType.rawValue) - Note: \(note ?? "Error") + Note: \(String(describing: note)) Is System Timezone: \(isSystemTimezone) Override: \(overrideFormat) """ diff --git a/Clocker/Overall App/AppDefaults.swift b/Clocker/Overall App/AppDefaults.swift index 85ed394..33ecc56 100644 --- a/Clocker/Overall App/AppDefaults.swift +++ b/Clocker/Overall App/AppDefaults.swift @@ -20,8 +20,9 @@ class AppDefaults { private class func initializeDefaults() { let userDefaults = UserDefaults.standard + let dataStore = DataStore.shared() - let timezones = userDefaults.object(forKey: CLDefaultPreferenceKey) + let timezones = dataStore.timezones() let selectedCalendars = userDefaults.object(forKey: CLSelectedCalendars) // Now delete the old preferences @@ -39,7 +40,7 @@ class AppDefaults { // If we already have timezones to display in menubar, do nothing. // Else, we switch the menubar mode default to compact mode for new users if userDefaults.bool(forKey: CLDefaultMenubarMode) == false { - if let menubarFavourites = userDefaults.object(forKey: CLDefaultPreferenceKey) as? [Data], menubarFavourites.isEmpty == false { + if let menubarFavourites = dataStore.menubarTimezones(), menubarFavourites.isEmpty == false { userDefaults.set(1, forKey: CLMenubarCompactMode) } else { userDefaults.set(0, forKey: CLMenubarCompactMode) diff --git a/Clocker/Preferences/Menu Bar/StatusItemHandler.swift b/Clocker/Preferences/Menu Bar/StatusItemHandler.swift index ca9dbf8..4c52168 100644 --- a/Clocker/Preferences/Menu Bar/StatusItemHandler.swift +++ b/Clocker/Preferences/Menu Bar/StatusItemHandler.swift @@ -150,7 +150,7 @@ class StatusItemHandler: NSObject { } private func retrieveSyncedMenubarTimezones() -> [Data] { - let defaultPreferences = DataStore.shared().retrieve(key: CLDefaultPreferenceKey) as? [Data] ?? [] + let defaultPreferences = DataStore.shared().timezones() let menubarTimezones = defaultPreferences.filter { data -> Bool in if let timezoneObj = TimezoneData.customObject(from: data) {