toolkit/components/social/test/browser/browser_notifications.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/social/test/browser/browser_notifications.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,87 @@
     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 +const TEST_PROVIDER_ORIGIN = 'http://example.com';
     1.9 +
    1.10 +Cu.import("resource://gre/modules/Services.jsm");
    1.11 +
    1.12 +function ensureProvider(workerFunction, cb) {
    1.13 +  let manifest = {
    1.14 +    origin: TEST_PROVIDER_ORIGIN,
    1.15 +    name: "Example Provider",
    1.16 +    workerURL: "http://example.com/browser/toolkit/components/social/test/browser/echo.sjs?"
    1.17 +               + encodeURI("let run=" + workerFunction.toSource()) + ";run();"
    1.18 +  };
    1.19 +
    1.20 +  SocialService.addProvider(manifest, function (p) {
    1.21 +    cb(p);
    1.22 +  });
    1.23 +}
    1.24 +
    1.25 +function test() {
    1.26 +  waitForExplicitFinish();
    1.27 +
    1.28 +  let cbPostTest = function(cb) {
    1.29 +    SocialService.removeProvider(TEST_PROVIDER_ORIGIN, function() {cb()});
    1.30 +  };
    1.31 +  replaceAlertsService();
    1.32 +  registerCleanupFunction(restoreAlertsService);
    1.33 +  runTests(tests, undefined, cbPostTest);
    1.34 +}
    1.35 +
    1.36 +let tests = {
    1.37 +  testNotificationCallback: function(cbnext) {
    1.38 +    let run = function() {
    1.39 +      let testPort, apiPort;
    1.40 +      onconnect = function(e) {
    1.41 +        let port = e.ports[0];
    1.42 +        port.onmessage = function(e) {
    1.43 +          if (e.data.topic == "social.initialize") { // this is the api port.
    1.44 +            apiPort = port;
    1.45 +          } else if (e.data.topic == "test.initialize") { // test suite port.
    1.46 +            testPort = port;
    1.47 +            apiPort.postMessage({topic: 'social.notification-create',
    1.48 +                                 data: {
    1.49 +                                        id: "the id",
    1.50 +                                        body: 'test notification',
    1.51 +                                        action: 'callback',
    1.52 +                                        actionArgs: { data: "something" }
    1.53 +                                       }
    1.54 +                                });
    1.55 +          } else if (e.data.topic == "social.notification-action") {
    1.56 +            let data = e.data.data;
    1.57 +            let ok = data && data.action == "callback" &&
    1.58 +                     data.actionArgs && e.data.data.actionArgs.data == "something";
    1.59 +            testPort.postMessage({topic: "test.done", data: ok});
    1.60 +          }
    1.61 +        }
    1.62 +      }
    1.63 +    }
    1.64 +    ensureProvider(run, function(provider) {
    1.65 +      let observer = {
    1.66 +        observedData: null,
    1.67 +        observe: function(subject, topic, data) {
    1.68 +          this.observedData = JSON.parse(data);
    1.69 +          Services.obs.removeObserver(observer, "social-test:notification-alert");
    1.70 +        }
    1.71 +      }
    1.72 +      Services.obs.addObserver(observer, "social-test:notification-alert", false);
    1.73 +
    1.74 +      let port = provider.getWorkerPort();
    1.75 +      ok(port, "got port from worker");
    1.76 +      port.onmessage = function(e) {
    1.77 +        if (e.data.topic == "test.done") {
    1.78 +          ok(e.data.data, "check the test worked");
    1.79 +          ok(observer.observedData, "test observer fired");
    1.80 +          is(observer.observedData.text, "test notification", "check the alert text is correct");
    1.81 +          is(observer.observedData.title, "Example Provider", "check the alert title is correct");
    1.82 +          is(observer.observedData.textClickable, true, "check the alert is clickable");
    1.83 +          port.close();
    1.84 +          cbnext();
    1.85 +        }
    1.86 +      }
    1.87 +      port.postMessage({topic: "test.initialize"});
    1.88 +    });
    1.89 +  }
    1.90 +};

mercurial