| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 (function() | |
| 2 { | |
| 3 let testRunner = null; | |
| 4 let server = null; | |
| 5 let randomResult = 0.5; | |
| 6 | |
| 7 let originalApplication; | |
| 8 let originalAddonVersion; | |
| 9 let info = require("info"); | |
| 10 | |
| 11 module("Notification", | |
| 12 { | |
| 13 setup: function() | |
| 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 | |
| 27 originalApplication = info.application; | |
| 28 info.application = "chrome"; | |
| 29 originalAddonVersion = info.addonVersion; | |
| 30 info.addonVersion = "1.4.1"; | |
| 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
| |
| 40 }, | |
| 41 | |
| 42 teardown: function() | |
| 43 { | |
| 44 restorePrefs.call(this); | |
| 45 restoreVirtualTime.call(this); | |
| 46 | |
| 47 stop(); | |
| 48 server.stop(function() | |
| 49 { | |
| 50 server = null; | |
| 51 start(); | |
| 52 }); | |
| 53 | |
| 54 info.application = originalApplication; | |
| 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(); | |
| 65 } | |
| 66 }); | |
| 67 | |
| 68 function registerHandler(notifications) | |
| 69 { | |
| 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({ | |
| 102 timestamp: 1, | |
| 103 severity: "information", | |
| 104 message: {en: "Information"} | |
| 105 }); | |
| 106 | |
| 107 registerHandler([information]); | |
| 108 testRunner.runScheduledTasks(1); | |
| 109 | |
| 110 deepEqual(Notification.getNextToShow(), information, "The notification is sh own"); | |
| 111 equal(Notification.getNextToShow(), null, "Informational notifications aren' t shown more than once"); | |
| 112 }); | |
| 113 | |
| 114 test("Information and critical", 2, function() | |
| 115 { | |
| 116 let information = fixConstructors({ | |
| 117 timestamp: 1, | |
| 118 severity: "information", | |
| 119 message: {en: "Information"} | |
| 120 }); | |
| 121 let critical = fixConstructors({ | |
| 122 timestamp: 2, | |
| 123 severity: "critical", | |
| 124 message: {en: "Critical"} | |
| 125 }); | |
| 126 | |
| 127 registerHandler([information, critical]); | |
| 128 testRunner.runScheduledTasks(1); | |
| 129 | |
| 130 deepEqual(Notification.getNextToShow(), critical, "The critical notification is given priority"); | |
| 131 deepEqual(Notification.getNextToShow(), critical, "Critical notifications ca n be shown multiple times"); | |
| 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({ | |
| 151 timestamp: 1, | |
| 152 severity: "information", | |
| 153 message: {en: "Information"}, | |
| 154 platforms: ["chrome", "firefox"] | |
| 155 }); | |
| 156 let critical = fixConstructors({ | |
| 157 timestamp: 2, | |
| 158 severity: "critical", | |
| 159 message: {en: "Critical"}, | |
| 160 platforms: ["firefox"] | |
| 161 }); | |
| 162 | |
| 163 registerHandler([information, critical]); | |
| 164 testRunner.runScheduledTasks(1); | |
| 165 | |
| 166 deepEqual(Notification.getNextToShow(), information, "Critical notification is ignored if platform doesn't match"); | |
| 167 deepEqual(Notification.getNextToShow(), null, "Critical notification still i gnored even if no other notifications available"); | |
| 168 }); | |
| 169 | |
| 170 test("Min version", 2, function() | |
| 171 { | |
| 172 let information = fixConstructors({ | |
| 173 timestamp: 1, | |
| 174 severity: "information", | |
| 175 message: {en: "Information"}, | |
| 176 minVersion: "1.4" | |
| 177 }); | |
| 178 let critical = fixConstructors({ | |
| 179 timestamp: 2, | |
| 180 severity: "critical", | |
| 181 message: {en: "Critical"}, | |
| 182 minVersion: "1.5" | |
| 183 }); | |
| 184 | |
| 185 registerHandler([information, critical]); | |
| 186 testRunner.runScheduledTasks(1); | |
| 187 | |
| 188 deepEqual(Notification.getNextToShow(), information, "Critical notification is ignored if minVersion doesn't match"); | |
| 189 deepEqual(Notification.getNextToShow(), null, "Critical notification still i gnored even if no other notifications available"); | |
| 190 }); | |
| 191 | |
| 192 test("Max version", 2, function() | |
| 193 { | |
| 194 let information = fixConstructors({ | |
| 195 timestamp: 1, | |
| 196 severity: "information", | |
| 197 message: {en: "Information"}, | |
| 198 maxVersion: "1.5" | |
| 199 }); | |
| 200 let critical = fixConstructors({ | |
| 201 timestamp: 2, | |
| 202 severity: "critical", | |
| 203 message: {en: "Critical"}, | |
| 204 maxVersion: "1.4" | |
| 205 }); | |
| 206 | |
| 207 registerHandler([information, critical]); | |
| 208 testRunner.runScheduledTasks(1); | |
| 209 | |
| 210 deepEqual(Notification.getNextToShow(), information, "Critical notification is ignored if maxVersion doesn't match"); | |
| 211 deepEqual(Notification.getNextToShow(), null, "Critical notification still i gnored even if no other notifications available"); | |
| 212 }); | |
| 213 })(); | |
| OLD | NEW |