michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: "use strict"; michael@0: michael@0: function test() { michael@0: /** Tests formdata format **/ michael@0: waitForExplicitFinish(); michael@0: michael@0: let formData = [ michael@0: { }, michael@0: // old format michael@0: { "#input1" : "value0" }, michael@0: { "#input1" : "value1", "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value2" }, michael@0: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value3" }, michael@0: // new format michael@0: { id: { "input1" : "value4" } }, michael@0: { id: { "input1" : "value5" }, xpath: {} }, michael@0: { id: { "input1" : "value6" }, xpath: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value7" } }, michael@0: { id: {}, xpath: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value8" } }, michael@0: { xpath: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value9" } }, michael@0: // combinations michael@0: { "#input1" : "value10", id: { "input1" : "value11" } }, michael@0: { "#input1" : "value12", id: { "input1" : "value13" }, xpath: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value14" } }, michael@0: { "#input1" : "value15", xpath: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value16" } }, michael@0: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value17", id: { "input1" : "value18" } }, michael@0: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value19", id: { "input1" : "value20" }, xpath: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value21" } }, michael@0: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value22", xpath: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value23" } }, michael@0: { "#input1" : "value24", "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value25", id: { "input1" : "value26" } }, michael@0: { "#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: { "#input1" : "value31", "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value32", xpath: { "/xhtml:html/xhtml:body/xhtml:input[@name='input2']" : "value33" } } michael@0: ] michael@0: let expectedValues = [ michael@0: [ "" , "" ], michael@0: // old format michael@0: [ "value0", "" ], michael@0: [ "value1", "value2" ], michael@0: [ "", "value3" ], michael@0: // new format michael@0: [ "value4", "" ], michael@0: [ "value5", "" ], michael@0: [ "value6", "value7" ], michael@0: [ "", "value8" ], michael@0: [ "", "value9" ], michael@0: // combinations michael@0: [ "value11", "" ], michael@0: [ "value13", "value14" ], michael@0: [ "", "value16" ], michael@0: [ "value18", "" ], michael@0: [ "value20", "value21" ], michael@0: [ "", "value23" ], michael@0: [ "value26", "" ], michael@0: [ "value29", "value30" ], michael@0: [ "", "value33" ] michael@0: ]; michael@0: let testTabCount = 0; michael@0: let callback = function() { michael@0: testTabCount--; michael@0: if (testTabCount == 0) { michael@0: finish(); michael@0: } michael@0: }; michael@0: michael@0: for (let i = 0; i < formData.length; i++) { michael@0: testTabCount++; michael@0: testTabRestoreData(formData[i], expectedValues[i], callback); michael@0: } michael@0: } michael@0: michael@0: function testTabRestoreData(aFormData, aExpectedValue, aCallback) { michael@0: let URL = ROOT + "browser_formdata_format_sample.html"; michael@0: let tab = gBrowser.addTab("about:blank"); michael@0: let browser = tab.linkedBrowser; michael@0: let tabState = { entries: [{ url: URL, formdata: aFormData}] }; michael@0: michael@0: Task.spawn(function () { michael@0: yield promiseBrowserLoaded(tab.linkedBrowser); michael@0: michael@0: ss.setTabState(tab, JSON.stringify(tabState)); michael@0: yield promiseTabRestored(tab); michael@0: michael@0: SyncHandlers.get(tab.linkedBrowser).flush(); michael@0: let restoredTabState = JSON.parse(ss.getTabState(tab)); michael@0: let restoredFormData = restoredTabState.formdata; michael@0: michael@0: if (restoredFormData) { michael@0: let doc = tab.linkedBrowser.contentDocument; michael@0: let input1 = doc.getElementById("input1"); michael@0: let input2 = doc.querySelector("input[name=input2]"); michael@0: michael@0: // test format michael@0: ok("id" in restoredFormData || "xpath" in restoredFormData, michael@0: "FormData format is valid: " + restoredFormData); michael@0: // validate that there are no old keys michael@0: for (let key of Object.keys(restoredFormData)) { michael@0: if (["id", "xpath", "url"].indexOf(key) === -1) { michael@0: ok(false, "FormData format is invalid."); michael@0: } michael@0: } michael@0: // test id michael@0: is(input1.value, aExpectedValue[0], michael@0: "FormData by 'id' has been restored correctly"); michael@0: // test xpath michael@0: is(input2.value, aExpectedValue[1], michael@0: "FormData by 'xpath' has been restored correctly"); michael@0: } michael@0: michael@0: // clean up michael@0: gBrowser.removeTab(tab); michael@0: michael@0: // This test might time out if the task fails. michael@0: }).then(aCallback); michael@0: }