tor-browser

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

test_bug1742865.html (5169B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <meta charset="utf-8">
      5  <title>Auto refreshing pages shouldn't add an entry to session history</title>
      6  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      7  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
      8  <script>
      9    const REFRESH_REDIRECT_TIMER = 15;
     10 
     11    // 2 tests (same and cross origin) consisting of 2 refreshes of maximum 1 seconds
     12    // 2 tests (same and cross origin) consisting of 2 refreshes of REFRESH_REDIRECT_TIMER seconds
     13    // => We need (2 * 1) + (2 * 15) seconds
     14    SimpleTest.requestLongerTimeout(3);
     15    SimpleTest.waitForExplicitFinish();
     16 
     17    const SJS = new URL("file_bug1742865.sjs", location.href);
     18    const SJS_OUTER = new URL("file_bug1742865_outer.sjs", location.href);
     19    const SCROLL = 500;
     20 
     21    let tolerance;
     22    function setup() {
     23      return SpecialPowers.spawn(window.top, [], () => {
     24        return SpecialPowers.getDOMWindowUtils(content.window).getResolution();
     25      }).then(resolution => {
     26        // Allow a half pixel difference if the top document's resolution is lower
     27        // than 1.0 because the scroll position is aligned with screen pixels
     28        // instead of CSS pixels.
     29        tolerance = resolution < 1.0 ? 0.5 : 0.0;
     30      });
     31    }
     32 
     33    function checkScrollPosition(scrollPosition, shouldKeepScrollPosition) {
     34      isfuzzy(scrollPosition, shouldKeepScrollPosition ? SCROLL : 0, tolerance,
     35              `Scroll position ${shouldKeepScrollPosition ? "should" : "shouldn't"} be maintained for meta refresh`);
     36    }
     37 
     38    function openWindowAndCheckRefresh(url, params, shouldAddToHistory, shouldKeepScrollPosition) {
     39      info(`Running test for ${JSON.stringify(params)}`);
     40 
     41      url = new URL(url);
     42      Object.entries(params).forEach(([k, v]) => { url.searchParams.append(k, v) });
     43      url.searchParams.append("scrollTo", SCROLL);
     44 
     45      let resetURL = new URL(SJS);
     46      resetURL.search = "?reset";
     47      return fetch(resetURL).then(() => {
     48        return new Promise((resolve) => {
     49          let count = 0;
     50          window.addEventListener("message", function listener({ data: { commandType, commandData = {} } }) {
     51            if (commandType == "onChangedInputValue") {
     52              let { historyLength, inputValue } = commandData;
     53 
     54              if (shouldAddToHistory) {
     55                is(historyLength, count, "Auto-refresh should add entries to session history");
     56              } else {
     57                is(historyLength, 1, "Auto-refresh shouldn't add entries to session history");
     58              }
     59 
     60              is(inputValue, "1234", "Input's value should have been changed");
     61 
     62              win.postMessage("loadNext", "*");
     63              return;
     64            }
     65 
     66            is(commandType, "pageShow", "Unknown command type");
     67 
     68            let { inputValue, scrollPosition } = commandData;
     69 
     70            switch (++count) {
     71              // file_bug1742865.sjs causes 3 loads:
     72              //  * first load, returns first meta refresh
     73              //  * second load, caused by first meta refresh, returns second meta refresh
     74              //  * third load, caused by second meta refresh, doesn't return a meta refresh
     75              case 2:
     76                checkScrollPosition(scrollPosition, shouldKeepScrollPosition);
     77                break;
     78              case 3:
     79                checkScrollPosition(scrollPosition, shouldKeepScrollPosition);
     80                win.postMessage("changeInputValue", "*");
     81                break;
     82              case 4:
     83                win.postMessage("back", "*");
     84                break;
     85              case 5:
     86                is(inputValue, "1234", "Entries for auto-refresh should be attached to session history");
     87                checkScrollPosition(scrollPosition, shouldKeepScrollPosition);
     88                removeEventListener("message", listener);
     89                win.close();
     90                resolve();
     91                break;
     92            }
     93          });
     94          let win = window.open(url);
     95        });
     96      });
     97    }
     98 
     99    function doTest(seconds, crossOrigin, shouldAddToHistory, shouldKeepScrollPosition) {
    100      let params = {
    101        seconds,
    102        crossOrigin,
    103      };
    104 
    105      return openWindowAndCheckRefresh(SJS, params, shouldAddToHistory, shouldKeepScrollPosition).then(() =>
    106        openWindowAndCheckRefresh(SJS_OUTER, params, shouldAddToHistory, shouldKeepScrollPosition)
    107      );
    108    }
    109 
    110    async function runTest() {
    111      const FAST = Math.min(1, REFRESH_REDIRECT_TIMER);
    112      const SLOW = REFRESH_REDIRECT_TIMER + 1;
    113      let tests = [
    114        // [ time, crossOrigin, shouldAddToHistory, shouldKeepScrollPosition ]
    115        [ FAST, false, false, true ],
    116        [ FAST, true, false, false ],
    117        [ SLOW, false, false, true ],
    118        [ SLOW, true, true, false ],
    119      ];
    120 
    121      await setup();
    122 
    123      for (let [ time, crossOrigin, shouldAddToHistory, shouldKeepScrollPosition ] of tests) {
    124        await doTest(time, crossOrigin, shouldAddToHistory, shouldKeepScrollPosition);
    125      }
    126 
    127      SimpleTest.finish();
    128    }
    129  </script>
    130 </head>
    131 <body onload="runTest();">
    132 <p id="display"></p>
    133 <div id="content" style="display: none">
    134 </div>
    135 <pre id="test"></pre>
    136 </body>
    137 </html>