summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_pageAction_simple.js
blob: dc323afd94f8ead78ab5f801407fae663189b63b (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

const BASE =
  "http://example.com/browser/browser/components/extensions/test/browser/";

add_task(async function test_pageAction_basic() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      page_action: {
        default_popup: "popup.html",
        unrecognized_property: "with-a-random-value",
      },
    },

    files: {
      "popup.html": `
      <!DOCTYPE html>
      <html><body>
      <script src="popup.js"></script>
      </body></html>
      `,

      "popup.js": function () {
        browser.runtime.sendMessage("from-popup");
      },
    },

    background: function () {
      browser.runtime.onMessage.addListener(msg => {
        browser.test.assertEq(msg, "from-popup", "correct message received");
        browser.test.sendMessage("popup");
      });
      browser.tabs.query({ active: true, currentWindow: true }, tabs => {
        let tabId = tabs[0].id;

        browser.pageAction.show(tabId).then(() => {
          browser.test.sendMessage("page-action-shown");
        });
      });
    },
  });

  SimpleTest.waitForExplicitFinish();
  let waitForConsole = new Promise(resolve => {
    SimpleTest.monitorConsole(resolve, [
      {
        message:
          /Reading manifest: Warning processing page_action.unrecognized_property: An unexpected property was found/,
      },
    ]);
  });

  ExtensionTestUtils.failOnSchemaWarnings(false);
  await extension.startup();
  ExtensionTestUtils.failOnSchemaWarnings(true);
  await extension.awaitMessage("page-action-shown");

  let elem = await getPageActionButton(extension);
  let parent = window.document.getElementById("page-action-buttons");
  is(
    elem && elem.parentNode,
    parent,
    `pageAction pinned to urlbar ${elem.parentNode.getAttribute("id")}`
  );

  clickPageAction(extension);

  await extension.awaitMessage("popup");

  await extension.unload();

  SimpleTest.endMonitorConsole();
  await waitForConsole;
});

add_task(async function test_pageAction_pinned() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      page_action: {
        default_popup: "popup.html",
        pinned: false,
      },
    },

    files: {
      "popup.html": `
      <!DOCTYPE html>
      <html><body>
      </body></html>
      `,
    },

    background: function () {
      browser.tabs.query({ active: true, currentWindow: true }, tabs => {
        let tabId = tabs[0].id;

        browser.pageAction.show(tabId).then(() => {
          browser.test.sendMessage("page-action-shown");
        });
      });
    },
  });

  await extension.startup();
  await extension.awaitMessage("page-action-shown");

  // There are plenty of tests for the main action button, we just verify
  // that we've properly set the pinned value.
  // This test used to check that the button was not pinned, but that is no
  // longer supported.
  // TODO bug 1703537: consider removal of the pinned property.
  let action = PageActions.actionForID(makeWidgetId(extension.id));
  ok(action && action.pinnedToUrlbar, "Check pageAction pinning");

  await extension.unload();
});

add_task(async function test_pageAction_icon_on_subframe_navigation() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      page_action: {
        default_popup: "popup.html",
      },
    },

    files: {
      "popup.html": `
      <!DOCTYPE html>
      <html><body>
      </body></html>
      `,
    },

    background: function () {
      browser.tabs.query({ active: true, currentWindow: true }, tabs => {
        let tabId = tabs[0].id;

        browser.pageAction.show(tabId).then(() => {
          browser.test.sendMessage("page-action-shown");
        });
      });
    },
  });

  await navigateTab(
    gBrowser.selectedTab,
    "data:text/html,<h1>Top Level Frame</h1>"
  );

  await extension.startup();
  await extension.awaitMessage("page-action-shown");

  const pageActionId = BrowserPageActions.urlbarButtonNodeIDForActionID(
    makeWidgetId(extension.id)
  );

  await BrowserTestUtils.waitForCondition(() => {
    return document.getElementById(pageActionId);
  }, "pageAction is initially visible");

  info("Create a sub-frame");

  let subframeURL = `${BASE}#subframe-url-1`;
  await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [subframeURL],
    async url => {
      const iframe = this.content.document.createElement("iframe");
      iframe.setAttribute("id", "test-subframe");
      iframe.setAttribute("src", url);
      iframe.setAttribute("style", "height: 200px; width: 200px");

      // Await the initial url to be loaded in the subframe.
      await new Promise(resolve => {
        iframe.onload = resolve;
        this.content.document.body.appendChild(iframe);
      });
    }
  );

  await BrowserTestUtils.waitForCondition(() => {
    return document.getElementById(pageActionId);
  }, "pageAction should be visible when a subframe is created");

  info("Navigating the sub-frame");

  subframeURL = `${BASE}/file_dummy.html#subframe-url-2`;
  await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [subframeURL],
    async url => {
      const iframe = this.content.document.querySelector(
        "iframe#test-subframe"
      );

      // Await the subframe navigation.
      await new Promise(resolve => {
        iframe.onload = resolve;
        iframe.setAttribute("src", url);
      });
    }
  );

  info("Subframe location changed");

  await BrowserTestUtils.waitForCondition(() => {
    return document.getElementById(pageActionId);
  }, "pageAction should be visible after a subframe navigation");

  await extension.unload();
});