test-preferences-target.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 { PrefsTarget } = require('sdk/preferences/event-target');
  6. const { get, set, reset } = require('sdk/preferences/service');
  7. const { Loader } = require('sdk/test/loader');
  8. const { setTimeout } = require('sdk/timers');
  9. const root = PrefsTarget();
  10. exports.testPrefsTarget = function(assert, done) {
  11. let loader = Loader(module);
  12. let pt = loader.require('sdk/preferences/event-target').PrefsTarget({});
  13. let name = 'test';
  14. assert.equal(get(name, ''), '', 'test pref is blank');
  15. pt.once(name, function() {
  16. assert.equal(pt.prefs[name], 2, 'test pref is 2');
  17. pt.once(name, function() {
  18. assert.fail('should not have heard a pref change');
  19. });
  20. loader.unload();
  21. root.once(name, function() {
  22. assert.pass('test pref was changed');
  23. reset(name);
  24. // NOTE: using setTimeout to make sure that the other listener had
  25. // a chance to fail
  26. // end test
  27. setTimeout(done);
  28. });
  29. set(name, 3);
  30. });
  31. pt.prefs[name] = 2;
  32. };
  33. require('sdk/test').run(exports);