summaryrefslogtreecommitdiffstats
path: root/toolkit/components/xulstore/tests/xpcshell/test_XULStore.js
blob: d100592e814aa7bef341c91e602ad75a88d6c937 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/◦
*/

"use strict";

var XULStore = null;
var browserURI = "chrome://browser/content/browser.xhtml";
var aboutURI = "about:config";

function run_test() {
  do_get_profile();
  run_next_test();
}

function checkValue(uri, id, attr, reference) {
  let value = XULStore.getValue(uri, id, attr);
  Assert.equal(value, reference);
}

function checkValueExists(uri, id, attr, exists) {
  Assert.equal(XULStore.hasValue(uri, id, attr), exists);
}

function getIDs(uri) {
  return Array.from(XULStore.getIDsEnumerator(uri)).sort();
}

function getAttributes(uri, id) {
  return Array.from(XULStore.getAttributeEnumerator(uri, id)).sort();
}

function checkArrays(a, b) {
  a.sort();
  b.sort();
  Assert.equal(a.toString(), b.toString());
}

add_task(async function setup() {
  // Set a value that a future test depends on manually
  XULStore = Services.xulStore;
  XULStore.setValue(browserURI, "main-window", "width", "994");
});

add_task(async function testTruncation() {
  let dos = Array(8192).join("~");
  // Long id names should trigger an exception
  Assert.throws(
    () => XULStore.setValue(browserURI, dos, "foo", "foo"),
    /NS_ERROR_ILLEGAL_VALUE/
  );

  // Long attr names should trigger an exception
  Assert.throws(
    () => XULStore.setValue(browserURI, "foo", dos, "foo"),
    /NS_ERROR_ILLEGAL_VALUE/
  );

  // Long values should be truncated
  XULStore.setValue(browserURI, "dos", "dos", dos);
  dos = XULStore.getValue(browserURI, "dos", "dos");
  Assert.ok(dos.length == 4096);
  XULStore.removeValue(browserURI, "dos", "dos");
});

add_task(async function testGetValue() {
  // Get non-existing property
  checkValue(browserURI, "side-window", "height", "");

  // Get existing property
  checkValue(browserURI, "main-window", "width", "994");
});

add_task(async function testHasValue() {
  // Check non-existing property
  checkValueExists(browserURI, "side-window", "height", false);

  // Check existing property
  checkValueExists(browserURI, "main-window", "width", true);
});

add_task(async function testSetValue() {
  // Set new attribute
  checkValue(browserURI, "side-bar", "width", "");
  XULStore.setValue(browserURI, "side-bar", "width", "1000");
  checkValue(browserURI, "side-bar", "width", "1000");
  checkArrays(["main-window", "side-bar"], getIDs(browserURI));
  checkArrays(["width"], getAttributes(browserURI, "side-bar"));

  // Modify existing property
  checkValue(browserURI, "side-bar", "width", "1000");
  XULStore.setValue(browserURI, "side-bar", "width", "1024");
  checkValue(browserURI, "side-bar", "width", "1024");
  checkArrays(["main-window", "side-bar"], getIDs(browserURI));
  checkArrays(["width"], getAttributes(browserURI, "side-bar"));

  // Add another attribute
  checkValue(browserURI, "side-bar", "height", "");
  XULStore.setValue(browserURI, "side-bar", "height", "1000");
  checkValue(browserURI, "side-bar", "height", "1000");
  checkArrays(["main-window", "side-bar"], getIDs(browserURI));
  checkArrays(["width", "height"], getAttributes(browserURI, "side-bar"));
});

add_task(async function testRemoveValue() {
  // Remove first attribute
  checkValue(browserURI, "side-bar", "width", "1024");
  XULStore.removeValue(browserURI, "side-bar", "width");
  checkValue(browserURI, "side-bar", "width", "");
  checkValueExists(browserURI, "side-bar", "width", false);
  checkArrays(["main-window", "side-bar"], getIDs(browserURI));
  checkArrays(["height"], getAttributes(browserURI, "side-bar"));

  // Remove second attribute
  checkValue(browserURI, "side-bar", "height", "1000");
  XULStore.removeValue(browserURI, "side-bar", "height");
  checkValue(browserURI, "side-bar", "height", "");
  checkArrays(["main-window"], getIDs(browserURI));

  // Removing an attribute that doesn't exists shouldn't fail
  XULStore.removeValue(browserURI, "main-window", "bar");

  // Removing from an id that doesn't exists shouldn't fail
  XULStore.removeValue(browserURI, "foo", "bar");

  // Removing from a document that doesn't exists shouldn't fail
  let nonDocURI = "chrome://example/content/other.xul";
  XULStore.removeValue(nonDocURI, "foo", "bar");

  // Remove all attributes in browserURI
  XULStore.removeValue(browserURI, "addon-bar", "collapsed");
  checkArrays([], getAttributes(browserURI, "addon-bar"));
  XULStore.removeValue(browserURI, "main-window", "width");
  XULStore.removeValue(browserURI, "main-window", "height");
  XULStore.removeValue(browserURI, "main-window", "screenX");
  XULStore.removeValue(browserURI, "main-window", "screenY");
  XULStore.removeValue(browserURI, "main-window", "sizemode");
  checkArrays([], getAttributes(browserURI, "main-window"));
  XULStore.removeValue(browserURI, "sidebar-title", "value");
  checkArrays([], getAttributes(browserURI, "sidebar-title"));
  checkArrays([], getIDs(browserURI));

  // Remove all attributes in aboutURI
  XULStore.removeValue(aboutURI, "prefCol", "ordinal");
  XULStore.removeValue(aboutURI, "prefCol", "sortDirection");
  checkArrays([], getAttributes(aboutURI, "prefCol"));
  XULStore.removeValue(aboutURI, "lockCol", "ordinal");
  checkArrays([], getAttributes(aboutURI, "lockCol"));
  checkArrays([], getIDs(aboutURI));
});