michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: let provider; michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: replaceAlertsService(); michael@0: registerCleanupFunction(restoreAlertsService); michael@0: michael@0: let manifest = { michael@0: origin: 'http://example.com', michael@0: name: "Example Provider", michael@0: workerURL: "http://example.com/browser/toolkit/components/social/test/browser/worker_social.js" michael@0: }; michael@0: michael@0: SocialService.addProvider(manifest, function (p) { michael@0: // toolkit does not enable providers by default, that is handled in the michael@0: // browser, we need to enable it in our tests. michael@0: p.enabled = true; michael@0: provider = p; michael@0: runTests(tests, undefined, undefined, function () { michael@0: SocialService.removeProvider(provider.origin, finish); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: let tests = { michael@0: testProfile: function(next) { michael@0: let expect = { michael@0: portrait: "https://example.com/portrait.jpg", michael@0: userName: "trickster", michael@0: displayName: "Kuma Lisa", michael@0: profileURL: "http://en.wikipedia.org/wiki/Kuma_Lisa" michael@0: } michael@0: function ob(aSubject, aTopic, aData) { michael@0: Services.obs.removeObserver(ob, "social:profile-changed"); michael@0: is(aData, provider.origin, "update of profile from our provider"); michael@0: let profile = provider.profile; michael@0: is(profile.portrait, expect.portrait, "portrait is set"); michael@0: is(profile.userName, expect.userName, "userName is set"); michael@0: is(profile.displayName, expect.displayName, "displayName is set"); michael@0: is(profile.profileURL, expect.profileURL, "profileURL is set"); michael@0: next(); michael@0: } michael@0: Services.obs.addObserver(ob, "social:profile-changed", false); michael@0: let port = provider.getWorkerPort(); michael@0: port.postMessage({topic: "test-profile", data: expect}); michael@0: port.close(); michael@0: }, michael@0: michael@0: testAmbientNotification: function(next) { michael@0: let expect = { michael@0: name: "test-ambient" michael@0: } michael@0: function ob(aSubject, aTopic, aData) { michael@0: Services.obs.removeObserver(ob, "social:ambient-notification-changed"); michael@0: is(aData, provider.origin, "update is from our provider"); michael@0: let notif = provider.ambientNotificationIcons[expect.name]; michael@0: is(notif.name, expect.name, "ambientNotification reflected"); michael@0: michael@0: next(); michael@0: } michael@0: Services.obs.addObserver(ob, "social:ambient-notification-changed", false); michael@0: let port = provider.getWorkerPort(); michael@0: port.postMessage({topic: "test-ambient", data: expect}); michael@0: port.close(); michael@0: }, michael@0: michael@0: testProfileCleared: function(next) { michael@0: let sent = { michael@0: userName: "" michael@0: }; michael@0: function ob(aSubject, aTopic, aData) { michael@0: Services.obs.removeObserver(ob, "social:profile-changed"); michael@0: is(aData, provider.origin, "update of profile from our provider"); michael@0: is(Object.keys(provider.profile).length, 0, "profile was cleared by empty username"); michael@0: is(Object.keys(provider.ambientNotificationIcons).length, 0, "icons were cleared by empty username"); michael@0: michael@0: next(); michael@0: } michael@0: Services.obs.addObserver(ob, "social:profile-changed", false); michael@0: provider.workerAPI._port.postMessage({topic: "test-profile", data: sent}); michael@0: }, michael@0: michael@0: testNoCookies: function(next) { michael@0: // use a big, blunt stick to remove cookies. michael@0: Services.cookies.removeAll(); michael@0: let port = provider.getWorkerPort(); michael@0: port.onmessage = function onMessage(event) { michael@0: let {topic, data} = event.data; michael@0: if (topic == "test.cookies-get-response") { michael@0: is(data.length, 0, "got no cookies"); michael@0: port.close(); michael@0: next(); michael@0: } michael@0: } michael@0: port.postMessage({topic: "test-initialization"}); michael@0: port.postMessage({topic: "test.cookies-get"}); michael@0: }, michael@0: michael@0: testCookies: function(next) { michael@0: let port = provider.getWorkerPort(); michael@0: port.onmessage = function onMessage(event) { michael@0: let {topic, data} = event.data; michael@0: if (topic == "test.cookies-get-response") { michael@0: is(data.length, 2, "got 2 cookies"); michael@0: is(data[0].name, "cheez", "cookie has the correct name"); michael@0: is(data[0].value, "burger", "cookie has the correct value"); michael@0: is(data[1].name, "moar", "cookie has the correct name"); michael@0: is(data[1].value, "bacon", "cookie has the correct value"); michael@0: Services.cookies.remove('.example.com', '/', 'cheez', false); michael@0: Services.cookies.remove('.example.com', '/', 'moar', false); michael@0: port.close(); michael@0: next(); michael@0: } michael@0: } michael@0: var MAX_EXPIRY = Math.pow(2, 62); michael@0: Services.cookies.add('.example.com', '/', 'cheez', 'burger', false, false, true, MAX_EXPIRY); michael@0: Services.cookies.add('.example.com', '/', 'moar', 'bacon', false, false, true, MAX_EXPIRY); michael@0: port.postMessage({topic: "test-initialization"}); michael@0: port.postMessage({topic: "test.cookies-get"}); michael@0: }, michael@0: michael@0: testWorkerReload: function(next) { michael@0: let fw = {}; michael@0: Cu.import("resource://gre/modules/FrameWorker.jsm", fw); michael@0: michael@0: let worker = fw.getFrameWorkerHandle(provider.workerURL, undefined, "testWorkerReload"); michael@0: let port = provider.getWorkerPort(); michael@0: // this observer will be fired when the worker reloads - it ensures the michael@0: // old port was closed and the new worker is functioning. michael@0: Services.obs.addObserver(function reloadObserver() { michael@0: Services.obs.removeObserver(reloadObserver, "social:provider-reload"); michael@0: ok(port._closed, "old port was closed by the reload"); michael@0: let newWorker = fw.getFrameWorkerHandle(provider.workerURL, undefined, "testWorkerReload - worker2"); michael@0: let newPort = provider.getWorkerPort(); michael@0: newPort.onmessage = function (e) { michael@0: let topic = e.data.topic; michael@0: switch (topic) { michael@0: case "test-initialization-complete": michael@0: // and we are done. michael@0: newPort.close(); michael@0: next(); michael@0: break; michael@0: } michael@0: } michael@0: newPort.postMessage({topic: "test-initialization"}); michael@0: }, "social:provider-reload", false); michael@0: michael@0: port.onmessage = function (e) { michael@0: let topic = e.data.topic; michael@0: switch (topic) { michael@0: case "test-initialization-complete": michael@0: // tell the worker to send the reload msg - that will trigger the michael@0: // frameworker to unload and for our content script's unload michael@0: // handler to post a "test-result" message. michael@0: port.postMessage({topic: "test-reload-init"}); michael@0: // and the "social:provider-reload" observer should fire... michael@0: break; michael@0: } michael@0: } michael@0: port.postMessage({topic: "test-initialization"}); michael@0: }, michael@0: michael@0: testNotificationLinks: function(next) { michael@0: let port = provider.getWorkerPort(); michael@0: let data = { michael@0: id: 'an id', michael@0: body: 'the text', michael@0: action: 'link', michael@0: actionArgs: {} // will get a toURL elt during the tests... michael@0: } michael@0: let testArgs = [ michael@0: // toURL, expectedLocation, expectedWhere] michael@0: ["http://example.com", "http://example.com/", "tab"], michael@0: // bug 815970 - test that a mis-matched scheme gets patched up. michael@0: ["https://example.com", "http://example.com/", "tab"], michael@0: // check an off-origin link is not opened. michael@0: ["https://mochitest:8888/", null, null] michael@0: ]; michael@0: michael@0: // we monkey-patch openUILinkIn michael@0: let oldopenUILinkIn = window.openUILinkIn; michael@0: registerCleanupFunction(function () { michael@0: // restore the monkey-patch michael@0: window.openUILinkIn = oldopenUILinkIn; michael@0: }); michael@0: let openLocation; michael@0: let openWhere; michael@0: window.openUILinkIn = function(location, where) { michael@0: openLocation = location; michael@0: openWhere = where; michael@0: } michael@0: michael@0: // the testing framework. michael@0: let toURL, expectedLocation, expectedWhere; michael@0: function nextTest() { michael@0: if (testArgs.length == 0) { michael@0: port.close(); michael@0: next(); // all out of tests! michael@0: return; michael@0: } michael@0: openLocation = openWhere = null; michael@0: [toURL, expectedLocation, expectedWhere] = testArgs.shift(); michael@0: data.actionArgs.toURL = toURL; michael@0: port.postMessage({topic: 'test-notification-create', data: data}); michael@0: }; michael@0: michael@0: port.onmessage = function(evt) { michael@0: if (evt.data.topic == "did-notification-create") { michael@0: is(openLocation, expectedLocation, "url actually opened was " + openLocation); michael@0: is(openWhere, expectedWhere, "the url was opened in a " + expectedWhere); michael@0: nextTest(); michael@0: } michael@0: } michael@0: // and kick off the tests. michael@0: port.postMessage({topic: "test-initialization"}); michael@0: nextTest(); michael@0: }, michael@0: michael@0: };