Commit | Line | Data |
---|---|---|
ff93f0e2 MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Matthew Khouzam - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared; | |
14 | ||
15 | import java.awt.AWTException; | |
16 | import java.awt.Robot; | |
17 | import java.awt.event.InputEvent; | |
18 | ||
19 | import org.eclipse.swt.graphics.Point; | |
20 | import org.eclipse.swt.widgets.Sash; | |
21 | import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; | |
22 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; | |
23 | import org.eclipse.swtbot.swt.finder.results.Result; | |
24 | import org.eclipse.swtbot.swt.finder.results.VoidResult; | |
25 | import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBotControl; | |
26 | import org.hamcrest.SelfDescribing; | |
27 | ||
28 | /** | |
29 | * SWT Bot sash control | |
30 | */ | |
31 | public class SWTBotSash extends AbstractSWTBotControl<Sash> { | |
32 | ||
33 | /** | |
34 | * The widget wrapper | |
35 | * | |
36 | * @param w | |
37 | * the sash | |
38 | * @param description | |
39 | * the description | |
40 | * @throws WidgetNotFoundException | |
41 | * if there is no widget | |
42 | */ | |
43 | public SWTBotSash(Sash w, SelfDescribing description) throws WidgetNotFoundException { | |
44 | super(w, description); | |
45 | } | |
46 | ||
47 | /** | |
48 | * Get the central point of the sash | |
49 | * | |
50 | * @return the center point, good for dragging | |
51 | */ | |
52 | public Point getPoint() { | |
53 | return UIThreadRunnable.syncExec(new Result<Point>() { | |
54 | ||
55 | @Override | |
56 | public Point run() { | |
57 | return widget.toDisplay(0, widget.getSize().y / 2); | |
58 | } | |
59 | ||
60 | }); | |
61 | } | |
62 | ||
63 | /** | |
64 | * Simulate a drag | |
65 | * | |
66 | * @param dst | |
67 | * to this destination | |
68 | */ | |
69 | public void drag(final Point dst) { | |
70 | final Point src = getPoint(); | |
71 | /* | |
72 | * example of a move | |
73 | * | |
74 | * dn : MouseEvent{Sash {} time=262463957 data=null button=1 | |
75 | * stateMask=0x0 x=5 y=59 count=1} | |
76 | * | |
77 | * move : MouseEvent{Sash {} time=262464038 data=null button=0 | |
78 | * stateMask=0x80000 x=131 y=103 count=0} | |
79 | * | |
80 | * move : MouseEvent{Sash {} time=262464171 data=null button=0 | |
81 | * stateMask=0x80000 x=90 y=116 count=0} | |
82 | * | |
83 | * up : MouseEvent{Sash {} time=262464796 data=null button=1 | |
84 | * stateMask=0x80000 x=5 y=116 count=1} | |
85 | */ | |
86 | try { | |
87 | final Robot awtRobot = new Robot(); | |
88 | ||
89 | // move a maximum of 10 points / event | |
90 | final int magDist = (src.x - dst.x) * (src.x - dst.x) + (src.y - dst.y) * (src.y - dst.y); | |
91 | ||
92 | final int steps = Math.max(1, (int) Math.sqrt(magDist / 100.0)); | |
93 | ||
94 | final int stepX = (dst.x - src.x) / steps; | |
95 | final int stepY = (dst.y - src.y) / steps; | |
96 | syncExec(new VoidResult() { | |
97 | @Override | |
98 | public void run() { | |
99 | awtRobot.mouseMove(src.x, src.y); | |
100 | SWTBotUtils.delay(15); | |
101 | awtRobot.mousePress(InputEvent.BUTTON1_MASK); | |
102 | ||
103 | } | |
104 | }); | |
105 | for (int i = 0; i < steps; i++) { | |
106 | final int index = i; | |
107 | asyncExec(new VoidResult() { | |
108 | @Override | |
109 | public void run() { | |
110 | int x = src.x + index * stepX; | |
111 | int y = src.y + index * stepY; | |
112 | awtRobot.mouseMove(x, y); | |
113 | } | |
114 | }); | |
115 | // drag delay | |
116 | SWTBotUtils.delay(10); | |
117 | } | |
118 | // drop delay | |
119 | SWTBotUtils.delay(100); | |
120 | syncExec(new VoidResult() { | |
121 | @Override | |
122 | public void run() { | |
123 | awtRobot.mouseRelease(InputEvent.BUTTON1_MASK); | |
124 | } | |
125 | }); | |
126 | ||
127 | } catch (final AWTException e) { | |
128 | // log.error(e.getMessage(), e); | |
129 | throw new RuntimeException(e); | |
130 | } | |
131 | } | |
132 | ||
133 | } |