summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/mochitest/test_settings.html
blob: 5665b46b3cd654194224f2ca63246fdf508dc280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!DOCTYPE HTML>
<html>
<!--
Bug 1022797 - Settings support from WebIDE
-->
<head>
  <meta charset="utf-8">
  <title>Test Settings Actor</title>
  <script type="text/javascript" src="chrome://mochikit/content/MochiKit/MochiKit.js"></script>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
</head>
<body>
<pre id="test">
<script>

function runTests() {
  var Cu = Components.utils;
  var Cc = Components.classes;
  var Ci = Components.interfaces;

  var {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
  var {DebuggerClient} = require("devtools/shared/client/main");
  var {DebuggerServer} = require("devtools/server/main");

  if (SpecialPowers.isMainProcess()) {
    Cu.import("resource://gre/modules/SettingsRequestManager.jsm");
  }

  SimpleTest.waitForExplicitFinish();

  var {getSettingsFront} = require("devtools/shared/fronts/settings");
  var {_setDefaultSettings} = require("devtools/server/actors/settings");

  DebuggerServer.init(function () { return true; });
  DebuggerServer.addBrowserActors();

  var client = new DebuggerClient(DebuggerServer.connectPipe());
  client.connect().then(function onConnect() {
    client.listTabs(function onListTabs(aResponse) {
      var s = getSettingsFront(client, aResponse);

      var settings = {};
      var resetSettings = {};
      var fakeSettings = {
        "wifi.enabled": true,
        "audio.volume.alarm": 15,
        "app.reportCrashes": "ask",
        "app.someObject": { active: true }
      };
      var localSetting = {
        "wifi.enabled": false,
        "audio.volume.alarm": 0,
        "app.reportCrashes": "none",
        "app.someObject": {}
      };

      function checkValues() {
        is(settings.allSettings["wifi.enabled"].hasUserValue, false, "original unchanged bool setting");
        is(settings.allSettings["audio.volume.alarm"].hasUserValue, false, "original unchanged int setting");
        is(settings.allSettings["app.reportCrashes"].hasUserValue, false, "original unchanged string setting");
        is(settings.allSettings["app.someObject"].hasUserValue, false, "original unchanged object setting");

        is(settings.allSettings["wifi.enabled"].value, fakeSettings["wifi.enabled"], "original read/write bool setting");
        is(settings.allSettings["audio.volume.alarm"].value, fakeSettings["audio.volume.alarm"], "original read/write int setting");
        is(settings.allSettings["app.reportCrashes"].value, fakeSettings["app.reportCrashes"], "original read/write string setting");
        is(JSON.stringify(settings.allSettings["app.someObject"].value), JSON.stringify(fakeSettings["app.someObject"]), "original read/write object setting");

        is(settings.allUpdatedSettings["wifi.enabled"].hasUserValue, true, "updated user-changed bool setting");
        is(settings.allUpdatedSettings["audio.volume.alarm"].hasUserValue, true, "updated user-changed int setting");
        is(settings.allUpdatedSettings["app.reportCrashes"].hasUserValue, true, "updated user-changed string setting");
        is(settings.allUpdatedSettings["app.someObject"].hasUserValue, true, "updated user-changed object setting");

        is(settings["wifi.enabled"], localSetting["wifi.enabled"], "updated bool setting");
        is(settings["audio.volume.alarm"], localSetting["audio.volume.alarm"], "updated int setting");
        is(settings["app.reportCrashes"], localSetting["app.reportCrashes"], "updated string setting");
        is(JSON.stringify(settings["app.someObject"]), JSON.stringify(localSetting["app.someObject"]), "updated object as string setting");

        is(resetSettings["wifi.enabled"], fakeSettings["wifi.enabled"], "reset to original bool setting");
        is(resetSettings["audio.volume.alarm"], fakeSettings["audio.volume.alarm"], "reset to original int setting");
        is(resetSettings["app.reportCrashes"], fakeSettings["app.reportCrashes"], "reset to original string setting");
        is(JSON.stringify(resetSettings["app.someObject"]), JSON.stringify(fakeSettings["app.someObject"]), "reset to original object setting");

        client.close().then(() => {
          DebuggerServer.destroy();
          SimpleTest.finish();
        });
      }

      // settings.json doesn't exist outside of b2g so we will fake it.
      _setDefaultSettings(fakeSettings);
      s.setSetting("wifi.enabled", fakeSettings["wifi.enabled"])
      .then(() => s.setSetting("audio.volume.alarm", fakeSettings["audio.volume.alarm"]))
      .then(() => s.setSetting("app.reportCrashes", fakeSettings["app.reportCrashes"]))
      .then(() => s.setSetting("app.someObject", fakeSettings["app.someObject"]))
      .then(() => s.getAllSettings().then(json => settings.allSettings = json))
      .then(() => s.setSetting("wifi.enabled", localSetting["wifi.enabled"]))
      .then(() => s.setSetting("audio.volume.alarm", localSetting["audio.volume.alarm"]))
      .then(() => s.setSetting("app.reportCrashes", localSetting["app.reportCrashes"]))
      .then(() => s.setSetting("app.someObject", localSetting["app.someObject"]))
      .then(() => s.getAllSettings().then(json => settings.allUpdatedSettings = json))
      .then(() => s.getSetting("wifi.enabled")).then(value => settings["wifi.enabled"] = value)
      .then(() => s.getSetting("audio.volume.alarm")).then(value => settings["audio.volume.alarm"] = value)
      .then(() => s.getSetting("app.reportCrashes")).then(value => settings["app.reportCrashes"] = value)
      .then(() => s.getSetting("app.someObject")).then(value => settings["app.someObject"] = value)
      .then(() => s.clearUserSetting("wifi.enabled")).then(() => {
        s.getSetting("wifi.enabled").then(value => resetSettings["wifi.enabled"] = value);
      })
      .then(() => s.clearUserSetting("audio.volume.alarm")).then(() => {
        s.getSetting("audio.volume.alarm").then(value => resetSettings["audio.volume.alarm"] = value);
      })
      .then(() => s.clearUserSetting("app.reportCrashes")).then(() => {
        s.getSetting("app.reportCrashes").then(value => resetSettings["app.reportCrashes"] = value);
      })
      .then(() => s.clearUserSetting("app.someObject")).then(() => {
        s.getSetting("app.someObject").then(value => {
          resetSettings["app.someObject"] = value
        }).then(checkValues);
      });
    });
  });
}

window.onload = function () {
  runTests();
}
</script>
</pre>
</body>
</html>