test-places-utils.js 2.6 KB

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