Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 "use strict";
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.
10 function test() {
11 /** Tests selected options **/
12 requestLongerTimeout(2);
13 waitForExplicitFinish();
15 let testTabCount = 0;
16 let formData = [
17 // default case
18 { },
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 { id:{},"xpath":{ "/xhtml:html/xhtml:body/xhtml:select[@name='select_name']": {"selectedIndex":1,"value":"val7"} } },
31 // index and value match the default option "selectedIndex":3,"value":"val3"
32 { xpath: { "/xhtml:html/xhtml:body/xhtml:select[@name='select_name']" : {"selectedIndex":3,"value":"val3"} } },
33 // index matches default option however it doesn't match value
34 { id:{ "select_id": {"selectedIndex":3,"value":"val4"} } },
35 ];
37 let expectedValues = [
38 null, // default value
39 "val2",
40 null, // default value (invalid value)
41 "val5", // value is still valid (even it has an invalid index)
42 "val0",
43 "val7",
44 null,
45 "val4",
46 ];
47 let callback = function() {
48 testTabCount--;
49 if (testTabCount == 0) {
50 finish();
51 }
52 };
54 for (let i = 0; i < formData.length; i++) {
55 testTabCount++;
56 testTabRestoreData(formData[i], expectedValues[i], callback);
57 }
58 }
60 function testTabRestoreData(aFormData, aExpectedValue, aCallback) {
61 let testURL =
62 getRootDirectory(gTestPath) + "browser_662743_sample.html";
63 let tab = gBrowser.addTab(testURL);
64 let tabState = { entries: [{ url: testURL, formdata: aFormData}] };
66 whenBrowserLoaded(tab.linkedBrowser, function() {
67 ss.setTabState(tab, JSON.stringify(tabState));
69 whenTabRestored(tab, function() {
70 let doc = tab.linkedBrowser.contentDocument;
71 let select = doc.getElementById("select_id");
72 let value = select.options[select.selectedIndex].value;
74 // Flush to make sure we have the latest form data.
75 SyncHandlers.get(tab.linkedBrowser).flush();
76 let restoredTabState = JSON.parse(ss.getTabState(tab));
78 // If aExpectedValue=null we don't expect any form data to be collected.
79 if (!aExpectedValue) {
80 ok(!restoredTabState.hasOwnProperty("formdata"), "no formdata collected");
81 gBrowser.removeTab(tab);
82 aCallback();
83 return;
84 }
86 // test select options values
87 is(value, aExpectedValue,
88 "Select Option by selectedIndex &/or value has been restored correctly");
90 let restoredFormData = restoredTabState.formdata;
91 let selectIdFormData = restoredFormData.id.select_id;
92 let value = restoredFormData.id.select_id.value;
94 // test format
95 ok("id" in restoredFormData || "xpath" in restoredFormData,
96 "FormData format is valid");
97 // test format
98 ok("selectedIndex" in selectIdFormData && "value" in selectIdFormData,
99 "select format is valid");
100 // test set collection values
101 is(value, aExpectedValue,
102 "Collection has been saved correctly");
104 // clean up
105 gBrowser.removeTab(tab);
107 aCallback();
108 });
109 });
110 }