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, widthBeforeClose, heightBeforeClose; 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,mop"; michael@0: michael@0: function startTest() { michael@0: document.getElementById("Tools:ResponsiveUI").removeAttribute("disabled"); michael@0: mgr.once("on", function() {executeSoon(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: // Menus are correctly updated? michael@0: is(document.getElementById("Tools:ResponsiveUI").getAttribute("checked"), "true", "menus checked"); 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: if (instance._floatingScrollbars) { michael@0: ensureScrollbarsAreFloating(); michael@0: } michael@0: michael@0: instance.transitionsEnabled = false; michael@0: michael@0: testPresets(); michael@0: } michael@0: michael@0: function ensureScrollbarsAreFloating() { michael@0: let body = gBrowser.contentDocument.body; michael@0: let html = gBrowser.contentDocument.documentElement; michael@0: michael@0: let originalWidth = body.getBoundingClientRect().width; michael@0: michael@0: html.style.overflowY = "scroll"; // Force scrollbars michael@0: // Flush. Should not be needed as getBoundingClientRect() should flush, michael@0: // but just in case. michael@0: gBrowser.contentWindow.getComputedStyle(html).overflowY; michael@0: let newWidth = body.getBoundingClientRect().width; michael@0: is(originalWidth, newWidth, "Floating scrollbars are presents"); michael@0: } michael@0: michael@0: function testPresets() { michael@0: function testOnePreset(c) { michael@0: if (c == 0) { michael@0: executeSoon(testCustom); michael@0: return; michael@0: } michael@0: instance.menulist.selectedIndex = c; michael@0: let item = instance.menulist.firstChild.childNodes[c]; michael@0: let [width, height] = extractSizeFromString(item.getAttribute("label")); michael@0: is(content.innerWidth, width, "preset " + c + ": dimension valid (width)"); michael@0: is(content.innerHeight, height, "preset " + c + ": dimension valid (height)"); michael@0: michael@0: testOnePreset(c - 1); michael@0: } michael@0: // Starting from length - 4 because last 3 items are not presets : separator, addbutton and removebutton michael@0: testOnePreset(instance.menulist.firstChild.childNodes.length - 4); michael@0: } michael@0: michael@0: function extractSizeFromString(str) { michael@0: let numbers = str.match(/(\d+)[^\d]*(\d+)/); michael@0: if (numbers) { michael@0: return [numbers[1], numbers[2]]; michael@0: } else { michael@0: return [null, null]; michael@0: } michael@0: } michael@0: michael@0: function testCustom() { michael@0: let initialWidth = content.innerWidth; michael@0: let initialHeight = content.innerHeight; michael@0: michael@0: let x = 2, y = 2; michael@0: EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousedown"}, window); michael@0: x += 20; y += 10; michael@0: EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousemove"}, window); michael@0: EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mouseup"}, window); michael@0: michael@0: let expectedWidth = initialWidth + 20; michael@0: let expectedHeight = initialHeight + 10; michael@0: info("initial width: " + initialWidth); michael@0: info("initial height: " + initialHeight); michael@0: is(content.innerWidth, expectedWidth, "Size correcty updated (width)."); michael@0: is(content.innerHeight, expectedHeight, "Size correcty updated (height)."); michael@0: is(instance.menulist.selectedIndex, 0, "Custom menuitem selected"); michael@0: let [width, height] = extractSizeFromString(instance.menulist.firstChild.firstChild.getAttribute("label")); michael@0: is(width, expectedWidth, "Label updated (width)."); michael@0: is(height, expectedHeight, "Label updated (height)."); michael@0: testCustom2(); michael@0: } michael@0: michael@0: function testCustom2() { michael@0: let initialWidth = content.innerWidth; michael@0: let initialHeight = content.innerHeight; michael@0: michael@0: let x = 2, y = 2; michael@0: EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousedown"}, window); michael@0: x += 23; y += 13; michael@0: EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousemove", shiftKey: true}, window); michael@0: EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mouseup"}, window); michael@0: michael@0: let expectedWidth = initialWidth + 20; michael@0: let expectedHeight = initialHeight + 10; michael@0: is(content.innerWidth, expectedWidth, "with shift: Size correcty updated (width)."); michael@0: is(content.innerHeight, expectedHeight, "with shift: Size correcty updated (height)."); michael@0: is(instance.menulist.selectedIndex, 0, "with shift: Custom menuitem selected"); michael@0: let [width, height] = extractSizeFromString(instance.menulist.firstChild.firstChild.getAttribute("label")); michael@0: is(width, expectedWidth, "Label updated (width)."); michael@0: is(height, expectedHeight, "Label updated (height)."); michael@0: testCustom3(); michael@0: } michael@0: michael@0: function testCustom3() { michael@0: let initialWidth = content.innerWidth; michael@0: let initialHeight = content.innerHeight; michael@0: michael@0: let x = 2, y = 2; michael@0: EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousedown"}, window); michael@0: x += 60; y += 30; michael@0: EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mousemove", ctrlKey: true}, window); michael@0: EventUtils.synthesizeMouse(instance.resizer, x, y, {type: "mouseup"}, window); michael@0: michael@0: let expectedWidth = initialWidth + 10; michael@0: let expectedHeight = initialHeight + 5; michael@0: is(content.innerWidth, expectedWidth, "with ctrl: Size correcty updated (width)."); michael@0: is(content.innerHeight, expectedHeight, "with ctrl: Size correcty updated (height)."); michael@0: is(instance.menulist.selectedIndex, 0, "with ctrl: Custom menuitem selected"); michael@0: let [width, height] = extractSizeFromString(instance.menulist.firstChild.firstChild.getAttribute("label")); michael@0: is(width, expectedWidth, "Label updated (width)."); michael@0: is(height, expectedHeight, "Label updated (height)."); michael@0: michael@0: rotate(); michael@0: } michael@0: michael@0: michael@0: function rotate() { michael@0: let initialWidth = content.innerWidth; michael@0: let initialHeight = content.innerHeight; michael@0: michael@0: info("rotate"); michael@0: instance.rotate(); michael@0: michael@0: is(content.innerWidth, initialHeight, "The width is now the height."); michael@0: is(content.innerHeight, initialWidth, "The height is now the width."); michael@0: let [width, height] = extractSizeFromString(instance.menulist.firstChild.firstChild.getAttribute("label")); michael@0: is(width, initialHeight, "Label updated (width)."); michael@0: is(height, initialWidth, "Label updated (height)."); michael@0: michael@0: widthBeforeClose = content.innerWidth; michael@0: heightBeforeClose = content.innerHeight; michael@0: michael@0: info("XXX BUG 851296: instance.closing: " + !!instance.closing); michael@0: michael@0: mgr.once("off", function() { michael@0: info("XXX BUG 851296: 'off' received."); michael@0: executeSoon(restart); michael@0: }); michael@0: EventUtils.synthesizeKey("VK_ESCAPE", {}); michael@0: } michael@0: michael@0: function restart() { michael@0: info("XXX BUG 851296: restarting."); michael@0: info("XXX BUG 851296: __responsiveUI: " + gBrowser.selectedTab.__responsiveUI); michael@0: mgr.once("on", function() { michael@0: info("XXX BUG 851296: 'on' received."); michael@0: executeSoon(onUIOpen2); michael@0: }); michael@0: //XXX BUG 851296: synthesizeKeyFromKeyTag("key_responsiveUI"); michael@0: mgr.toggle(window, gBrowser.selectedTab); michael@0: info("XXX BUG 851296: restart() finished."); michael@0: } michael@0: michael@0: function onUIOpen2() { michael@0: info("XXX BUG 851296: onUIOpen2."); michael@0: let container = gBrowser.getBrowserContainer(); michael@0: is(container.getAttribute("responsivemode"), "true", "In responsive mode."); michael@0: michael@0: // Menus are correctly updated? michael@0: is(document.getElementById("Tools:ResponsiveUI").getAttribute("checked"), "true", "menus checked"); michael@0: michael@0: is(content.innerWidth, widthBeforeClose, "width restored."); michael@0: is(content.innerHeight, heightBeforeClose, "height restored."); michael@0: michael@0: mgr.once("off", function() {executeSoon(testScreenshot)}); michael@0: EventUtils.synthesizeKey("VK_ESCAPE", {}); michael@0: } michael@0: michael@0: function testScreenshot() { michael@0: let isWinXP = navigator.userAgent.indexOf("Windows NT 5.1") != -1; michael@0: if (isWinXP) { michael@0: // We have issues testing this on Windows XP. michael@0: // See https://bugzilla.mozilla.org/show_bug.cgi?id=848760#c17 michael@0: return finishUp(); michael@0: } michael@0: michael@0: info("screenshot"); michael@0: instance.screenshot("responsiveui"); michael@0: let FileUtils = (Cu.import("resource://gre/modules/FileUtils.jsm", {})).FileUtils; michael@0: michael@0: // while(1) until we find the file. michael@0: // no need for a timeout, the test will get killed anyway. michael@0: info("checking if file exists in 200ms"); michael@0: function checkIfFileExist() { michael@0: let file = FileUtils.getFile("DfltDwnld", [ "responsiveui.png" ]); michael@0: if (file.exists()) { michael@0: ok(true, "Screenshot file exists"); michael@0: file.remove(false); michael@0: finishUp(); michael@0: } else { michael@0: setTimeout(checkIfFileExist, 200); michael@0: } michael@0: } michael@0: checkIfFileExist(); michael@0: } michael@0: michael@0: function finishUp() { michael@0: michael@0: // Menus are correctly updated? michael@0: is(document.getElementById("Tools:ResponsiveUI").getAttribute("checked"), "false", "menu unchecked"); michael@0: michael@0: delete instance; michael@0: gBrowser.removeCurrentTab(); michael@0: finish(); 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 modifiersAttr = key.getAttribute("modifiers"); 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: let modifiers = { michael@0: shiftKey: modifiersAttr.match("shift"), michael@0: ctrlKey: modifiersAttr.match("ctrl"), michael@0: altKey: modifiersAttr.match("alt"), michael@0: metaKey: modifiersAttr.match("meta"), michael@0: accelKey: modifiersAttr.match("accel") michael@0: } michael@0: michael@0: info("XXX BUG 851296: key name: " + name); michael@0: info("XXX BUG 851296: key modifiers: " + JSON.stringify(modifiers)); michael@0: EventUtils.synthesizeKey(name, modifiers); michael@0: } michael@0: }