Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
@testable import Client
6
import Foundation
7
import Shared
8
9
import XCTest
10
11
class PrefsTests: XCTestCase {
12
var prefs: NSUserDefaultsPrefs!
13
14
override func setUp() {
15
super.setUp()
16
prefs = NSUserDefaultsPrefs(prefix: "PrefsTests")
17
}
18
19
override func tearDown() {
20
prefs.clearAll()
21
super.tearDown()
22
}
23
24
func testClearPrefs() {
25
prefs.setObject("foo", forKey: "bar")
26
XCTAssertEqual(prefs.stringForKey("bar")!, "foo")
27
28
// Ensure clearing prefs is branch-specific.
29
let otherPrefs = NSUserDefaultsPrefs(prefix: "othermockaccount")
30
otherPrefs.clearAll()
31
XCTAssertEqual(prefs.stringForKey("bar")!, "foo")
32
33
prefs.clearAll()
34
XCTAssertNil(prefs.stringForKey("bar"))
35
}
36
37
func testStringForKey() {
38
XCTAssertNil(prefs.stringForKey("key"))
39
prefs.setObject("value", forKey: "key")
40
XCTAssertEqual(prefs.stringForKey("key")!, "value")
41
// Non-String values return nil.
42
prefs.setObject(1, forKey: "key")
43
XCTAssertNil(prefs.stringForKey("key"))
44
}
45
46
func testBoolForKey() {
47
XCTAssertNil(prefs.boolForKey("key"))
48
prefs.setObject(true, forKey: "key")
49
XCTAssertEqual(prefs.boolForKey("key")!, true)
50
prefs.setObject(false, forKey: "key")
51
XCTAssertEqual(prefs.boolForKey("key")!, false)
52
// We would like non-Bool values to return nil, but I can't figure out how to differentiate.
53
// Instead, this documents the undesired behaviour.
54
prefs.setObject(1, forKey: "key")
55
XCTAssertEqual(prefs.boolForKey("key")!, true)
56
prefs.setObject("1", forKey: "key")
57
XCTAssertNil(prefs.boolForKey("key"))
58
prefs.setObject("x", forKey: "key")
59
XCTAssertNil(prefs.boolForKey("key"))
60
}
61
62
func testStringArrayForKey() {
63
XCTAssertNil(prefs.stringArrayForKey("key"))
64
prefs.setObject(["value1", "value2"], forKey: "key")
65
XCTAssertEqual(prefs.stringArrayForKey("key")!, ["value1", "value2"])
66
// Non-[String] values return nil.
67
prefs.setObject(1, forKey: "key")
68
XCTAssertNil(prefs.stringArrayForKey("key"))
69
// [Non-String] values return nil.
70
prefs.setObject([1, 2], forKey: "key")
71
XCTAssertNil(prefs.stringArrayForKey("key"))
72
}
73
74
func testMockProfilePrefsRoundtripsTimestamps() {
75
let prefs = MockProfilePrefs().branch("baz")
76
let val: Timestamp = Date.now()
77
prefs.setLong(val, forKey: "foobar")
78
XCTAssertEqual(val, prefs.unsignedLongForKey("foobar")!)
79
}
80
81
func testMockProfilePrefsKeys() {
82
let prefs = MockProfilePrefs().branch("baz") as! MockProfilePrefs
83
let val: Timestamp = Date.now()
84
prefs.setLong(val, forKey: "foobar")
85
XCTAssertEqual(val, (prefs.things["baz.foobar"] as! NSNumber).uint64Value)
86
}
87
88
func testMockProfilePrefsClearAll() {
89
let prefs1 = MockProfilePrefs().branch("bar") as! MockProfilePrefs
90
let prefs2 = MockProfilePrefs().branch("baz") as! MockProfilePrefs
91
92
// Ensure clearing prefs is branch-specific.
93
prefs1.setInt(123, forKey: "foo")
94
prefs2.clearAll()
95
XCTAssertEqual(123, prefs1.intForKey("foo")!)
96
97
prefs1.clearAll()
98
XCTAssertNil(prefs1.intForKey("foo") as AnyObject?)
99
}
100
}