1 /*
2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation. Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package org.netbeans.jemmy.drivers;
26 
27 import org.netbeans.jemmy.EventDispatcher;
28 import org.netbeans.jemmy.JemmyException;
29 import org.netbeans.jemmy.JemmyProperties;
30 import org.netbeans.jemmy.Timeout;
31 import org.netbeans.jemmy.drivers.input.KeyEventDriver;
32 import org.netbeans.jemmy.drivers.input.KeyRobotDriver;
33 import org.netbeans.jemmy.drivers.input.MouseEventDriver;
34 import org.netbeans.jemmy.drivers.input.MouseRobotDriver;
35 
36 /**
37  * Installs drivers for low-level drivers.
38  *
39  * @author Alexandre Iline(alexandre.iline@oracle.com)
40  */
41 public class InputDriverInstaller {
42 
43     Timeout robotAutoDelay;
44     boolean useEventDrivers;
45     boolean smooth = false;
46 
47     /**
48      * Constructs an InputDriverInstaller object.
49      *
50      * @param useEventDrivers Tells whether to use event drivers, otherwise
51      * robot drivers.
52      * @param robotAutoDelay Time for {@code Robot.setAutoDelay(long)}
53      * method.
54      */
InputDriverInstaller(boolean useEventDrivers, Timeout robotAutoDelay)55     public InputDriverInstaller(boolean useEventDrivers, Timeout robotAutoDelay) {
56         this.robotAutoDelay = robotAutoDelay;
57         this.useEventDrivers = useEventDrivers;
58     }
59 
60     /**
61      * Constructs an InputDriverInstaller object. Takes autodelay time from
62      * JemmyProperties' timeouts.
63      *
64      * @param useEventDrivers Tells whether to use event drivers, otherwise
65      * robot drivers.
66      */
InputDriverInstaller(boolean useEventDrivers)67     public InputDriverInstaller(boolean useEventDrivers) {
68         this(useEventDrivers,
69                 JemmyProperties.getCurrentTimeouts().
70                 create("EventDispatcher.RobotAutoDelay"));
71     }
72 
73     /**
74      * Constructs an InputDriverInstaller object. Takes autodelay time from
75      * JemmyProperties' timeouts.
76      *
77      * @param useEventDrivers Tells whether to use event drivers, otherwise
78      * robot drivers.
79      * @param smooth whether to move mouse smoothly.
80      */
InputDriverInstaller(boolean useEventDrivers, boolean smooth)81     public InputDriverInstaller(boolean useEventDrivers, boolean smooth) {
82         this(useEventDrivers);
83         this.smooth = smooth;
84     }
85 
86     /**
87      * Constructs an InputDriverInstaller object. Uses event drivers.
88      *
89      * @param robotAutoDelay Time for {@code Robot.setAutoDelay(long)}
90      * method.
91      */
InputDriverInstaller(Timeout robotAutoDelay)92     public InputDriverInstaller(Timeout robotAutoDelay) {
93         this(true,
94                 robotAutoDelay);
95     }
96 
97     /**
98      * Constructs an InputDriverInstaller object. Takes autodelay time from
99      * JemmyProperties' timeouts. Uses event drivers.
100      */
InputDriverInstaller()101     public InputDriverInstaller() {
102         this(true);
103     }
104 
105     static {
EventDispatcher.performInit()106         EventDispatcher.performInit();
107     }
108 
109     /**
110      * Installs input drivers.
111      */
install()112     public void install() {
113         if (useEventDrivers) {
114             LightDriver keyE = new KeyEventDriver();
115             LightDriver mouseE = new MouseEventDriver();
116             DriverManager.removeDriver(DriverManager.KEY_DRIVER_ID,
117                     keyE.getSupported());
118             DriverManager.removeDriver(DriverManager.MOUSE_DRIVER_ID,
119                     mouseE.getSupported());
120             DriverManager.setDriver(DriverManager.KEY_DRIVER_ID, keyE);
121             DriverManager.setDriver(DriverManager.MOUSE_DRIVER_ID, mouseE);
122             try {
123                 String[] awtOperators
124                         = {
125                             "org.netbeans.jemmy.operators.ButtonOperator",
126                             "org.netbeans.jemmy.operators.CheckboxOperator",
127                             "org.netbeans.jemmy.operators.ChoiceOperator",
128                             "org.netbeans.jemmy.operators.LabelOperator",
129                             "org.netbeans.jemmy.operators.ListOperator",
130                             "org.netbeans.jemmy.operators.ScrollPaneOperator",
131                             "org.netbeans.jemmy.operators.ScrollbarOperator",
132                             "org.netbeans.jemmy.operators.TextAreaOperator",
133                             "org.netbeans.jemmy.operators.TextComponentOperator",
134                             "org.netbeans.jemmy.operators.TextFieldOperator"
135                         };
136                 LightDriver keyR = new KeyRobotDriver(robotAutoDelay, awtOperators);
137                 LightDriver mouseR = new MouseRobotDriver(robotAutoDelay, awtOperators);
138                 DriverManager.removeDriver(DriverManager.KEY_DRIVER_ID,
139                         keyR.getSupported());
140                 DriverManager.removeDriver(DriverManager.MOUSE_DRIVER_ID,
141                         mouseR.getSupported());
142                 DriverManager.setDriver(DriverManager.KEY_DRIVER_ID, keyR);
143                 DriverManager.setDriver(DriverManager.MOUSE_DRIVER_ID, mouseR);
144             } catch (JemmyException e) {
145                 if (!(e.getInnerThrowable() instanceof ClassNotFoundException)) {
146                     throw (e);
147                 }
148             }
149         } else {
150             LightDriver keyR = new KeyRobotDriver(robotAutoDelay);
151             LightDriver mouseR = new MouseRobotDriver(robotAutoDelay, smooth);
152             DriverManager.removeDriver(DriverManager.KEY_DRIVER_ID,
153                     keyR.getSupported());
154             DriverManager.removeDriver(DriverManager.MOUSE_DRIVER_ID,
155                     mouseR.getSupported());
156             DriverManager.setDriver(DriverManager.KEY_DRIVER_ID, keyR);
157             DriverManager.setDriver(DriverManager.MOUSE_DRIVER_ID, mouseR);
158         }
159     }
160 }
161