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 { identify } = require('sdk/ui/id'); michael@0: const { Class } = require('sdk/core/heritage'); michael@0: michael@0: const signature = /{[0-9a-f\-]+}/; michael@0: michael@0: exports['test generate id'] = function(assert) { michael@0: let first = identify({}); michael@0: let second = identify({}); michael@0: michael@0: assert.ok(signature.test(first), 'first id has a correct signature'); michael@0: assert.ok(signature.test(second), 'second id has a correct signature'); michael@0: michael@0: assert.notEqual(first, identify({}), 'identify generated new uuid [1]'); michael@0: assert.notEqual(first, second, 'identify generated new uuid [2]'); michael@0: }; michael@0: michael@0: exports['test get id'] = function(assert) { michael@0: let thing = {}; michael@0: let thingID = identify(thing); michael@0: michael@0: assert.equal(thingID, identify(thing), 'ids for things are cached by default'); michael@0: assert.notEqual(identify(thing), identify({}), 'new ids for new things'); michael@0: }; michael@0: michael@0: exports['test custom id definition for classes'] = function(assert) { michael@0: let Thing = Class({}); michael@0: let thing = Thing(); michael@0: let counter = 0; michael@0: michael@0: identify.define(Thing, function(thing) { michael@0: return ++counter; michael@0: }); michael@0: michael@0: assert.equal(identify(thing), counter, 'it is possible to define custom identifications'); michael@0: assert.ok(signature.test(identify({})), 'defining a custom identification does not affect the default behavior'); michael@0: } michael@0: michael@0: require('sdk/test').run(exports);