1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/social/test/browser/browser_workerAPI.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,224 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +let provider; 1.9 + 1.10 +function test() { 1.11 + waitForExplicitFinish(); 1.12 + 1.13 + replaceAlertsService(); 1.14 + registerCleanupFunction(restoreAlertsService); 1.15 + 1.16 + let manifest = { 1.17 + origin: 'http://example.com', 1.18 + name: "Example Provider", 1.19 + workerURL: "http://example.com/browser/toolkit/components/social/test/browser/worker_social.js" 1.20 + }; 1.21 + 1.22 + SocialService.addProvider(manifest, function (p) { 1.23 + // toolkit does not enable providers by default, that is handled in the 1.24 + // browser, we need to enable it in our tests. 1.25 + p.enabled = true; 1.26 + provider = p; 1.27 + runTests(tests, undefined, undefined, function () { 1.28 + SocialService.removeProvider(provider.origin, finish); 1.29 + }); 1.30 + }); 1.31 +} 1.32 + 1.33 +let tests = { 1.34 + testProfile: function(next) { 1.35 + let expect = { 1.36 + portrait: "https://example.com/portrait.jpg", 1.37 + userName: "trickster", 1.38 + displayName: "Kuma Lisa", 1.39 + profileURL: "http://en.wikipedia.org/wiki/Kuma_Lisa" 1.40 + } 1.41 + function ob(aSubject, aTopic, aData) { 1.42 + Services.obs.removeObserver(ob, "social:profile-changed"); 1.43 + is(aData, provider.origin, "update of profile from our provider"); 1.44 + let profile = provider.profile; 1.45 + is(profile.portrait, expect.portrait, "portrait is set"); 1.46 + is(profile.userName, expect.userName, "userName is set"); 1.47 + is(profile.displayName, expect.displayName, "displayName is set"); 1.48 + is(profile.profileURL, expect.profileURL, "profileURL is set"); 1.49 + next(); 1.50 + } 1.51 + Services.obs.addObserver(ob, "social:profile-changed", false); 1.52 + let port = provider.getWorkerPort(); 1.53 + port.postMessage({topic: "test-profile", data: expect}); 1.54 + port.close(); 1.55 + }, 1.56 + 1.57 + testAmbientNotification: function(next) { 1.58 + let expect = { 1.59 + name: "test-ambient" 1.60 + } 1.61 + function ob(aSubject, aTopic, aData) { 1.62 + Services.obs.removeObserver(ob, "social:ambient-notification-changed"); 1.63 + is(aData, provider.origin, "update is from our provider"); 1.64 + let notif = provider.ambientNotificationIcons[expect.name]; 1.65 + is(notif.name, expect.name, "ambientNotification reflected"); 1.66 + 1.67 + next(); 1.68 + } 1.69 + Services.obs.addObserver(ob, "social:ambient-notification-changed", false); 1.70 + let port = provider.getWorkerPort(); 1.71 + port.postMessage({topic: "test-ambient", data: expect}); 1.72 + port.close(); 1.73 + }, 1.74 + 1.75 + testProfileCleared: function(next) { 1.76 + let sent = { 1.77 + userName: "" 1.78 + }; 1.79 + function ob(aSubject, aTopic, aData) { 1.80 + Services.obs.removeObserver(ob, "social:profile-changed"); 1.81 + is(aData, provider.origin, "update of profile from our provider"); 1.82 + is(Object.keys(provider.profile).length, 0, "profile was cleared by empty username"); 1.83 + is(Object.keys(provider.ambientNotificationIcons).length, 0, "icons were cleared by empty username"); 1.84 + 1.85 + next(); 1.86 + } 1.87 + Services.obs.addObserver(ob, "social:profile-changed", false); 1.88 + provider.workerAPI._port.postMessage({topic: "test-profile", data: sent}); 1.89 + }, 1.90 + 1.91 + testNoCookies: function(next) { 1.92 + // use a big, blunt stick to remove cookies. 1.93 + Services.cookies.removeAll(); 1.94 + let port = provider.getWorkerPort(); 1.95 + port.onmessage = function onMessage(event) { 1.96 + let {topic, data} = event.data; 1.97 + if (topic == "test.cookies-get-response") { 1.98 + is(data.length, 0, "got no cookies"); 1.99 + port.close(); 1.100 + next(); 1.101 + } 1.102 + } 1.103 + port.postMessage({topic: "test-initialization"}); 1.104 + port.postMessage({topic: "test.cookies-get"}); 1.105 + }, 1.106 + 1.107 + testCookies: function(next) { 1.108 + let port = provider.getWorkerPort(); 1.109 + port.onmessage = function onMessage(event) { 1.110 + let {topic, data} = event.data; 1.111 + if (topic == "test.cookies-get-response") { 1.112 + is(data.length, 2, "got 2 cookies"); 1.113 + is(data[0].name, "cheez", "cookie has the correct name"); 1.114 + is(data[0].value, "burger", "cookie has the correct value"); 1.115 + is(data[1].name, "moar", "cookie has the correct name"); 1.116 + is(data[1].value, "bacon", "cookie has the correct value"); 1.117 + Services.cookies.remove('.example.com', '/', 'cheez', false); 1.118 + Services.cookies.remove('.example.com', '/', 'moar', false); 1.119 + port.close(); 1.120 + next(); 1.121 + } 1.122 + } 1.123 + var MAX_EXPIRY = Math.pow(2, 62); 1.124 + Services.cookies.add('.example.com', '/', 'cheez', 'burger', false, false, true, MAX_EXPIRY); 1.125 + Services.cookies.add('.example.com', '/', 'moar', 'bacon', false, false, true, MAX_EXPIRY); 1.126 + port.postMessage({topic: "test-initialization"}); 1.127 + port.postMessage({topic: "test.cookies-get"}); 1.128 + }, 1.129 + 1.130 + testWorkerReload: function(next) { 1.131 + let fw = {}; 1.132 + Cu.import("resource://gre/modules/FrameWorker.jsm", fw); 1.133 + 1.134 + let worker = fw.getFrameWorkerHandle(provider.workerURL, undefined, "testWorkerReload"); 1.135 + let port = provider.getWorkerPort(); 1.136 + // this observer will be fired when the worker reloads - it ensures the 1.137 + // old port was closed and the new worker is functioning. 1.138 + Services.obs.addObserver(function reloadObserver() { 1.139 + Services.obs.removeObserver(reloadObserver, "social:provider-reload"); 1.140 + ok(port._closed, "old port was closed by the reload"); 1.141 + let newWorker = fw.getFrameWorkerHandle(provider.workerURL, undefined, "testWorkerReload - worker2"); 1.142 + let newPort = provider.getWorkerPort(); 1.143 + newPort.onmessage = function (e) { 1.144 + let topic = e.data.topic; 1.145 + switch (topic) { 1.146 + case "test-initialization-complete": 1.147 + // and we are done. 1.148 + newPort.close(); 1.149 + next(); 1.150 + break; 1.151 + } 1.152 + } 1.153 + newPort.postMessage({topic: "test-initialization"}); 1.154 + }, "social:provider-reload", false); 1.155 + 1.156 + port.onmessage = function (e) { 1.157 + let topic = e.data.topic; 1.158 + switch (topic) { 1.159 + case "test-initialization-complete": 1.160 + // tell the worker to send the reload msg - that will trigger the 1.161 + // frameworker to unload and for our content script's unload 1.162 + // handler to post a "test-result" message. 1.163 + port.postMessage({topic: "test-reload-init"}); 1.164 + // and the "social:provider-reload" observer should fire... 1.165 + break; 1.166 + } 1.167 + } 1.168 + port.postMessage({topic: "test-initialization"}); 1.169 + }, 1.170 + 1.171 + testNotificationLinks: function(next) { 1.172 + let port = provider.getWorkerPort(); 1.173 + let data = { 1.174 + id: 'an id', 1.175 + body: 'the text', 1.176 + action: 'link', 1.177 + actionArgs: {} // will get a toURL elt during the tests... 1.178 + } 1.179 + let testArgs = [ 1.180 + // toURL, expectedLocation, expectedWhere] 1.181 + ["http://example.com", "http://example.com/", "tab"], 1.182 + // bug 815970 - test that a mis-matched scheme gets patched up. 1.183 + ["https://example.com", "http://example.com/", "tab"], 1.184 + // check an off-origin link is not opened. 1.185 + ["https://mochitest:8888/", null, null] 1.186 + ]; 1.187 + 1.188 + // we monkey-patch openUILinkIn 1.189 + let oldopenUILinkIn = window.openUILinkIn; 1.190 + registerCleanupFunction(function () { 1.191 + // restore the monkey-patch 1.192 + window.openUILinkIn = oldopenUILinkIn; 1.193 + }); 1.194 + let openLocation; 1.195 + let openWhere; 1.196 + window.openUILinkIn = function(location, where) { 1.197 + openLocation = location; 1.198 + openWhere = where; 1.199 + } 1.200 + 1.201 + // the testing framework. 1.202 + let toURL, expectedLocation, expectedWhere; 1.203 + function nextTest() { 1.204 + if (testArgs.length == 0) { 1.205 + port.close(); 1.206 + next(); // all out of tests! 1.207 + return; 1.208 + } 1.209 + openLocation = openWhere = null; 1.210 + [toURL, expectedLocation, expectedWhere] = testArgs.shift(); 1.211 + data.actionArgs.toURL = toURL; 1.212 + port.postMessage({topic: 'test-notification-create', data: data}); 1.213 + }; 1.214 + 1.215 + port.onmessage = function(evt) { 1.216 + if (evt.data.topic == "did-notification-create") { 1.217 + is(openLocation, expectedLocation, "url actually opened was " + openLocation); 1.218 + is(openWhere, expectedWhere, "the url was opened in a " + expectedWhere); 1.219 + nextTest(); 1.220 + } 1.221 + } 1.222 + // and kick off the tests. 1.223 + port.postMessage({topic: "test-initialization"}); 1.224 + nextTest(); 1.225 + }, 1.226 + 1.227 +};