LEFT | RIGHT |
1 /* globals preparePrefs, restorePrefs, Prefs */ | |
2 | |
3 "use strict"; | 1 "use strict"; |
4 | 2 |
5 /* eslint-disable max-len */ | 3 (function() |
6 { | 4 { |
| 5 const {Prefs} = require("prefs"); |
| 6 |
7 module("Preferences", { | 7 module("Preferences", { |
8 setup() | 8 setup() |
9 { | 9 { |
10 preparePrefs.call(this); | 10 this._pbackup = Object.create(null); |
| 11 for (let pref in Prefs) |
| 12 { |
| 13 let value = Prefs[pref]; |
| 14 this._pbackup[pref] = value; |
| 15 } |
| 16 Prefs.enabled = true; |
11 }, | 17 }, |
12 | 18 |
13 teardown() | 19 teardown() |
14 { | 20 { |
15 restorePrefs.call(this); | 21 for (let pref in this._pbackup) |
| 22 Prefs[pref] = this._pbackup[pref]; |
16 } | 23 } |
17 }); | 24 }); |
18 | 25 |
19 function checkPrefExists(name, expectedValue, description, assert) | 26 function checkPrefExists(name, expectedValue, description, assert) |
20 { | 27 { |
21 let done = assert.async(); | 28 let done = assert.async(); |
22 let key = "pref:" + name; | 29 let key = "pref:" + name; |
23 chrome.storage.local.get(key, items => | 30 chrome.storage.local.get(key, items => |
24 { | 31 { |
25 equal(key in items, expectedValue, description); | 32 equal(key in items, expectedValue, description); |
(...skipping 10 matching lines...) Expand all Loading... |
36 deepEqual(items[key], expectedValue, description); | 43 deepEqual(items[key], expectedValue, description); |
37 done(); | 44 done(); |
38 }); | 45 }); |
39 } | 46 } |
40 | 47 |
41 test("Numerical pref", assert => | 48 test("Numerical pref", assert => |
42 { | 49 { |
43 Prefs.patternsbackups = 5; | 50 Prefs.patternsbackups = 5; |
44 equal( | 51 equal( |
45 Prefs.patternsbackups, 5, | 52 Prefs.patternsbackups, 5, |
46 "Prefs object returns the correct value after setting pref to default valu
e" | 53 "Prefs object returns the correct value after setting pref to " + |
| 54 "default value" |
47 ); | 55 ); |
48 checkPrefExists( | 56 checkPrefExists( |
49 "patternsbackups", false, "User-defined pref has been removed", assert | 57 "patternsbackups", false, "User-defined pref has been removed", assert |
50 ); | 58 ); |
51 Prefs.patternsbackups = 12; | 59 Prefs.patternsbackups = 12; |
52 equal( | 60 equal( |
53 Prefs.patternsbackups, 12, | 61 Prefs.patternsbackups, 12, |
54 "Prefs object returns the correct value after setting pref to non-default
value" | 62 "Prefs object returns the correct value after setting pref to " + |
| 63 "non-default value" |
55 ); | 64 ); |
56 checkPrefExists( | 65 checkPrefExists( |
57 "patternsbackups", true, "User-defined pref has been created", assert | 66 "patternsbackups", true, "User-defined pref has been created", assert |
58 ); | 67 ); |
59 checkPref("patternsbackups", 12, "Value has been written", assert); | 68 checkPref("patternsbackups", 12, "Value has been written", assert); |
60 }); | 69 }); |
61 | 70 |
62 test("Boolean pref", assert => | 71 test("Boolean pref", assert => |
63 { | 72 { |
64 Prefs.enabled = true; | 73 Prefs.enabled = true; |
65 equal( | 74 equal( |
66 Prefs.enabled, true, | 75 Prefs.enabled, true, |
67 "Prefs object returns the correct value after setting pref to default valu
e" | 76 "Prefs object returns the correct value after setting pref to " + |
| 77 "default value" |
68 ); | 78 ); |
69 checkPrefExists("enabled", false, "User-defined pref has been removed", | 79 checkPrefExists("enabled", false, "User-defined pref has been removed", |
70 assert); | 80 assert); |
71 Prefs.enabled = false; | 81 Prefs.enabled = false; |
72 equal( | 82 equal( |
73 Prefs.enabled, false, | 83 Prefs.enabled, false, |
74 "Prefs object returns the correct value after setting pref to non-default
value" | 84 "Prefs object returns the correct value after setting pref to " + |
| 85 "non-default value" |
75 ); | 86 ); |
76 checkPrefExists("enabled", true, "User-defined pref has been created", | 87 checkPrefExists("enabled", true, "User-defined pref has been created", |
77 assert); | 88 assert); |
78 checkPref("enabled", false, "Value has been written", assert); | 89 checkPref("enabled", false, "Value has been written", assert); |
79 }); | 90 }); |
80 | 91 |
81 test("String pref", assert => | 92 test("String pref", assert => |
82 { | 93 { |
83 let defaultValue = "https://notification.adblockplus.org/notification.json"; | 94 let defaultValue = "https://notification.adblockplus.org/notification.json"; |
84 Prefs.notificationurl = defaultValue; | 95 Prefs.notificationurl = defaultValue; |
85 equal( | 96 equal( |
86 Prefs.notificationurl, defaultValue, | 97 Prefs.notificationurl, defaultValue, |
87 "Prefs object returns the correct value after setting pref to default valu
e" | 98 "Prefs object returns the correct value after setting pref to " + |
| 99 "default value" |
88 ); | 100 ); |
89 checkPrefExists("notificationurl", false, | 101 checkPrefExists("notificationurl", false, |
90 "User-defined pref has been removed", assert); | 102 "User-defined pref has been removed", assert); |
91 | 103 |
92 let newValue = "https://notification.adblockplus.org/foo\u1234bar.json"; | 104 let newValue = "https://notification.adblockplus.org/foo\u1234bar.json"; |
93 Prefs.notificationurl = newValue; | 105 Prefs.notificationurl = newValue; |
94 equal( | 106 equal( |
95 Prefs.notificationurl, newValue, | 107 Prefs.notificationurl, newValue, |
96 "Prefs object returns the correct value after setting pref to non-default
value" | 108 "Prefs object returns the correct value after setting pref to " + |
| 109 "non-default value" |
97 ); | 110 ); |
98 checkPrefExists("notificationurl", true, | 111 checkPrefExists("notificationurl", true, |
99 "User-defined pref has been created", assert); | 112 "User-defined pref has been created", assert); |
100 checkPref("notificationurl", newValue, "Value has been written", assert); | 113 checkPref("notificationurl", newValue, "Value has been written", assert); |
101 }); | 114 }); |
102 | 115 |
103 test("Object pref (complete replacement)", assert => | 116 test("Object pref (complete replacement)", assert => |
104 { | 117 { |
105 Prefs.notificationdata = {}; | 118 Prefs.notificationdata = {}; |
106 deepEqual( | 119 deepEqual( |
107 Prefs.notificationdata, {}, | 120 Prefs.notificationdata, {}, |
108 "Prefs object returns the correct value after setting pref to default valu
e" | 121 "Prefs object returns the correct value after setting pref to " + |
| 122 "default value" |
109 ); | 123 ); |
110 | 124 |
111 let newValue = {foo: 1, bar: "adsf\u1234"}; | 125 let newValue = {foo: 1, bar: "adsf\u1234"}; |
112 Prefs.notificationdata = newValue; | 126 Prefs.notificationdata = newValue; |
113 equal( | 127 equal( |
114 Prefs.notificationdata, newValue, | 128 Prefs.notificationdata, newValue, |
115 "Prefs object returns the correct value after setting pref to non-default
value" | 129 "Prefs object returns the correct value after setting pref to " + |
| 130 "non-default value" |
116 ); | 131 ); |
117 checkPrefExists("notificationdata", true, | 132 checkPrefExists("notificationdata", true, |
118 "User-defined pref has been created", assert); | 133 "User-defined pref has been created", assert); |
119 checkPref("notificationdata", newValue, "Value has been written", assert); | 134 checkPref("notificationdata", newValue, "Value has been written", assert); |
120 }); | 135 }); |
121 | 136 |
122 test("Property-wise modification", assert => | 137 test("Property-wise modification", assert => |
123 { | 138 { |
124 Prefs.notificationdata = {}; | 139 Prefs.notificationdata = {}; |
125 | 140 |
126 Prefs.notificationdata.foo = 1; | 141 Prefs.notificationdata.foo = 1; |
127 Prefs.notificationdata.bar = 2; | 142 Prefs.notificationdata.bar = 2; |
128 Prefs.notificationdata = JSON.parse(JSON.stringify(Prefs.notificationdata)); | 143 Prefs.notificationdata = JSON.parse(JSON.stringify(Prefs.notificationdata)); |
129 deepEqual( | 144 deepEqual( |
130 Prefs.notificationdata, {foo: 1, bar: 2}, | 145 Prefs.notificationdata, {foo: 1, bar: 2}, |
131 "Prefs object returns the correct value after setting pref to non-default
value" | 146 "Prefs object returns the correct value after setting pref to " + |
| 147 "non-default value" |
132 ); | 148 ); |
133 checkPrefExists("notificationdata", true, | 149 checkPrefExists("notificationdata", true, |
134 "User-defined pref has been created", assert); | 150 "User-defined pref has been created", assert); |
135 checkPref("notificationdata", {foo: 1, bar: 2}, "Value has been written", | 151 checkPref("notificationdata", {foo: 1, bar: 2}, "Value has been written", |
136 assert); | 152 assert); |
137 | 153 |
138 delete Prefs.notificationdata.foo; | 154 delete Prefs.notificationdata.foo; |
139 delete Prefs.notificationdata.bar; | 155 delete Prefs.notificationdata.bar; |
140 Prefs.notificationdata = JSON.parse(JSON.stringify(Prefs.notificationdata)); | 156 Prefs.notificationdata = JSON.parse(JSON.stringify(Prefs.notificationdata)); |
141 deepEqual( | 157 deepEqual( |
142 Prefs.notificationdata, {}, | 158 Prefs.notificationdata, {}, |
143 "Prefs object returns the correct value after setting pref to default valu
e" | 159 "Prefs object returns the correct value after setting pref to " + |
| 160 "default value" |
144 ); | 161 ); |
145 }); | 162 }); |
146 } | 163 }()); |
147 /* eslint-enable max-len */ | |
LEFT | RIGHT |