summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/test_ext_pageAction_onClicked.html
blob: 5fce66159d1fcc6288b94801bc2e946086005658 (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>pageAction.onClicked test</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
</head>
<body>

<script type="text/javascript">
"use strict";

function createExtension(background = {}) {
  return ExtensionTestUtils.loadExtension({
    manifest: {
      background,
      page_action: { show_matches: ["<all_urls>"] },
    },
    async background() {
      async function checkIsHandlingUserInput() {
        try {
          // permissions.request is declared with requireUserInput,
          // so it would reject if inputHandling is false.
          let granted = await browser.permissions.request({});
          // We haven't requested any permissions, so the API call grants the
          // requested permissions without actually prompting the user.
          browser.test.assertTrue(granted, "empty permissions granted");
          return true;
        } catch (e) {
          browser.test.assertEq(
            e?.message,
            "permissions.request may only be called from a user input handler",
            "Expected error when permissions.request rejects"
          );
          return false;
        }
      }
      browser.pageAction.onClicked.addListener(async () => {
        browser.test.assertTrue(
          await checkIsHandlingUserInput(),
          "pageAction.onClicked is handling user input"
        );
        browser.test.notifyPass("action-clicked");
      });

      // Sanity check: Verify that pageAction is shown (because it needs to be
      // in order to trigger pageAction.onClicked).
      let [tab] = await browser.tabs.query({
        active: true,
        currentWindow: true,
      });
      browser.test.assertTrue(
        await browser.pageAction.isShown({ tabId: tab.id }),
        "pageAction should be visible (due to page_action.show_matches)"
      );

      browser.test.assertFalse(
        await checkIsHandlingUserInput(),
        "not handling user input by default"
      );
      browser.test.sendMessage("background-ready");
    },
  });
}

add_task(async function test_pageAction_onClicked_and_inputHandling() {
  const extension = createExtension();

  await extension.startup();
  await extension.awaitMessage("background-ready");

  await AppTestDelegate.clickPageAction(window, extension);
  await extension.awaitFinish("action-clicked");
  await AppTestDelegate.closePageAction(window, extension);

  await extension.unload();
});

add_task(async function test_pageAction_onClicked_persistent_event() {
  const extension = createExtension({ persistent: false });

  await extension.startup();
  await extension.awaitMessage("background-ready");

  assertPersistentListeners(extension, "pageAction", ["onClicked"], {
    primed: false,
  });

  await extension.terminateBackground();
  assertPersistentListeners(extension, "pageAction", ["onClicked"], {
    primed: true,
  });

  await AppTestDelegate.clickPageAction(window, extension);

  // Background script will run again.
  await extension.awaitMessage("background-ready");
  await extension.awaitFinish("action-clicked");

  await AppTestDelegate.closePageAction(window, extension);

  await extension.unload();
});

</script>

</body>
</html>