addon-sdk/source/test/addons/places/tests/test-places-utils.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 'use strict';
michael@0 5
michael@0 6 module.metadata = {
michael@0 7 'engines': {
michael@0 8 'Firefox': '*'
michael@0 9 }
michael@0 10 };
michael@0 11
michael@0 12 const { defer, all } = require('sdk/core/promise');
michael@0 13 const { setTimeout } = require('sdk/timers');
michael@0 14 const { TreeNode } = require('sdk/places/utils');
michael@0 15
michael@0 16 exports['test construct tree'] = function (assert) {
michael@0 17 let tree = TreeNode(1);
michael@0 18 tree.add([2, 3, 4]);
michael@0 19 tree.get(2).add([2.1, 2.2, 2.3]);
michael@0 20 let newTreeNode = TreeNode(4.3);
michael@0 21 newTreeNode.add([4.31, 4.32]);
michael@0 22 tree.get(4).add([4.1, 4.2, newTreeNode]);
michael@0 23
michael@0 24 assert.equal(tree.get(2).value, 2, 'get returns node with correct value');
michael@0 25 assert.equal(tree.get(2.3).value, 2.3, 'get returns node with correct value');
michael@0 26 assert.equal(tree.get(4.32).value, 4.32, 'get returns node even if created from nested node');
michael@0 27 assert.equal(tree.get(4).children.length, 3, 'nodes have correct children length');
michael@0 28 assert.equal(tree.get(3).children.length, 0, 'nodes have correct children length');
michael@0 29
michael@0 30 assert.equal(tree.get(4).get(4.32).value, 4.32, 'node.get descends from itself');
michael@0 31 assert.equal(tree.get(4).get(2), null, 'node.get descends from itself fails if not descendant');
michael@0 32 };
michael@0 33
michael@0 34 exports['test walk'] = function (assert, done) {
michael@0 35 let resultsAll = [];
michael@0 36 let resultsNode = [];
michael@0 37 let tree = TreeNode(1);
michael@0 38 tree.add([2, 3, 4]);
michael@0 39 tree.get(2).add([2.1, 2.2]);
michael@0 40
michael@0 41 tree.walk(function (node) {
michael@0 42 resultsAll.push(node.value);
michael@0 43 }).then(() => {
michael@0 44 [1, 2, 2.1, 2.2, 3, 4].forEach(num => {
michael@0 45 assert.ok(~resultsAll.indexOf(num), 'function applied to each node from root');
michael@0 46 });
michael@0 47 return tree.get(2).walk(node => resultsNode.push(node.value));
michael@0 48 }).then(() => {
michael@0 49 [2, 2.1, 2.2].forEach(function (num) {
michael@0 50 assert.ok(~resultsNode.indexOf(num), 'function applied to each node from node');
michael@0 51 });
michael@0 52 }).catch(assert.fail).then(done);
michael@0 53 };
michael@0 54
michael@0 55 exports['test async walk'] = function (assert, done) {
michael@0 56 let resultsAll = [];
michael@0 57 let tree = TreeNode(1);
michael@0 58 tree.add([2, 3, 4]);
michael@0 59 tree.get(2).add([2.1, 2.2]);
michael@0 60
michael@0 61 tree.walk(function (node) {
michael@0 62 let deferred = defer();
michael@0 63 setTimeout(function () {
michael@0 64 resultsAll.push(node.value);
michael@0 65 deferred.resolve(node.value);
michael@0 66 }, node.value === 2 ? 50 : 5);
michael@0 67 return deferred.promise;
michael@0 68 }).then(function () {
michael@0 69 [1, 2, 2.1, 2.2, 3, 4].forEach(function (num) {
michael@0 70 assert.ok(~resultsAll.indexOf(num), 'function applied to each node from root');
michael@0 71 });
michael@0 72 assert.ok(resultsAll.indexOf(2) < resultsAll.indexOf(2.1),
michael@0 73 'child should wait for parent to complete');
michael@0 74 assert.ok(resultsAll.indexOf(2) < resultsAll.indexOf(2.2),
michael@0 75 'child should wait for parent to complete');
michael@0 76 done();
michael@0 77 });
michael@0 78 };

mercurial