1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.eclipse.swtbot.swt.finder.keyboard; |
12 | |
|
13 | |
import org.eclipse.jface.bindings.keys.KeyStroke; |
14 | |
import org.eclipse.swt.SWT; |
15 | |
import org.eclipse.swt.widgets.Event; |
16 | |
import org.eclipse.swt.widgets.Widget; |
17 | |
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; |
18 | |
import org.eclipse.swtbot.swt.finder.utils.internal.Assert; |
19 | |
import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot; |
20 | |
import org.hamcrest.SelfDescribing; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 1 | class MockKeyboardStrategy extends AbstractKeyboardStrategy { |
30 | |
|
31 | |
private MyWidget widget; |
32 | |
|
33 | |
public void init(Widget widget, SelfDescribing description) { |
34 | 0 | this.widget = new MyWidget(widget, description); |
35 | 0 | } |
36 | |
|
37 | |
public void pressKeys(KeyStroke... keys) { |
38 | 0 | assertNotDisposed(); |
39 | 0 | widget.notify(SWT.KeyDown, event(keys)); |
40 | 0 | } |
41 | |
|
42 | |
public void releaseKeys(KeyStroke... keys) { |
43 | 0 | assertNotDisposed(); |
44 | 0 | widget.notify(SWT.KeyUp, event(keys)); |
45 | 0 | } |
46 | |
|
47 | |
private Event event(KeyStroke... keys) { |
48 | 0 | int modifiers = SWT.NONE; |
49 | 0 | int ch = 0; |
50 | |
|
51 | 0 | for (KeyStroke key : keys) { |
52 | 0 | modifiers |= key.getModifierKeys(); |
53 | |
} |
54 | |
|
55 | 0 | Event e = new Event(); |
56 | 0 | e.character = Keystrokes.toCharacter(keys); |
57 | 0 | e.widget = widget.widget; |
58 | 0 | e.keyCode = ch; |
59 | 0 | e.stateMask = modifiers; |
60 | 0 | return e; |
61 | |
} |
62 | |
|
63 | |
private void assertNotDisposed() { |
64 | 0 | Assert.isTrue(!widget.widget.isDisposed(), "The widget has been disposed."); |
65 | 0 | } |
66 | |
|
67 | |
public void pressKey(KeyStroke key) { |
68 | 0 | throw new UnsupportedOperationException("This operation is not supported"); |
69 | |
} |
70 | |
|
71 | |
public void releaseKey(KeyStroke key) { |
72 | 0 | throw new UnsupportedOperationException("This operation is not supported"); |
73 | |
} |
74 | |
|
75 | |
private static class MyWidget extends AbstractSWTBot<Widget> { |
76 | |
|
77 | |
public MyWidget(Widget w, SelfDescribing description) throws WidgetNotFoundException { |
78 | 0 | super(w, description); |
79 | 0 | } |
80 | |
|
81 | |
public void notify(int eventType, Event createEvent) { |
82 | 0 | super.notify(eventType, createEvent); |
83 | 0 | } |
84 | |
} |
85 | |
|
86 | |
} |