summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_toolbar_overflow.js
blob: 4d207a4206e854be2a9b22a21187d5c73dfab217 (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* 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/. */

/**
 * Tests the bookmarks toolbar overflow.
 */

var gToolbar = document.getElementById("PersonalToolbar");
var gChevron = document.getElementById("PlacesChevron");

const BOOKMARKS_COUNT = 250;

add_setup(async function() {
  let wasCollapsed = gToolbar.collapsed;
  await PlacesUtils.bookmarks.eraseEverything();

  // Add bookmarks.
  await PlacesUtils.bookmarks.insertTree({
    guid: PlacesUtils.bookmarks.toolbarGuid,
    children: Array(BOOKMARKS_COUNT)
      .fill("")
      .map((_, i) => ({ url: `http://test.places.${i}/` })),
  });

  // Toggle the bookmarks toolbar so that we start from a stable situation and
  // are not affected by all bookmarks removal.
  await toggleToolbar(false);
  await toggleToolbar(true);

  registerCleanupFunction(async () => {
    if (wasCollapsed) {
      await toggleToolbar(false);
    }
    await PlacesUtils.bookmarks.eraseEverything();
    await PlacesUtils.history.clear();
  });
});

add_task(async function test_overflow() {
  // Check that the overflow chevron is visible.
  Assert.ok(!gChevron.collapsed, "The overflow chevron should be visible");
  let children = getPlacesChildren();
  Assert.ok(
    children.length < BOOKMARKS_COUNT,
    "Not all the nodes should be built by default"
  );
  let visibleNodes = [];
  for (let node of children) {
    if (getComputedStyle(node).visibility == "visible") {
      visibleNodes.push(node);
    }
  }
  Assert.ok(
    visibleNodes.length < children.length,
    `The number of visible nodes (${visibleNodes.length}) should be smaller than the number of built nodes (${children.length})`
  );

  await test_index(
    "Node at the last visible index",
    visibleNodes.length - 1,
    "visible"
  );
  await test_index(
    "Node at the first invisible index",
    visibleNodes.length,
    "hidden"
  );
  await test_index("First non-built node", children.length, undefined);
  await test_index("Later non-built node", children.length + 1, undefined);

  await test_move_index(
    "Move node from last visible to first hidden",
    visibleNodes.length - 1,
    visibleNodes.length,
    "visible",
    "hidden"
  );
  await test_move_index(
    "Move node from fist visible to last built",
    0,
    children.length - 1,
    "visible",
    "hidden"
  );
  await test_move_index(
    "Move node from fist visible to first non built",
    0,
    children.length,
    "visible",
    undefined
  );
});

add_task(async function test_separator_first() {
  await toggleToolbar(false);
  // Check that if a separator is the first node, we still calculate overflow
  // properly.
  let bm = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    type: PlacesUtils.bookmarks.TYPE_SEPARATOR,
    index: 0,
  });
  await toggleToolbar(true, 2);

  let children = getPlacesChildren();
  Assert.greater(children.length, 2, "Multiple elements are visible");
  Assert.equal(
    children[1]._placesNode.uri,
    "http://test.places.0/",
    "Found the first bookmark"
  );
  Assert.equal(
    getComputedStyle(children[1]).visibility,
    "visible",
    "The first bookmark is visible"
  );

  await PlacesUtils.bookmarks.remove(bm);
});

add_task(async function test_newWindow_noOverflow() {
  info(
    "Check toolbar in a new widow when it was already visible and not overflowed"
  );
  Assert.ok(!gToolbar.collapsed, "Toolbar is not collapsed in original window");
  await PlacesUtils.bookmarks.eraseEverything();
  // Add a single bookmark.
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    url: "http://toolbar.overflow/",
    title: "Example",
  });
  // Add a favicon for the bookmark.
  let favicon =
    "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAA" +
    "AAAA6fptVAAAACklEQVQI12NgAAAAAgAB4iG8MwAAAABJRU5ErkJggg==";
  await PlacesTestUtils.addFavicons(
    new Map([["http://toolbar.overflow/", favicon]])
  );

  let win = await BrowserTestUtils.openNewBrowserWindow();
  try {
    let toolbar = win.document.getElementById("PersonalToolbar");
    Assert.ok(!toolbar.collapsed, "The toolbar is not collapsed");
    let content = win.document.getElementById("PlacesToolbarItems");
    await TestUtils.waitForCondition(() => {
      return (
        content.children.length == 1 &&
        content.children[0].hasAttribute("image")
      );
    });
    let chevron = win.document.getElementById("PlacesChevron");
    Assert.ok(chevron.collapsed, "The chevron should be collapsed");
  } finally {
    await BrowserTestUtils.closeWindow(win);
  }
});

async function test_index(desc, index, expected) {
  info(desc);
  let children = getPlacesChildren();
  let originalLen = children.length;
  let nodeExisted = children.length > index;
  let previousNodeIsVisible =
    nodeExisted &&
    getComputedStyle(children[index - 1]).visibility == "visible";
  let promise = promiseUpdateVisibility(
    expected == "visible" || previousNodeIsVisible
  );

  let bm = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    url: "http://test.places.added/",
    index,
  });
  Assert.equal(bm.index, index, "Sanity check the bookmark index");
  await promise;
  children = getPlacesChildren();

  if (!expected) {
    Assert.ok(
      children.length <= index,
      "The new node should not have been added"
    );
  } else {
    Assert.equal(
      children[index]._placesNode.bookmarkGuid,
      bm.guid,
      "Found the added bookmark at the expected position"
    );
    Assert.equal(
      getComputedStyle(children[index]).visibility,
      expected,
      `The bookmark node should be ${expected}`
    );
  }
  Assert.equal(
    children.length,
    originalLen,
    "Number of built nodes should stay the same"
  );

  info("Remove the node");
  promise = promiseUpdateVisibility(expected == "visible");

  await PlacesUtils.bookmarks.remove(bm);
  await promise;
  children = getPlacesChildren();

  if (expected && nodeExisted) {
    Assert.equal(
      children[index]._placesNode.uri,
      `http://test.places.${index}/`,
      "Found the previous bookmark at the expected position"
    );
    Assert.equal(
      getComputedStyle(children[index]).visibility,
      expected,
      `The bookmark node should be ${expected}`
    );
  }
  Assert.equal(
    children.length,
    originalLen,
    "Number of built nodes should stay the same"
  );
}

async function test_move_index(desc, fromIndex, toIndex, original, expected) {
  info(desc);
  let children = getPlacesChildren();
  let originalLen = children.length;
  let movedGuid = children[fromIndex]._placesNode.bookmarkGuid;
  let existingGuid = children[toIndex]
    ? children[toIndex]._placesNode.bookmarkGuid
    : null;
  let existingIndex = fromIndex < toIndex ? toIndex - 1 : toIndex + 1;

  Assert.equal(
    getComputedStyle(children[fromIndex]).visibility,
    original,
    `The bookmark node should be ${original}`
  );
  let promise = promiseUpdateVisibility(
    original == "visible" || expected == "visible"
  );

  await PlacesUtils.bookmarks.update({
    guid: movedGuid,
    index: toIndex,
  });
  await promise;
  children = getPlacesChildren();

  if (!expected) {
    Assert.ok(
      children.length <= toIndex,
      "Node in the new position is not expected"
    );
    Assert.ok(
      children[originalLen - 1],
      "We should keep number of built nodes consistent"
    );
  } else {
    Assert.equal(
      children[toIndex]._placesNode.bookmarkGuid,
      movedGuid,
      "Found the moved bookmark at the expected position"
    );
    Assert.equal(
      getComputedStyle(children[toIndex]).visibility,
      expected,
      `The destination bookmark node should be ${expected}`
    );
  }
  Assert.equal(
    getComputedStyle(children[fromIndex]).visibility,
    original,
    `The origin bookmark node should be ${original}`
  );
  if (existingGuid) {
    Assert.equal(
      children[existingIndex]._placesNode.bookmarkGuid,
      existingGuid,
      "Found the pushed away bookmark at the expected position"
    );
  }
  Assert.equal(
    children.length,
    originalLen,
    "Number of built nodes should stay the same"
  );

  info("Moving back the node");
  promise = promiseUpdateVisibility(
    original == "visible" || expected == "visible"
  );

  await PlacesUtils.bookmarks.update({
    guid: movedGuid,
    index: fromIndex,
  });
  await promise;
  children = getPlacesChildren();

  Assert.equal(
    children[fromIndex]._placesNode.bookmarkGuid,
    movedGuid,
    "Found the moved bookmark at the expected position"
  );
  if (expected) {
    Assert.equal(
      getComputedStyle(children[toIndex]).visibility,
      expected,
      `The bookmark node should be ${expected}`
    );
  }
  Assert.equal(
    getComputedStyle(children[fromIndex]).visibility,
    original,
    `The bookmark node should be ${original}`
  );
  if (existingGuid) {
    Assert.equal(
      children[toIndex]._placesNode.bookmarkGuid,
      existingGuid,
      "Found the pushed away bookmark at the expected position"
    );
  }
  Assert.equal(
    children.length,
    originalLen,
    "Number of built nodes should stay the same"
  );
}

add_task(async function test_separator_first() {
  // Check that if there are only separators, we still show nodes properly.
  await toggleToolbar(false);
  await PlacesUtils.bookmarks.eraseEverything();
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    type: PlacesUtils.bookmarks.TYPE_SEPARATOR,
    index: 0,
  });
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    type: PlacesUtils.bookmarks.TYPE_SEPARATOR,
    index: 0,
  });
  await toggleToolbar(true, 2);

  let children = getPlacesChildren();
  Assert.equal(children.length, 2, "The expected elements are visible");
  Assert.equal(
    getComputedStyle(children[0]).visibility,
    "visible",
    "The first bookmark is visible"
  );
  Assert.equal(
    getComputedStyle(children[1]).visibility,
    "visible",
    "The second bookmark is visible"
  );

  await toggleToolbar(false);
  await PlacesUtils.bookmarks.eraseEverything();
});

/**
 * If the passed-in condition is fulfilled, awaits for the toolbar nodes
 * visibility to have been updated.
 *
 * @param {boolean} [condition] Awaits for visibility only if this condition is true.
 * @returns {Promise} resolved when the condition is not fulfilled or the
 *          visilibily update happened.
 */
function promiseUpdateVisibility(condition = true) {
  if (condition) {
    return BrowserTestUtils.waitForEvent(
      gToolbar,
      "BookmarksToolbarVisibilityUpdated"
    );
  }
  return Promise.resolve();
}

/**
 * Returns an array of toolbar children that are Places nodes, ignoring things
 * like the chevron or other additional buttons.
 *
 * @returns {Array} An array of Places element nodes.
 */
function getPlacesChildren() {
  return Array.prototype.filter.call(
    document.getElementById("PlacesToolbarItems").children,
    c => c._placesNode?.itemId
  );
}

/**
 * Toggles the toolbar on or off.
 *
 * @param {boolean} show Whether to show or hide the toolbar.
 * @param {number} [expectedMinChildCount] Optional number of Places nodes that
 *        should be visible on the toolbar.
 */
async function toggleToolbar(show, expectedMinChildCount = 0) {
  let promiseReady = Promise.resolve();
  if (show) {
    promiseReady = promiseUpdateVisibility();
  }

  await promiseSetToolbarVisibility(gToolbar, show);
  await promiseReady;

  if (show) {
    if (getPlacesChildren().length < expectedMinChildCount) {
      await new Promise(resolve => {
        info("Waiting for bookmark elements to appear");
        let mut = new MutationObserver(mutations => {
          let children = getPlacesChildren();
          info(`${children.length} bookmark elements appeared`);
          if (children.length >= expectedMinChildCount) {
            resolve();
            mut.disconnect();
          }
        });
        mut.observe(document.getElementById("PlacesToolbarItems"), {
          childList: true,
        });
      });
    }
  }
}