Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: chrome/content/tests/prefs.js

Issue 5203041586249728: Issue 2040 - Adapted tests for chrome.storage.local (Closed)
Patch Set: Created Feb. 26, 2015, 12:12 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 (function() 1 (function()
2 { 2 {
3 module("Preferences", 3 module("Preferences",
4 { 4 {
5 setup: function() 5 setup: function()
6 { 6 {
7 preparePrefs.call(this); 7 preparePrefs.call(this);
8 }, 8 },
9 9
10 teardown: function() 10 teardown: function()
11 { 11 {
12 restorePrefs.call(this); 12 restorePrefs.call(this);
13 } 13 }
14 }); 14 });
15 15
16 function prefExists(name) 16 function checkPrefExists(name, expectedValue, description, assert)
17 { 17 {
18 if ("chrome" in window) 18 if ("chrome" in window)
Wladimir Palant 2015/02/26 14:43:11 Looks like this test doesn't consider Safari. Is t
Sebastian Noack 2015/02/26 16:03:43 Tests are currently and ever since pretty much bro
19 return name in localStorage; 19 {
20 let done = assert.async();
21 » let key = "pref:" + name;
Wladimir Palant 2015/02/26 14:43:11 Nit: no smart tabs please.
Sebastian Noack 2015/02/26 16:03:43 Just regular tabs. I'm still not a fan of spaces.
22 chrome.storage.local.get([key], function(items)
Wladimir Palant 2015/02/26 14:43:11 According to documentation, passing in key without
Sebastian Noack 2015/02/26 16:03:43 Yes, it does, but I find it weird. Since you alway
23 {
24 equal(key in items, expectedValue, description);
25 done();
26 });
27 }
20 else 28 else
21 return Services.prefs.prefHasUserValue("extensions.adblockplus." + name); 29 {
30 equal(Services.prefs.prefHasUserValue("extensions.adblockplus." + name), e xpectedValue, description);
31 }
22 } 32 }
23 33
24 function checkPref(name, expectedValue, description) 34 function checkPref(name, expectedValue, description, assert)
25 { 35 {
26 let value = null;
27 if ("chrome" in window) 36 if ("chrome" in window)
28 { 37 {
29 try 38 let done = assert.async();
39 » let key = "pref:" + name;
Wladimir Palant 2015/02/26 14:43:11 Nit: no smart tabs please.
Sebastian Noack 2015/02/26 16:03:43 Done, here as well.
40 chrome.storage.local.get([key], function(items)
Wladimir Palant 2015/02/26 14:43:11 As above, passing in key without wrapping it in an
Sebastian Noack 2015/02/26 16:03:43 Done, here as wel.
30 { 41 {
31 value = JSON.parse(localStorage[name]); 42 deepEqual(items[key], expectedValue, description);
32 } 43 done();
33 catch (e) 44 });
34 {
35 Cu.reportError(e);
36 }
37 } 45 }
38 else 46 else
39 { 47 {
40 let pref = "extensions.adblockplus." + name; 48 let pref = "extensions.adblockplus." + name;
49 let value = null;
50
41 switch (typeof expectedValue) 51 switch (typeof expectedValue)
42 { 52 {
43 case "number": 53 case "number":
44 value = Services.prefs.getIntPref(pref); 54 value = Services.prefs.getIntPref(pref);
45 break; 55 break;
46 case "boolean": 56 case "boolean":
47 value = Services.prefs.getBoolPref(pref); 57 value = Services.prefs.getBoolPref(pref);
48 break; 58 break;
49 case "string": 59 case "string":
50 value = Services.prefs.getComplexValue(pref, Ci.nsISupportsString).dat a; 60 value = Services.prefs.getComplexValue(pref, Ci.nsISupportsString).dat a;
51 break; 61 break;
52 case "object": 62 case "object":
53 value = JSON.parse(Services.prefs.getComplexValue(pref, Ci.nsISupports String).data); 63 value = JSON.parse(Services.prefs.getComplexValue(pref, Ci.nsISupports String).data);
54 break; 64 break;
55 } 65 }
66
67 deepEqual(value, expectedValue, description);
56 } 68 }
57 deepEqual(value, expectedValue, description);
58 } 69 }
59 70
60 test("Numerical pref", function() 71 test("Numerical pref", function(assert)
61 { 72 {
62 Prefs.patternsbackups = 5; 73 Prefs.patternsbackups = 5;
63 equal(Prefs.patternsbackups, 5, "Prefs object returns the correct value afte r setting pref to default value"); 74 equal(Prefs.patternsbackups, 5, "Prefs object returns the correct value afte r setting pref to default value");
64 equal(prefExists("patternsbackups"), false, "User-defined pref has been remo ved"); 75 checkPrefExists("patternsbackups", false, "User-defined pref has been remove d", assert);
65 Prefs.patternsbackups = 12; 76 Prefs.patternsbackups = 12;
66 equal(Prefs.patternsbackups, 12, "Prefs object returns the correct value aft er setting pref to non-default value"); 77 equal(Prefs.patternsbackups, 12, "Prefs object returns the correct value aft er setting pref to non-default value");
67 equal(prefExists("patternsbackups"), true, "User-defined pref has been creat ed"); 78 checkPrefExists("patternsbackups", true, "User-defined pref has been created ", assert);
68 checkPref("patternsbackups", 12, "Value has been written"); 79 checkPref("patternsbackups", 12, "Value has been written", assert);
69 }); 80 });
70 81
71 test("Boolean pref", function() 82 test("Boolean pref", function(assert)
72 { 83 {
73 Prefs.enabled = true; 84 Prefs.enabled = true;
74 equal(Prefs.enabled, true, "Prefs object returns the correct value after set ting pref to default value"); 85 equal(Prefs.enabled, true, "Prefs object returns the correct value after set ting pref to default value");
75 equal(prefExists("enabled"), false, "User-defined pref has been removed"); 86 checkPrefExists("enabled", false, "User-defined pref has been removed", asse rt);
76 Prefs.enabled = false; 87 Prefs.enabled = false;
77 equal(Prefs.enabled, false, "Prefs object returns the correct value after se tting pref to non-default value"); 88 equal(Prefs.enabled, false, "Prefs object returns the correct value after se tting pref to non-default value");
78 equal(prefExists("enabled"), true, "User-defined pref has been created"); 89 checkPrefExists("enabled", true, "User-defined pref has been created", asser t);
79 checkPref("enabled", false, "Value has been written"); 90 checkPref("enabled", false, "Value has been written", assert);
80 }); 91 });
81 92
82 test("String pref", function() 93 test("String pref", function(assert)
83 { 94 {
84 let defaultValue = "https://notification.adblockplus.org/notification.json"; 95 let defaultValue = "https://notification.adblockplus.org/notification.json";
85 Prefs.notificationurl = defaultValue; 96 Prefs.notificationurl = defaultValue;
86 equal(Prefs.notificationurl, defaultValue, "Prefs object returns the correct value after setting pref to default value"); 97 equal(Prefs.notificationurl, defaultValue, "Prefs object returns the correct value after setting pref to default value");
87 equal(prefExists("notificationurl"), false, "User-defined pref has been remo ved"); 98 checkPrefExists("notificationurl", false, "User-defined pref has been remove d", assert);
88 99
89 let newValue = "https://notification.adblockplus.org/foo\u1234bar.json"; 100 let newValue = "https://notification.adblockplus.org/foo\u1234bar.json";
90 Prefs.notificationurl = newValue; 101 Prefs.notificationurl = newValue;
91 equal(Prefs.notificationurl, newValue, "Prefs object returns the correct val ue after setting pref to non-default value"); 102 equal(Prefs.notificationurl, newValue, "Prefs object returns the correct val ue after setting pref to non-default value");
92 equal(prefExists("notificationurl"), true, "User-defined pref has been creat ed"); 103 checkPrefExists("notificationurl", true, "User-defined pref has been created ", assert);
93 checkPref("notificationurl", newValue, "Value has been written"); 104 checkPref("notificationurl", newValue, "Value has been written", assert);
94 }); 105 });
95 106
96 test("Object pref (complete replacement)", function() 107 test("Object pref (complete replacement)", function(assert)
97 { 108 {
98 Prefs.notificationdata = {}; 109 Prefs.notificationdata = {};
99 deepEqual(Prefs.notificationdata, {}, "Prefs object returns the correct valu e after setting pref to default value"); 110 deepEqual(Prefs.notificationdata, {}, "Prefs object returns the correct valu e after setting pref to default value");
100 equal(prefExists("notificationdata"), false, "User-defined pref has been rem oved");
Sebastian Noack 2015/02/26 12:52:38 Do we really need to make sure that the item is re
Wladimir Palant 2015/02/26 14:43:11 The point of this test isn't really ensuring that
Sebastian Noack 2015/02/26 16:03:43 So I suppose removing this line (and the same belo
101 111
102 let newValue = {foo:1, bar: "adsf\u1234"}; 112 let newValue = {foo:1, bar: "adsf\u1234"};
103 Prefs.notificationdata = newValue; 113 Prefs.notificationdata = newValue;
104 equal(Prefs.notificationdata, newValue, "Prefs object returns the correct va lue after setting pref to non-default value"); 114 equal(Prefs.notificationdata, newValue, "Prefs object returns the correct va lue after setting pref to non-default value");
105 equal(prefExists("notificationdata"), true, "User-defined pref has been crea ted"); 115 checkPrefExists("notificationdata", true, "User-defined pref has been create d", assert);
106 checkPref("notificationdata", newValue, "Value has been written"); 116 checkPref("notificationdata", newValue, "Value has been written", assert);
107 }); 117 });
108 118
109 test("Property-wise modification", function() 119 test("Property-wise modification", function(assert)
110 { 120 {
111 Prefs.notificationdata = {}; 121 Prefs.notificationdata = {};
112 122
113 Prefs.notificationdata.foo = 1; 123 Prefs.notificationdata.foo = 1;
114 Prefs.notificationdata.bar = 2; 124 Prefs.notificationdata.bar = 2;
115 Prefs.notificationdata = JSON.parse(JSON.stringify(Prefs.notificationdata)); 125 Prefs.notificationdata = JSON.parse(JSON.stringify(Prefs.notificationdata));
116 deepEqual(Prefs.notificationdata, {foo:1, bar: 2}, "Prefs object returns the correct value after setting pref to non-default value"); 126 deepEqual(Prefs.notificationdata, {foo:1, bar: 2}, "Prefs object returns the correct value after setting pref to non-default value");
117 equal(prefExists("notificationdata"), true, "User-defined pref has been crea ted"); 127 checkPrefExists("notificationdata", true, "User-defined pref has been create d", assert);
118 checkPref("notificationdata", {foo:1, bar: 2}, "Value has been written"); 128 checkPref("notificationdata", {foo:1, bar: 2}, "Value has been written", ass ert);
119 129
120 delete Prefs.notificationdata.foo; 130 delete Prefs.notificationdata.foo;
121 delete Prefs.notificationdata.bar; 131 delete Prefs.notificationdata.bar;
122 Prefs.notificationdata = JSON.parse(JSON.stringify(Prefs.notificationdata)); 132 Prefs.notificationdata = JSON.parse(JSON.stringify(Prefs.notificationdata));
123 deepEqual(Prefs.notificationdata, {}, "Prefs object returns the correct valu e after setting pref to default value"); 133 deepEqual(Prefs.notificationdata, {}, "Prefs object returns the correct valu e after setting pref to default value");
124 equal(prefExists("notificationdata"), false, "User-defined pref has been rem oved");
125 }); 134 });
126 })(); 135 })();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld