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

Delta Between Two Patch Sets: chrome/content/tests/notification.js

Issue 11144022: Add tests for the Notification module (Closed)
Left Patch Set: Created July 18, 2013, 12:23 p.m.
Right Patch Set: Test downloading (changes by Wladimir) Created July 18, 2013, 5:58 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 (function() 1 (function()
2 { 2 {
3 let testRunner = null;
4 let server = null;
5 let randomResult = 0.5;
6
3 let originalApplication; 7 let originalApplication;
4 let originalAddonVersion; 8 let originalAddonVersion;
5 let info = require("info"); 9 let info = require("info");
6 let {Notification} = require("notification");
7 10
8 module("Notification", 11 module("Notification",
9 { 12 {
10 setup: function() 13 setup: function()
11 { 14 {
15 testRunner = this;
16
17 preparePrefs.call(this);
18 setupVirtualTime.call(this, function(wrapTimer)
19 {
20 let NotificationModule = getModuleGlobal("notification");
21 NotificationModule.downloader._timer = wrapTimer(NotificationModule.down loader._timer);
22 }, "notification", "downloader");
23
24 server = new nsHttpServer();
25 server.start(1234);
26
12 originalApplication = info.application; 27 originalApplication = info.application;
13 info.application = "chrome"; 28 info.application = "chrome";
14 originalAddonVersion = info.addonVersion; 29 originalAddonVersion = info.addonVersion;
15 info.addonVersion = "1.4.1"; 30 info.addonVersion = "1.4.1";
16 Prefs.shownNotifications = null; 31
32 Prefs.notificationurl = "http://127.0.0.1:1234/notification.json";
33 Prefs.notificationdata = {};
34
35 // Replace Math.random() function
36 let DownloaderGlobal = Cu.getGlobalForObject(getModuleGlobal("downloader") );
37 this._origRandom = DownloaderGlobal.Math.random;
38 DownloaderGlobal.Math.random = function() randomResult;
39 randomResult = 0.5;
Felix Dahlke 2013/07/19 14:30:41 This seems redundant, looks like randomResult is a
Wladimir Palant 2013/07/19 14:58:49 Actually, the point here is to reset randomResult
17 }, 40 },
41
18 teardown: function() 42 teardown: function()
19 { 43 {
44 restorePrefs.call(this);
45 restoreVirtualTime.call(this);
46
47 stop();
48 server.stop(function()
49 {
50 server = null;
51 start();
52 });
53
20 info.application = originalApplication; 54 info.application = originalApplication;
21 info.addonVersion = originalAddonVersion; 55 info.addonVersion = originalAddonVersion;
56
57 if (this._origRandom)
58 {
59 let DownloaderGlobal = Cu.getGlobalForObject(getModuleGlobal("downloader "));
60 DownloaderGlobal.Math.random = this._origRandom;
61 delete this._origRandom;
62 }
63
64 Notification.init();
22 } 65 }
23 }); 66 });
24 67
25 test("Single notification", 1, function() 68 function registerHandler(notifications)
Wladimir Palant 2013/07/19 08:18:29 I would rather not specify the number of checks ex
Felix Dahlke 2013/07/19 14:30:40 Agreed, that was a misconception on my side. You a
26 { 69 {
27 let information = { 70 server.registerPathHandler("/notification.json", function(metadata, response )
71 {
72 response.setStatusLine("1.1", "200", "OK");
73 response.setHeader("Content-Type", "application/json");
74
75 let notification = {
76 version: 55,
77 notifications: notifications
78 };
79
80 let result = JSON.stringify(notification);
81 response.bodyOutputStream.write(result, result.length);
82 });
83 }
84
85 function fixConstructors(object)
86 {
87 // deepEqual() expects that the constructors used in expected objects and
88 // the ones in the actual results are the same. That means that we actually
89 // have to construct our objects in the context of the notification module.
90 let JSON = Cu.getGlobalForObject(Notification).JSON;
91 return JSON.parse(JSON.stringify(object));
92 }
93
94 test("No data", 1, function()
95 {
96 equal(Notification.getNextToShow(), null, "null should be returned if there is no data");
97 });
98
99 test("Single notification", 2, function()
100 {
101 let information = fixConstructors({
28 timestamp: 1, 102 timestamp: 1,
29 severity: "information", 103 severity: "information",
30 message: {en: "Information"} 104 message: {en: "Information"}
31 }; 105 });
32 let notification = Notification.getNextToShow([information]); 106
33 equal(notification, information); 107 registerHandler([information]);
34 }); 108 testRunner.runScheduledTasks(1);
35 109
36 test("Information and critical", 1, function() 110 deepEqual(Notification.getNextToShow(), information, "The notification is sh own");
37 { 111 equal(Notification.getNextToShow(), null, "Informational notifications aren' t shown more than once");
38 let information = { 112 });
113
114 test("Information and critical", 2, function()
115 {
116 let information = fixConstructors({
39 timestamp: 1, 117 timestamp: 1,
40 severity: "information", 118 severity: "information",
41 message: {en: "Information"} 119 message: {en: "Information"}
42 }; 120 });
43 let critical = { 121 let critical = fixConstructors({
44 timestamp: 2, 122 timestamp: 2,
45 severity: "critical", 123 severity: "critical",
46 message: {en: "Critical"} 124 message: {en: "Critical"}
47 }; 125 });
48 let notification = Notification.getNextToShow([information, critical]); 126
49 equal(notification, critical); 127 registerHandler([information, critical]);
50 }); 128 testRunner.runScheduledTasks(1);
51 129
52 test("Different platforms", 1, function() 130 deepEqual(Notification.getNextToShow(), critical, "The critical notification is given priority");
53 { 131 deepEqual(Notification.getNextToShow(), critical, "Critical notifications ca n be shown multiple times");
54 let information = { 132 });
133
134 test("No severity", 2, function()
135 {
136 let information = fixConstructors({
137 timestamp: 1,
138 message: {en: "Information"}
139 });
140
141 registerHandler([information]);
142 testRunner.runScheduledTasks(1);
143
144 deepEqual(Notification.getNextToShow(), information, "The notification is sh own");
145 equal(Notification.getNextToShow(), null, "Notification is treated as severi ty information");
146 });
147
148 test("Different platforms", 2, function()
149 {
150 let information = fixConstructors({
55 timestamp: 1, 151 timestamp: 1,
56 severity: "information", 152 severity: "information",
57 message: {en: "Information"}, 153 message: {en: "Information"},
58 platforms: ["chrome", "firefox"] 154 platforms: ["chrome", "firefox"]
59 }; 155 });
60 let critical = { 156 let critical = fixConstructors({
61 timestamp: 2, 157 timestamp: 2,
62 severity: "critical", 158 severity: "critical",
63 message: {en: "Critical"}, 159 message: {en: "Critical"},
64 platforms: ["firefox"] 160 platforms: ["firefox"]
65 }; 161 });
66 let notification = Notification.getNextToShow([information, critical]); 162
67 equal(notification, information); 163 registerHandler([information, critical]);
68 }); 164 testRunner.runScheduledTasks(1);
69 165
70 test("Min version", 1, function() 166 deepEqual(Notification.getNextToShow(), information, "Critical notification is ignored if platform doesn't match");
71 { 167 deepEqual(Notification.getNextToShow(), null, "Critical notification still i gnored even if no other notifications available");
72 let information = { 168 });
169
170 test("Min version", 2, function()
171 {
172 let information = fixConstructors({
73 timestamp: 1, 173 timestamp: 1,
74 severity: "information", 174 severity: "information",
75 message: {en: "Information"}, 175 message: {en: "Information"},
76 minVersion: "1.4" 176 minVersion: "1.4"
77 }; 177 });
78 let critical = { 178 let critical = fixConstructors({
79 timestamp: 2, 179 timestamp: 2,
80 severity: "critical", 180 severity: "critical",
81 message: {en: "Critical"}, 181 message: {en: "Critical"},
82 minVersion: "1.5" 182 minVersion: "1.5"
83 }; 183 });
84 let notification = Notification.getNextToShow([information, critical]); 184
85 equal(notification, information); 185 registerHandler([information, critical]);
86 }); 186 testRunner.runScheduledTasks(1);
87 187
88 test("Max version", 1, function() 188 deepEqual(Notification.getNextToShow(), information, "Critical notification is ignored if minVersion doesn't match");
89 { 189 deepEqual(Notification.getNextToShow(), null, "Critical notification still i gnored even if no other notifications available");
90 let information = { 190 });
191
192 test("Max version", 2, function()
193 {
194 let information = fixConstructors({
91 timestamp: 1, 195 timestamp: 1,
92 severity: "information", 196 severity: "information",
93 message: {en: "Information"}, 197 message: {en: "Information"},
94 maxVersion: "1.5" 198 maxVersion: "1.5"
95 }; 199 });
96 let critical = { 200 let critical = fixConstructors({
97 timestamp: 2, 201 timestamp: 2,
98 severity: "critical", 202 severity: "critical",
99 message: {en: "Critical"}, 203 message: {en: "Critical"},
100 maxVersion: "1.4" 204 maxVersion: "1.4"
101 }; 205 });
102 let notification = Notification.getNextToShow([information, critical]); 206
103 equal(notification, information); 207 registerHandler([information, critical]);
104 }); 208 testRunner.runScheduledTasks(1);
105 209
106 test("Information notifications appear just once", 2, function() 210 deepEqual(Notification.getNextToShow(), information, "Critical notification is ignored if maxVersion doesn't match");
107 { 211 deepEqual(Notification.getNextToShow(), null, "Critical notification still i gnored even if no other notifications available");
108 let information = {
109 timestamp: 1,
110 severity: "information",
111 message: {en: "Information"}
112 };
113 let notification = Notification.getNextToShow([information]);
114 equal(notification, information);
115 notification = Notification.getNextToShow([information]);
116 ok(!notification, "Notification shouldn't be shown twice");
117 });
118
119 test("Critical notifications appear every time", 2, function()
120 {
121 let critical = {
122 timestamp: 1,
123 severity: "critical",
124 message: {en: "Critical"}
125 };
126 let notification = Notification.getNextToShow([critical]);
127 equal(notification, critical);
128 notification = Notification.getNextToShow([critical]);
129 equal(notification, critical);
130 }); 212 });
131 })(); 213 })();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld