michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: const collection = require("sdk/util/collection"); michael@0: michael@0: exports.testAddRemove = function (assert) { michael@0: let coll = new collection.Collection(); michael@0: compare(assert, coll, []); michael@0: addRemove(assert, coll, [], false); michael@0: }; michael@0: michael@0: exports.testAddRemoveBackingArray = function (assert) { michael@0: let items = ["foo"]; michael@0: let coll = new collection.Collection(items); michael@0: compare(assert, coll, items); michael@0: addRemove(assert, coll, items, true); michael@0: michael@0: items = ["foo", "bar"]; michael@0: coll = new collection.Collection(items); michael@0: compare(assert, coll, items); michael@0: addRemove(assert, coll, items, true); michael@0: }; michael@0: michael@0: exports.testProperty = function (assert) { michael@0: let obj = makeObjWithCollProp(); michael@0: compare(assert, obj.coll, []); michael@0: addRemove(assert, obj.coll, [], false); michael@0: michael@0: // Test single-value set. michael@0: let items = ["foo"]; michael@0: obj.coll = items[0]; michael@0: compare(assert, obj.coll, items); michael@0: addRemove(assert, obj.coll, items, false); michael@0: michael@0: // Test array set. michael@0: items = ["foo", "bar"]; michael@0: obj.coll = items; michael@0: compare(assert, obj.coll, items); michael@0: addRemove(assert, obj.coll, items, false); michael@0: }; michael@0: michael@0: exports.testPropertyBackingArray = function (assert) { michael@0: let items = ["foo"]; michael@0: let obj = makeObjWithCollProp(items); michael@0: compare(assert, obj.coll, items); michael@0: addRemove(assert, obj.coll, items, true); michael@0: michael@0: items = ["foo", "bar"]; michael@0: obj = makeObjWithCollProp(items); michael@0: compare(assert, obj.coll, items); michael@0: addRemove(assert, obj.coll, items, true); michael@0: }; michael@0: michael@0: // Adds some values to coll and then removes them. initialItems is an array michael@0: // containing coll's initial items. isBacking is true if initialItems is coll's michael@0: // backing array; the point is that updates to coll should affect initialItems michael@0: // if that's the case. michael@0: function addRemove(assert, coll, initialItems, isBacking) { michael@0: let items = isBacking ? initialItems : initialItems.slice(0); michael@0: let numInitialItems = items.length; michael@0: michael@0: // Test add(val). michael@0: let numInsertions = 5; michael@0: for (let i = 0; i < numInsertions; i++) { michael@0: compare(assert, coll, items); michael@0: coll.add(i); michael@0: if (!isBacking) michael@0: items.push(i); michael@0: } michael@0: compare(assert, coll, items); michael@0: michael@0: // Add the items we just added to make sure duplicates aren't added. michael@0: for (let i = 0; i < numInsertions; i++) michael@0: coll.add(i); michael@0: compare(assert, coll, items); michael@0: michael@0: // Test remove(val). Do a kind of shuffled remove. Remove item 1, then michael@0: // item 0, 3, 2, 5, 4, ... michael@0: for (let i = 0; i < numInsertions; i++) { michael@0: let val = i % 2 ? i - 1 : michael@0: i === numInsertions - 1 ? i : i + 1; michael@0: coll.remove(val); michael@0: if (!isBacking) michael@0: items.splice(items.indexOf(val), 1); michael@0: compare(assert, coll, items); michael@0: } michael@0: assert.equal(coll.length, numInitialItems, michael@0: "All inserted items should be removed"); michael@0: michael@0: // Remove the items we just removed. coll should be unchanged. michael@0: for (let i = 0; i < numInsertions; i++) michael@0: coll.remove(i); michael@0: compare(assert, coll, items); michael@0: michael@0: // Test add and remove([val1, val2]). michael@0: let newItems = [0, 1]; michael@0: coll.add(newItems); michael@0: compare(assert, coll, isBacking ? items : items.concat(newItems)); michael@0: coll.remove(newItems); michael@0: compare(assert, coll, items); michael@0: assert.equal(coll.length, numInitialItems, michael@0: "All inserted items should be removed"); michael@0: } michael@0: michael@0: // Asserts that the items in coll are the items of array. michael@0: function compare(assert, coll, array) { michael@0: assert.equal(coll.length, array.length, michael@0: "Collection length should be correct"); michael@0: let numItems = 0; michael@0: for (let item in coll) { michael@0: assert.equal(item, array[numItems], "Items should be equal"); michael@0: numItems++; michael@0: } michael@0: assert.equal(numItems, array.length, michael@0: "Number of items in iteration should be correct"); michael@0: } michael@0: michael@0: // Returns a new object with a collection property named "coll". backingArray, michael@0: // if defined, will create the collection with that backing array. michael@0: function makeObjWithCollProp(backingArray) { michael@0: let obj = {}; michael@0: collection.addCollectionProperty(obj, "coll", backingArray); michael@0: return obj; michael@0: } michael@0: michael@0: require("sdk/test").run(exports);