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.
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 | function test() { |
michael@0 | 7 | /** Tests formdata format **/ |
michael@0 | 8 | waitForExplicitFinish(); |
michael@0 | 9 | |
michael@0 | 10 | let formData = [ |
michael@0 | 11 | { }, |
michael@0 | 12 | // old format |
michael@0 | 13 | { "#input1" : "value0" }, |
michael@0 | 14 | { "#input1" : "value1", "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value2" }, |
michael@0 | 15 | { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value3" }, |
michael@0 | 16 | // new format |
michael@0 | 17 | { id: { "input1" : "value4" } }, |
michael@0 | 18 | { id: { "input1" : "value5" }, xpath: {} }, |
michael@0 | 19 | { id: { "input1" : "value6" }, xpath: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value7" } }, |
michael@0 | 20 | { id: {}, xpath: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value8" } }, |
michael@0 | 21 | { xpath: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value9" } }, |
michael@0 | 22 | // combinations |
michael@0 | 23 | { "#input1" : "value10", id: { "input1" : "value11" } }, |
michael@0 | 24 | { "#input1" : "value12", id: { "input1" : "value13" }, xpath: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value14" } }, |
michael@0 | 25 | { "#input1" : "value15", xpath: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value16" } }, |
michael@0 | 26 | { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value17", id: { "input1" : "value18" } }, |
michael@0 | 27 | { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value19", id: { "input1" : "value20" }, xpath: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value21" } }, |
michael@0 | 28 | { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value22", xpath: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value23" } }, |
michael@0 | 29 | { "#input1" : "value24", "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value25", id: { "input1" : "value26" } }, |
michael@0 | 30 | { "#input1" : "value27", "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value28", id: { "input1" : "value29" }, xpath: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value30" } }, |
michael@0 | 31 | { "#input1" : "value31", "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value32", xpath: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value33" } } |
michael@0 | 32 | ] |
michael@0 | 33 | let expectedValues = [ |
michael@0 | 34 | [ "" , "" ], |
michael@0 | 35 | // old format |
michael@0 | 36 | [ "value0", "" ], |
michael@0 | 37 | [ "value1", "value2" ], |
michael@0 | 38 | [ "", "value3" ], |
michael@0 | 39 | // new format |
michael@0 | 40 | [ "value4", "" ], |
michael@0 | 41 | [ "value5", "" ], |
michael@0 | 42 | [ "value6", "value7" ], |
michael@0 | 43 | [ "", "value8" ], |
michael@0 | 44 | [ "", "value9" ], |
michael@0 | 45 | // combinations |
michael@0 | 46 | [ "value11", "" ], |
michael@0 | 47 | [ "value13", "value14" ], |
michael@0 | 48 | [ "", "value16" ], |
michael@0 | 49 | [ "value18", "" ], |
michael@0 | 50 | [ "value20", "value21" ], |
michael@0 | 51 | [ "", "value23" ], |
michael@0 | 52 | [ "value26", "" ], |
michael@0 | 53 | [ "value29", "value30" ], |
michael@0 | 54 | [ "", "value33" ] |
michael@0 | 55 | ]; |
michael@0 | 56 | let testTabCount = 0; |
michael@0 | 57 | let callback = function() { |
michael@0 | 58 | testTabCount--; |
michael@0 | 59 | if (testTabCount == 0) { |
michael@0 | 60 | finish(); |
michael@0 | 61 | } |
michael@0 | 62 | }; |
michael@0 | 63 | |
michael@0 | 64 | for (let i = 0; i < formData.length; i++) { |
michael@0 | 65 | testTabCount++; |
michael@0 | 66 | testTabRestoreData(formData[i], expectedValues[i], callback); |
michael@0 | 67 | } |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | function testTabRestoreData(aFormData, aExpectedValue, aCallback) { |
michael@0 | 71 | let URL = ROOT + "browser_formdata_format_sample.html"; |
michael@0 | 72 | let tab = gBrowser.addTab("about:blank"); |
michael@0 | 73 | let browser = tab.linkedBrowser; |
michael@0 | 74 | let tabState = { entries: [{ url: URL, formdata: aFormData}] }; |
michael@0 | 75 | |
michael@0 | 76 | Task.spawn(function () { |
michael@0 | 77 | yield promiseBrowserLoaded(tab.linkedBrowser); |
michael@0 | 78 | |
michael@0 | 79 | ss.setTabState(tab, JSON.stringify(tabState)); |
michael@0 | 80 | yield promiseTabRestored(tab); |
michael@0 | 81 | |
michael@0 | 82 | SyncHandlers.get(tab.linkedBrowser).flush(); |
michael@0 | 83 | let restoredTabState = JSON.parse(ss.getTabState(tab)); |
michael@0 | 84 | let restoredFormData = restoredTabState.formdata; |
michael@0 | 85 | |
michael@0 | 86 | if (restoredFormData) { |
michael@0 | 87 | let doc = tab.linkedBrowser.contentDocument; |
michael@0 | 88 | let input1 = doc.getElementById("input1"); |
michael@0 | 89 | let input2 = doc.querySelector("input[name=input2]"); |
michael@0 | 90 | |
michael@0 | 91 | // test format |
michael@0 | 92 | ok("id" in restoredFormData || "xpath" in restoredFormData, |
michael@0 | 93 | "FormData format is valid: " + restoredFormData); |
michael@0 | 94 | // validate that there are no old keys |
michael@0 | 95 | for (let key of Object.keys(restoredFormData)) { |
michael@0 | 96 | if (["id", "xpath", "url"].indexOf(key) === -1) { |
michael@0 | 97 | ok(false, "FormData format is invalid."); |
michael@0 | 98 | } |
michael@0 | 99 | } |
michael@0 | 100 | // test id |
michael@0 | 101 | is(input1.value, aExpectedValue[0], |
michael@0 | 102 | "FormData by 'id' has been restored correctly"); |
michael@0 | 103 | // test xpath |
michael@0 | 104 | is(input2.value, aExpectedValue[1], |
michael@0 | 105 | "FormData by 'xpath' has been restored correctly"); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | // clean up |
michael@0 | 109 | gBrowser.removeTab(tab); |
michael@0 | 110 | |
michael@0 | 111 | // This test might time out if the task fails. |
michael@0 | 112 | }).then(aCallback); |
michael@0 | 113 | } |