addon-sdk/source/test/test-weak-set.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.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 'use strict';
michael@0 6
michael@0 7 const { Cu } = require('chrome');
michael@0 8 const { Loader } = require('sdk/test/loader');
michael@0 9 const { defer } = require('sdk/core/promise');
michael@0 10
michael@0 11 function gc() {
michael@0 12 let { promise, resolve } = defer();
michael@0 13
michael@0 14 Cu.schedulePreciseGC(resolve);
michael@0 15
michael@0 16 return promise;
michael@0 17 };
michael@0 18
michael@0 19 exports['test adding item'] = function(assert, done) {
michael@0 20 let loader = Loader(module);
michael@0 21 let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
michael@0 22
michael@0 23 let items = {};
michael@0 24 let item = {};
michael@0 25
michael@0 26 add(items, item);
michael@0 27
michael@0 28 gc().
michael@0 29 then(() => {
michael@0 30 assert.ok(has(items, item),
michael@0 31 'the item is in the weak set');
michael@0 32 }).
michael@0 33 then(loader.unload).
michael@0 34 then(done, assert.fail);
michael@0 35 };
michael@0 36
michael@0 37 exports['test remove item'] = function(assert, done) {
michael@0 38 let loader = Loader(module);
michael@0 39 let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
michael@0 40
michael@0 41 let items = {};
michael@0 42 let item = {};
michael@0 43
michael@0 44 add(items, item);
michael@0 45
michael@0 46 remove(items, item);
michael@0 47
michael@0 48 gc().
michael@0 49 then(() => {
michael@0 50 assert.ok(!has(items, item),
michael@0 51 'the item is not in weak set');
michael@0 52 }).
michael@0 53 then(loader.unload).
michael@0 54 then(done, assert.fail);
michael@0 55 };
michael@0 56
michael@0 57 exports['test iterate'] = function(assert, done) {
michael@0 58 let loader = Loader(module);
michael@0 59 let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
michael@0 60
michael@0 61 let items = {};
michael@0 62 let addedItems = [{}, {}];
michael@0 63
michael@0 64 add(items, addedItems[0]);
michael@0 65 add(items, addedItems[1]);
michael@0 66 add(items, addedItems[0]); // weak set shouldn't add this twice
michael@0 67
michael@0 68 gc().
michael@0 69 then(() => {
michael@0 70 let count = 0;
michael@0 71
michael@0 72 for (let item of iterator(items)) {
michael@0 73 assert.equal(item, addedItems[count],
michael@0 74 'item in the expected order');
michael@0 75
michael@0 76 count++;
michael@0 77 }
michael@0 78
michael@0 79 assert.equal(count, 2,
michael@0 80 'items in the expected number');
michael@0 81 }).
michael@0 82 then(loader.unload).
michael@0 83 then(done, assert.fail);
michael@0 84 };
michael@0 85
michael@0 86 exports['test clear'] = function(assert, done) {
michael@0 87 let loader = Loader(module);
michael@0 88 let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
michael@0 89
michael@0 90 let items = {};
michael@0 91 let addedItems = [{}, {}];
michael@0 92
michael@0 93 add(items, addedItems[0]);
michael@0 94 add(items, addedItems[1]);
michael@0 95
michael@0 96 clear(items)
michael@0 97
michael@0 98 gc().
michael@0 99 then(() => {
michael@0 100 let count = 0;
michael@0 101
michael@0 102 for (let item of iterator(items))
michael@0 103 assert.fail('the loop should not be executed');
michael@0 104
michael@0 105 assert.equal(count, 0,
michael@0 106 'no items in the weak set');
michael@0 107 }).
michael@0 108 then(loader.unload).
michael@0 109 then(done, assert.fail);
michael@0 110 };
michael@0 111
michael@0 112 exports['test adding item without reference'] = function(assert, done) {
michael@0 113 let loader = Loader(module);
michael@0 114 let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
michael@0 115
michael@0 116 let items = {};
michael@0 117
michael@0 118 add(items, {});
michael@0 119
michael@0 120 gc().
michael@0 121 then(() => {
michael@0 122 let count = 0;
michael@0 123
michael@0 124 for (let item of iterator(items))
michael@0 125 assert.fail('the loop should not be executed');
michael@0 126
michael@0 127 assert.equal(count, 0,
michael@0 128 'no items in the weak set');
michael@0 129 }).
michael@0 130 then(loader.unload).
michael@0 131 then(done, assert.fail);
michael@0 132 };
michael@0 133
michael@0 134 exports['test adding non object or null item'] = function(assert) {
michael@0 135 let loader = Loader(module);
michael@0 136 let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
michael@0 137
michael@0 138 let items = {};
michael@0 139
michael@0 140 assert.throws(() => {
michael@0 141 add(items, 'foo');
michael@0 142 },
michael@0 143 /^value is not a non-null object/,
michael@0 144 'only non-null object are allowed');
michael@0 145
michael@0 146 assert.throws(() => {
michael@0 147 add(items, 0);
michael@0 148 },
michael@0 149 /^value is not a non-null object/,
michael@0 150 'only non-null object are allowed');
michael@0 151
michael@0 152 assert.throws(() => {
michael@0 153 add(items, undefined);
michael@0 154 },
michael@0 155 /^value is not a non-null object/,
michael@0 156 'only non-null object are allowed');
michael@0 157
michael@0 158 assert.throws(() => {
michael@0 159 add(items, null);
michael@0 160 },
michael@0 161 /^value is not a non-null object/,
michael@0 162 'only non-null object are allowed');
michael@0 163
michael@0 164 assert.throws(() => {
michael@0 165 add(items, true);
michael@0 166 },
michael@0 167 /^value is not a non-null object/,
michael@0 168 'only non-null object are allowed');
michael@0 169 };
michael@0 170
michael@0 171 exports['test adding to non object or null item'] = function(assert) {
michael@0 172 let loader = Loader(module);
michael@0 173 let { add, remove, has, clear, iterator } = loader.require('sdk/lang/weak-set');
michael@0 174
michael@0 175 let item = {};
michael@0 176
michael@0 177 assert.throws(() => {
michael@0 178 add('foo', item);
michael@0 179 },
michael@0 180 /^value is not a non-null object/,
michael@0 181 'only non-null object are allowed');
michael@0 182
michael@0 183 assert.throws(() => {
michael@0 184 add(0, item);
michael@0 185 },
michael@0 186 /^value is not a non-null object/,
michael@0 187 'only non-null object are allowed');
michael@0 188
michael@0 189 assert.throws(() => {
michael@0 190 add(undefined, item);
michael@0 191 },
michael@0 192 /^value is not a non-null object/,
michael@0 193 'only non-null object are allowed');
michael@0 194
michael@0 195 assert.throws(() => {
michael@0 196 add(null, item);
michael@0 197 },
michael@0 198 /^value is not a non-null object/,
michael@0 199 'only non-null object are allowed');
michael@0 200
michael@0 201 assert.throws(() => {
michael@0 202 add(true, item);
michael@0 203 },
michael@0 204 /^value is not a non-null object/,
michael@0 205 'only non-null object are allowed');
michael@0 206 };
michael@0 207
michael@0 208 require('test').run(exports);

mercurial