| /******************************************************************************* |
| * Copyright (c) 2010 SAP AG, Walldorf. |
| * All rights reserved. This program and the accompanying materials |
| * are 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 |
| * |
| * Contributors: |
| * SAP AG - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.platform.discovery.ui.test.comp.internal;
|
|
|
| import java.text.MessageFormat; |
| import java.util.Arrays; |
| import java.util.HashSet; |
| import java.util.Set; |
| |
| import org.easymock.EasyMock; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.NullProgressMonitor; |
| import org.eclipse.jface.viewers.ArrayContentProvider; |
| import org.eclipse.jface.viewers.ILabelProvider; |
| import org.eclipse.jface.viewers.ITreeContentProvider; |
| import org.eclipse.jface.viewers.LabelProvider; |
| import org.eclipse.platform.discovery.core.api.IContributedAction; |
| import org.eclipse.platform.discovery.testutils.utils.abbot.matchers.EnhancedTextMatcher; |
| import org.eclipse.platform.discovery.testutils.utils.junit.util.assertions.Assertions; |
| import org.eclipse.platform.discovery.testutils.utils.junit.util.assertions.ConditionCheckException; |
| import org.eclipse.platform.discovery.testutils.utils.junit.util.assertions.IWaitCondition; |
| import org.eclipse.platform.discovery.ui.api.IFormTextBuilder; |
| import org.eclipse.platform.discovery.ui.api.IGenericViewCustomization; |
| import org.eclipse.platform.discovery.ui.api.IResultsViewAccessor; |
| import org.eclipse.platform.discovery.ui.api.ITooltipProvider; |
| import org.eclipse.platform.discovery.ui.api.impl.GenericViewCustomizationImpl; |
| import org.eclipse.platform.discovery.ui.internal.view.result.impl.DiscoveryTreeViewerFactory; |
| import org.eclipse.platform.discovery.util.api.env.IDiscoveryEnvironment; |
| import org.eclipse.platform.discovery.util.api.env.IErrorHandler; |
| import org.eclipse.platform.discovery.util.api.longop.ILongOperationRunner; |
| import org.eclipse.platform.discovery.util.internal.longop.CurrentThreadOperationRunner; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.graphics.Point; |
| import org.eclipse.swt.graphics.Rectangle; |
| import org.eclipse.swt.layout.FillLayout; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Control; |
| import org.eclipse.swt.widgets.Display; |
| import org.eclipse.swt.widgets.Label; |
| import org.eclipse.swt.widgets.Shell; |
| import org.eclipse.swt.widgets.TreeItem; |
| import org.eclipse.swt.widgets.Widget; |
| |
| import abbot.swt.WidgetLocator; |
| import abbot.swt.finder.generic.MultipleFoundException; |
| import abbot.swt.finder.generic.NotFoundException; |
| import abbot.swt.tester.LabelTester; |
|
|
|
|
| public class DiscoveryTreeViewerFactoryAbbotTest extends DicoveryViewTestFixture
|
| {
|
| private static final String TOOLTIP_TEMPLATE = "Tooltip for {0}";
|
|
|
| private IGenericViewCustomization viewCustomization;
|
| private IResultsViewAccessor vAccessor;
|
|
|
| private Object element1;
|
|
|
| @Override
|
| public Display setUpPDE() throws Exception
|
| {
|
| super.setUpPDE();
|
|
|
| element1 = new Object();
|
|
|
| viewCustomization = createViewCustomization();
|
|
|
| return getDisplay();
|
| }
|
|
|
| private IGenericViewCustomization createViewCustomization()
|
| {
|
| return new GenericViewCustomizationImpl()
|
| {
|
| @Override
|
| public ITreeContentProvider getContentProvider()
|
| {
|
| return new MyContentProvider();
|
| }
|
|
|
| @Override
|
| public ILabelProvider getLabelProvider()
|
| {
|
| return new LabelProvider();
|
| }
|
|
|
| @Override
|
| public ITooltipProvider getTooltipProvider()
|
| {
|
| return new ITooltipProvider()
|
| {
|
| @Override
|
| public void createTooltipContent(IFormTextBuilder tooltipTextBuilder, Object element)
|
| {
|
| tooltipTextBuilder.appendText(MessageFormat.format(TOOLTIP_TEMPLATE, element.toString()));
|
| }
|
| };
|
| }
|
| };
|
| }
|
|
|
| private void openTestView()
|
| {
|
| testShell = createTestShell();
|
| getDisplay().asyncExec(new Runnable()
|
| {
|
| public void run()
|
| {
|
| testShell.setLayout(new FillLayout());
|
| vAccessor = createDiscoveryTreeViewer(testShell);
|
| vAccessor.getTreeViewer().setInput(Arrays.asList(new Object[] { element1 }));
|
| testShell.open();
|
| testShell.layout();
|
| }
|
|
|
| });
|
|
|
| waitForShell(TEST_SHELL_TEXT);
|
| }
|
|
|
| private IResultsViewAccessor createDiscoveryTreeViewer(final Composite parent)
|
| {
|
| final DiscoveryTreeViewerFactory f = new DiscoveryTreeViewerFactory();
|
| final Set<IGenericViewCustomization> viewC = new HashSet<IGenericViewCustomization>();
|
| viewC.add(viewCustomization);
|
|
|
| return f.createTreeViewer(parent, viewC, new HashSet<IContributedAction>(), discoveryEnv());
|
| }
|
|
|
| private IDiscoveryEnvironment discoveryEnv()
|
| {
|
| return new IDiscoveryEnvironment()
|
| {
|
|
|
| @Override
|
| public IErrorHandler errorHandler()
|
| {
|
| return EasyMock.createMock(IErrorHandler.class);
|
| }
|
|
|
| @Override
|
| public ILongOperationRunner operationRunner()
|
| {
|
| return new CurrentThreadOperationRunner(progressMonitor());
|
| }
|
|
|
| @Override
|
| public IProgressMonitor progressMonitor()
|
| {
|
| return new NullProgressMonitor();
|
| }
|
| };
|
| }
|
|
|
| private class MyContentProvider extends ArrayContentProvider implements ITreeContentProvider
|
| {
|
| @Override
|
| public Object[] getChildren(Object parentElement)
|
| {
|
| return getElements(parentElement);
|
| }
|
|
|
| @Override
|
| public Object getParent(Object element)
|
| {
|
| return null;
|
| }
|
|
|
| @Override
|
| public boolean hasChildren(Object element)
|
| {
|
| return getElements(element).length > 0;
|
| }
|
| }
|
|
|
| public void testTooltipIsShowingOnMouseHover() throws NotFoundException, MultipleFoundException
|
| {
|
| openTestView();
|
| final TreeItem ti = treeItemForElement(element1);
|
| treeItemTester.mouseMove(ti, 5, 5);
|
| waitForTooltipVisible(element1);
|
| }
|
|
|
| public void testTooltipIsHiddenOnMoveAway() throws NotFoundException, MultipleFoundException
|
| {
|
| testTooltipIsShowingOnMouseHover();
|
| final TreeItem ti = treeItemForElement(element1);
|
| final Point tiLocation = getWidgetLocation(ti);
|
| final Rectangle tiRect = treeItemTester.getBounds(ti);
|
| treeItemTester.mouseMove(tiLocation.x + tiRect.width + 10, tiLocation.y + tiRect.height + 10);
|
| waitForTooltipInvisible(element1);
|
| }
|
|
|
| private Point getWidgetLocation(final Widget w)
|
| {
|
| final Point[] result = new Point[1];
|
| getDisplay().syncExec(new Runnable()
|
| {
|
| @Override
|
| public void run()
|
| {
|
| result[0] = WidgetLocator.getLocation(w);
|
| }
|
| });
|
|
|
| return result[0];
|
| }
|
|
|
| private TreeItem treeItemForElement(final Object element) throws NotFoundException, MultipleFoundException
|
| {
|
| return (TreeItem) FINDER.find(new EnhancedTextMatcher(element.toString(), TreeItem.class));
|
| }
|
|
|
| private boolean isTooltipVisible(final Object item) throws MultipleFoundException
|
| {
|
| try
|
| {
|
| final Label tooltipLabel = (Label) QUICK_FINDER.find(new EnhancedTextMatcher(item.toString(), Label.class));
|
| final Shell parentShell = parentShell(tooltipLabel);
|
| return LabelTester.getLabelTester().isVisible(tooltipLabel) && ((TESTER.getStyle(parentShell) & SWT.TOOL) > 0); // A tooltip's shell has the SWT.TOOL style
|
| } catch (NotFoundException e)
|
| {
|
| return false;
|
| }
|
| }
|
|
|
| private void waitForTooltipVisible(final Object item)
|
| {
|
| Assertions.waitAssert(new IWaitCondition()
|
| {
|
| @Override
|
| public boolean checkCondition() throws ConditionCheckException
|
| {
|
| try
|
| {
|
| return isTooltipVisible(item);
|
| } catch (MultipleFoundException e)
|
| {
|
| throw new ConditionCheckException(e);
|
| }
|
| }
|
| }, "Tooltip did not show");
|
| }
|
|
|
| private void waitForTooltipInvisible(final Object item)
|
| {
|
| Assertions.waitAssert(new IWaitCondition()
|
| {
|
| @Override
|
| public boolean checkCondition() throws ConditionCheckException
|
| {
|
| try
|
| {
|
| return !isTooltipVisible(item);
|
| } catch (MultipleFoundException e)
|
| {
|
| throw new ConditionCheckException(e);
|
| }
|
| }
|
| }, "Tooltip did not hide");
|
| }
|
|
|
| private Shell parentShell(final Control control)
|
| {
|
| final Control[] parent = new Control[] { null };
|
|
|
| getDisplay().syncExec(new Runnable()
|
| {
|
| @Override
|
| public void run()
|
| {
|
| parent[0] = control.getParent();
|
| }
|
| });
|
| if (parent[0] instanceof Shell)
|
| {
|
| return (Shell) parent[0];
|
| }
|
|
|
| return parentShell(parent[0]);
|
| }
|
| }
|