Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SWTBotPreferences |
|
| 3.25;3.25 | ||||
SWTBotPreferences$KeyboardLayoutDetector |
|
| 3.25;3.25 |
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 | * Hans Schwaebli - http://swtbot.org/bugzilla/show_bug.cgi?id=108 | |
11 | * Hans Schwaebli - http://swtbot.org/bugzilla/show_bug.cgi?id=112 | |
12 | *******************************************************************************/ | |
13 | package org.eclipse.swtbot.swt.finder.utils; | |
14 | ||
15 | import java.awt.im.InputContext; | |
16 | import java.util.Locale; | |
17 | ||
18 | import org.eclipse.swt.SWT; | |
19 | import org.eclipse.swtbot.swt.finder.SWTBot; | |
20 | import org.eclipse.swtbot.swt.finder.keyboard.Keyboard; | |
21 | import org.eclipse.swtbot.swt.finder.keyboard.KeyboardStrategy; | |
22 | import org.eclipse.swtbot.swt.finder.waits.ICondition; | |
23 | ||
24 | /** | |
25 | * Holds the preferences for the SWT Bot. | |
26 | * | |
27 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
28 | * @version $Id$ | |
29 | * @since 1.1 | |
30 | */ | |
31 | 1 | public class SWTBotPreferences implements SWTBotPreferenceConstants { |
32 | ||
33 | /** | |
34 | * The default key used to match SWT widgets. Defaults to {@code org.eclipse.swtbot.widget.key}. To set another | |
35 | * default use the system property | |
36 | * {@value org.eclipse.swtbot.swt.finder.utils.SWTBotPreferenceConstants#KEY_DEFAULT_KEY}. | |
37 | */ | |
38 | 1 | public static String DEFAULT_KEY = System.getProperty(KEY_DEFAULT_KEY, "org.eclipse.swtbot.widget.key"); |
39 | ||
40 | /** | |
41 | * The timeout for finding widgets among other things. Defaults to 5000ms. To set another default use the system | |
42 | * property {@value org.eclipse.swtbot.swt.finder.utils.SWTBotPreferenceConstants#KEY_TIMEOUT}. | |
43 | */ | |
44 | 1 | public static long TIMEOUT = toLong(System.getProperty(KEY_TIMEOUT, "5000"), 5000); |
45 | ||
46 | /** | |
47 | * The name of the file in which the recorder records. Defaults to "swtbot.record.txt". To set another default, use | |
48 | * the system property {@value org.eclipse.swtbot.swt.finder.utils.SWTBotPreferenceConstants#KEY_RECORER_FILE_NAME}. | |
49 | * | |
50 | * @deprecated - this will be removed in future versions | |
51 | */ | |
52 | 1 | public static String RECORDER_FILE_NAME = System.getProperty(KEY_RECORER_FILE_NAME, "swtbot.record.txt"); |
53 | ||
54 | /** | |
55 | * The speed of playback in milliseconds. Defaults to 0. To set another default, use the system property {@code | |
56 | * org.eclipse.swtbot.playback.delay}. | |
57 | */ | |
58 | 1 | public static long PLAYBACK_DELAY = toLong(System.getProperty(KEY_PLAYBACK_DELAY, "0"), 0); |
59 | ||
60 | /** | |
61 | * The maximum number of screenshots that SWTBot should capture. Defaults to 100. To set another default use the | |
62 | * system property | |
63 | * {@value org.eclipse.swtbot.swt.finder.utils.SWTBotPreferenceConstants#KEY_MAX_ERROR_SCREENSHOT_COUNT}. | |
64 | */ | |
65 | 1 | public static int MAX_ERROR_SCREENSHOT_COUNT = toInt(System.getProperty(KEY_MAX_ERROR_SCREENSHOT_COUNT, "100"), 100); |
66 | ||
67 | /** | |
68 | * The directory in which screenshots should be generated. Defaults to "screenshots". To set another default use the | |
69 | * system property {@value org.eclipse.swtbot.swt.finder.utils.SWTBotPreferenceConstants#KEY_SCREENSHOTS_DIR}. | |
70 | */ | |
71 | 1 | public static String SCREENSHOTS_DIR = System.getProperty(KEY_SCREENSHOTS_DIR, "screenshots"); |
72 | ||
73 | /** | |
74 | * The screenshot image format to be used. Defaults to "jpeg". To set another default use the system property | |
75 | * {@value org.eclipse.swtbot.swt.finder.utils.SWTBotPreferenceConstants#KEY_SCREENSHOT_FORMAT}. The value must be | |
76 | * one these: BMP, GIF, ICO, JPEG, JPG, PNG or TIFF. | |
77 | */ | |
78 | 1 | public static String SCREENSHOT_FORMAT = System.getProperty(KEY_SCREENSHOT_FORMAT, "jpeg"); |
79 | ||
80 | /** | |
81 | * The keyboard layout. This value is autodetected at runtime. This can be set using the system property | |
82 | * {@value org.eclipse.swtbot.swt.finder.utils.SWTBotPreferenceConstants#KEY_KEYBOARD_LAYOUT}. | |
83 | * <p> | |
84 | * <strong>Note:</strong> the layout must be of the form foo.bar.baz.[MAC_][LANGUAGE_][COUNTRY_][VARIANT_] This | |
85 | * expects a file named "foo/bar/baz/[MAC_][LANGUAGE_][COUNTRY_][VARIANT_].keyboard" | |
86 | * </p> | |
87 | * Eg: | |
88 | * <table border="1"> | |
89 | * <tr> | |
90 | * <th align="left"><b>Layout Name</b></th> | |
91 | * <th align="left"><b>Configuration File</b></th> | |
92 | * </tr> | |
93 | * <tr> | |
94 | * <td>com.foo.bar.MAC.EN.US</td> | |
95 | * <td>com/foo/bar/MAC_EN_US.keyboard</td> | |
96 | * </tr> | |
97 | * <tr> | |
98 | * <td>com.foo.bar.MAC.EN_GB</td> | |
99 | * <td>com/foo/bar/MAC_EN_GB.keyboard</td> | |
100 | * </tr> | |
101 | * <tr> | |
102 | * <td>com.foo.bar.FR_FR</td> | |
103 | * <td>com/foo/bar/FR_FR.keyboard</td> | |
104 | * </tr> | |
105 | * <tr> | |
106 | * <td>com.foo.bar.DE</td> | |
107 | * <td>/com/foo/bar/DE.keyboard</td> | |
108 | * </tr> | |
109 | * </table> | |
110 | * | |
111 | * @see Locale | |
112 | */ | |
113 | 2 | public static String KEYBOARD_LAYOUT = System.getProperty(KEY_KEYBOARD_LAYOUT, KeyboardLayoutDetector |
114 | 1 | .detectKeyboard()); |
115 | ||
116 | /** | |
117 | * The the time interval in milliseconds between typing characters in a string. Defaults to 50ms. To set another | |
118 | * default use the system property {@code org.eclipse.swtbot.keyboard.interval}. | |
119 | */ | |
120 | 1 | public static int TYPE_INTERVAL = toInt(System.getProperty(KEY_TYPE_INTERVAL, "50"), 50); |
121 | /** | |
122 | * The default keyboard strategy. Defaults to org.eclipse.swtbot.swt.finder.keyboard.AWTKeyboardStrategy. To set | |
123 | * another default use the system property {@code org.eclipse.swtbot.keyboard.strategy}. This property must be set | |
124 | * to a subclass of {@link KeyboardStrategy}. | |
125 | * | |
126 | * @see KeyboardStrategy | |
127 | * @see Keyboard | |
128 | */ | |
129 | 2 | public static String KEYBOARD_STRATEGY = System.getProperty(KEY_KEYBOARD_STRATEGY, |
130 | 1 | "org.eclipse.swtbot.swt.finder.keyboard.AWTKeyboardStrategy"); |
131 | ||
132 | /** | |
133 | * The default time delay between successive polling while waiting for a condition to be evaluated. Defaults to | |
134 | * 500ms. To set another default use the system property | |
135 | * {@value org.eclipse.swtbot.swt.finder.utils.SWTBotPreferenceConstants#KEY_DEFAULT_POLL_DELAY}. | |
136 | * | |
137 | * @see SWTBot#waitUntil(ICondition, long, long) | |
138 | * @see SWTBot#waitWhile(ICondition, long, long) | |
139 | */ | |
140 | 1 | public static final long DEFAULT_POLL_DELAY = toLong(System.getProperty(KEY_DEFAULT_POLL_DELAY, "500"), 500); |
141 | ||
142 | private static long toLong(String timeoutValue, long defaultValue) { | |
143 | try { | |
144 | 3 | Long timeout = Long.valueOf(timeoutValue); |
145 | 3 | return timeout.longValue(); |
146 | 0 | } catch (Exception e) { |
147 | 0 | return defaultValue; |
148 | } | |
149 | } | |
150 | ||
151 | private static int toInt(String value, int defaultValue) { | |
152 | try { | |
153 | 2 | Integer integerValue = Integer.valueOf(value); |
154 | 2 | return integerValue.intValue(); |
155 | 0 | } catch (Exception e) { |
156 | 0 | return defaultValue; |
157 | } | |
158 | } | |
159 | ||
160 | 0 | public static class KeyboardLayoutDetector { |
161 | public static String detectKeyboard() { | |
162 | 3 | String keyboardLayout = ""; |
163 | 3 | if (isMac()) { |
164 | 0 | keyboardLayout += "MAC_"; |
165 | } | |
166 | ||
167 | 3 | Locale locale = InputContext.getInstance().getLocale(); |
168 | 3 | if (locale == null) |
169 | 0 | locale = Locale.getDefault(); |
170 | ||
171 | 3 | String layout = StringUtils.join(new String[] { locale.getLanguage(), locale.getCountry(), locale.getVariant() }, "_"); |
172 | ||
173 | 3 | layout = layout.replaceAll("\\_\\_", "_"); |
174 | 3 | layout = layout.replaceAll("\\_$", ""); |
175 | ||
176 | 3 | if (layout.equals("")) |
177 | 0 | throw new IllegalStateException("Could not determine keyboard layout."); |
178 | ||
179 | 3 | keyboardLayout += layout.toUpperCase(); |
180 | ||
181 | 3 | return keyboardLayout; |
182 | } | |
183 | ||
184 | private static boolean isMac() { | |
185 | 3 | String swtPlatform = SWT.getPlatform(); |
186 | 3 | return ("carbon".equals(swtPlatform) || "cocoa".equals(swtPlatform)); |
187 | } | |
188 | ||
189 | } | |
190 | } |