summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/browser/browser_about_debugging_link.js
blob: c7351f054c162c0cfb2b0280ea2384d8a9df19e8 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { PromiseTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/PromiseTestUtils.sys.mjs"
);
// Allow rejections related to closing an about:debugging too soon after it has been
// just opened in a new tab and loaded.
PromiseTestUtils.allowMatchingRejectionsGlobally(/Connection closed/);

function waitForDispatch(store, type) {
  return new Promise(resolve => {
    store.dispatch({
      type: "@@service/waitUntil",
      predicate: action => action.type === type,
      run: (dispatch, getState, action) => {
        resolve(action);
      },
    });
  });
}

/**
 * Wait for all client requests to settle, meaning here that no new request has been
 * dispatched after the provided delay. (NOTE: same test helper used in about:debugging tests)
 */
async function waitForRequestsToSettle(store, delay = 500) {
  let hasSettled = false;

  // After each iteration of this while loop, we check is the timerPromise had the time
  // to resolve or if we captured a REQUEST_*_SUCCESS action before.
  while (!hasSettled) {
    let timer;

    // This timer will be executed only if no REQUEST_*_SUCCESS action is dispatched
    // during the delay. We consider that when no request are received for some time, it
    // means there are no ongoing requests anymore.
    const timerPromise = new Promise(resolve => {
      // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
      timer = setTimeout(() => {
        hasSettled = true;
        resolve();
      }, delay);
    });

    // Wait either for a REQUEST_*_SUCCESS to be dispatched, or for the timer to resolve.
    await Promise.race([
      waitForDispatch(store, "REQUEST_EXTENSIONS_SUCCESS"),
      waitForDispatch(store, "REQUEST_TABS_SUCCESS"),
      waitForDispatch(store, "REQUEST_WORKERS_SUCCESS"),
      timerPromise,
    ]);

    // Clear the timer to avoid setting hasSettled to true accidently unless timerPromise
    // was the first to resolve.
    clearTimeout(timer);
  }
}

function waitForRequestsSuccess(store) {
  return Promise.all([
    waitForDispatch(store, "REQUEST_EXTENSIONS_SUCCESS"),
    waitForDispatch(store, "REQUEST_TABS_SUCCESS"),
    waitForDispatch(store, "REQUEST_WORKERS_SUCCESS"),
  ]);
}

add_task(async function testAboutDebugging() {
  let win = await loadInitialView("extension");

  let aboutAddonsTab = gBrowser.selectedTab;
  let debugAddonsBtn = win.document.querySelector(
    '#page-options [action="debug-addons"]'
  );

  // Verify the about:debugging is loaded.
  info(`Check about:debugging loads`);
  let loaded = BrowserTestUtils.waitForNewTab(
    gBrowser,
    "about:debugging#/runtime/this-firefox",
    true
  );
  debugAddonsBtn.click();
  await loaded;
  let aboutDebuggingTab = gBrowser.selectedTab;
  const { AboutDebugging } = aboutDebuggingTab.linkedBrowser.contentWindow;
  // Avoid test failures due to closing the about:debugging tab
  // while it is still initializing.
  info("Wait until about:debugging actions are finished");
  await waitForRequestsSuccess(AboutDebugging.store);

  info("Switch back to about:addons");
  await BrowserTestUtils.switchTab(gBrowser, aboutAddonsTab);
  is(gBrowser.selectedTab, aboutAddonsTab, "Back to about:addons");

  info("Re-open about:debugging");
  let switched = TestUtils.waitForCondition(
    () => gBrowser.selectedTab == aboutDebuggingTab
  );
  debugAddonsBtn.click();
  await switched;
  await waitForRequestsToSettle(AboutDebugging.store);

  info("Force about:debugging to a different hash URL");
  aboutDebuggingTab.linkedBrowser.contentWindow.location.hash = "/setup";

  info("Switch back to about:addons again");
  await BrowserTestUtils.switchTab(gBrowser, aboutAddonsTab);
  is(gBrowser.selectedTab, aboutAddonsTab, "Back to about:addons");

  info("Re-open about:debugging a second time");
  switched = TestUtils.waitForCondition(
    () => gBrowser.selectedTab == aboutDebuggingTab
  );
  debugAddonsBtn.click();
  await switched;

  info("Wait until any new about:debugging request did settle");
  // Avoid test failures due to closing the about:debugging tab
  // while it is still initializing.
  await waitForRequestsToSettle(AboutDebugging.store);

  info("Remove the about:debugging tab");
  BrowserTestUtils.removeTab(aboutDebuggingTab);

  await closeView(win);
});