summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/addons/places/lib/test-places-utils.js
blob: c909a2cbb1c2bb83c4cc0c55eda24a39fb1e9d93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';

module.metadata = {
  'engines': {
    'Firefox': '*'
  }
};

const { defer, all } = require('sdk/core/promise');
const { setTimeout } = require('sdk/timers');
const { TreeNode } = require('sdk/places/utils');

exports['test construct tree'] = function (assert) {
  let tree = TreeNode(1);
  tree.add([2, 3, 4]);
  tree.get(2).add([2.1, 2.2, 2.3]);
  let newTreeNode = TreeNode(4.3);
  newTreeNode.add([4.31, 4.32]);
  tree.get(4).add([4.1, 4.2, newTreeNode]);

  assert.equal(tree.get(2).value, 2, 'get returns node with correct value');
  assert.equal(tree.get(2.3).value, 2.3, 'get returns node with correct value');
  assert.equal(tree.get(4.32).value, 4.32, 'get returns node even if created from nested node');
  assert.equal(tree.get(4).children.length, 3, 'nodes have correct children length');
  assert.equal(tree.get(3).children.length, 0, 'nodes have correct children length');

  assert.equal(tree.get(4).get(4.32).value, 4.32, 'node.get descends from itself');
  assert.equal(tree.get(4).get(2), null, 'node.get descends from itself fails if not descendant');
};

exports['test walk'] = function (assert, done) {
  let resultsAll = [];
  let resultsNode = [];
  let tree = TreeNode(1);
  tree.add([2, 3, 4]);
  tree.get(2).add([2.1, 2.2]);

  tree.walk(function (node) {
    resultsAll.push(node.value);
  }).then(() => {
    [1, 2, 2.1, 2.2, 3, 4].forEach(num => {
      assert.ok(~resultsAll.indexOf(num), 'function applied to each node from root');
    });
    return tree.get(2).walk(node => resultsNode.push(node.value));
  }).then(() => {
    [2, 2.1, 2.2].forEach(function (num) {
      assert.ok(~resultsNode.indexOf(num), 'function applied to each node from node');
    });
  }).catch(assert.fail).then(done);
};

exports['test async walk'] = function (assert, done) {
  let resultsAll = [];
  let tree = TreeNode(1);
  tree.add([2, 3, 4]);
  tree.get(2).add([2.1, 2.2]);

  tree.walk(function (node) {
    let deferred = defer();
    setTimeout(function () {
      resultsAll.push(node.value);
      deferred.resolve(node.value);
    }, node.value === 2 ? 50 : 5);
    return deferred.promise;
  }).then(function () {
    [1, 2, 2.1, 2.2, 3, 4].forEach(function (num) {
      assert.ok(~resultsAll.indexOf(num), 'function applied to each node from root');
    });
    assert.ok(resultsAll.indexOf(2) < resultsAll.indexOf(2.1),
      'child should wait for parent to complete');
    assert.ok(resultsAll.indexOf(2) < resultsAll.indexOf(2.2),
      'child should wait for parent to complete');
    done();
  });
};