/******************************************************************************* * Copyright (c) 2007-2009 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html * * Contributor: * Red Hat, Inc. - initial API and implementation ******************************************************************************/ package org.jboss.tools.jsf.vpe.jsf.test.jbide; import java.io.IOException; import java.lang.reflect.Field; import junit.framework.Assert; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.part.FileEditorInput; import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; import org.jboss.tools.jsf.vpe.jsf.test.JsfAllTests; import org.jboss.tools.jst.jsp.jspeditor.JSPMultiPageEditor; import org.jboss.tools.vpe.dnd.VpeDnD; import org.jboss.tools.vpe.editor.VpeController; import org.jboss.tools.vpe.editor.VpeEditorPart; import org.jboss.tools.vpe.editor.mozilla.MozillaEditor; import org.jboss.tools.vpe.editor.mozilla.MozillaEventAdapter; import org.jboss.tools.vpe.ui.test.TestUtil; import org.jboss.tools.vpe.ui.test.VpeTest; import org.jboss.tools.vpe.xulrunner.editor.XulRunnerVpeUtils; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.mozilla.interfaces.nsIDOMElement; import org.mozilla.interfaces.nsIDOMEvent; import org.mozilla.interfaces.nsIDOMEventTarget; import org.mozilla.interfaces.nsIDOMMouseEvent; import org.mozilla.interfaces.nsIDOMNSHTMLElement; import org.mozilla.interfaces.nsIDOMNSUIEvent; import org.mozilla.interfaces.nsIDOMNode; import org.mozilla.interfaces.nsIDragService; import org.mozilla.interfaces.nsIDragSession; import org.mozilla.interfaces.nsIScriptableRegion; import org.mozilla.interfaces.nsISupportsArray; import org.w3c.dom.Element; import org.w3c.dom.Node; import static org.mockito.Mockito.*; //import static org.powermock.api.mockito.PowerMockito.*; import static org.mockito.Matchers.*; /** * Test for JIRA Issue JBIDE-5042 ( https://jira.jboss.org/jira/browse/JBIDE-5042 ): * "Enhance DnD support in VPE" * * @author yradtsevich */ public class DnD_JBIDE5042Test extends VpeTest { private static final String DRAG_ICON_ID = "dragIcon"; private static final String DROPABLE_ID = "dropable"; //$NON-NLS-1$ private static final String DRAGGABLE_ID = "draggable"; //$NON-NLS-1$ private static final String TAGGED_DIV_CONTENT = "tagged div content"; //$NON-NLS-1$ private static final String TEST_PAGE_NAME = "JBIDE/5042/JBIDE-5042.html"; //$NON-NLS-1$ public DnD_JBIDE5042Test(String name) { super(name); } /** * Try to open two pages in VPE and refresh them n times. */ public void testDnD() throws Throwable { setException(null); JSPMultiPageEditor editor = openPageInVpe(TEST_PAGE_NAME); MozillaEditor visualEditor = ((VpeEditorPart) editor.getVisualEditor()) .getVisualEditor(); VpeController controller = TestUtil.getVpeController(editor); nsIDragService dragService = mock(nsIDragService.class); nsIDragSession dragSession = mock(nsIDragSession.class); when(dragService.getCurrentSession()).thenReturn(dragSession); when(dragSession.getSourceDocument()).thenReturn(visualEditor.getDomDocument()); replaceDragService(controller.getVpeDnD(), dragService); TestUtil.waitForJobs(); Element draggable = findSourceElementById(controller, DRAGGABLE_ID); setSelectedNode(controller, draggable); TestUtil.waitForJobs(); final nsIDOMMouseEvent mouseDownEvent = mock(nsIDOMMouseEvent.class); final nsIDOMEventTarget mouseDownEventTarget = mock(nsIDOMEventTarget.class); final nsIDOMNSUIEvent mouseDownNsUIEvent = mock(nsIDOMNSUIEvent.class); final MozillaEventAdapter eventListener = visualEditor.getMozillaEventAdapter(); final nsIDOMElement dragIcon = controller.getXulRunnerEditor() .getDOMDocument().getElementById(DRAG_ICON_ID); when(mouseDownEvent.getType()).thenReturn("mousedown"); when(mouseDownEvent.getButton()).thenReturn(VpeController.LEFT_BUTTON); when(mouseDownEvent.getTarget()).thenReturn(mouseDownEventTarget); when(mouseDownEvent.queryInterface(nsIDOMMouseEvent.NS_IDOMMOUSEEVENT_IID)).thenReturn(mouseDownEvent); when(mouseDownEventTarget.queryInterface(nsIDOMElement.NS_IDOMELEMENT_IID)).thenReturn(dragIcon); when(mouseDownEvent.queryInterface(nsIDOMNSUIEvent.NS_IDOMNSUIEVENT_IID)).thenReturn(mouseDownNsUIEvent); // XXX when(mouseDownNsUIEvent.getPageX()).thenReturn(0); when(mouseDownNsUIEvent.getPageY()).thenReturn(0); //when(mouseDownEvent.stopPropagation()); //when(mouseDownEvent.preventDefault(); final nsIDOMMouseEvent dragOverMouseEvent = mock(nsIDOMMouseEvent.class); final nsIDOMNSUIEvent dragOverNsUIEvent = mock(nsIDOMNSUIEvent.class); when(dragOverMouseEvent.getType()).thenReturn("dragover"); when(dragOverMouseEvent.queryInterface(nsIDOMMouseEvent.NS_IDOMMOUSEEVENT_IID)).thenReturn(dragOverMouseEvent); when(dragOverMouseEvent.queryInterface(nsIDOMNSUIEvent.NS_IDOMNSUIEVENT_IID)).thenReturn(dragOverNsUIEvent); // XXX when(dragOverMouseEvent.getClientX()).thenReturn(150); when(dragOverMouseEvent.getClientY()).thenReturn(10); // XXX when(dragOverNsUIEvent.getPageX()).thenReturn(150); when(dragOverNsUIEvent.getPageY()).thenReturn(10); //when(dragOverMouseEvent.preventDefault()); //when(dragOverMouseEvent.stopPropagation()); doAnswer(new Answer() { public Void answer(InvocationOnMock invocation) throws Throwable { eventListener.handleEvent(dragOverMouseEvent); TestUtil.waitForJobs(); when(dragOverMouseEvent.getType()).thenReturn("dragdrop"); eventListener.handleEvent(dragOverMouseEvent); TestUtil.waitForJobs(); return null; } }).when(dragService).invokeDragSession((nsIDOMNode)any(), (nsISupportsArray)any(), (nsIScriptableRegion)any(), anyLong()); eventListener.handleEvent(mouseDownEvent); TestUtil.waitForJobs(); draggable = findSourceElementById(controller, DRAGGABLE_ID); assertEquals("cell_01", ((Element)draggable.getParentNode()).getAttribute("id")); if (getException() != null) { throw getException(); } } private void replaceDragService(VpeDnD vpeDnD, nsIDragService dragService) throws Throwable { Field dragServiceField = vpeDnD.getClass().getDeclaredField("dragService"); dragServiceField.setAccessible(true); dragServiceField.set(vpeDnD, dragService); } private void checkTaggedDivValue(VpeController controller) { nsIDOMElement taggedDiv = controller.getXulRunnerEditor().getDOMDocument().getElementById(DROPABLE_ID); assertNotNull("taggedDiv should be not null", taggedDiv); //$NON-NLS-1$ nsIDOMNode innerSpan = taggedDiv.getFirstChild(); assertNotNull("taggedDiv should have inner span", innerSpan); //$NON-NLS-1$ nsIDOMNode taggedDivTextNode = innerSpan.getFirstChild(); assertNotNull("taggedDiv should have inner span with text node inside", taggedDivTextNode); //$NON-NLS-1$ String taggedDivTextValue = taggedDivTextNode.getNodeValue(); Assert.assertEquals("body of taggedDiv should be equal to \'" + TAGGED_DIV_CONTENT //$NON-NLS-1$ + "\', but it is \'" + taggedDivTextValue + "\'", //$NON-NLS-1$ //$NON-NLS-2$ taggedDivTextValue, TAGGED_DIV_CONTENT); } private JSPMultiPageEditor openPageInVpe(final String pageName) throws CoreException, PartInitException, IOException { IFile elementPageFile = (IFile) TestUtil.getComponentPath( pageName, JsfAllTests.IMPORT_PROJECT_NAME); IEditorInput input = new FileEditorInput(elementPageFile); JSPMultiPageEditor editor = (JSPMultiPageEditor) PlatformUI.getWorkbench() .getActiveWorkbenchWindow().getActivePage().openEditor(input, EDITOR_ID, true); return editor; } private void setSelectedNode(VpeController controller, Node node) { IndexedRegion sourceNodeBounds = ((IndexedRegion)node); controller.getPageContext().getSourceBuilder().getStructuredTextViewer() .setSelectedRange(sourceNodeBounds.getStartOffset(), sourceNodeBounds.getEndOffset() - sourceNodeBounds.getStartOffset()); } } class MyVpeDnD { private boolean isInnerDragSession() { return true; } }