addon-sdk/source/test/test-weak-set.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     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 'use strict';
     7 const { Cu } = require('chrome');
     8 const { Loader } = require('sdk/test/loader');
     9 const { defer } = require('sdk/core/promise');
    11 function gc() {
    12   let { promise, resolve } = defer();
    14   Cu.schedulePreciseGC(resolve);
    16   return promise;
    17 };
    19 exports['test adding item'] = function(assert, done) {
    20   let loader = Loader(module);
    21   let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
    23   let items = {};
    24   let item = {};
    26   add(items, item);
    28   gc().
    29     then(() => {
    30       assert.ok(has(items, item),
    31         'the item is in the weak set');
    32     }).
    33     then(loader.unload).
    34     then(done, assert.fail);
    35 };
    37 exports['test remove item'] = function(assert, done) {
    38   let loader = Loader(module);
    39   let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
    41   let items = {};
    42   let item = {};
    44   add(items, item);
    46   remove(items, item);
    48   gc().
    49     then(() => {
    50       assert.ok(!has(items, item),
    51         'the item is not in weak set');
    52     }).
    53     then(loader.unload).
    54     then(done, assert.fail);
    55 };
    57 exports['test iterate'] = function(assert, done) {
    58   let loader = Loader(module);
    59   let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
    61   let items = {};
    62   let addedItems = [{}, {}];
    64   add(items, addedItems[0]);
    65   add(items, addedItems[1]);
    66   add(items, addedItems[0]); // weak set shouldn't add this twice
    68   gc().
    69     then(() => {
    70       let count = 0;
    72       for (let item of iterator(items)) {
    73         assert.equal(item, addedItems[count],
    74           'item in the expected order');
    76         count++;
    77       }
    79       assert.equal(count, 2,
    80         'items in the expected number');
    81     }).
    82     then(loader.unload).
    83     then(done, assert.fail);
    84 };
    86 exports['test clear'] = function(assert, done) {
    87   let loader = Loader(module);
    88   let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
    90   let items = {};
    91   let addedItems = [{}, {}];
    93   add(items, addedItems[0]);
    94   add(items, addedItems[1]);
    96   clear(items)
    98   gc().
    99     then(() => {
   100       let count = 0;
   102       for (let item of iterator(items))
   103         assert.fail('the loop should not be executed');
   105       assert.equal(count, 0,
   106         'no items in the weak set');
   107     }).
   108     then(loader.unload).
   109     then(done, assert.fail);
   110 };
   112 exports['test adding item without reference'] = function(assert, done) {
   113   let loader = Loader(module);
   114   let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
   116   let items = {};
   118   add(items, {});
   120   gc().
   121     then(() => {
   122       let count = 0;
   124       for (let item of iterator(items))
   125         assert.fail('the loop should not be executed');
   127       assert.equal(count, 0,
   128         'no items in the weak set');
   129     }).
   130     then(loader.unload).
   131     then(done, assert.fail);
   132 };
   134 exports['test adding non object or null item'] = function(assert) {
   135   let loader = Loader(module);
   136   let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
   138   let items = {};
   140   assert.throws(() => {
   141     add(items, 'foo');
   142   },
   143   /^value is not a non-null object/,
   144   'only non-null object are allowed');
   146   assert.throws(() => {
   147     add(items, 0);
   148   },
   149   /^value is not a non-null object/,
   150   'only non-null object are allowed');
   152   assert.throws(() => {
   153     add(items, undefined);
   154   },
   155   /^value is not a non-null object/,
   156   'only non-null object are allowed');
   158   assert.throws(() => {
   159     add(items, null);
   160   },
   161   /^value is not a non-null object/,
   162   'only non-null object are allowed');
   164   assert.throws(() => {
   165     add(items, true);
   166   },
   167   /^value is not a non-null object/,
   168   'only non-null object are allowed');
   169 };
   171 exports['test adding to non object or null item'] = function(assert) {
   172   let loader = Loader(module);
   173   let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
   175   let item = {};
   177   assert.throws(() => {
   178     add('foo', item);
   179   },
   180   /^value is not a non-null object/,
   181   'only non-null object are allowed');
   183   assert.throws(() => {
   184     add(0, item);
   185   },
   186   /^value is not a non-null object/,
   187   'only non-null object are allowed');
   189   assert.throws(() => {
   190     add(undefined, item);
   191   },
   192   /^value is not a non-null object/,
   193   'only non-null object are allowed');
   195   assert.throws(() => {
   196     add(null, item);
   197   },
   198   /^value is not a non-null object/,
   199   'only non-null object are allowed');
   201   assert.throws(() => {
   202     add(true, item);
   203   },
   204   /^value is not a non-null object/,
   205   'only non-null object are allowed');
   206 };
   208 require('test').run(exports);

mercurial