TextAreaOperator.java revision 13978:1993af50385d
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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.netbeans.jemmy.operators;
24
25import java.awt.Component;
26import java.awt.Container;
27import java.awt.Dimension;
28import java.awt.TextArea;
29import java.util.Hashtable;
30
31import org.netbeans.jemmy.ComponentChooser;
32import org.netbeans.jemmy.Outputable;
33import org.netbeans.jemmy.TestOut;
34import org.netbeans.jemmy.Timeoutable;
35import org.netbeans.jemmy.Timeouts;
36
37/**
38 * This operator type covers java.awt.textArea component.
39 *
40 * @see org.netbeans.jemmy.Timeouts
41 *
42 * @author Alexandre Iline (alexandre.iline@oracle.com)
43 *
44 */
45public class TextAreaOperator extends TextComponentOperator
46        implements Timeoutable, Outputable {
47
48    /**
49     * Identifier for a "text" property.
50     *
51     * @see #getDump
52     */
53    public static final String TEXT_DPROP = "Text";
54
55    private final static long PUSH_KEY_TIMEOUT = 0;
56    private final static long BETWEEN_KEYS_TIMEOUT = 0;
57    private final static long CHANGE_CARET_POSITION_TIMEOUT = 60000;
58    private final static long TYPE_TEXT_TIMEOUT = 60000;
59
60    private Timeouts timeouts;
61    private TestOut output;
62
63    /**
64     * Constructor.
65     *
66     * @param b The {@code java.awt.TextArea} managed by this instance.
67     */
68    public TextAreaOperator(TextArea b) {
69        super(b);
70    }
71
72    /**
73     * Constructs a TextAreaOperator object.
74     *
75     * @param cont a container
76     * @param chooser a component chooser specifying searching criteria.
77     * @param index an index between appropriate ones.
78     */
79    public TextAreaOperator(ContainerOperator<?> cont, ComponentChooser chooser, int index) {
80        this((TextArea) cont.
81                waitSubComponent(new TextAreaFinder(chooser),
82                        index));
83        copyEnvironment(cont);
84    }
85
86    /**
87     * Constructs a TextAreaOperator object.
88     *
89     * @param cont a container
90     * @param chooser a component chooser specifying searching criteria.
91     */
92    public TextAreaOperator(ContainerOperator<?> cont, ComponentChooser chooser) {
93        this(cont, chooser, 0);
94    }
95
96    /**
97     * Constructor. Waits for a component in a container to show. The component
98     * is identified as the {@code index+1}'th
99     * {@code java.awt.TextArea} that shows, lies below the container in
100     * the display containment hierarchy, and that has the desired text. Uses
101     * cont's timeout and output for waiting and to init this operator.
102     *
103     * @param cont The operator for a container containing the sought for
104     * textArea.
105     * @param text TextArea text.
106     * @param index Ordinal component index. The first component has
107     * {@code index} 0.
108     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
109     */
110    public TextAreaOperator(ContainerOperator<?> cont, String text, int index) {
111        this((TextArea) waitComponent(cont,
112                new TextAreaByTextFinder(text,
113                        cont.getComparator()),
114                index));
115        copyEnvironment(cont);
116    }
117
118    /**
119     * Constructor. Waits for a component in a container to show. The component
120     * is identified as the first {@code java.awt.TextArea} that shows,
121     * lies below the container in the display containment hierarchy, and that
122     * has the desired text. Uses cont's timeout and output for waiting and to
123     * init this operator.
124     *
125     * @param cont The operator for a container containing the sought for
126     * textArea.
127     * @param text TextArea text.
128     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
129     */
130    public TextAreaOperator(ContainerOperator<?> cont, String text) {
131        this(cont, text, 0);
132    }
133
134    /**
135     * Constructor. Waits component in container first. Uses cont's timeout and
136     * output for waiting and to init operator.
137     *
138     * @param cont The operator for a container containing the sought for
139     * textArea.
140     * @param index Ordinal component index.
141     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
142     */
143    public TextAreaOperator(ContainerOperator<?> cont, int index) {
144        this((TextArea) waitComponent(cont,
145                new TextAreaFinder(),
146                index));
147        copyEnvironment(cont);
148    }
149
150    /**
151     * Constructor. Waits component in container first. Uses cont's timeout and
152     * output for waiting and to init operator.
153     *
154     * @param cont The operator for a container containing the sought for
155     * textArea.
156     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
157     */
158    public TextAreaOperator(ContainerOperator<?> cont) {
159        this(cont, 0);
160    }
161
162    /**
163     * Searches TextArea in a container.
164     *
165     * @param cont Container in which to search for the component. The container
166     * lies above the component in the display containment hierarchy. The
167     * containment need not be direct.
168     * @param chooser org.netbeans.jemmy.ComponentChooser implementation,
169     * defining and applying search criteria.
170     * @param index Ordinal component index. The first {@code index} is 0.
171     * @return TextArea instance or null if component was not found.
172     */
173    public static TextArea findTextArea(Container cont, ComponentChooser chooser, int index) {
174        return (TextArea) findComponent(cont, new TextAreaFinder(chooser), index);
175    }
176
177    /**
178     * Searches for the first TextArea in a container.
179     *
180     * @param cont Container in which to search for the component. The container
181     * lies above the component in the display containment hierarchy. The
182     * containment need not be direct.
183     * @param chooser org.netbeans.jemmy.ComponentChooser implementation,
184     * defining and applying search criteria.
185     * @return TextArea instance or null if component was not found.
186     */
187    public static TextArea findTextArea(Container cont, ComponentChooser chooser) {
188        return findTextArea(cont, chooser, 0);
189    }
190
191    /**
192     * Searches TextArea by text.
193     *
194     * @param cont Container to search component in.
195     * @param text TextArea text. If null, contents is not checked.
196     * @param ce Compare text exactly.
197     * @param ccs Compare text case sensitively.
198     * @param index Ordinal component index.
199     * @return TextArea instance or null if component was not found.
200     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
201     */
202    public static TextArea findTextArea(Container cont, String text, boolean ce, boolean ccs, int index) {
203        return findTextArea(cont, new TextAreaByTextFinder(text, new DefaultStringComparator(ce, ccs)), index);
204    }
205
206    /**
207     * Searches TextArea by text.
208     *
209     * @param cont Container to search component in.
210     * @param text TextArea text. If null, contents is not checked.
211     * @param ce Compare text exactly.
212     * @param ccs Compare text case sensitively.
213     * @return TextArea instance or null if component was not found.
214     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
215     */
216    public static TextArea findTextArea(Container cont, String text, boolean ce, boolean ccs) {
217        return findTextArea(cont, text, ce, ccs, 0);
218    }
219
220    /**
221     * Waits TextArea in container.
222     *
223     * @param cont Container to search component in.
224     * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
225     * @param index Ordinal component index.
226     * @return TextArea instance.
227     */
228    public static TextArea waitTextArea(Container cont, ComponentChooser chooser, int index) {
229        return (TextArea) waitComponent(cont, new TextAreaFinder(chooser), index);
230    }
231
232    /**
233     * Waits 0'th TextArea in container.
234     *
235     * @param cont Container to search component in.
236     * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
237     * @return TextArea instance.
238     */
239    public static TextArea waitTextArea(Container cont, ComponentChooser chooser) {
240        return waitTextArea(cont, chooser, 0);
241    }
242
243    /**
244     * Waits TextArea by text.
245     *
246     * @param cont Container to search component in.
247     * @param text TextArea text. If null, contents is not checked.
248     * @param ce Compare text exactly.
249     * @param ccs Compare text case sensitively.
250     * @param index Ordinal component index.
251     * @return TextArea instance.
252     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
253     */
254    public static TextArea waitTextArea(Container cont, String text, boolean ce, boolean ccs, int index) {
255        return waitTextArea(cont, new TextAreaByTextFinder(text, new DefaultStringComparator(ce, ccs)), index);
256    }
257
258    /**
259     * Waits TextArea by text.
260     *
261     * @param cont Container to search component in.
262     * @param text TextArea text. If null, contents is not checked.
263     * @param ce Compare text exactly.
264     * @param ccs Compare text case sensitively.
265     * @return TextArea instance.
266     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
267     */
268    public static TextArea waitTextArea(Container cont, String text, boolean ce, boolean ccs) {
269        return waitTextArea(cont, text, ce, ccs, 0);
270    }
271
272    static {
273        Timeouts.initDefault("TextAreaOperator.PushKeyTimeout", PUSH_KEY_TIMEOUT);
274        Timeouts.initDefault("TextAreaOperator.BetweenKeysTimeout", BETWEEN_KEYS_TIMEOUT);
275        Timeouts.initDefault("TextAreaOperator.ChangeCaretPositionTimeout", CHANGE_CARET_POSITION_TIMEOUT);
276        Timeouts.initDefault("TextAreaOperator.TypeTextTimeout", TYPE_TEXT_TIMEOUT);
277    }
278
279    @Override
280    public void setTimeouts(Timeouts timeouts) {
281        super.setTimeouts(timeouts);
282        this.timeouts = timeouts;
283    }
284
285    @Override
286    public Timeouts getTimeouts() {
287        return timeouts;
288    }
289
290    @Override
291    public void setOutput(TestOut out) {
292        output = out;
293        super.setOutput(output.createErrorOutput());
294    }
295
296    @Override
297    public TestOut getOutput() {
298        return output;
299    }
300
301    @Override
302    public Hashtable<String, Object> getDump() {
303        Hashtable<String, Object> result = super.getDump();
304        result.put(TEXT_DPROP, ((TextArea) getSource()).getText());
305        return result;
306    }
307
308    ////////////////////////////////////////////////////////
309    //Mapping                                             //
310    /**
311     * Maps {@code TextArea.getColumns()} through queue
312     */
313    public int getColumns() {
314        return (runMapping(new MapIntegerAction("getColumns") {
315            @Override
316            public int map() {
317                return ((TextArea) getSource()).getColumns();
318            }
319        }));
320    }
321
322    /**
323     * Maps {@code TextArea.getMinimumSize(int, int)} through queue
324     */
325    public Dimension getMinimumSize(final int i, final int i1) {
326        return (runMapping(new MapAction<Dimension>("getMinimumSize") {
327            @Override
328            public Dimension map() {
329                return ((TextArea) getSource()).getMinimumSize(i, i1);
330            }
331        }));
332    }
333
334    /**
335     * Maps {@code TextArea.getPreferredSize(int, int)} through queue
336     */
337    public Dimension getPreferredSize(final int i, final int i1) {
338        return (runMapping(new MapAction<Dimension>("getPreferredSize") {
339            @Override
340            public Dimension map() {
341                return ((TextArea) getSource()).getPreferredSize(i, i1);
342            }
343        }));
344    }
345
346    /**
347     * Maps {@code TextArea.getRows()} through queue
348     */
349    public int getRows() {
350        return (runMapping(new MapIntegerAction("getRows") {
351            @Override
352            public int map() {
353                return ((TextArea) getSource()).getRows();
354            }
355        }));
356    }
357
358    /**
359     * Maps {@code TextArea.getScrollbarVisibility()} through queue
360     */
361    public int getScrollbarVisibility() {
362        return (runMapping(new MapIntegerAction("getScrollbarVisibility") {
363            @Override
364            public int map() {
365                return ((TextArea) getSource()).getScrollbarVisibility();
366            }
367        }));
368    }
369
370    /**
371     * Maps {@code TextArea.replaceRange(String, int, int)} through queue
372     */
373    public void replaceRange(final String string, final int i, final int i1) {
374        runMapping(new MapVoidAction("replaceRange") {
375            @Override
376            public void map() {
377                ((TextArea) getSource()).replaceRange(string, i, i1);
378            }
379        });
380    }
381
382    /**
383     * Maps {@code TextArea.setColumns(int)} through queue
384     */
385    public void setColumns(final int i) {
386        runMapping(new MapVoidAction("setColumns") {
387            @Override
388            public void map() {
389                ((TextArea) getSource()).setColumns(i);
390            }
391        });
392    }
393
394    /**
395     * Maps {@code TextArea.setRows(int)} through queue
396     */
397    public void setRows(final int i) {
398        runMapping(new MapVoidAction("setRows") {
399            @Override
400            public void map() {
401                ((TextArea) getSource()).setRows(i);
402            }
403        });
404    }
405
406    //End of mapping                                      //
407    ////////////////////////////////////////////////////////
408    /**
409     * Allows to find component by text.
410     */
411    public static class TextAreaByTextFinder implements ComponentChooser {
412
413        String label;
414        StringComparator comparator;
415
416        /**
417         * Constructs TextAreaByTextFinder.
418         *
419         * @param lb a text pattern
420         * @param comparator specifies string comparision algorithm.
421         */
422        public TextAreaByTextFinder(String lb, StringComparator comparator) {
423            label = lb;
424            this.comparator = comparator;
425        }
426
427        /**
428         * Constructs TextAreaByTextFinder.
429         *
430         * @param lb a text pattern
431         */
432        public TextAreaByTextFinder(String lb) {
433            this(lb, Operator.getDefaultStringComparator());
434        }
435
436        @Override
437        public boolean checkComponent(Component comp) {
438            if (comp instanceof TextArea) {
439                if (((TextArea) comp).getText() != null) {
440                    return (comparator.equals(((TextArea) comp).getText(),
441                            label));
442                }
443            }
444            return false;
445        }
446
447        @Override
448        public String getDescription() {
449            return "TextArea with text \"" + label + "\"";
450        }
451    }
452
453    /**
454     * Checks component type.
455     */
456    public static class TextAreaFinder extends Finder {
457
458        /**
459         * Constructs TextAreaFinder.
460         *
461         * @param sf other searching criteria.
462         */
463        public TextAreaFinder(ComponentChooser sf) {
464            super(TextArea.class, sf);
465        }
466
467        /**
468         * Constructs TextAreaFinder.
469         */
470        public TextAreaFinder() {
471            super(TextArea.class);
472        }
473    }
474}
475