browser/components/sessionstore/test/browser_662743.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial