tor-browser

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

browser_resources_getAllResources.js (3496B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test getAllResources function of the ResourceCommand.
      7 
      8 const TEST_URI = "data:text/html;charset=utf-8,getAllResources test";
      9 
     10 add_task(async function () {
     11  const tab = await addTab(TEST_URI);
     12 
     13  const { client, resourceCommand, targetCommand } =
     14    await initResourceCommand(tab);
     15 
     16  info("Check the resources gotten from getAllResources at initial");
     17  is(
     18    resourceCommand.getAllResources(resourceCommand.TYPES.CONSOLE_MESSAGE)
     19      .length,
     20    0,
     21    "There is no resources at initial"
     22  );
     23 
     24  info(
     25    "Start to watch the available resources in order to compare with resources gotten from getAllResources"
     26  );
     27  const availableResources = [];
     28  const onAvailable = resources => availableResources.push(...resources);
     29  await resourceCommand.watchResources(
     30    [resourceCommand.TYPES.CONSOLE_MESSAGE],
     31    { onAvailable }
     32  );
     33 
     34  info("Check the resources after some resources are available");
     35  const messages = ["a", "b", "c"];
     36  await logMessages(tab.linkedBrowser, messages);
     37 
     38  try {
     39    await waitFor(() => availableResources.length === messages.length);
     40  } catch (e) {
     41    ok(
     42      false,
     43      `Didn't receive the expected number of resources. Got ${
     44        availableResources.length
     45      }, expected ${messages.length} - ${availableResources
     46        .map(r => r.message.arguments[0])
     47        .join(" - ")}`
     48    );
     49  }
     50 
     51  assertResources(
     52    resourceCommand.getAllResources(resourceCommand.TYPES.CONSOLE_MESSAGE),
     53    availableResources
     54  );
     55  assertResources(
     56    resourceCommand.getAllResources(resourceCommand.TYPES.STYLESHEET),
     57    []
     58  );
     59 
     60  info("Check the resources after reloading");
     61  await BrowserTestUtils.reloadTab(tab);
     62  assertResources(
     63    resourceCommand.getAllResources(resourceCommand.TYPES.CONSOLE_MESSAGE),
     64    []
     65  );
     66 
     67  info("Append some resources again to test unwatching");
     68  const newMessages = ["d", "e", "f"];
     69  await logMessages(tab.linkedBrowser, messages);
     70  try {
     71    await waitFor(
     72      () =>
     73        resourceCommand.getAllResources(resourceCommand.TYPES.CONSOLE_MESSAGE)
     74          .length === newMessages.length
     75    );
     76  } catch (e) {
     77    const resources = resourceCommand.getAllResources(
     78      resourceCommand.TYPES.CONSOLE_MESSAGE
     79    );
     80    ok(
     81      false,
     82      `Didn't receive the expected number of resources. Got ${
     83        resources.length
     84      }, expected ${messages.length} - ${resources
     85        .map(r => r.arguments.join(" | "))
     86        .join(" - ")}`
     87    );
     88  }
     89 
     90  info("Check the resources after unwatching");
     91  resourceCommand.unwatchResources([resourceCommand.TYPES.CONSOLE_MESSAGE], {
     92    onAvailable,
     93  });
     94  assertResources(
     95    resourceCommand.getAllResources(resourceCommand.TYPES.CONSOLE_MESSAGE),
     96    []
     97  );
     98 
     99  targetCommand.destroy();
    100  await client.close();
    101 });
    102 
    103 function assertResources(resources, expectedResources) {
    104  is(
    105    resources.length,
    106    expectedResources.length,
    107    "Number of the resources is correct"
    108  );
    109 
    110  for (let i = 0; i < resources.length; i++) {
    111    const resource = resources[i];
    112    const expectedResource = expectedResources[i];
    113    Assert.strictEqual(
    114      resource,
    115      expectedResource,
    116      `The ${i}th resource is correct`
    117    );
    118  }
    119 }
    120 
    121 function logMessages(browser, messages) {
    122  return SpecialPowers.spawn(browser, [messages], innerMessages => {
    123    for (const message of innerMessages) {
    124      content.console.log(message);
    125    }
    126  });
    127 }