Browse Source

Tests..

pull/113/head
Abhishek Banthia 3 years ago
parent
commit
513dd1a392
  1. 46
      Clocker/ClockerUnitTests/ClockerUnitTests.swift
  2. 14
      Clocker/CoreModelKit/Sources/CoreModelKit/TimezoneData.swift
  3. 5
      Clocker/Overall App/AppDefaults.swift
  4. 2
      Clocker/Preferences/Menu Bar/StatusItemHandler.swift

46
Clocker/ClockerUnitTests/ClockerUnitTests.swift

@ -125,8 +125,21 @@ class ClockerUnitTests: XCTestCase {
func testHashing() { func testHashing() {
let timezoneData = TimezoneData(with: california) let timezoneData = TimezoneData(with: california)
let hash = timezoneData.hash XCTAssert(timezoneData.hash != -1)
XCTAssert(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() { func testDeletingATimezone() {
@ -228,6 +241,10 @@ class ClockerUnitTests: XCTestCase {
dataObject.setShouldOverrideGlobalTimeFormat(11) // 12-hour with preceding zero and seconds dataObject.setShouldOverrideGlobalTimeFormat(11) // 12-hour with preceding zero and seconds
XCTAssertTrue(dataObject.timezoneFormat(DataStore.shared().timezoneFormat()) == "hh:mm:ss") 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() { func testTimezoneFormatWithDefaultSetAs24HourFormat() {
@ -298,26 +315,39 @@ class ClockerUnitTests: XCTestCase {
func testTimezoneRetrieval() { func testTimezoneRetrieval() {
let dataObject = TimezoneData(with: mumbai) let dataObject = TimezoneData(with: mumbai)
let autoupdatingTimezone = TimeZone.autoupdatingCurrent.identifier
XCTAssertEqual(dataObject.timezone(), "Asia/Calcutta") XCTAssertEqual(dataObject.timezone(), "Asia/Calcutta")
// Unlikely
dataObject.timezoneID = nil
XCTAssertEqual(dataObject.timezone(), autoupdatingTimezone)
dataObject.isSystemTimezone = true dataObject.isSystemTimezone = true
let autoupdatingTimezone = TimeZone.autoupdatingCurrent.identifier
XCTAssertEqual(dataObject.timezone(), autoupdatingTimezone) XCTAssertEqual(dataObject.timezone(), autoupdatingTimezone)
} }
func testFormattedLabel() { func testFormattedLabel() {
let dataObject = TimezoneData(with: mumbai) 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("") 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 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") 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() { func testEquality() {

14
Clocker/CoreModelKit/Sources/CoreModelKit/TimezoneData.swift

@ -276,10 +276,6 @@ public class TimezoneData: NSObject, NSCoding {
return placeIdentifier.hashValue ^ timezone.hashValue return placeIdentifier.hashValue ^ timezone.hashValue
} }
static func == (lhs: TimezoneData, rhs: TimezoneData) -> Bool {
return lhs.isEqual(rhs)
}
public override func isEqual(_ object: Any?) -> Bool { public override func isEqual(_ object: Any?) -> Bool {
guard let compared = object as? TimezoneData else { guard let compared = object as? TimezoneData else {
return false return false
@ -301,17 +297,17 @@ public extension TimezoneData {
private func objectDescription() -> String { private func objectDescription() -> String {
let customString = """ let customString = """
TimezoneID: \(timezoneID ?? "Error") TimezoneID: \(String(describing: timezoneID))
Formatted Address: \(formattedAddress ?? "Error") Formatted Address: \(formattedAddress ?? "Error")
Custom Label: \(customLabel ?? "Error") Custom Label: \(customLabel ?? "Error")
Latitude: \(latitude ?? -0.0) Latitude: \(latitude ?? -0.0)
Longitude: \(longitude ?? -0.0) Longitude: \(longitude ?? -0.0)
Place Identifier: \(placeID ?? "Error") Place Identifier: \(String(describing: placeID))
Is Favourite: \(isFavourite) Is Favourite: \(isFavourite)
Sunrise Time: \(sunriseTime?.debugDescription ?? "N/A") Sunrise Time: \(String(describing: sunriseTime))
Sunset Time: \(sunsetTime?.debugDescription ?? "N/A") Sunset Time: \(String(describing: sunsetTime))
Selection Type: \(selectionType.rawValue) Selection Type: \(selectionType.rawValue)
Note: \(note ?? "Error") Note: \(String(describing: note))
Is System Timezone: \(isSystemTimezone) Is System Timezone: \(isSystemTimezone)
Override: \(overrideFormat) Override: \(overrideFormat)
""" """

5
Clocker/Overall App/AppDefaults.swift

@ -20,8 +20,9 @@ class AppDefaults {
private class func initializeDefaults() { private class func initializeDefaults() {
let userDefaults = UserDefaults.standard let userDefaults = UserDefaults.standard
let dataStore = DataStore.shared()
let timezones = userDefaults.object(forKey: CLDefaultPreferenceKey) let timezones = dataStore.timezones()
let selectedCalendars = userDefaults.object(forKey: CLSelectedCalendars) let selectedCalendars = userDefaults.object(forKey: CLSelectedCalendars)
// Now delete the old preferences // Now delete the old preferences
@ -39,7 +40,7 @@ class AppDefaults {
// If we already have timezones to display in menubar, do nothing. // If we already have timezones to display in menubar, do nothing.
// Else, we switch the menubar mode default to compact mode for new users // Else, we switch the menubar mode default to compact mode for new users
if userDefaults.bool(forKey: CLDefaultMenubarMode) == false { 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) userDefaults.set(1, forKey: CLMenubarCompactMode)
} else { } else {
userDefaults.set(0, forKey: CLMenubarCompactMode) userDefaults.set(0, forKey: CLMenubarCompactMode)

2
Clocker/Preferences/Menu Bar/StatusItemHandler.swift

@ -150,7 +150,7 @@ class StatusItemHandler: NSObject {
} }
private func retrieveSyncedMenubarTimezones() -> [Data] { 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 let menubarTimezones = defaultPreferences.filter { data -> Bool in
if let timezoneObj = TimezoneData.customObject(from: data) { if let timezoneObj = TimezoneData.customObject(from: data) {

Loading…
Cancel
Save