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: const TEST_PROVIDER_ORIGIN = 'http://example.com'; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: function ensureProvider(workerFunction, cb) { michael@0: let manifest = { michael@0: origin: TEST_PROVIDER_ORIGIN, michael@0: name: "Example Provider", michael@0: workerURL: "http://example.com/browser/toolkit/components/social/test/browser/echo.sjs?" michael@0: + encodeURI("let run=" + workerFunction.toSource()) + ";run();" michael@0: }; michael@0: michael@0: SocialService.addProvider(manifest, function (p) { michael@0: cb(p); michael@0: }); michael@0: } michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: let cbPostTest = function(cb) { michael@0: SocialService.removeProvider(TEST_PROVIDER_ORIGIN, function() {cb()}); michael@0: }; michael@0: replaceAlertsService(); michael@0: registerCleanupFunction(restoreAlertsService); michael@0: runTests(tests, undefined, cbPostTest); michael@0: } michael@0: michael@0: let tests = { michael@0: testNotificationCallback: function(cbnext) { michael@0: let run = function() { michael@0: let testPort, apiPort; michael@0: onconnect = function(e) { michael@0: let port = e.ports[0]; michael@0: port.onmessage = function(e) { michael@0: if (e.data.topic == "social.initialize") { // this is the api port. michael@0: apiPort = port; michael@0: } else if (e.data.topic == "test.initialize") { // test suite port. michael@0: testPort = port; michael@0: apiPort.postMessage({topic: 'social.notification-create', michael@0: data: { michael@0: id: "the id", michael@0: body: 'test notification', michael@0: action: 'callback', michael@0: actionArgs: { data: "something" } michael@0: } michael@0: }); michael@0: } else if (e.data.topic == "social.notification-action") { michael@0: let data = e.data.data; michael@0: let ok = data && data.action == "callback" && michael@0: data.actionArgs && e.data.data.actionArgs.data == "something"; michael@0: testPort.postMessage({topic: "test.done", data: ok}); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: ensureProvider(run, function(provider) { michael@0: let observer = { michael@0: observedData: null, michael@0: observe: function(subject, topic, data) { michael@0: this.observedData = JSON.parse(data); michael@0: Services.obs.removeObserver(observer, "social-test:notification-alert"); michael@0: } michael@0: } michael@0: Services.obs.addObserver(observer, "social-test:notification-alert", false); michael@0: michael@0: let port = provider.getWorkerPort(); michael@0: ok(port, "got port from worker"); michael@0: port.onmessage = function(e) { michael@0: if (e.data.topic == "test.done") { michael@0: ok(e.data.data, "check the test worked"); michael@0: ok(observer.observedData, "test observer fired"); michael@0: is(observer.observedData.text, "test notification", "check the alert text is correct"); michael@0: is(observer.observedData.title, "Example Provider", "check the alert title is correct"); michael@0: is(observer.observedData.textClickable, true, "check the alert is clickable"); michael@0: port.close(); michael@0: cbnext(); michael@0: } michael@0: } michael@0: port.postMessage({topic: "test.initialize"}); michael@0: }); michael@0: } michael@0: };