dom/settings/tests/test_settings_service.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 "use strict";
michael@0 2
michael@0 3 const Cu = Components.utils;
michael@0 4 const Cc = Components.classes;
michael@0 5 const Ci = Components.interfaces;
michael@0 6
michael@0 7 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 8 Cu.import("resource://gre/modules/Services.jsm");
michael@0 9
michael@0 10 SimpleTest.waitForExplicitFinish();
michael@0 11
michael@0 12 XPCOMUtils.defineLazyServiceGetter(this, "SettingsService",
michael@0 13 "@mozilla.org/settingsService;1",
michael@0 14 "nsISettingsService");
michael@0 15
michael@0 16 let tests = [
michael@0 17 /* Callback tests */
michael@0 18 function() {
michael@0 19 let callbackCount = 10;
michael@0 20
michael@0 21 let callback = {
michael@0 22 handle: function(name, result) {
michael@0 23 switch (callbackCount) {
michael@0 24 case 10:
michael@0 25 case 9:
michael@0 26 is(result, true, "result is true");
michael@0 27 break;
michael@0 28 case 8:
michael@0 29 case 7:
michael@0 30 is(result, false, "result is false");
michael@0 31 break;
michael@0 32 case 6:
michael@0 33 case 5:
michael@0 34 is(result, 9, "result is 9");
michael@0 35 break;
michael@0 36 case 4:
michael@0 37 case 3:
michael@0 38 is(result, 9.4, "result is 9.4");
michael@0 39 break;
michael@0 40 case 2:
michael@0 41 is(result, false, "result is false");
michael@0 42 break;
michael@0 43 case 1:
michael@0 44 is(result, null, "result is null");
michael@0 45 break;
michael@0 46 default:
michael@0 47 ok(false, "Unexpected call: " + callbackCount);
michael@0 48 }
michael@0 49
michael@0 50 --callbackCount;
michael@0 51 if (callbackCount === 0) {
michael@0 52 next();
michael@0 53 }
michael@0 54 },
michael@0 55
michael@0 56 handleError: function(name) {
michael@0 57 ok(false, "error: " + name);
michael@0 58 }
michael@0 59 };
michael@0 60
michael@0 61 let lock = SettingsService.createLock();
michael@0 62 let lock1 = SettingsService.createLock();
michael@0 63
michael@0 64 lock.set("asdf", true, callback, null);
michael@0 65 lock1.get("asdf", callback);
michael@0 66 lock.get("asdf", callback);
michael@0 67 lock.set("asdf", false, callback, null);
michael@0 68 lock.get("asdf", callback);
michael@0 69 lock.set("int", 9, callback, null);
michael@0 70 lock.get("int", callback);
michael@0 71 lock.set("doub", 9.4, callback, null);
michael@0 72 lock.get("doub", callback);
michael@0 73 lock1.get("asdfxxx", callback);
michael@0 74 },
michael@0 75
michael@0 76 /* Observer tests */
michael@0 77 function() {
michael@0 78 const XPCOM_SHUTDOWN = "xpcom-shutdown";
michael@0 79 const MOZSETTINGS_CHANGED = "mozsettings-changed";
michael@0 80
michael@0 81 const TEST_OBSERVER_KEY = "test.observer.key";
michael@0 82 const TEST_OBSERVER_VALUE = true;
michael@0 83 const TEST_OBSERVER_MESSAGE = "test.observer.message";
michael@0 84
michael@0 85 let observerCount = 2;
michael@0 86
michael@0 87 function observer(subject, topic, data) {
michael@0 88 if (topic === XPCOM_SHUTDOWN) {
michael@0 89 Services.obs.removeObserver(this, XPCOM_SHUTDOWN);
michael@0 90 Services.obs.removeObserver(this, MOZSETTINGS_CHANGED);
michael@0 91 return;
michael@0 92 }
michael@0 93
michael@0 94 if (topic !== MOZSETTINGS_CHANGED) {
michael@0 95 ok(false, "Event is not mozsettings-changed.");
michael@0 96 return;
michael@0 97 }
michael@0 98
michael@0 99 data = JSON.parse(data);
michael@0 100
michael@0 101 function checkProp(name, type, value) {
michael@0 102 ok(name in data, "data." + name + " is present");
michael@0 103 is(typeof data[name], type, "data." + name + " is " + type);
michael@0 104 is(data[name], value, "data." + name + " is " + value);
michael@0 105 }
michael@0 106
michael@0 107 checkProp("key", "string", TEST_OBSERVER_KEY);
michael@0 108 checkProp("value", "boolean", TEST_OBSERVER_VALUE);
michael@0 109 if (observerCount === 2) {
michael@0 110 checkProp("message", "object", null);
michael@0 111 } else {
michael@0 112 checkProp("message", "string", TEST_OBSERVER_MESSAGE);
michael@0 113 }
michael@0 114 --observerCount;
michael@0 115
michael@0 116 if (observerCount === 0) {
michael@0 117 next();
michael@0 118 }
michael@0 119 }
michael@0 120
michael@0 121 Services.obs.addObserver(observer, XPCOM_SHUTDOWN, false);
michael@0 122 Services.obs.addObserver(observer, MOZSETTINGS_CHANGED, false);
michael@0 123
michael@0 124 let lock = SettingsService.createLock();
michael@0 125 lock.set(TEST_OBSERVER_KEY, TEST_OBSERVER_VALUE, null, null);
michael@0 126 lock.set(TEST_OBSERVER_KEY, TEST_OBSERVER_VALUE, null, TEST_OBSERVER_MESSAGE);
michael@0 127 }
michael@0 128 ];
michael@0 129
michael@0 130 function next() {
michael@0 131 let step = tests.shift();
michael@0 132 if (step) {
michael@0 133 try {
michael@0 134 step();
michael@0 135 } catch(e) {
michael@0 136 ok(false, "Test threw: " + e);
michael@0 137 }
michael@0 138 } else {
michael@0 139 SimpleTest.finish();
michael@0 140 }
michael@0 141 }
michael@0 142
michael@0 143 next();

mercurial