Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
KeyboardFactory |
|
| 1.5555555555555556;1.556 |
1 | /******************************************************************************* | |
2 | * Copyright (c) 2008 Ketan Padegaonkar and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Ketan Padegaonkar - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.swtbot.swt.finder.keyboard; | |
12 | ||
13 | import java.awt.Robot; | |
14 | ||
15 | import org.eclipse.swt.widgets.Display; | |
16 | import org.eclipse.swt.widgets.Event; | |
17 | import org.eclipse.swt.widgets.Widget; | |
18 | import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; | |
19 | import org.hamcrest.SelfDescribing; | |
20 | ||
21 | /** | |
22 | * Creates a keyboard using the specified strategy. | |
23 | * | |
24 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
25 | * @version $Id$ | |
26 | */ | |
27 | public class KeyboardFactory { | |
28 | ||
29 | final Class<?> strategyClass; | |
30 | ||
31 | /** | |
32 | * @param strategyClass the name of the class that is an instance of {@link KeyboardStrategy}. | |
33 | * @throws Exception if the class cannot be instantiated. | |
34 | */ | |
35 | public KeyboardFactory(String strategyClass) throws Exception { | |
36 | 7 | this(Class.forName(strategyClass)); |
37 | 7 | } |
38 | ||
39 | /** | |
40 | * @param strategyClass the class representing an instance of {@link KeyboardStrategy}. | |
41 | * @throws Exception if the class cannot be instantiated. | |
42 | */ | |
43 | 7 | public KeyboardFactory(Class<?> strategyClass) throws Exception { |
44 | 7 | this.strategyClass = strategyClass; |
45 | 7 | createStrategy(); |
46 | 7 | } |
47 | ||
48 | /** | |
49 | * @param widget the widget on which the mock events are typed. | |
50 | * @param description the description of the widget. | |
51 | * @return the keyboard configured with the specified strategy. | |
52 | */ | |
53 | public Keyboard keyboard(Widget widget, SelfDescribing description) { | |
54 | 4 | return new Keyboard(strategy(widget, description)); |
55 | } | |
56 | ||
57 | private KeyboardStrategy strategy(Widget widget, SelfDescribing description) { | |
58 | try { | |
59 | 4 | KeyboardStrategy strategy = createStrategy(); |
60 | 4 | strategy.init(widget, description); |
61 | 4 | return strategy; |
62 | 0 | } catch (Exception e) { |
63 | 0 | throw new RuntimeException(e); |
64 | } | |
65 | } | |
66 | ||
67 | private KeyboardStrategy createStrategy() throws Exception { | |
68 | 11 | return (KeyboardStrategy) strategyClass.newInstance(); |
69 | } | |
70 | ||
71 | /** | |
72 | * @param widget the widget on which the mock events are typed. | |
73 | * @param description the description of the widget. | |
74 | * @return the default keyboard as defined by {@link SWTBotPreferences#KEYBOARD_STRATEGY}. | |
75 | */ | |
76 | public static Keyboard getDefaultKeyboard(Widget widget, SelfDescribing description) { | |
77 | try { | |
78 | 4 | return new KeyboardFactory(SWTBotPreferences.KEYBOARD_STRATEGY).keyboard(widget, description); |
79 | 0 | } catch (Exception e) { |
80 | 0 | return getAWTKeyboard(); |
81 | } | |
82 | } | |
83 | ||
84 | /** | |
85 | * Creates a keyboard that creates mock events directly pumped to the widget. | |
86 | * | |
87 | * @param widget the widget on which the mock events are typed. | |
88 | * @param description the description of the widget. | |
89 | * @return a keyboard | |
90 | */ | |
91 | public static Keyboard getMockKeyboard(Widget widget, SelfDescribing description) { | |
92 | 0 | MockKeyboardStrategy strategy = new MockKeyboardStrategy(); |
93 | 0 | strategy.init(widget, description); |
94 | 0 | return new Keyboard(strategy); |
95 | } | |
96 | ||
97 | /** | |
98 | * Creates a keyboard that uses AWT {@link Robot} to press keys. | |
99 | * | |
100 | * @return a keyboard. | |
101 | */ | |
102 | public static Keyboard getAWTKeyboard() { | |
103 | 0 | return new Keyboard(new AWTKeyboardStrategy()); |
104 | } | |
105 | ||
106 | /** | |
107 | * Creates a keyboard that uses {@link Display#post(Event)} to press keys. | |
108 | * | |
109 | * @return a keyboard. | |
110 | */ | |
111 | public static Keyboard getSWTKeyboard() { | |
112 | 0 | return new Keyboard(new SWTKeyboardStrategy()); |
113 | } | |
114 | ||
115 | } |