addon-sdk/source/test/test-notifications.js

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 const { Loader } = require('sdk/test/loader');
     7 exports.testOnClick = function (assert) {
     8   let [loader, mockAlertServ] = makeLoader(module);
     9   let notifs = loader.require("sdk/notifications");
    10   let data = "test data";
    11   let opts = {
    12     onClick: function (clickedData) {
    13       assert.equal(this, notifs, "|this| should be notifications module");
    14       assert.equal(clickedData, data,
    15                        "data passed to onClick should be correct");
    16     },
    17     data: data,
    18     title: "test title",
    19     text: "test text",
    20     iconURL: "test icon URL"
    21   };
    22   notifs.notify(opts);
    23   mockAlertServ.click();
    24   loader.unload();
    25 };
    27 exports['test:numbers and URLs in options'] = function(assert) {
    28   let [loader] = makeLoader(module);
    29   let notifs = loader.require('sdk/notifications');
    30   let opts = {
    31     title: 123,
    32     text: 45678,
    33     // must use in-loader `sdk/url` module for the validation type check to work
    34     iconURL: loader.require('sdk/url').URL('data:image/png,blah')
    35   };
    36   try {
    37     notifs.notify(opts);
    38     assert.pass('using numbers and URLs in options works');
    39   } catch (e) {
    40     assert.fail('using numbers and URLs in options must not throw');
    41   }
    42   loader.unload();
    43 }
    45 exports['test:new tag, dir and lang options'] = function(assert) {
    46   let [loader] = makeLoader(module);
    47   let notifs = loader.require('sdk/notifications');
    48   let opts = {
    49     title: 'best',
    50     tag: 'tagging',
    51     lang: 'en'
    52   };
    54   try {
    55     opts.dir = 'ttb';
    56     notifs.notify(opts);
    57     assert.fail('`dir` option must not accept TopToBottom direction.');
    58   } catch (e) {
    59     assert.equal(e.message, 
    60       '`dir` option must be one of: "auto", "ltr" or "rtl".');
    61   }
    63   try {
    64     opts.dir = 'rtl';
    65     notifs.notify(opts);
    66     assert.pass('`dir` option accepts "rtl" direction.');
    67   } catch (e) {
    68     assert.fail('`dir` option must accept "rtl" direction.');
    69   }
    71   loader.unload();
    72 }
    74 // Returns [loader, mockAlertService].
    75 function makeLoader(module) {
    76   let loader = Loader(module);
    77   let mockAlertServ = {
    78     showAlertNotification: function (imageUrl, title, text, textClickable,
    79                                      cookie, alertListener, name) {
    80       this._cookie = cookie;
    81       this._alertListener = alertListener;
    82     },
    83     click: function () {
    84       this._alertListener.observe(null, "alertclickcallback", this._cookie);
    85     }
    86   };
    87   loader.require("sdk/notifications");
    88   let scope = loader.sandbox("sdk/notifications");
    89   scope.notify = mockAlertServ.showAlertNotification.bind(mockAlertServ);
    90   return [loader, mockAlertServ];
    91 }
    93 require('sdk/test').run(exports);

mercurial