You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
10 KiB

// Copyright © 2015 Abhishek Banthia
import XCTest
@testable import Clocker
class ClockerUnitTests: XCTestCase {
6 years ago
private let california = ["customLabel" : "Test",
"formattedAddress" : "San Francisco",
"place_id" : "TestIdentifier",
"timezoneID" : "America/Los_Angeles",
"nextUpdate" : "",
"latitude" : "37.7749295",
"longitude" : "-122.4194155"]
6 years ago
private let mumbai = ["customLabel" : "Ghar",
"formattedAddress" : "Mumbai",
"place_id" : "ChIJwe1EZjDG5zsRaYxkjY_tpF0",
"timezoneID" : "Asia/Calcutta",
"nextUpdate" : "",
"latitude" : "19.0759837",
"longitude" : "72.8776559"]
6 years ago
private let auckland = ["customLabel" : "Auckland",
"formattedAddress" : "New Zealand",
"place_id" : "ChIJh5Z3Fw4gLG0RM0dqdeIY1rE",
"timezoneID" : "Pacific/Auckland",
"nextUpdate" : "",
"latitude" : "-40.900557",
"longitude" : "174.885971"]
6 years ago
private let florida = ["customLabel" : "Gainesville",
"formattedAddress" : "Florida",
"place_id" : "ChIJvypWkWV2wYgR0E7HW9MTLvc",
"timezoneID" : "America/New_York",
"nextUpdate" : "",
"latitude" : "27.664827",
"longitude" : "-81.5157535"]
6 years ago
private let onlyTimezone: [String: Any] = ["timezoneID": "Africa/Algiers",
"formattedAddress" : "Africa/Algiers",
"place_id": "",
"customLabel": "",
"latitude": "",
"longitude": ""]
6 years ago
private let omaha: [String: Any] = ["timezoneID": "America/Chicago",
"formattedAddress" : "Omaha",
"place_id": "ChIJ7fwMtciNk4cRBLY3rk9NQkY",
"customLabel": "",
"latitude": "41.2565369",
"longitude": "-95.9345034"]
6 years ago
private var operations: TimezoneDataOperations {
return TimezoneDataOperations(with: TimezoneData(with: mumbai))
}
6 years ago
private var californiaOperations: TimezoneDataOperations {
return TimezoneDataOperations(with: TimezoneData(with: california))
}
6 years ago
private var floridaOperations: TimezoneDataOperations {
return TimezoneDataOperations(with: TimezoneData(with: florida))
}
6 years ago
private var aucklandOperations: TimezoneDataOperations {
return TimezoneDataOperations(with: TimezoneData(with: auckland))
}
6 years ago
private var omahaOperations: TimezoneDataOperations {
return TimezoneDataOperations(with: TimezoneData(with: omaha))
}
func testAddingATimezoneToDefaults() {
let timezoneData = TimezoneData(with: california)
6 years ago
let defaults = UserDefaults.standard
let currentFavourites = (defaults.object(forKey: CLDefaultPreferenceKey) as? [Data]) ?? []
let oldCount = currentFavourites.count
6 years ago
let operationsObject = TimezoneDataOperations(with: timezoneData)
operationsObject.saveObject()
6 years ago
let newDefaults = UserDefaults.standard.object(forKey: CLDefaultPreferenceKey) as? [Data]
6 years ago
XCTAssert(newDefaults != nil)
XCTAssert(newDefaults?.count == oldCount + 1)
}
6 years ago
func testDeletingATimezone() {
let defaults = UserDefaults.standard
var currentFavourites = defaults.object(forKey: CLDefaultPreferenceKey) as! [Data]
let oldCount = currentFavourites.count
6 years ago
currentFavourites = currentFavourites.filter {
let timezone = TimezoneData.customObject(from: $0)
return timezone?.placeID != "TestIdentifier"
}
6 years ago
defaults.set(currentFavourites, forKey: CLDefaultPreferenceKey)
6 years ago
XCTAssertTrue(currentFavourites.count == oldCount - 1)
}
// The below test might fail outside California or if DST is active!
func testTimeDifference() {
XCTAssertTrue(operations.timeDifference() == ", 12 hours 30 mins ahead", "Difference was unexpectedly: \(operations.timeDifference())")
XCTAssertTrue(californiaOperations.timeDifference() == "", "Difference was unexpectedly: \(californiaOperations.timeDifference())")
XCTAssertTrue(floridaOperations.timeDifference() == ", 3 hours ahead", "Difference was unexpectedly: \(floridaOperations.timeDifference())")
XCTAssertTrue(aucklandOperations.timeDifference() == ", 19 hours ahead", "Difference was unexpectedly: \(aucklandOperations.timeDifference())")
XCTAssertTrue(omahaOperations.timeDifference() == ", 2 hours ahead", "Difference was unexpectedly: \(omahaOperations.timeDifference())")
}
6 years ago
func testSunriseSunset() {
let dataObject = TimezoneData(with: mumbai)
let operations = TimezoneDataOperations(with: dataObject)
6 years ago
XCTAssertNotNil(operations.formattedSunriseTime(with: 0))
XCTAssertNotNil(dataObject.sunriseTime)
XCTAssertNotNil(dataObject.sunriseTime)
6 years ago
let timezoneObject = TimezoneData(with: onlyTimezone)
timezoneObject.selectionType = .timezone
let timezoneOperations = TimezoneDataOperations(with: timezoneObject)
6 years ago
XCTAssertTrue(timezoneOperations.formattedSunriseTime(with: 0) == "")
XCTAssertNil(timezoneObject.sunriseTime)
XCTAssertNil(timezoneObject.sunsetTime)
}
6 years ago
func testDateWithSliderValue() {
6 years ago
let dataObject = TimezoneData(with: mumbai)
let operations = TimezoneDataOperations(with: dataObject)
6 years ago
XCTAssertNotNil(operations.date(with: 0, displayType: .menuDisplay))
}
6 years ago
func testTimezoneFormat() {
let dataObject = TimezoneData(with: mumbai)
UserDefaults.standard.set(NSNumber(value: 0), forKey: CLShowSecondsInMenubar) // Set to show seconds
UserDefaults.standard.set(NSNumber(value: 0), forKey: CL24hourFormatSelectedKey) // Set to 12 hour format
6 years ago
dataObject.setShouldOverrideGlobalTimeFormat(0)
XCTAssertTrue(dataObject.timezoneFormat() == "h:mm:ss a")
6 years ago
dataObject.setShouldOverrideGlobalTimeFormat(1)
XCTAssertTrue(dataObject.timezoneFormat() == "HH:mm:ss")
6 years ago
dataObject.setShouldOverrideGlobalTimeFormat(2)
XCTAssertTrue(dataObject.timezoneFormat() == "h:mm:ss a")
6 years ago
UserDefaults.standard.set(NSNumber(value: 1), forKey: CL24hourFormatSelectedKey) // Set to 24-Hour Format
XCTAssertTrue(dataObject.timezoneFormat() == "HH:mm:ss")
6 years ago
UserDefaults.standard.set(NSNumber(value: 1), forKey: CLShowSecondsInMenubar)
6 years ago
dataObject.setShouldOverrideGlobalTimeFormat(0)
XCTAssertTrue(dataObject.timezoneFormat() == "h:mm a")
6 years ago
dataObject.setShouldOverrideGlobalTimeFormat(1)
XCTAssertTrue(dataObject.timezoneFormat() == "HH:mm")
6 years ago
dataObject.setShouldOverrideGlobalTimeFormat(2)
XCTAssertTrue(dataObject.timezoneFormat() == "HH:mm")
6 years ago
UserDefaults.standard.set(NSNumber(value: 0), forKey: CL24hourFormatSelectedKey)
XCTAssertTrue(dataObject.timezoneFormat() == "h:mm a")
}
6 years ago
func testFormattedLabel() {
6 years ago
let dataObject = TimezoneData(with: mumbai)
XCTAssertTrue(dataObject.formattedTimezoneLabel() == "Ghar", "Incorrect custom label returned by model.")
6 years ago
dataObject.customLabel = nil
XCTAssertTrue(dataObject.formattedTimezoneLabel() == "Mumbai", "Incorrect custom label returned by model.")
6 years ago
dataObject.formattedAddress = nil
XCTAssertTrue(dataObject.formattedTimezoneLabel() == "Asia", "Incorrect custom label returned by model.")
}
6 years ago
func testEquality() {
let dataObject1 = TimezoneData(with: mumbai)
let dataObject2 = TimezoneData(with: auckland)
6 years ago
XCTAssertFalse(dataObject1 == dataObject2)
XCTAssertFalse(dataObject1.isEqual(dataObject2))
6 years ago
let dataObject3 = TimezoneData(with: mumbai)
XCTAssertTrue(dataObject1 == dataObject3)
XCTAssertTrue(dataObject1.isEqual(dataObject3))
6 years ago
XCTAssertFalse(dataObject1.isEqual(nil))
}
6 years ago
func testWithAllLocales() {
6 years ago
let dataObject1 = TimezoneData(with: mumbai)
let operations = TimezoneDataOperations(with: dataObject1)
6 years ago
for locale in Locale.availableIdentifiers {
let currentLocale = Locale(identifier: locale)
let localizedDate = operations.todaysDate(with: 0, locale: currentLocale)
XCTAssertNotNil(localizedDate)
}
}
6 years ago
func testTimeWithAllLocales() {
6 years ago
let dataObject = TimezoneData(with: mumbai)
6 years ago
let cal = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)
6 years ago
guard let newDate = cal?.date(byAdding: .minute,
value: 0,
to: Date(),
options: .matchFirst) else {
XCTFail("Unable to add dates!")
return
}
6 years ago
for locale in Locale.availableIdentifiers {
let currentLocale = Locale(identifier: locale)
let dateFormatter = DateFormatterManager.dateFormatterWithFormat(with: .none,
format: dataObject.timezoneFormat(),
timezoneIdentifier: dataObject.timezone(),
locale: currentLocale)
let convertedDate = dateFormatter.string(from: newDate)
XCTAssertNotNil(convertedDate)
}
}
}