test-ui-id.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. const { identify } = require('sdk/ui/id');
  6. const { Class } = require('sdk/core/heritage');
  7. const signature = /{[0-9a-f\-]+}/;
  8. exports['test generate id'] = function(assert) {
  9. let first = identify({});
  10. let second = identify({});
  11. assert.ok(signature.test(first), 'first id has a correct signature');
  12. assert.ok(signature.test(second), 'second id has a correct signature');
  13. assert.notEqual(first, identify({}), 'identify generated new uuid [1]');
  14. assert.notEqual(first, second, 'identify generated new uuid [2]');
  15. };
  16. exports['test get id'] = function(assert) {
  17. let thing = {};
  18. let thingID = identify(thing);
  19. assert.equal(thingID, identify(thing), 'ids for things are cached by default');
  20. assert.notEqual(identify(thing), identify({}), 'new ids for new things');
  21. };
  22. exports['test custom id definition for classes'] = function(assert) {
  23. let Thing = Class({});
  24. let thing = Thing();
  25. let counter = 0;
  26. identify.define(Thing, function(thing) {
  27. return ++counter;
  28. });
  29. assert.equal(identify(thing), counter, 'it is possible to define custom identifications');
  30. assert.ok(signature.test(identify({})), 'defining a custom identification does not affect the default behavior');
  31. }
  32. require('sdk/test').run(exports);