browser/components/sessionstore/test/browser_662743.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/sessionstore/test/browser_662743.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,110 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +"use strict";
     1.8 +
     1.9 +// This tests that session restore component does restore the right <select> option.
    1.10 +// Session store should not rely only on previous user's selectedIndex, it should
    1.11 +// check its value as well.
    1.12 +
    1.13 +function test() {
    1.14 +  /** Tests selected options **/
    1.15 +  requestLongerTimeout(2);
    1.16 +  waitForExplicitFinish();
    1.17 +
    1.18 +  let testTabCount = 0;
    1.19 +  let formData = [
    1.20 +  // default case
    1.21 +    { },
    1.22 +
    1.23 +  // new format
    1.24 +    // index doesn't match value (testing an option in between (two))
    1.25 +    { id:{ "select_id": {"selectedIndex":0,"value":"val2"} } },
    1.26 +    // index doesn't match value (testing an invalid value)
    1.27 +    { id:{ "select_id": {"selectedIndex":4,"value":"val8"} } },
    1.28 +    // index doesn't match value (testing an invalid index)
    1.29 +    { id:{ "select_id": {"selectedIndex":8,"value":"val5"} } },
    1.30 +    // index and value match position zero
    1.31 +    { id:{ "select_id": {"selectedIndex":0,"value":"val0"} }, xpath: {} },
    1.32 +    // index doesn't match value (testing the last option (seven))
    1.33 +    { id:{},"xpath":{ "/xhtml:html/xhtml:body/xhtml:select[@name='select_name']": {"selectedIndex":1,"value":"val7"} } },
    1.34 +    // index and value match the default option "selectedIndex":3,"value":"val3"
    1.35 +    { xpath: { "/xhtml:html/xhtml:body/xhtml:select[@name='select_name']" : {"selectedIndex":3,"value":"val3"} } },
    1.36 +    // index matches default option however it doesn't match value
    1.37 +    { id:{ "select_id": {"selectedIndex":3,"value":"val4"} } },
    1.38 +  ];
    1.39 +
    1.40 +  let expectedValues = [
    1.41 +    null,   // default value
    1.42 +    "val2",
    1.43 +    null,   // default value (invalid value)
    1.44 +    "val5", // value is still valid (even it has an invalid index)
    1.45 +    "val0",
    1.46 +    "val7",
    1.47 +    null,
    1.48 +    "val4",
    1.49 +  ];
    1.50 +  let callback = function() {
    1.51 +    testTabCount--;
    1.52 +    if (testTabCount == 0) {
    1.53 +      finish();
    1.54 +    }
    1.55 +  };
    1.56 +
    1.57 +  for (let i = 0; i < formData.length; i++) {
    1.58 +    testTabCount++;
    1.59 +    testTabRestoreData(formData[i], expectedValues[i], callback);
    1.60 +  }
    1.61 +}
    1.62 +
    1.63 +function testTabRestoreData(aFormData, aExpectedValue, aCallback) {
    1.64 +  let testURL =
    1.65 +    getRootDirectory(gTestPath) + "browser_662743_sample.html";
    1.66 +  let tab = gBrowser.addTab(testURL);
    1.67 +  let tabState = { entries: [{ url: testURL, formdata: aFormData}] };
    1.68 +
    1.69 +  whenBrowserLoaded(tab.linkedBrowser, function() {
    1.70 +    ss.setTabState(tab, JSON.stringify(tabState));
    1.71 +
    1.72 +    whenTabRestored(tab, function() {
    1.73 +      let doc = tab.linkedBrowser.contentDocument;
    1.74 +      let select = doc.getElementById("select_id");
    1.75 +      let value = select.options[select.selectedIndex].value;
    1.76 +
    1.77 +      // Flush to make sure we have the latest form data.
    1.78 +      SyncHandlers.get(tab.linkedBrowser).flush();
    1.79 +      let restoredTabState = JSON.parse(ss.getTabState(tab));
    1.80 +
    1.81 +      // If aExpectedValue=null we don't expect any form data to be collected.
    1.82 +      if (!aExpectedValue) {
    1.83 +        ok(!restoredTabState.hasOwnProperty("formdata"), "no formdata collected");
    1.84 +        gBrowser.removeTab(tab);
    1.85 +        aCallback();
    1.86 +        return;
    1.87 +      }
    1.88 +
    1.89 +      // test select options values
    1.90 +      is(value, aExpectedValue,
    1.91 +        "Select Option by selectedIndex &/or value has been restored correctly");
    1.92 +
    1.93 +      let restoredFormData = restoredTabState.formdata;
    1.94 +      let selectIdFormData = restoredFormData.id.select_id;
    1.95 +      let value = restoredFormData.id.select_id.value;
    1.96 +
    1.97 +      // test format
    1.98 +      ok("id" in restoredFormData || "xpath" in restoredFormData,
    1.99 +        "FormData format is valid");
   1.100 +      // test format
   1.101 +      ok("selectedIndex" in selectIdFormData && "value" in selectIdFormData,
   1.102 +        "select format is valid");
   1.103 +       // test set collection values
   1.104 +      is(value, aExpectedValue,
   1.105 +        "Collection has been saved correctly");
   1.106 +
   1.107 +      // clean up
   1.108 +      gBrowser.removeTab(tab);
   1.109 +
   1.110 +      aCallback();
   1.111 +    });
   1.112 +  });
   1.113 +}

mercurial