summaryrefslogtreecommitdiffstats
path: root/browser/components/firefoxview/tests/chrome/test_fxview_tab_list.html
blob: 92a645c431af14aab1128f4dc29500cd235f1467 (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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>FxviewTabList Tests</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
  <link rel="localization" href="browser/places.ftl">
  <link rel="stylesheet" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
  <link rel="stylesheet" href="chrome://global/skin/in-content/common.css">
  <script type="module" src="chrome://browser/content/firefoxview/fxview-tab-list.mjs"></script>
</head>
<body>
  <style>
    fxview-tab-list.history::part(secondary-button) {
      background-image: url("chrome://global/skin/icons/more.svg");
    }
  </style>
<p id="display"></p>
<div id="content" style="max-width: 750px">
  <fxview-tab-list class="history" .dateTimeFormat="relative" .hasPopup="menu">
    <panel-list slot="menu">
      <panel-item data-l10n-id="fxviewtabrow-delete"></panel-item>
      <panel-item data-l10n-id="fxviewtabrow-forget-about-this-site"></panel-item>
      <hr />
      <panel-item data-l10n-id="fxviewtabrow-open-in-window"></panel-item>
      <panel-item data-l10n-id="fxviewtabrow-open-in-private-window"></panel-item>
      <hr />
      <panel-item data-l10n-id="fxviewtabrow-add-bookmark"></panel-item>
      <panel-item data-l10n-id="fxviewtabrow-save-to-pocket"></panel-item>
      <panel-item data-l10n-id="fxviewtabrow-copy-link"></panel-item>
    </panel-list>
  </fxview-tab-list>
</div>
<pre id="test">
<script class="testbody" type="application/javascript">
  Services.scriptloader.loadSubScript(
    "chrome://browser/content/utilityOverlay.js",
    this
  );

  const { BrowserTestUtils } = ChromeUtils.import(
    "resource://testing-common/BrowserTestUtils.jsm"
  );
  const { PlacesQuery } = ChromeUtils.importESModule(
    "resource://gre/modules/PlacesQuery.sys.mjs"
  );
  const { PromiseUtils } = ChromeUtils.importESModule(
    "resource://gre/modules/PromiseUtils.sys.mjs"
  );
  const { PlacesUtils } = ChromeUtils.importESModule(
    "resource://gre/modules/PlacesUtils.sys.mjs"
  );
  const { PlacesUIUtils } = ChromeUtils.importESModule(
    "resource:///modules/PlacesUIUtils.sys.mjs"
  );
  const { PlacesTestUtils } = ChromeUtils.importESModule(
    "resource://testing-common/PlacesTestUtils.sys.mjs"
  );
  const { TestUtils } = ChromeUtils.import(
    "resource://testing-common/TestUtils.jsm"
  );

  const fxviewTabList = document.querySelector("fxview-tab-list");
  let tabItems = [];
  const placesQuery = new PlacesQuery();

  const URLs = [
    "http://mochi.test:8888/browser/",
    "https://www.example.com/",
    "https://example.net/",
    "https://example.org/",
    "https://www.mozilla.org/"
  ];

  async function addHistoryItems() {
    await PlacesUtils.history.clear();
    let history = await placesQuery.getHistory();

    const now = new Date();
    await PlacesUtils.history.insert({
      url: URLs[0],
      title: "Example Domain 1",
      visits: [{ date: now }],
    });
    let historyUpdated = PromiseUtils.defer();
    placesQuery.observeHistory(newHistory => {
      history = newHistory;
      historyUpdated.resolve();
    });
    await PlacesUtils.history.insert({
      url: URLs[1],
      title: "Example Domain 2",
      visits: [{ date: now }],
    });
    await historyUpdated.promise;
    historyUpdated = PromiseUtils.defer();
    placesQuery.observeHistory(newHistory => {
      history = newHistory;
      historyUpdated.resolve();
    });
    await PlacesUtils.history.insert({
      url: URLs[2],
      title: "Example Domain 3",
      visits: [{ date: now }],
    });
    await historyUpdated.promise;
    historyUpdated = PromiseUtils.defer();
    placesQuery.observeHistory(newHistory => {
      history = newHistory;
      historyUpdated.resolve();
    });
    await PlacesUtils.history.insert({
      url: URLs[3],
      title: "Example Domain 4",
      visits: [{ date: now }],
    });
    await historyUpdated.promise;

    let normalized = normalizeHistoryData(history);
    fxviewTabList.tabItems = normalized;

    await fxviewTabList.getUpdateComplete();
    tabItems = Array.from(fxviewTabList.rowEls);
  }

  function normalizeHistoryData(history) {
    history.forEach(historyItem => {
      historyItem.time = historyItem.date.getTime();
      historyItem.icon = `page-icon:${historyItem.url}`;
      historyItem.primaryL10nId = "fxviewtabrow-tabs-list-tab";
      historyItem.primaryL10nArgs = JSON.stringify({ targetURI: historyItem.url });
      historyItem.secondaryL10nId = "fxviewtabrow-open-menu-button";
    });
    return history;
  }

  function getCurrentDisplayDate() {
    let lastItemMainEl = tabItems[tabItems.length - 1].mainEl;
    return lastItemMainEl.querySelector("#fxview-tab-row-date span:not([hidden])")?.textContent.trim() ?? "";
  }

  function getCurrentDisplayTime() {
    let lastItemMainEl = tabItems[tabItems.length - 1].mainEl;
    return lastItemMainEl.querySelector("#fxview-tab-row-time")?.textContent.trim() ?? "";
  }

  function isActiveElement(expectedLinkEl) {
    return expectedLinkEl.getRootNode().activeElement == expectedLinkEl;
  }

  function onPrimaryAction(e) {
    let gBrowser = BrowserWindowTracker.getTopWindow().top.gBrowser;
    gBrowser.addTrustedTab(e.originalTarget.url);
  }

  function onSecondaryAction(e) {
    e.target.querySelector("panel-list").toggle(e.detail.originalEvent);
  }

  add_setup(function setup() {
    fxviewTabList.addEventListener("fxview-tab-list-primary-action", onPrimaryAction);
    fxviewTabList.addEventListener("fxview-tab-list-secondary-action", onSecondaryAction);
  });

  /**
  * Tests that history items are loaded in the expected order
  */
  add_task(async function test_list_ordering() {
    await addHistoryItems();
    is(
      tabItems.length,
      4,
      "Four history items are shown in the list."
    );

    // Check ordering
    ok(
      tabItems[0].title === "Example Domain 4",
      "First history item in fxview-tab-list is in the correct order."
    )

    ok(
      tabItems[3].title === "Example Domain 1",
      "Last history item in fxview-tab-list is in the correct order."
    )
  });

  /**
  * Tests the primary action function is triggered when selecting the main row element
  */
  add_task(async function test_primary_action(){
    await addHistoryItems();
    let gBrowser = BrowserWindowTracker.getTopWindow().top.gBrowser;
    let newTabPromise = BrowserTestUtils.waitForNewTab(gBrowser, tabItems[0].url);
    tabItems[0].mainEl.click();
    await newTabPromise;

    is(
      tabItems.length,
      4,
      "Four history items are still shown in the list."
    );

    await BrowserTestUtils.removeTab(gBrowser.tabs[gBrowser.tabs.length - 1]);
  });

  /**
  * Tests that a max tabs length value can be given to fxview-tab-list
  */
  add_task(async function test_max_list_items() {
    const mockMaxTabsLength = 3;

    // override this value for testing purposes
    fxviewTabList.maxTabsLength = mockMaxTabsLength;
    await addHistoryItems();

    is(
      tabItems.length,
      mockMaxTabsLength,
      `fxview-tabs-list should have ${mockMaxTabsLength} list items`
    );

    // Add new history items
    let history = await placesQuery.getHistory();

    const now = new Date();
    await PlacesUtils.history.insert({
      url: URLs[4],
      title: "Internet for people, not profits - Mozilla",
      visits: [{ date: now }],
    });
    let historyUpdated = PromiseUtils.defer();
    placesQuery.observeHistory(newHistory => {
      history = newHistory;
      historyUpdated.resolve();
    });
    await historyUpdated.promise;

    ok(
      history.length === 5,
      "Five total history items after inserting another node"
    );

    // Update fxview-tab-list component with latest history data
    let normalized = normalizeHistoryData(history);
    fxviewTabList.tabItems = normalized;
    await fxviewTabList.getUpdateComplete();
    tabItems = Array.from(fxviewTabList.rowEls);

    is(
      tabItems.length,
      mockMaxTabsLength,
      `fxview-tabs-list should have ${mockMaxTabsLength} list items`
    );

    ok(
      tabItems[0].title === "Internet for people, not profits - Mozilla",
      "History list has been updated with the expected maxTabsLength."
    )
    fxviewTabList.maxTabsLength = 25;
  });

    /**
   * Tests keyboard navigation of the fxview-tab-list component
   */
  add_task(async function test_keyboard_navigation() {
    const arrowDown = async () => {
      info("Arrow down");
      synthesizeKey("KEY_ArrowDown", {});
      await fxviewTabList.getUpdateComplete();
    };
    const arrowUp = async () => {
      info("Arrow up");
      synthesizeKey("KEY_ArrowUp", {});
      await fxviewTabList.getUpdateComplete();
    };
    const arrowRight = async () => {
      info("Arrow right");
      synthesizeKey("KEY_ArrowRight", {});
      await fxviewTabList.getUpdateComplete();
    };
    const arrowLeft = async () => {
      info("Arrow left");
      synthesizeKey("KEY_ArrowLeft", {});
      await fxviewTabList.getUpdateComplete();
    };

    await addHistoryItems();
    tabItems[0].mainEl.focus();
    ok(
      isActiveElement(tabItems[0].mainEl),
      "Focus should be on the first main element of the list"
    );

    // Arrow down/up the list
    await arrowDown();
    ok(
      isActiveElement(tabItems[1].mainEl),
      "Focus should be on the second main element of the list"
    );
    await arrowDown();
    ok(
      isActiveElement(tabItems[2].mainEl),
      "Focus should be on the third main element of the list"
    );
    await arrowDown();
    ok(
      isActiveElement(tabItems[3].mainEl),
      "Focus should be on the fourth main element of the list"
    );
    await arrowUp();
    ok(
      isActiveElement(tabItems[2].mainEl),
      "Focus should be on the third main element of the list"
    );
    await arrowUp();
    ok(
      isActiveElement(tabItems[1].mainEl),
      "Focus should be on the second main element of the list"
    );
    await arrowUp();
    ok(
      isActiveElement(tabItems[0].mainEl),
      "Focus should be on the first main element of the list"
    );
    await arrowRight();
    ok(
      isActiveElement(tabItems[0].buttonEl),
      "Focus should be on the first row's context menu button element of the list"
    );
    await arrowDown();
    ok(
      isActiveElement(tabItems[1].buttonEl),
      "Focus should be on the second row's context menu button element of the list"
    );
    await arrowLeft();
    ok(
      isActiveElement(tabItems[1].mainEl),
      "Focus should be on the second main element of the list"
    );
    await arrowUp();
    ok(
      isActiveElement(tabItems[0].mainEl),
      "Focus should be on the first main element of the list"
    );
  });

  /**
  * Tests relative time format for the fxview-tab-list component
  */
  add_task(async function test_relative_format() {
    await addHistoryItems();
    ok(
      getCurrentDisplayDate().includes("Just now"),
      "Current dateTime format is 'relative' and date displays 'Just now' initially"
    );
    ok(
      !getCurrentDisplayTime().length,
      "Current dateTime format is 'relative' and time displays an empty string"
    );
  });

  /**
  * Tests date only format for the fxview-tab-list component
  */
  add_task(async function test_date_only_format() {
    await addHistoryItems();

    // Check date only format
    fxviewTabList.dateTimeFormat = "date";
    await fxviewTabList.getUpdateComplete();
    await BrowserTestUtils.waitForCondition(() => {
      return getCurrentDisplayDate().includes("/");
    });
    ok(
      getCurrentDisplayDate().includes("/"),
      "Current dateTime format is 'date' and displays the current date"
    );
    ok(
      !getCurrentDisplayTime().length,
      "Current dateTime format is 'date' and time displays an empty string"
    );
  });

  /**
  * Tests time only format for the fxview-tab-list component
  */
  add_task(async function test_time_only_format() {
    await addHistoryItems();

    // Check time only format
    fxviewTabList.dateTimeFormat = "time";
    await fxviewTabList.getUpdateComplete();
    await BrowserTestUtils.waitForCondition(() => {
      return getCurrentDisplayTime().includes("AM") || getCurrentDisplayTime().includes("PM");
    });
    ok(
      !getCurrentDisplayDate().length,
      "Current dateTime format is 'time' and date displays an empty string"
    );
    ok(
      getCurrentDisplayTime().includes("AM") || getCurrentDisplayTime().includes("PM"),
      "Current dateTime format is 'time' and displays the current time"
    );
  });

  /**
  * Tests date and time format for the fxview-tab-list component
  */
  add_task(async function test_date_and_time_format() {
    await addHistoryItems();

    // Check date and time format
    fxviewTabList.dateTimeFormat = "dateTime";
    await fxviewTabList.getUpdateComplete();
    await BrowserTestUtils.waitForCondition(() => {
      return getCurrentDisplayDate().includes("/") &&
      (getCurrentDisplayTime().includes("AM") || getCurrentDisplayTime().includes("PM"));
    });
    ok(
      getCurrentDisplayDate().includes("/"),
      "Current dateTime format is 'dateTime' and date displays the current date"
    );
    ok(
      getCurrentDisplayTime().includes("AM") || getCurrentDisplayTime().includes("PM"),
      "Current dateTime format is 'dateTime' and displays the current time"
    );

    // Reset dateTimeFormat to relative before next test
    fxviewTabList.dateTimeFormat = "relative";
    await fxviewTabList.getUpdateComplete();
  });

  /**
  * Tests that relative time updates properly for the fxview-tab-list component
  */
  add_task(async function test_relative_time_updates() {
    await addHistoryItems();

    await BrowserTestUtils.waitForCondition(() => {
      return getCurrentDisplayDate().includes("Just now");
    });

    ok(
      getCurrentDisplayDate().includes("Just now"),
      "Current date element displays 'Just now' initially"
    );

    // Set the updateTimeMs pref to something low to check that relative time updates properly
    const TAB_UPDATE_TIME_MS = 500;
    await SpecialPowers.pushPrefEnv({
      set: [["browser.tabs.firefox-view.updateTimeMs", TAB_UPDATE_TIME_MS]],
    });
    await BrowserTestUtils.waitForCondition(() => {
      return !getCurrentDisplayDate().includes("now");
    });
    info("Currently displayed date is something other than 'Just now'");

    await SpecialPowers.popPrefEnv();
  });
</script>
</pre>
</body>
</html>