tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_test_file_channel_observer.js (2784B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 add_task(async function test_file_channel_observer() {
      7  const TEST_URI = Services.io.newFileURI(
      8    new FileUtils.File(getTestFilePath("file_channel.html"))
      9  ).spec;
     10 
     11  // The channel for the document is created in the parent process.
     12  const waitForHTML = waitForFileChannelNotification(
     13    "file_channel.html",
     14    "text/html",
     15    true
     16  );
     17 
     18  // The channel for the image is created in the content process.
     19  // file_channel.html loads res_img.png using a relative path.
     20  const waitForImage = waitForFileChannelNotification(
     21    "res_img.png",
     22    "image/png",
     23    false
     24  );
     25 
     26  let tab = await BrowserTestUtils.addTab(gBrowser, TEST_URI);
     27  await Promise.all([waitForHTML, waitForImage]);
     28  info("We received the expected observer notifications");
     29  await BrowserTestUtils.removeTab(tab);
     30 });
     31 
     32 async function waitForFileChannelNotification(
     33  fileName,
     34  contentType,
     35  isDocument
     36 ) {
     37  let receivedNotifications = 0;
     38  const observer = {
     39    QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
     40    observe: function observe(subject, topic) {
     41      switch (topic) {
     42        case "file-channel-opened": {
     43          ok(
     44            subject instanceof Ci.nsIFileChannel,
     45            "Channel should be a nsIFileChannel instance"
     46          );
     47          ok(
     48            subject instanceof Ci.nsIIdentChannel,
     49            "Channel should be a nsIIdentChannel instance"
     50          );
     51 
     52          const channel = subject.QueryInterface(Ci.nsIChannel);
     53          channel.QueryInterface(Ci.nsIIdentChannel);
     54 
     55          is(
     56            typeof channel.channelId,
     57            "number",
     58            "File channel has a valid channelId"
     59          );
     60 
     61          // Note: Here we don't compare the exact uri and match the filename:
     62          // - URIs built via newFileURI refer to the symlink in objdir
     63          // - channel URI will be the actual file URI
     64          // Using originalURI work for the html file, but not for img file.
     65          if (channel.URI.spec.endsWith(fileName)) {
     66            is(
     67              channel.contentType,
     68              contentType,
     69              "File channel has the expected content type"
     70            );
     71 
     72            is(
     73              channel.isDocument,
     74              isDocument,
     75              "File channel has the expected isDocument flag"
     76            );
     77 
     78            receivedNotifications++;
     79          }
     80          break;
     81        }
     82      }
     83    },
     84  };
     85  Services.obs.addObserver(observer, "file-channel-opened");
     86  await BrowserTestUtils.waitForCondition(() => receivedNotifications > 0);
     87  is(receivedNotifications, 1, "Received exactly one notification");
     88  Services.obs.removeObserver(observer, "file-channel-opened");
     89 }