tor-browser

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

browser_662743.js (4155B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // This tests that session restore component does restore the right <select> option.
      7 // Session store should not rely only on previous user's selectedIndex, it should
      8 // check its value as well.
      9 
     10 function test() {
     11  /** Tests selected options */
     12  requestLongerTimeout(2);
     13  waitForExplicitFinish();
     14 
     15  let testTabCount = 0;
     16  let formData = [
     17    // default case
     18    {},
     19 
     20    // new format
     21    // index doesn't match value (testing an option in between (two))
     22    { id: { select_id: { selectedIndex: 0, value: "val2" } } },
     23    // index doesn't match value (testing an invalid value)
     24    { id: { select_id: { selectedIndex: 4, value: "val8" } } },
     25    // index doesn't match value (testing an invalid index)
     26    { id: { select_id: { selectedIndex: 8, value: "val5" } } },
     27    // index and value match position zero
     28    { id: { select_id: { selectedIndex: 0, value: "val0" } }, xpath: {} },
     29    // index doesn't match value (testing the last option (seven))
     30    {
     31      id: {},
     32      xpath: {
     33        "/xhtml:html/xhtml:body/xhtml:select[@name='select_name']": {
     34          selectedIndex: 1,
     35          value: "val7",
     36        },
     37      },
     38    },
     39    // index and value match the default option "selectedIndex":3,"value":"val3"
     40    {
     41      xpath: {
     42        "/xhtml:html/xhtml:body/xhtml:select[@name='select_name']": {
     43          selectedIndex: 3,
     44          value: "val3",
     45        },
     46      },
     47    },
     48    // index matches default option however it doesn't match value
     49    { id: { select_id: { selectedIndex: 3, value: "val4" } } },
     50  ];
     51 
     52  let expectedValues = [
     53    null, // default value
     54    "val2",
     55    null, // default value (invalid value)
     56    "val5", // value is still valid (even it has an invalid index)
     57    "val0",
     58    "val7",
     59    null,
     60    "val4",
     61  ];
     62  let callback = function () {
     63    testTabCount--;
     64    if (testTabCount == 0) {
     65      finish();
     66    }
     67  };
     68 
     69  for (let i = 0; i < formData.length; i++) {
     70    testTabCount++;
     71    testTabRestoreData(formData[i], expectedValues[i], callback);
     72  }
     73 }
     74 
     75 function testTabRestoreData(aFormData, aExpectedValue, aCallback) {
     76  let testURL = getRootDirectory(gTestPath) + "browser_662743_sample.html";
     77  let tab = BrowserTestUtils.addTab(gBrowser, testURL);
     78 
     79  aFormData.url = testURL;
     80  let tabState = {
     81    entries: [{ url: testURL, triggeringPrincipal_base64 }],
     82    formdata: aFormData,
     83  };
     84 
     85  promiseBrowserLoaded(tab.linkedBrowser).then(() => {
     86    promiseTabState(tab, tabState)
     87      .then(() => {
     88        // Flush to make sure we have the latest form data.
     89        return TabStateFlusher.flush(tab.linkedBrowser);
     90      })
     91      .then(() => {
     92        let doc = tab.linkedBrowser.contentDocument;
     93        let select = doc.getElementById("select_id");
     94        let value = select.options[select.selectedIndex].value;
     95        let restoredTabState = JSON.parse(ss.getTabState(tab));
     96 
     97        // If aExpectedValue=null we don't expect any form data to be collected.
     98        if (!aExpectedValue) {
     99          ok(
    100            !restoredTabState.hasOwnProperty("formdata"),
    101            "no formdata collected"
    102          );
    103          gBrowser.removeTab(tab);
    104          aCallback();
    105          return;
    106        }
    107 
    108        // test select options values
    109        is(
    110          value,
    111          aExpectedValue,
    112          "Select Option by selectedIndex &/or value has been restored correctly"
    113        );
    114 
    115        let restoredFormData = restoredTabState.formdata;
    116        let selectIdFormData = restoredFormData.id.select_id;
    117        value = restoredFormData.id.select_id.value;
    118 
    119        // test format
    120        ok(
    121          "id" in restoredFormData || "xpath" in restoredFormData,
    122          "FormData format is valid"
    123        );
    124        // test format
    125        ok(
    126          "selectedIndex" in selectIdFormData && "value" in selectIdFormData,
    127          "select format is valid"
    128        );
    129        // test set collection values
    130        is(value, aExpectedValue, "Collection has been saved correctly");
    131 
    132        // clean up
    133        gBrowser.removeTab(tab);
    134 
    135        aCallback();
    136      });
    137  });
    138 }