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: waitForExplicitFinish(); michael@0: michael@0: newWindowWithTabView(onTabViewWindowLoaded, null, 1000, 800); michael@0: } michael@0: michael@0: function onTabViewWindowLoaded(win) { michael@0: let contentWindow = win.document.getElementById("tab-view").contentWindow; michael@0: let [originalTab] = win.gBrowser.visibleTabs; michael@0: michael@0: ok(win.TabView.isVisible(), "Tab View is visible"); michael@0: is(contentWindow.GroupItems.groupItems.length, 1, "There is only one group"); michael@0: let currentActiveGroup = contentWindow.GroupItems.getActiveGroupItem(); michael@0: michael@0: // Create a group michael@0: // Note: 150 x 150 should be larger than the minimum size for a group item michael@0: let firstBox = new contentWindow.Rect(80, 80, 160, 160); michael@0: let firstGroup = new contentWindow.GroupItem([], { bounds: firstBox, michael@0: immediately: true }); michael@0: ok(firstGroup.getBounds().equals(firstBox), "This group got its bounds"); michael@0: michael@0: // Create a second group michael@0: let secondBox = new contentWindow.Rect(80, 280, 160, 160); michael@0: let secondGroup = new contentWindow.GroupItem([], { bounds: secondBox, michael@0: immediately: true }); michael@0: ok(secondGroup.getBounds().equals(secondBox), "This second group got its bounds"); michael@0: michael@0: // A third group is created later, but multiple functions need access to it. michael@0: let thirdGroup = null; michael@0: michael@0: is(secondGroup.getBounds().top - firstGroup.getBounds().bottom, 40, michael@0: "There's currently 40 px between the first group and second group"); michael@0: michael@0: let endGame = function() { michael@0: firstGroup.container.parentNode.removeChild(firstGroup.container); michael@0: firstGroup.close(); michael@0: thirdGroup.container.parentNode.removeChild(thirdGroup.container); michael@0: thirdGroup.close(); michael@0: michael@0: win.close(); michael@0: ok(win.closed, "new window is closed"); michael@0: finish(); michael@0: } michael@0: michael@0: let continueWithPart2 = function() { michael@0: michael@0: ok(firstGroup.getBounds().equals(firstBox), "The first group should still have its bounds"); michael@0: michael@0: // Create a third group michael@0: let thirdBox = new contentWindow.Rect(80, 280, 200, 160); michael@0: thirdGroup = new contentWindow.GroupItem([], { bounds: thirdBox, michael@0: immediately: true }); michael@0: ok(thirdGroup.getBounds().equals(thirdBox), "This third group got its bounds"); michael@0: michael@0: is(thirdGroup.getBounds().top - firstGroup.getBounds().bottom, 40, michael@0: "There's currently 40 px between the first group and third group"); michael@0: michael@0: // Just move it to the left and drop it. michael@0: checkSnap(thirdGroup, 0, 0, contentWindow, function(snapped){ michael@0: ok(!snapped,"Offset: Just move it to the left and drop it"); michael@0: michael@0: // Move the second group up 10 px. It shouldn't snap yet. michael@0: checkSnap(thirdGroup, 0, -10, contentWindow, function(snapped){ michael@0: ok(!snapped,"Offset: Moving up 10 should not snap"); michael@0: michael@0: // Move the second group up 10 px. It now should snap. michael@0: checkSnap(thirdGroup, 0, -10, contentWindow, function(snapped){ michael@0: ok(snapped,"Offset: Moving up 10 again should snap!"); michael@0: endGame(); michael@0: }); michael@0: }); michael@0: }); michael@0: }; michael@0: michael@0: let part1 = function() { michael@0: // Just pick it up and drop it. michael@0: checkSnap(secondGroup, 0, 0, contentWindow, function(snapped){ michael@0: ok(!snapped,"Right under: Just pick it up and drop it"); michael@0: michael@0: // Move the second group up 10 px. It shouldn't snap yet. michael@0: checkSnap(secondGroup, 0, -10, contentWindow, function(snapped){ michael@0: ok(!snapped,"Right under: Moving up 10 should not snap"); michael@0: michael@0: // Move the second group up 10 px. It now should snap. michael@0: checkSnap(secondGroup, 0, -10, contentWindow, function(snapped){ michael@0: ok(snapped,"Right under: Moving up 10 again should snap!"); michael@0: // cheat by removing the second group, so that we disappear immediately michael@0: secondGroup.container.parentNode.removeChild(secondGroup.container); michael@0: secondGroup.close(); michael@0: continueWithPart2(); michael@0: }); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: part1(); michael@0: } michael@0: michael@0: function simulateDragDrop(tabItem, offsetX, offsetY, contentWindow) { michael@0: // enter drag mode michael@0: let dataTransfer; michael@0: michael@0: EventUtils.synthesizeMouse( michael@0: tabItem.container, 1, 1, { type: "mousedown" }, contentWindow); michael@0: let event = contentWindow.document.createEvent("DragEvents"); michael@0: event.initDragEvent( michael@0: "dragenter", true, true, contentWindow, 0, 0, 0, 0, 0, michael@0: false, false, false, false, 1, null, dataTransfer); michael@0: tabItem.container.dispatchEvent(event); michael@0: michael@0: // drag over michael@0: if (offsetX || offsetY) { michael@0: let Ci = Components.interfaces; michael@0: let utils = contentWindow.QueryInterface(Ci.nsIInterfaceRequestor). michael@0: getInterface(Ci.nsIDOMWindowUtils); michael@0: let rect = tabItem.getBounds(); michael@0: for (let i = 1; i <= 5; i++) { michael@0: let left = rect.left + 1 + Math.round(i * offsetX / 5); michael@0: let top = rect.top + 1 + Math.round(i * offsetY / 5); michael@0: utils.sendMouseEvent("mousemove", left, top, 0, 1, 0); michael@0: } michael@0: event = contentWindow.document.createEvent("DragEvents"); michael@0: event.initDragEvent( michael@0: "dragover", true, true, contentWindow, 0, 0, 0, 0, 0, michael@0: false, false, false, false, 0, null, dataTransfer); michael@0: tabItem.container.dispatchEvent(event); michael@0: } michael@0: michael@0: // drop michael@0: EventUtils.synthesizeMouse( michael@0: tabItem.container, 0, 0, { type: "mouseup" }, contentWindow); michael@0: event = contentWindow.document.createEvent("DragEvents"); michael@0: event.initDragEvent( michael@0: "drop", true, true, contentWindow, 0, 0, 0, 0, 0, michael@0: false, false, false, false, 0, null, dataTransfer); michael@0: tabItem.container.dispatchEvent(event); michael@0: } michael@0: michael@0: function checkSnap(item, offsetX, offsetY, contentWindow, callback) { michael@0: let firstTop = item.getBounds().top; michael@0: let firstLeft = item.getBounds().left; michael@0: let onDrop = function() { michael@0: let snapped = false; michael@0: item.container.removeEventListener('drop', onDrop, false); michael@0: if (item.getBounds().top != firstTop + offsetY) michael@0: snapped = true; michael@0: if (item.getBounds().left != firstLeft + offsetX) michael@0: snapped = true; michael@0: callback(snapped); michael@0: }; michael@0: item.container.addEventListener('drop', onDrop, false); michael@0: simulateDragDrop(item, offsetX, offsetY, contentWindow); michael@0: } michael@0: