summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_library_commands.js
blob: 254c9503c25d0893beb2b5bb2ab645bf71fe99a4 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* 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/. */

/**
 *  Test enabled commands in the left pane folder of the Library.
 */

const TEST_URI = NetUtil.newURI("http://www.mozilla.org/");

registerCleanupFunction(async function () {
  await PlacesUtils.bookmarks.eraseEverything();
  await PlacesUtils.history.clear();
});

add_task(async function test_date_container() {
  let library = await promiseLibrary();
  info("Ensure date containers under History cannot be cut but can be deleted");

  await PlacesTestUtils.addVisits(TEST_URI);

  // Select and open the left pane "History" query.
  let PO = library.PlacesOrganizer;

  PO.selectLeftPaneBuiltIn("History");
  Assert.notEqual(
    PO._places.selectedNode,
    null,
    "We correctly selected History"
  );

  // Check that both delete and cut commands are disabled, cause this is
  // a child of the left pane folder.
  Assert.ok(
    PO._places.controller.isCommandEnabled("cmd_copy"),
    "Copy command is enabled"
  );
  Assert.ok(
    !PO._places.controller.isCommandEnabled("cmd_cut"),
    "Cut command is disabled"
  );
  Assert.ok(
    !PO._places.controller.isCommandEnabled("cmd_delete"),
    "Delete command is disabled"
  );
  let historyNode = PlacesUtils.asContainer(PO._places.selectedNode);
  historyNode.containerOpen = true;

  // Check that we have a child container. It is "Today" container.
  Assert.equal(historyNode.childCount, 1, "History node has one child");
  let todayNode = historyNode.getChild(0);
  let todayNodeExpectedTitle = PlacesUtils.getString("finduri-AgeInDays-is-0");
  Assert.equal(
    todayNode.title,
    todayNodeExpectedTitle,
    "History child is the expected container"
  );

  // Select "Today" container.
  PO._places.selectNode(todayNode);
  Assert.equal(
    PO._places.selectedNode,
    todayNode,
    "We correctly selected Today container"
  );
  // Check that delete command is enabled but cut command is disabled, cause
  // this is an history item.
  Assert.ok(
    PO._places.controller.isCommandEnabled("cmd_copy"),
    "Copy command is enabled"
  );
  Assert.ok(
    !PO._places.controller.isCommandEnabled("cmd_cut"),
    "Cut command is disabled"
  );
  Assert.ok(
    PO._places.controller.isCommandEnabled("cmd_delete"),
    "Delete command is enabled"
  );

  // Execute the delete command and check visit has been removed.
  const promiseURIRemoved = PlacesTestUtils.waitForNotification(
    "page-removed",
    events => events[0].url === TEST_URI.spec
  );
  PO._places.controller.doCommand("cmd_delete");
  const removeEvents = await promiseURIRemoved;
  Assert.ok(
    removeEvents[0].isRemovedFromStore,
    "isRemovedFromStore should be true"
  );

  // Test live update of "History" query.
  Assert.equal(historyNode.childCount, 0, "History node has no more children");

  historyNode.containerOpen = false;

  Assert.ok(
    !(await PlacesUtils.history.hasVisits(TEST_URI)),
    "Visit has been removed"
  );

  library.close();
});

add_task(async function test_query_on_toolbar() {
  let library = await promiseLibrary();
  info("Ensure queries can be cut or deleted");

  // Select and open the left pane "Bookmarks Toolbar" folder.
  let PO = library.PlacesOrganizer;

  PO.selectLeftPaneBuiltIn("BookmarksToolbar");
  Assert.notEqual(PO._places.selectedNode, null, "We have a valid selection");
  Assert.equal(
    PlacesUtils.getConcreteItemGuid(PO._places.selectedNode),
    PlacesUtils.bookmarks.toolbarGuid,
    "We have correctly selected bookmarks toolbar node."
  );

  // Check that both cut and delete commands are disabled, cause this is a child
  // of the All Bookmarks special query.
  Assert.ok(
    PO._places.controller.isCommandEnabled("cmd_copy"),
    "Copy command is enabled"
  );
  Assert.ok(
    !PO._places.controller.isCommandEnabled("cmd_cut"),
    "Cut command is disabled"
  );
  Assert.ok(
    !PO._places.controller.isCommandEnabled("cmd_delete"),
    "Delete command is disabled"
  );

  let toolbarNode = PlacesUtils.asContainer(PO._places.selectedNode);
  toolbarNode.containerOpen = true;

  // Add an History query to the toolbar.
  let query = await PlacesUtils.bookmarks.insert({
    type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
    url: "place:sort=4",
    title: "special_query",
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    index: 0,
  });

  // Get first child and check it is the just inserted query.
  Assert.greater(toolbarNode.childCount, 0, "Toolbar node has children");
  let queryNode = toolbarNode.getChild(0);
  Assert.equal(
    queryNode.title,
    "special_query",
    "Query node is correctly selected"
  );

  // Select query node.
  PO._places.selectNode(queryNode);
  Assert.equal(
    PO._places.selectedNode,
    queryNode,
    "We correctly selected query node"
  );

  // Check that both cut and delete commands are enabled.
  Assert.ok(
    PO._places.controller.isCommandEnabled("cmd_copy"),
    "Copy command is enabled"
  );
  Assert.ok(
    PO._places.controller.isCommandEnabled("cmd_cut"),
    "Cut command is enabled"
  );
  Assert.ok(
    PO._places.controller.isCommandEnabled("cmd_delete"),
    "Delete command is enabled"
  );

  // Execute the delete command and check bookmark has been removed.
  let promiseItemRemoved = PlacesTestUtils.waitForNotification(
    "bookmark-removed",
    events => events.some(event => query.guid == event.guid)
  );
  PO._places.controller.doCommand("cmd_delete");
  await promiseItemRemoved;

  Assert.equal(
    await PlacesUtils.bookmarks.fetch(query.guid),
    null,
    "Query node bookmark has been correctly removed"
  );

  toolbarNode.containerOpen = false;

  library.close();
});

add_task(async function test_search_contents() {
  await PlacesUtils.bookmarks.insert({
    type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
    url: "http://example.com/",
    title: "example page",
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    index: 0,
  });

  let library = await promiseLibrary();
  info("Ensure query contents can be cut or deleted");

  // Select and open the left pane "Bookmarks Toolbar" folder.
  let PO = library.PlacesOrganizer;

  PO.selectLeftPaneBuiltIn("BookmarksToolbar");
  Assert.notEqual(PO._places.selectedNode, null, "We have a valid selection");
  Assert.equal(
    PlacesUtils.getConcreteItemGuid(PO._places.selectedNode),
    PlacesUtils.bookmarks.toolbarGuid,
    "We have correctly selected bookmarks toolbar node."
  );

  let searchBox = library.document.getElementById("searchFilter");
  searchBox.value = "example";
  library.PlacesSearchBox.search(searchBox.value);

  let bookmarkNode = library.ContentTree.view.selectedNode;
  Assert.equal(
    bookmarkNode.uri,
    "http://example.com/",
    "Found the expected bookmark"
  );

  // Check that both cut and delete commands are enabled.
  Assert.ok(
    library.ContentTree.view.controller.isCommandEnabled("cmd_copy"),
    "Copy command is enabled"
  );
  Assert.ok(
    library.ContentTree.view.controller.isCommandEnabled("cmd_cut"),
    "Cut command is enabled"
  );
  Assert.ok(
    library.ContentTree.view.controller.isCommandEnabled("cmd_delete"),
    "Delete command is enabled"
  );

  library.close();
});

add_task(async function test_tags() {
  await PlacesUtils.bookmarks.insert({
    type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
    url: "http://example.com/",
    title: "example page",
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    index: 0,
  });
  PlacesUtils.tagging.tagURI(NetUtil.newURI("http://example.com/"), ["test"]);

  let library = await promiseLibrary();
  info("Ensure query contents can be cut or deleted");

  // Select and open the left pane "Bookmarks Toolbar" folder.
  let PO = library.PlacesOrganizer;

  PO.selectLeftPaneBuiltIn("Tags");
  let tagsNode = PO._places.selectedNode;
  Assert.notEqual(tagsNode, null, "We have a valid selection");
  let tagsTitle = PlacesUtils.getString("TagsFolderTitle");
  Assert.equal(tagsNode.title, tagsTitle, "Tags has been properly selected");

  // Check that both cut and delete commands are disabled.
  Assert.ok(
    PO._places.controller.isCommandEnabled("cmd_copy"),
    "Copy command is enabled"
  );
  Assert.ok(
    !PO._places.controller.isCommandEnabled("cmd_cut"),
    "Cut command is disabled"
  );
  Assert.ok(
    !PO._places.controller.isCommandEnabled("cmd_delete"),
    "Delete command is disabled"
  );

  // Now select the tag.
  PlacesUtils.asContainer(tagsNode).containerOpen = true;
  let tag = tagsNode.getChild(0);
  PO._places.selectNode(tag);
  Assert.equal(
    PO._places.selectedNode.title,
    "test",
    "The created tag has been properly selected"
  );

  // Check that cut is disabled but delete is enabled.
  Assert.ok(
    PO._places.controller.isCommandEnabled("cmd_copy"),
    "Copy command is enabled"
  );
  Assert.ok(
    !PO._places.controller.isCommandEnabled("cmd_cut"),
    "Cut command is disabled"
  );
  Assert.ok(
    PO._places.controller.isCommandEnabled("cmd_delete"),
    "Delete command is enabled"
  );

  let bookmarkNode = library.ContentTree.view.selectedNode;
  Assert.equal(
    bookmarkNode.uri,
    "http://example.com/",
    "Found the expected bookmark"
  );

  // Check that both cut and delete commands are enabled.
  Assert.ok(
    library.ContentTree.view.controller.isCommandEnabled("cmd_copy"),
    "Copy command is enabled"
  );
  Assert.ok(
    !library.ContentTree.view.controller.isCommandEnabled("cmd_cut"),
    "Cut command is disabled"
  );
  Assert.ok(
    library.ContentTree.view.controller.isCommandEnabled("cmd_delete"),
    "Delete command is enabled"
  );

  tagsNode.containerOpen = false;

  library.close();
});