michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: function test() { michael@0: let instance, deletedPresetA, deletedPresetB, oldPrompt; michael@0: let mgr = ResponsiveUI.ResponsiveUIManager; michael@0: michael@0: waitForExplicitFinish(); michael@0: michael@0: gBrowser.selectedTab = gBrowser.addTab(); michael@0: gBrowser.selectedBrowser.addEventListener("load", function onload() { michael@0: gBrowser.selectedBrowser.removeEventListener("load", onload, true); michael@0: waitForFocus(startTest, content); michael@0: }, true); michael@0: michael@0: content.location = "data:text/html;charset=utf8,test custom presets in responsive mode"; michael@0: michael@0: // This test uses executeSoon() when responsive mode is initialized and when michael@0: // it is destroyed such that we get out of the init/destroy loops. If we try michael@0: // to init/destroy immediately, without waiting for the next loop, we get michael@0: // intermittent test failures. michael@0: michael@0: function startTest() { michael@0: // Mocking prompt michael@0: oldPrompt = Services.prompt; michael@0: Services.prompt = { michael@0: value: "", michael@0: returnBool: true, michael@0: prompt: function(aParent, aDialogTitle, aText, aValue, aCheckMsg, aCheckState) { michael@0: aValue.value = this.value; michael@0: return this.returnBool; michael@0: } michael@0: }; michael@0: michael@0: registerCleanupFunction(() => Services.prompt = oldPrompt); michael@0: michael@0: info("test started, waiting for responsive mode to activate"); michael@0: michael@0: document.getElementById("Tools:ResponsiveUI").removeAttribute("disabled"); michael@0: mgr.once("on", onUIOpen); michael@0: synthesizeKeyFromKeyTag("key_responsiveUI"); michael@0: } michael@0: michael@0: function onUIOpen() { michael@0: // Is it open? michael@0: let container = gBrowser.getBrowserContainer(); michael@0: is(container.getAttribute("responsivemode"), "true", "In responsive mode."); michael@0: michael@0: instance = gBrowser.selectedTab.__responsiveUI; michael@0: ok(instance, "instance of the module is attached to the tab."); michael@0: michael@0: instance.transitionsEnabled = false; michael@0: michael@0: testAddCustomPreset(); michael@0: } michael@0: michael@0: function testAddCustomPreset() { michael@0: // Tries to add a custom preset and cancel the prompt michael@0: let idx = instance.menulist.selectedIndex; michael@0: let presetCount = instance.presets.length; michael@0: michael@0: Services.prompt.value = ""; michael@0: Services.prompt.returnBool = false; michael@0: instance.addbutton.doCommand(); michael@0: michael@0: is(idx, instance.menulist.selectedIndex, "selected item didn't change after add preset and cancel"); michael@0: is(presetCount, instance.presets.length, "number of presets didn't change after add preset and cancel"); michael@0: michael@0: let customHeight = 123, customWidth = 456; michael@0: instance.setSize(customWidth, customHeight); michael@0: michael@0: // Adds the custom preset with "Testing preset" michael@0: Services.prompt.value = "Testing preset"; michael@0: Services.prompt.returnBool = true; michael@0: instance.addbutton.doCommand(); michael@0: michael@0: instance.menulist.selectedIndex = 1; michael@0: michael@0: info("waiting for responsive mode to turn off"); michael@0: mgr.once("off", restart); michael@0: michael@0: // Force document reflow to avoid intermittent failures. michael@0: info("document height " + document.height); michael@0: michael@0: // We're still in the loop of initializing the responsive mode. michael@0: // Let's wait next loop to stop it. michael@0: executeSoon(function() { michael@0: instance.close(); michael@0: }); michael@0: } michael@0: michael@0: function restart() { michael@0: info("Restarting Responsive Mode"); michael@0: mgr.once("on", function() { michael@0: let container = gBrowser.getBrowserContainer(); michael@0: is(container.getAttribute("responsivemode"), "true", "In responsive mode."); michael@0: michael@0: instance = gBrowser.selectedTab.__responsiveUI; michael@0: michael@0: testCustomPresetInList(); michael@0: }); michael@0: michael@0: // We're still in the loop of destroying the responsive mode. michael@0: // Let's wait next loop to start it. michael@0: executeSoon(function() { michael@0: synthesizeKeyFromKeyTag("key_responsiveUI"); michael@0: }); michael@0: } michael@0: michael@0: function testCustomPresetInList() { michael@0: let customPresetIndex = getPresetIndex("456x123 (Testing preset)"); michael@0: ok(customPresetIndex >= 0, "is the previously added preset (idx = " + customPresetIndex + ") in the list of items"); michael@0: michael@0: instance.menulist.selectedIndex = customPresetIndex; michael@0: michael@0: is(content.innerWidth, 456, "add preset, and selected in the list, dimension valid (width)"); michael@0: is(content.innerHeight, 123, "add preset, and selected in the list, dimension valid (height)"); michael@0: michael@0: testDeleteCustomPresets(); michael@0: } michael@0: michael@0: function testDeleteCustomPresets() { michael@0: instance.removebutton.doCommand(); michael@0: michael@0: instance.menulist.selectedIndex = 2; michael@0: deletedPresetA = instance.menulist.selectedItem.getAttribute("label"); michael@0: instance.removebutton.doCommand(); michael@0: michael@0: instance.menulist.selectedIndex = 2; michael@0: deletedPresetB = instance.menulist.selectedItem.getAttribute("label"); michael@0: instance.removebutton.doCommand(); michael@0: michael@0: info("waiting for responsive mode to turn off"); michael@0: mgr.once("off", restartAgain); michael@0: michael@0: // We're still in the loop of initializing the responsive mode. michael@0: // Let's wait next loop to stop it. michael@0: executeSoon(() => instance.close()); michael@0: } michael@0: michael@0: function restartAgain() { michael@0: info("waiting for responsive mode to turn on"); michael@0: mgr.once("on", () => { michael@0: instance = gBrowser.selectedTab.__responsiveUI; michael@0: testCustomPresetsNotInListAnymore(); michael@0: }); michael@0: michael@0: // We're still in the loop of destroying the responsive mode. michael@0: // Let's wait next loop to start it. michael@0: executeSoon(() => synthesizeKeyFromKeyTag("key_responsiveUI")); michael@0: } michael@0: michael@0: function testCustomPresetsNotInListAnymore() { michael@0: let customPresetIndex = getPresetIndex(deletedPresetA); michael@0: is(customPresetIndex, -1, "deleted preset " + deletedPresetA + " is not in the list anymore"); michael@0: michael@0: customPresetIndex = getPresetIndex(deletedPresetB); michael@0: is(customPresetIndex, -1, "deleted preset " + deletedPresetB + " is not in the list anymore"); michael@0: michael@0: executeSoon(finishUp); michael@0: } michael@0: michael@0: function finishUp() { michael@0: delete instance; michael@0: gBrowser.removeCurrentTab(); michael@0: michael@0: finish(); michael@0: } michael@0: michael@0: function getPresetIndex(presetLabel) { michael@0: function testOnePreset(c) { michael@0: if (c == 0) { michael@0: return -1; michael@0: } michael@0: instance.menulist.selectedIndex = c; michael@0: michael@0: let item = instance.menulist.firstChild.childNodes[c]; michael@0: if (item.getAttribute("label") === presetLabel) { michael@0: return c; michael@0: } else { michael@0: return testOnePreset(c - 1); michael@0: } michael@0: } michael@0: return testOnePreset(instance.menulist.firstChild.childNodes.length - 4); michael@0: } michael@0: michael@0: function synthesizeKeyFromKeyTag(aKeyId) { michael@0: let key = document.getElementById(aKeyId); michael@0: isnot(key, null, "Successfully retrieved the node"); michael@0: michael@0: let name = null; michael@0: michael@0: if (key.getAttribute("keycode")) michael@0: name = key.getAttribute("keycode"); michael@0: else if (key.getAttribute("key")) michael@0: name = key.getAttribute("key"); michael@0: michael@0: isnot(name, null, "Successfully retrieved keycode/key"); michael@0: michael@0: key.doCommand(); michael@0: } michael@0: }