summaryrefslogtreecommitdiffstats
path: root/dom/settings/tests/test_settings_permissions.html
blob: 4cc02385aa514ad73726f9be06e97fabce63330e (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id={678695}
-->
<head>
  <title>Test for Bug {678695} Settings API</title>
  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
  <script type="text/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>

<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id={900551}">Mozilla Bug {900551}</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
<script class="testbody" type="text/javascript">

"use strict";

var url = SimpleTest.getTestFileURL("file_loadserver.js");
var script = SpecialPowers.loadChromeScript(url);

function onUnwantedSuccess() {
  ok(false, "onUnwantedSuccess: shouldn't get here");
}

// No more permissions, so failure cannot happen
function onFailure() {
  ok(true, "in on Failure!");
  next();
}

const wifi  = {"wifi.enabled": false}
const wallpaper = {"wallpaper.image": "test-image"};

var combination = {
  "wifi.enabled": false,
  "wallpaper.image": "test-image"
}

function equals(o1, o2) {
  var k1 = Object.keys(o1).sort();
  var k2 = Object.keys(o2).sort();
  if (k1.length != k2.length) return false;
  return k1.zip(k2, function(keyPair) {
    if(typeof o1[keyPair[0]] == typeof o2[keyPair[1]] == "object"){
      return equals(o1[keyPair[0]], o2[keyPair[1]])
    } else {
      return o1[keyPair[0]] == o2[keyPair[1]];
    }
  }).all();
};

function observer1(setting) {
  is(setting.settingName, "screen.brightness", "Same settingName");
  is(setting.settingValue, "0.7", "Same settingvalue");
};

function onsettingschangeWithNext(event) {
  is(event.settingName, "screen.brightness", "Same settingName");
  is(event.settingValue, "0.7", "Same settingvalue");
  next();
};

function check(o1, o2) {
  is(JSON.stringify(o1), JSON.stringify(o2), "same");
}

var req, req2, req3, req4, req5, req6;
var index = 0;

var steps = [
  // Can't delete database here since that requires permissions we don't want
  // to give the page.
  function () {
    ok(true, "Setting wallpaper");
    var lock = navigator.mozSettings.createLock();
    req = lock.set(wallpaper);
    req.onsuccess = function () {
      ok(true, "set done");
    }
    req.onerror = onFailure;

    var lock2 = navigator.mozSettings.createLock();
    req2 = lock2.get("wallpaper.image");
    req2.onsuccess = function () {
      is(Object.keys(req2.result).length, 1, "length 1");
      check(wallpaper, req2.result);
      ok(true, "Get wallpaper Done");
      next();
    };
    req2.onerror = onFailure;
  },
  function () {
    ok(true, "Get Wifi");
    var lock = navigator.mozSettings.createLock();
    req = lock.get("wifi.enabled");
    req.onerror = function () {
      ok(true, "get failed (expected)");
      next();
    }
    req.onsuccess = onFailure;
  },
  function () {
    ok(true, "Set Wifi");
    var lock = navigator.mozSettings.createLock();
    req = lock.set(wifi);
    req.onerror = function () {
      ok(true, "set failed (expected)");
      next();
    }
    req.onsuccess = onFailure;
  },
  function () {
    ok(true, "Set combination (1 valid 1 not valid)");
    var lock = navigator.mozSettings.createLock();
    req = lock.set(combination);
    req.onerror = function () {
      ok(true, "set failed (expected)");
      next();
    }
    req.onsuccess = onFailure;
  },
  function () {
    ok(true, "All requests on a failed lock should fail");
    var lock = navigator.mozSettings.createLock();
    lock.onsettingstransactionfailure = function (evt) {
      ok(evt.error == "Lock failed a permissions check, all requests now failing.", "transaction failure on permissions error message correct.");
      ok(true, "transaction failed (expected) ");
      next();
    };
    lock.onsettingstransactionsuccess = onFailure;

    req = lock.set(wifi);
    req.onerror = function () {
      ok(true, "set failed (expected)");
    }
    req.onsuccess = onFailure;
    req2 = lock.get("wallpaper.image");
    req2.onerror = function () {
      ok(true, "get failed (expected)");
    }
    req2.onsuccess = onFailure;
  },
  function () {
    ok(true, "Set combination (1 valid 1 not valid)");
    var lock = navigator.mozSettings.createLock();
    req = lock.set(combination);
    req.onerror = function () {
      ok(true, "set failed (expected)");
      next();
    }
    req.onsuccess = onFailure;
  },
  function () {
    ok(true, "all done!\n");
    SimpleTest.finish();
  }
];

function next() {
  ok(true, "Begin!");
  if (index >= steps.length) {
    ok(false, "Shouldn't get here!");
    return;
  }
  try {
    steps[index]();
  } catch(ex) {
    ok(false, "Caught exception", ex);
  }
  index += 1;
}

SimpleTest.waitForExplicitFinish();
addLoadEvent(next);
</script>
</pre>
</body>
</html>