summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/browser/browser_ext_eventpage_disableResetIdleForTest.js
blob: 8178411e800fffee295005cbb6ddc04582cc9b2a (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
"use strict";

const { PromiseTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/PromiseTestUtils.sys.mjs"
);

const { AppUiTestDelegate } = ChromeUtils.importESModule(
  "resource://testing-common/AppUiTestDelegate.sys.mjs"
);

// Ignore error "Actor 'Conduits' destroyed before query 'RunListener' was resolved"
PromiseTestUtils.allowMatchingRejectionsGlobally(
  /Actor 'Conduits' destroyed before query 'RunListener'/
);

async function run_test_disableResetIdleForTest(options) {
  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      manifest_version: 3,
      action: {},
    },
    background() {
      browser.action.onClicked.addListener(async () => {
        browser.test.notifyPass("action-clicked");
        // Deliberately keep this listener active to simulate a still active listener
        // callback, while calling extension.terminateBackground().
        await new Promise(() => {});
      });

      browser.test.sendMessage("background-ready");
    },
  });

  await extension.startup();
  await extension.awaitMessage("background-ready");
  // After startup, the listener should be persistent but not primed.
  assertPersistentListeners(extension, "browserAction", "onClicked", {
    primed: false,
  });

  // Terminating the background should prime the persistent listener.
  await extension.terminateBackground();
  assertPersistentListeners(extension, "browserAction", "onClicked", {
    primed: true,
  });

  // Wake up the background, and verify the listener is no longer primed.
  await AppUiTestDelegate.clickBrowserAction(window, extension.id);
  await extension.awaitFinish("action-clicked");
  await AppUiTestDelegate.closeBrowserAction(window, extension.id);
  await extension.awaitMessage("background-ready");
  assertPersistentListeners(extension, "browserAction", "onClicked", {
    primed: false,
  });

  // Terminate the background again, while the onClicked listener is still
  // being executed.
  // With options.disableResetIdleForTest = true, the termination should NOT
  // be skipped and the listener should become primed again.
  // With options.disableResetIdleForTest = false or unset, the termination
  // should be skipped and the listener should not become primed.
  await extension.terminateBackground(options);
  assertPersistentListeners(extension, "browserAction", "onClicked", {
    primed: !!options?.disableResetIdleForTest,
  });

  await extension.unload();
}

// Verify default behaviour when terminating a background while a
// listener is still running: The background should not be terminated
// and the listener should not become primed. Not specifyiny a value
// for disableResetIdleForTest defauls to disableResetIdleForTest:false.
add_task(async function test_disableResetIdleForTest_default() {
  await run_test_disableResetIdleForTest();
});

// Verify that disableResetIdleForTest:true is honoured and terminating
// a background while a listener is still running is enforced: The
// background should be terminated and the listener should become primed.
add_task(async function test_disableResetIdleForTest_true() {
  await run_test_disableResetIdleForTest({ disableResetIdleForTest: true });
});