summaryrefslogtreecommitdiffstats
path: root/browser/components/contextualidentity/test/browser/browser_favicon.js
blob: d6374ecd3cd69ec8044c7cb5a101b8b7df67cc43 (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
/*
 * Bug 1270678 - A test case to test does the favicon obey originAttributes.
 */

let { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");

const USER_CONTEXTS = ["default", "personal", "work"];

let gHttpServer = null;
let gUserContextId;
let gFaviconData;

function getIconFile() {
  new Promise(resolve => {
    NetUtil.asyncFetch(
      {
        uri: "http://www.example.com/browser/browser/components/contextualidentity/test/browser/favicon-normal32.png",
        loadUsingSystemPrincipal: true,
        contentPolicyType: Ci.nsIContentPolicy.TYPE_INTERNAL_IMAGE_FAVICON,
      },
      function (inputStream, status) {
        let size = inputStream.available();
        gFaviconData = NetUtil.readInputStreamToString(inputStream, size);
        resolve();
      }
    );
  });
}

async function openTabInUserContext(uri, userContextId) {
  // open the tab in the correct userContextId
  let tab = BrowserTestUtils.addTab(gBrowser, uri, { userContextId });

  // select tab and make sure its browser is focused
  gBrowser.selectedTab = tab;
  tab.ownerGlobal.focus();

  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);
  return { tab, browser };
}

function loadIndexHandler(metadata, response) {
  response.setStatusLine(metadata.httpVersion, 200, "Ok");
  response.setHeader("Content-Type", "text/html", false);
  let body = `
    <!DOCTYPE HTML>
      <html>
        <head>
          <meta charset='utf-8'>
          <title>Favicon Test</title>
        </head>
        <body>
          Favicon!!
        </body>
    </html>`;
  response.bodyOutputStream.write(body, body.length);
}

function loadFaviconHandler(metadata, response) {
  let expectedCookie = "userContext=" + USER_CONTEXTS[gUserContextId];

  if (metadata.hasHeader("Cookie")) {
    is(
      metadata.getHeader("Cookie"),
      expectedCookie,
      "The cookie has matched with the expected cookie."
    );
  } else {
    ok(false, "The request should have a cookie.");
  }

  response.setStatusLine(metadata.httpVersion, 200, "Ok");
  response.setHeader("Content-Type", "image/png", false);
  response.bodyOutputStream.write(gFaviconData, gFaviconData.length);
}

add_setup(async function () {
  // Make sure userContext is enabled.
  await SpecialPowers.pushPrefEnv({
    set: [["privacy.userContext.enabled", true]],
  });

  // Create a http server for the image cache test.
  if (!gHttpServer) {
    gHttpServer = new HttpServer();
    gHttpServer.registerPathHandler("/", loadIndexHandler);
    gHttpServer.registerPathHandler("/favicon.png", loadFaviconHandler);
    gHttpServer.start(-1);
  }
});

registerCleanupFunction(() => {
  gHttpServer.stop(() => {
    gHttpServer = null;
  });
});

add_task(async function test() {
  waitForExplicitFinish();

  // First, get the icon data.
  await getIconFile();

  let serverPort = gHttpServer.identity.primaryPort;
  let testURL = "http://localhost:" + serverPort + "/";
  let testFaviconURL = "http://localhost:" + serverPort + "/favicon.png";

  for (let userContextId of Object.keys(USER_CONTEXTS)) {
    gUserContextId = userContextId;

    // Load the page in 3 different contexts and set a cookie
    // which should only be visible in that context.

    // Open our tab in the given user context.
    let tabInfo = await openTabInUserContext(testURL, userContextId);

    // Write a cookie according to the userContext.
    await SpecialPowers.spawn(
      tabInfo.browser,
      [{ userContext: USER_CONTEXTS[userContextId] }],
      function (arg) {
        content.document.cookie = "userContext=" + arg.userContext;
      }
    );

    let pageURI = NetUtil.newURI(testURL);
    let favIconURI = NetUtil.newURI(testFaviconURL);

    await new Promise(resolve => {
      PlacesUtils.favicons.setAndFetchFaviconForPage(
        pageURI,
        favIconURI,
        true,
        PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
        {
          onComplete() {
            resolve();
          },
        },
        tabInfo.browser.contentPrincipal
      );
    });

    BrowserTestUtils.removeTab(tabInfo.tab);
  }
});