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

/* import-globals-from ../../../thumbnails/test/head.js */
loadTestSubscript("../../../thumbnails/test/head.js");

// The service that creates thumbnails of webpages in the background loads a
// web page in the background (with several features disabled). Extensions
// should be able to observe requests, but not run content scripts.
add_task(async function test_thumbnails_background_visibility_to_extensions() {
  const iframeUrl = "http://example.com/?iframe";
  const testPageUrl = bgTestPageURL({ iframe: iframeUrl });
  // ^ testPageUrl is http://mochi.test:8888/.../thumbnails_background.sjs?...

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      content_scripts: [
        {
          // ":8888" omitted due to bug 1362809.
          matches: [
            "http://mochi.test/*/thumbnails_background.sjs*",
            "http://example.com/?iframe*",
          ],
          js: ["contentscript.js"],
          run_at: "document_start",
          all_frames: true,
        },
      ],
      permissions: [
        "webRequest",
        "webRequestBlocking",
        "http://example.com/*",
        "http://mochi.test/*",
      ],
    },
    files: {
      "contentscript.js": () => {
        // Content scripts are not expected to be run in the page of the
        // thumbnail service, so this should never execute.
        new Image().src = "http://example.com/?unexpected-content-script";
        browser.test.fail("Content script ran in thumbs, unexpectedly.");
      },
    },
    background() {
      let requests = [];
      browser.webRequest.onBeforeRequest.addListener(
        ({ url, tabId, frameId, type }) => {
          browser.test.assertEq(-1, tabId, "Thumb page is not a tab");
          // We want to know if frameId is 0 or non-negative (or possibly -1).
          if (type === "sub_frame") {
            browser.test.assertTrue(frameId > 0, `frame ${frameId} for ${url}`);
          } else {
            browser.test.assertEq(0, frameId, `frameId for ${type} ${url}`);
          }
          requests.push({ type, url });
        },
        {
          types: ["main_frame", "sub_frame", "image"],
          urls: ["*://*/*"],
        },
        ["blocking"]
      );
      browser.test.onMessage.addListener(msg => {
        browser.test.assertEq("get-results", msg, "expected message");
        browser.test.sendMessage("webRequest-results", requests);
      });
    },
  });

  await extension.startup();

  ok(!thumbnailExists(testPageUrl), "Thumbnail should not be cached yet.");

  await bgCapture(testPageUrl);
  ok(thumbnailExists(testPageUrl), "Thumbnail should be cached after capture");
  removeThumbnail(testPageUrl);

  extension.sendMessage("get-results");
  Assert.deepEqual(
    await extension.awaitMessage("webRequest-results"),
    [
      {
        type: "main_frame",
        url: testPageUrl,
      },
      {
        type: "sub_frame",
        url: iframeUrl,
      },
    ],
    "Expected requests via webRequest"
  );

  await extension.unload();
});