tor-browser

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

browser_grids_grid-list-on-target-added-removed.js (6267B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Tests that the list of grids does refresh when targets are added or removed (e.g. when
      7 // there's a navigation and iframe are added or removed)
      8 
      9 add_task(async function () {
     10  await addTab(getDocumentBuilderUrl("example.com", "top-level-com-grid"));
     11  const { gridInspector } = await openLayoutView();
     12  const { document: doc } = gridInspector;
     13 
     14  const checkGridList = (expected, assertionMessage) =>
     15    checkGridListItems(doc, expected, assertionMessage);
     16 
     17  checkGridList(
     18    ["div#top-level-com-grid"],
     19    "One grid item is displayed at first"
     20  );
     21 
     22  info(
     23    "Check that adding same-origin iframe with a grid will update the grid list"
     24  );
     25  const sameOriginIframeBrowsingContext = await SpecialPowers.spawn(
     26    gBrowser.selectedBrowser,
     27    [getDocumentBuilderUrl("example.com", "iframe-com-grid")],
     28    src => {
     29      const iframe = content.document.createElement("iframe");
     30      iframe.id = "same-origin";
     31      iframe.src = src;
     32      content.document.body.append(iframe);
     33      return iframe.browsingContext;
     34    }
     35  );
     36 
     37  await waitFor(() => getGridListItems(doc).length == 2);
     38  checkGridList(
     39    ["div#top-level-com-grid", "div#iframe-com-grid"],
     40    "The same-origin iframe grid is displayed"
     41  );
     42 
     43  info("Check that adding remote iframe with a grid will update the grid list");
     44  const remoteIframeBrowsingContext = await SpecialPowers.spawn(
     45    gBrowser.selectedBrowser,
     46    [getDocumentBuilderUrl("example.org", "iframe-org-grid")],
     47    src => {
     48      const iframe = content.document.createElement("iframe");
     49      iframe.id = "remote";
     50      iframe.src = src;
     51      content.document.body.append(iframe);
     52      return iframe.browsingContext;
     53    }
     54  );
     55 
     56  await waitFor(() => getGridListItems(doc).length == 3);
     57  checkGridList(
     58    ["div#top-level-com-grid", "div#iframe-com-grid", "div#iframe-org-grid"],
     59    "The remote iframe grid is displayed"
     60  );
     61 
     62  info("Check that adding new grids in iframes does update the grid list");
     63  SpecialPowers.spawn(sameOriginIframeBrowsingContext, [], () => {
     64    const section = content.document.createElement("section");
     65    section.id = "com-added-grid-container";
     66    section.style = "display: grid;";
     67    content.document.body.append(section);
     68  });
     69 
     70  await waitFor(() => getGridListItems(doc).length == 4);
     71  checkGridList(
     72    [
     73      "div#top-level-com-grid",
     74      "div#iframe-com-grid",
     75      "section#com-added-grid-container",
     76      "div#iframe-org-grid",
     77    ],
     78    "The new grid in the same origin iframe is displayed"
     79  );
     80 
     81  SpecialPowers.spawn(remoteIframeBrowsingContext, [], () => {
     82    const section = content.document.createElement("section");
     83    section.id = "org-added-grid-container";
     84    section.style = "display: grid;";
     85    content.document.body.append(section);
     86  });
     87 
     88  await waitFor(() => getGridListItems(doc).length == 5);
     89  checkGridList(
     90    [
     91      "div#top-level-com-grid",
     92      "div#iframe-com-grid",
     93      "section#com-added-grid-container",
     94      "div#iframe-org-grid",
     95      "section#org-added-grid-container",
     96    ],
     97    "The new grid in the same origin iframe is displayed"
     98  );
     99 
    100  info("Check that removing iframes will update the grid list");
    101  SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    102    content.document.querySelector("iframe#same-origin").remove();
    103  });
    104 
    105  await waitFor(() => getGridListItems(doc).length == 3);
    106  checkGridList(
    107    [
    108      "div#top-level-com-grid",
    109      "div#iframe-org-grid",
    110      "section#org-added-grid-container",
    111    ],
    112    "The same-origin iframe grids were removed from the list"
    113  );
    114 
    115  SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    116    content.document.querySelector("iframe#remote").remove();
    117  });
    118 
    119  await waitFor(() => getGridListItems(doc).length == 1);
    120  checkGridList(
    121    ["div#top-level-com-grid"],
    122    "The remote iframe grids were removed as well"
    123  );
    124 
    125  info("Navigate to a new origin");
    126  await navigateTo(getDocumentBuilderUrl("example.org", "top-level-org-grid"));
    127  await waitFor(() => {
    128    const listItems = getGridListItems(doc);
    129    return (
    130      listItems.length == 1 &&
    131      listItems[0].textContent.includes("#top-level-org-grid")
    132    );
    133  });
    134  checkGridList(
    135    ["div#top-level-org-grid"],
    136    "The grid from the new origin document is displayed"
    137  );
    138 
    139  info("Check that adding remote iframe will still update the grid list");
    140  SpecialPowers.spawn(
    141    gBrowser.selectedBrowser,
    142    [getDocumentBuilderUrl("example.com", "iframe-com-grid-remote")],
    143    src => {
    144      const iframe = content.document.createElement("iframe");
    145      iframe.id = "remote";
    146      iframe.src = src;
    147      content.document.body.append(iframe);
    148    }
    149  );
    150 
    151  await waitFor(() => getGridListItems(doc).length == 2);
    152  checkGridList(
    153    ["div#top-level-org-grid", "div#iframe-com-grid-remote"],
    154    "The grid from the new origin document is displayed"
    155  );
    156 
    157  info(
    158    "Check that adding same-origin iframe with a grid will update the grid list"
    159  );
    160  SpecialPowers.spawn(
    161    gBrowser.selectedBrowser,
    162    [getDocumentBuilderUrl("example.org", "iframe-org-grid-same-origin")],
    163    src => {
    164      const iframe = content.document.createElement("iframe");
    165      iframe.id = "same-origin";
    166      iframe.src = src;
    167      content.document.body.append(iframe);
    168    }
    169  );
    170  await waitFor(() => getGridListItems(doc).length == 3);
    171  checkGridList(
    172    [
    173      "div#top-level-org-grid",
    174      "div#iframe-com-grid-remote",
    175      "div#iframe-org-grid-same-origin",
    176    ],
    177    "The grid from the new same-origin iframe is displayed"
    178  );
    179 });
    180 
    181 function getDocumentBuilderUrl(origin, gridContainerId) {
    182  return `https://${origin}/document-builder.sjs?html=${encodeURIComponent(
    183    `<style>
    184      #${gridContainerId} {
    185        display: grid;
    186      }
    187    </style>
    188    <div id="${gridContainerId}"></div>`
    189  )}`;
    190 }
    191 
    192 function getGridListItems(doc) {
    193  return Array.from(doc.querySelectorAll("#grid-list .objectBox-node"));
    194 }
    195 
    196 function checkGridListItems(doc, expectedItems, assertionText) {
    197  const gridItems = getGridListItems(doc).map(el => el.textContent);
    198  is(
    199    JSON.stringify(gridItems.sort()),
    200    JSON.stringify(expectedItems.sort()),
    201    assertionText
    202  );
    203 }