summaryrefslogtreecommitdiffstats
path: root/devtools/client/storage/test/browser_storage_dynamic_updates.js
blob: f881146d277329fe0a939b4b87345fe1e3d44575 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

add_task(function* () {
  yield openTabAndSetupStorage(MAIN_DOMAIN + "storage-updates.html");

  let $ = id => gPanelWindow.document.querySelector(id);
  let $$ = sel => gPanelWindow.document.querySelectorAll(sel);

  gUI.tree.expandAll();

  ok(gUI.sidebar.hidden, "Sidebar is initially hidden");
  yield selectTableItem("c1");

  // test that value is something initially
  let initialValue = [[
    {name: "c1", value: "1.2.3.4.5.6.7"},
    {name: "c1.Path", value: "/browser"}
  ], [
    {name: "c1", value: "Array"},
    {name: "c1.0", value: "1"},
    {name: "c1.6", value: "7"}
  ]];

  // test that value is something initially
  let finalValue = [[
    {name: "c1", value: '{"foo": 4,"bar":6}'},
    {name: "c1.Path", value: "/browser"}
  ], [
    {name: "c1", value: "Object"},
    {name: "c1.foo", value: "4"},
    {name: "c1.bar", value: "6"}
  ]];
  // Check that sidebar shows correct initial value
  yield findVariableViewProperties(initialValue[0], false);
  yield findVariableViewProperties(initialValue[1], true);
  // Check if table shows correct initial value
  ok($("#value [data-id='c1'].table-widget-cell"), "cell is present");
  is($("#value [data-id='c1'].table-widget-cell").value, "1.2.3.4.5.6.7",
       "correct initial value in table");
  gWindow.addCookie("c1", '{"foo": 4,"bar":6}', "/browser");
  yield gUI.once("sidebar-updated");

  yield findVariableViewProperties(finalValue[0], false);
  yield findVariableViewProperties(finalValue[1], true);
  ok($("#value [data-id='c1'].table-widget-cell"),
     "cell is present after update");
  is($("#value [data-id='c1'].table-widget-cell").value, '{"foo": 4,"bar":6}',
     "correct final value in table");

  // Add a new entry
  is($$("#value .table-widget-cell").length, 2,
     "Correct number of rows before update 0");

  gWindow.addCookie("c3", "booyeah");

  // Wait once for update and another time for value fetching
  yield gUI.once("store-objects-updated");
  yield gUI.once("store-objects-updated");

  is($$("#value .table-widget-cell").length, 3,
     "Correct number of rows after update 1");

  // Add another
  gWindow.addCookie("c4", "booyeah");

  // Wait once for update and another time for value fetching
  yield gUI.once("store-objects-updated");
  yield gUI.once("store-objects-updated");

  is($$("#value .table-widget-cell").length, 4,
     "Correct number of rows after update 2");

  // Removing cookies
  gWindow.removeCookie("c1", "/browser");

  yield gUI.once("sidebar-updated");

  is($$("#value .table-widget-cell").length, 3,
     "Correct number of rows after delete update 3");

  ok(!$("#c1"), "Correct row got deleted");

  ok(!gUI.sidebar.hidden, "Sidebar still visible for next row");

  // Check if next element's value is visible in sidebar
  yield findVariableViewProperties([{name: "c2", value: "foobar"}]);

  // Keep deleting till no rows

  gWindow.removeCookie("c3");

  yield gUI.once("store-objects-updated");

  is($$("#value .table-widget-cell").length, 2,
     "Correct number of rows after delete update 4");

  // Check if next element's value is visible in sidebar
  yield findVariableViewProperties([{name: "c2", value: "foobar"}]);

  gWindow.removeCookie("c2", "/browser");

  yield gUI.once("sidebar-updated");

  yield findVariableViewProperties([{name: "c4", value: "booyeah"}]);

  is($$("#value .table-widget-cell").length, 1,
     "Correct number of rows after delete update 5");

  gWindow.removeCookie("c4");

  yield gUI.once("store-objects-updated");

  is($$("#value .table-widget-cell").length, 0,
     "Correct number of rows after delete update 6");
  ok(gUI.sidebar.hidden, "Sidebar is hidden when no rows");

  // Testing in local storage
  yield selectTreeItem(["localStorage", "http://test1.example.org"]);

  is($$("#value .table-widget-cell").length, 7,
     "Correct number of rows after delete update 7");

  ok($(".table-widget-cell[data-id='ls4']"), "ls4 exists before deleting");

  gWindow.localStorage.removeItem("ls4");

  yield gUI.once("store-objects-updated");

  is($$("#value .table-widget-cell").length, 6,
     "Correct number of rows after delete update 8");
  ok(!$(".table-widget-cell[data-id='ls4']"),
     "ls4 does not exists after deleting");

  gWindow.localStorage.setItem("ls4", "again");

  yield gUI.once("store-objects-updated");
  yield gUI.once("store-objects-updated");

  is($$("#value .table-widget-cell").length, 7,
     "Correct number of rows after delete update 9");
  ok($(".table-widget-cell[data-id='ls4']"),
     "ls4 came back after adding it again");

  // Updating a row
  gWindow.localStorage.setItem("ls2", "ls2-changed");

  yield gUI.once("store-objects-updated");
  yield gUI.once("store-objects-updated");

  is($("#value [data-id='ls2']").value, "ls2-changed",
      "Value got updated for local storage");

  // Testing in session storage
  yield selectTreeItem(["sessionStorage", "http://test1.example.org"]);

  is($$("#value .table-widget-cell").length, 3,
     "Correct number of rows for session storage");

  gWindow.sessionStorage.setItem("ss4", "new-item");

  yield gUI.once("store-objects-updated");
  yield gUI.once("store-objects-updated");

  is($$("#value .table-widget-cell").length, 4,
     "Correct number of rows after session storage update");

  // deleting item

  gWindow.sessionStorage.removeItem("ss3");

  yield gUI.once("store-objects-updated");

  gWindow.sessionStorage.removeItem("ss1");

  yield gUI.once("store-objects-updated");

  is($$("#value .table-widget-cell").length, 2,
     "Correct number of rows after removing items from session storage");

  yield selectTableItem("ss2");

  ok(!gUI.sidebar.hidden, "sidebar is visible");

  // Checking for correct value in sidebar before update
  yield findVariableViewProperties([{name: "ss2", value: "foobar"}]);

  gWindow.sessionStorage.setItem("ss2", "changed=ss2");

  yield gUI.once("sidebar-updated");

  is($("#value [data-id='ss2']").value, "changed=ss2",
      "Value got updated for session storage in the table");

  yield findVariableViewProperties([{name: "ss2", value: "changed=ss2"}]);

  // Clearing items. Bug 1233497 makes it so that we can no longer yield
  // CPOWs from Tasks. We work around this by calling clear via a ContentTask
  // instead.
  yield ContentTask.spawn(gBrowser.selectedBrowser, null, function* () {
    return Task.spawn(content.wrappedJSObject.clear);
  });

  yield gUI.once("store-objects-cleared");

  is($$("#value .table-widget-cell").length, 0,
     "Table should be cleared");

  yield finishTests();
});