summaryrefslogtreecommitdiffstats
path: root/mobile/android/components/extensions/test/mochitest/test_ext_pageAction_popup.html
blob: 89edc7c298e82d5c24cb062d0fb256226dbeee6d (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
<!DOCTYPE HTML>
<html>
<head>
  <title>PageAction Test</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" href="chrome://mochikit/contents/tests/SimpleTest/test.css"/>
</head>
<body>

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

Cu.import("resource://gre/modules/Services.jsm");

let dataURI = "iVBORw0KGgoAAAANSUhEUgAAACQAAAAkCAYAAADhAJiYAAAC4klEQVRYhdWXLWzbQBSADQtDAwsHC1tUhUxqfL67lk2tdn+OJg0ODU0rLByqgqINBY6tmlbn7LMTJ5FaFVVBk1G0oUGjG2jT2Y7jxmmcbU/6iJ+f36fz+e5sGP9riCGm9hB37RG+scd4Yo/wsDXCZyIE2xuXsce4bY+wXkAsQtzYmExrfFgvkJkRbkzo1ehoxx5iXcgI/9iYUGt8WH9MqDXEcmNChmEYrRCf2SHWeYgQx3x0tLNRIeKQLTtEFyJEep4NTuhk8BC+yMrwEE3+iozo42d8gK7FAOkMsRiiN8QhW2ttSK5QTfRRV4QoymVeJMvPvDp7gCZigD613MN6yRFA3SWarow9QB9LCfG+NeF9qCtjAKOSQjCqVKhfVsiHEQ+grgx/lRGqUihAc1uL8EFD+KCRO+GrF4J61phcoRoPoEzkYhZYpykh5sMb7kOdIeY+jHKur4QI4Feh4AFX1nVeLxrAvQchGsBz5ls6wa2QdwcvIcE2863bTH79KOvsz/uUYJsp+J0pSzNlDckVqqVGUAF+n6uS7txcOl6wot4JVy70ufDLy4pWLUQVPE81pRI0mGe9oxLMHSeohHvMs/STUNaUK6vDPCvOyxMFDx4achehRDJmHnydnkPww5OFfLxrGIZBFDyYl4LpMzlTQFIP6AQx86w2UeYBccFpJrcKv5L9eGDtUAU6RIELqsB74uynjy/UBRF1gS5BTFxwQT1wTiXoUg9MH7m/3NZRRoi5IJytUbMgzv4Wc832+oQkiKgEehmyMkkpKsFkQV11QsRJL5rJYBLItQgRaUZEmnoZXsomz3vGiWw+I9KMF9SVFOqZEemZekli1jN3U/UOqhHHvC6oWWGElhfSpGdOk6+O9prdwvtLj5BjRsQxdRnot+Zeifpy/2/0stktKTRNLmbk0mwXyl8253fyojj+8rxOHNAhjjm5n0/5OOCGOKBzkrMO0Z75lvSAzKlrF32Z/3z8BqLAn+yMV7VhAAAAAElFTkSuQmCC";

let image = atob(dataURI);
const IMAGE_ARRAYBUFFER = Uint8Array.from(image, byte => byte.charCodeAt(0)).buffer;

add_task(function* test_contentscript() {
  function background() {
    // TODO: Use the Tabs API to obtain the tab ids for showing pageActions.
    let tabId = 1;
    let onClickedListenerEnabled = false;

    browser.test.onMessage.addListener((msg, details) => {
      if (msg === "page-action-show") {
        // TODO: switch to using .show(tabId).then(...) once bug 1270742 lands.
        browser.pageAction.show(tabId).then(() => {
          browser.test.sendMessage("page-action-shown");
        });
      } else if (msg == "page-action-set-popup") {
        browser.pageAction.setPopup({popup: details.name, tabId: tabId}).then(() => {
          browser.test.sendMessage("page-action-popup-set");
        });
      } else if (msg == "page-action-get-popup") {
        browser.pageAction.getPopup({tabId: tabId}).then(url => {
          browser.test.sendMessage("page-action-got-popup", url);
        });
      } else if (msg == "page-action-enable-onClicked-listener") {
        onClickedListenerEnabled = true;
        browser.test.sendMessage("page-action-onClicked-listener-enabled");
      } else if (msg == "page-action-disable-onClicked-listener") {
        onClickedListenerEnabled = false;
        browser.test.sendMessage("page-action-onClicked-listener-disabled");
      }
    });

    browser.pageAction.onClicked.addListener(tab => {
      browser.test.assertTrue(onClickedListenerEnabled, "The onClicked listener should only fire when it is enabled.");
      browser.test.sendMessage("page-action-onClicked-fired");
    });

    let extensionInfo = {
      // Extract the assigned uuid from the background page url.
      uuid: `{${window.location.hostname}}`,
    };

    browser.test.sendMessage("ready", extensionInfo);
  }

  function popupScript() {
    window.onload = () => {
      browser.test.sendMessage("page-action-from-popup", location.href);
    };
    browser.test.onMessage.addListener((msg, details) => {
      if (msg == "page-action-close-popup") {
        if (details.location == location.href) {
          window.close();
        }
      }
    });
  }

  let extension = ExtensionTestUtils.loadExtension({
    background,
    manifest: {
      "name": "PageAction Extension",
      "page_action": {
        "default_title": "Page Action",
        "default_popup": "default.html",
        "default_icon": {
          "18": "extension.png",
        },
      },
    },
    files: {
      "default.html": `<html><head><meta charset="utf-8"><script src="popup.js"><\/script></head></html>`,
      "extension.png": IMAGE_ARRAYBUFFER,
      "a.html": `<html><head><meta charset="utf-8"><script src="popup.js"><\/script></head></html>`,
      "b.html": `<html><head><meta charset="utf-8"><script src="popup.js"><\/script></head></html>`,
      "popup.js": popupScript,
    },
  });

  let tabClosedPromise = () => {
    return new Promise(resolve => {
      let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
      let BrowserApp = chromeWin.BrowserApp;

      let tabCloseListener = (event) => {
        BrowserApp.deck.removeEventListener("TabClose", tabCloseListener, false);
        let browser = event.target;
        let url = browser.currentURI.spec;
        resolve(url);
      };

      BrowserApp.deck.addEventListener("TabClose", tabCloseListener, false);
    });
  };

  function* testPopup(name, uuid) {
    // We don't need to set the popup when testing default_popup.
    if (name != "default.html") {
      extension.sendMessage("page-action-set-popup", {name});
      yield extension.awaitMessage("page-action-popup-set");
    }

    extension.sendMessage("page-action-get-popup");
    let url = yield extension.awaitMessage("page-action-got-popup");

    if (name == "") {
      ok(url == name, "Calling pageAction.getPopup should return an empty string when the popup is not set.");

      // The onClicked listener should get called when the popup is set to an empty string.
      extension.sendMessage("page-action-enable-onClicked-listener");
      yield extension.awaitMessage("page-action-onClicked-listener-enabled");

      clickPageAction(uuid);
      yield extension.awaitMessage("page-action-onClicked-fired");

      extension.sendMessage("page-action-disable-onClicked-listener");
      yield extension.awaitMessage("page-action-onClicked-listener-disabled");
    } else {
      ok(url.includes(name), "Calling pageAction.getPopup should return the correct popup URL when the popup is set.");

      clickPageAction(uuid);
      let location = yield extension.awaitMessage("page-action-from-popup");
      ok(location.includes(name), "The popup with the correct URL should be shown.");

      extension.sendMessage("page-action-close-popup", {location});

      url = yield tabClosedPromise();
      ok(url.includes(name), "The tab for the popup should be closed.");
    }
  }

  yield extension.startup();
  let {uuid} = yield extension.awaitMessage("ready");

  extension.sendMessage("page-action-show");
  yield extension.awaitMessage("page-action-shown");
  ok(isPageActionShown(uuid), "The PageAction should be shown.");

  yield testPopup("default.html", uuid);
  yield testPopup("a.html", uuid);
  yield testPopup("", uuid);
  yield testPopup("b.html", uuid);

  yield extension.unload();
  ok(!isPageActionShown(uuid), "The PageAction should be removed after unload.");
});
</script>

</body>
</html>