tor-browser

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

browser_net_autoscroll.js (4098B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /**
      7 * Bug 863102 - Automatically scroll down upon new network requests.
      8 * edited to account for changes made to fix Bug 1360457
      9 */
     10 add_task(async function () {
     11  requestLongerTimeout(4);
     12 
     13  const { tab, monitor } = await initNetMonitor(INFINITE_GET_URL, {
     14    enableCache: true,
     15    requestCount: 1,
     16    expectedEventTimings: 1,
     17  });
     18  const { document, windowRequire, store } = monitor.panelWin;
     19  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     20 
     21  store.dispatch(Actions.batchEnable(false));
     22 
     23  // Wait until the first request makes the empty notice disappear
     24  await waitForRequestListToAppear();
     25 
     26  const requestsContainer = document.querySelector(".requests-list-scroll");
     27  ok(requestsContainer, "Container element exists as expected.");
     28 
     29  // (1) Check that the scroll position is maintained at the bottom
     30  // when the requests overflow the vertical size of the container.
     31  await waitForRequestsToOverflowContainer();
     32  await waitForScroll();
     33  ok(true, "Scrolled to bottom on overflow.");
     34 
     35  // (2) Now scroll to the top and check that additional requests
     36  // do not change the scroll position.
     37  requestsContainer.scrollTop = 0;
     38  await waitSomeTime();
     39  ok(!scrolledToBottom(requestsContainer), "Not scrolled to bottom.");
     40  // save for comparison later
     41  let { scrollTop } = requestsContainer;
     42  // As we are scrolled top, new request appended won't be fetching their event timings,
     43  // so do not wait for them
     44  await waitForNetworkEvents(monitor, 8, { expectedEventTimings: 0 });
     45  await waitSomeTime();
     46  is(requestsContainer.scrollTop, scrollTop, "Did not scroll.");
     47 
     48  // (3) Now set the scroll position back at the bottom and check that
     49  // additional requests *do* cause the container to scroll down.
     50  requestsContainer.scrollTop = requestsContainer.scrollHeight;
     51  ok(scrolledToBottom(requestsContainer), "Set scroll position to bottom.");
     52 
     53  // Make sure to only expect the same exact number of event timings, otherwise
     54  // waitForNetworkEvents waits for all pending requests to have received event
     55  // timings and since the page sends requests forever, on slow platform this
     56  // can easily timeout.
     57  await waitForNetworkEvents(monitor, 8, { expectedEventTimings: 8 });
     58 
     59  await waitForScroll();
     60  ok(true, "Still scrolled to bottom.");
     61 
     62  // (4) Now select the first item in the list
     63  // and check that additional requests do not change the scroll position
     64  // from just below the headers.
     65  store.dispatch(Actions.selectRequestByIndex(0));
     66  scrollTop = requestsContainer.scrollTop;
     67  await waitForNetworkEvents(monitor, 8, { expectedEventTimings: 8 });
     68  await waitSomeTime();
     69  is(requestsContainer.scrollTop, scrollTop, "Did not scroll.");
     70 
     71  // Stop doing requests.
     72  await SpecialPowers.spawn(tab.linkedBrowser, [], function () {
     73    content.wrappedJSObject.stopRequests();
     74  });
     75 
     76  // Done: clean up.
     77  return teardown(monitor);
     78 
     79  function waitForRequestListToAppear() {
     80    info(
     81      "Waiting until the empty notice disappears and is replaced with the list"
     82    );
     83    return waitUntil(
     84      () => !!document.querySelector(".requests-list-row-group")
     85    );
     86  }
     87 
     88  async function waitForRequestsToOverflowContainer() {
     89    info("Waiting for enough requests to overflow the container");
     90    while (true) {
     91      info("Waiting for one network request");
     92      await waitForNetworkEvents(monitor, 1, { expectedEventTimings: 1 });
     93      if (
     94        requestsContainer.scrollHeight >
     95        requestsContainer.clientHeight + 50
     96      ) {
     97        info("The list is long enough, returning");
     98        return;
     99      }
    100    }
    101  }
    102 
    103  function scrolledToBottom(element) {
    104    return element.scrollTop + element.clientHeight >= element.scrollHeight;
    105  }
    106 
    107  function waitSomeTime() {
    108    // Wait to make sure no scrolls happen
    109    return wait(50);
    110  }
    111 
    112  function waitForScroll() {
    113    info("Waiting for the list to scroll to bottom");
    114    return waitUntil(() => scrolledToBottom(requestsContainer));
    115  }
    116 });