xref: /trunk/test/testgui/source/testlib/gui/AppTool.java (revision f76c5999be4cb63182d2cfb1dfe9fb18e91e181d)
1b4d2d410SLiu Zhe /**************************************************************
2b4d2d410SLiu Zhe  *
3b4d2d410SLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4b4d2d410SLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5b4d2d410SLiu Zhe  * distributed with this work for additional information
6b4d2d410SLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7b4d2d410SLiu Zhe  * to you under the Apache License, Version 2.0 (the
8b4d2d410SLiu Zhe  * "License"); you may not use this file except in compliance
9b4d2d410SLiu Zhe  * with the License.  You may obtain a copy of the License at
10b4d2d410SLiu Zhe  *
11b4d2d410SLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12b4d2d410SLiu Zhe  *
13b4d2d410SLiu Zhe  * Unless required by applicable law or agreed to in writing,
14b4d2d410SLiu Zhe  * software distributed under the License is distributed on an
15b4d2d410SLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b4d2d410SLiu Zhe  * KIND, either express or implied.  See the License for the
17b4d2d410SLiu Zhe  * specific language governing permissions and limitations
18b4d2d410SLiu Zhe  * under the License.
19b4d2d410SLiu Zhe  *
20b4d2d410SLiu Zhe  *************************************************************/
21b4d2d410SLiu Zhe 
22b4d2d410SLiu Zhe package testlib.gui;
23b4d2d410SLiu Zhe 
24b4d2d410SLiu Zhe import static org.openoffice.test.common.Testspace.*;
25b4d2d410SLiu Zhe import static testlib.gui.UIMap.*;
26b4d2d410SLiu Zhe 
27b4d2d410SLiu Zhe import org.openoffice.test.common.Condition;
28b4d2d410SLiu Zhe import org.openoffice.test.common.FileUtil;
29b4d2d410SLiu Zhe import org.openoffice.test.common.SystemUtil;
30b4d2d410SLiu Zhe import org.openoffice.test.common.Testspace;
31b4d2d410SLiu Zhe import org.openoffice.test.vcl.Tester;
32b4d2d410SLiu Zhe import org.openoffice.test.vcl.widgets.VclWindow;
33b4d2d410SLiu Zhe 
34b4d2d410SLiu Zhe public class AppTool extends Tester {
35b4d2d410SLiu Zhe     static {
36b4d2d410SLiu Zhe         Testspace.getFile("temp").mkdirs();
37b4d2d410SLiu Zhe         // TODO move these shortcut into a file
38b4d2d410SLiu Zhe         setCustomizedShortcut("copy", "ctrl", "c");
39b4d2d410SLiu Zhe         setCustomizedShortcut("select_all", "ctrl", "a");
40b4d2d410SLiu Zhe         setCustomizedShortcut("paste", "ctrl", "v");
41b4d2d410SLiu Zhe         setCustomizedShortcut("cut", "ctrl", "x");
42b4d2d410SLiu Zhe         setCustomizedShortcut("startcenter", "ctrl", "n");
43b4d2d410SLiu Zhe         setCustomizedShortcut("find", "ctrl", "f");
44b4d2d410SLiu Zhe         setCustomizedShortcut("undo", "ctrl", "z");
45b4d2d410SLiu Zhe         setCustomizedShortcut("redo", "ctrl", "y");
46b4d2d410SLiu Zhe         if (SystemUtil.isMac()) {
47b4d2d410SLiu Zhe             setCustomizedShortcut("copy", "command", "c");
48b4d2d410SLiu Zhe             setCustomizedShortcut("select_all", "command", "a");
49b4d2d410SLiu Zhe             setCustomizedShortcut("paste", "command", "v");
50b4d2d410SLiu Zhe             setCustomizedShortcut("cut", "command", "x");
51b4d2d410SLiu Zhe             setCustomizedShortcut("startcenter", "command", "n");
52b4d2d410SLiu Zhe             setCustomizedShortcut("find", "command", "f");
53b4d2d410SLiu Zhe             setCustomizedShortcut("undo", "command", "z");
54b4d2d410SLiu Zhe             setCustomizedShortcut("redo", "command", "shift", "z");
55b4d2d410SLiu Zhe         } else if (SystemUtil.isLinux()) {
56b4d2d410SLiu Zhe 
57b4d2d410SLiu Zhe         }
58b4d2d410SLiu Zhe     }
59b4d2d410SLiu Zhe 
60b4d2d410SLiu Zhe     public static void newTextDocument() {
61b4d2d410SLiu Zhe         app.dispatch("private:factory/swriter");
62b4d2d410SLiu Zhe         writer.waitForExistence(10, 2);
63b4d2d410SLiu Zhe         sleep(1);
64b4d2d410SLiu Zhe     }
65b4d2d410SLiu Zhe 
66b4d2d410SLiu Zhe     public static void newSpreadsheet() {
67b4d2d410SLiu Zhe         app.dispatch("private:factory/scalc");
68b4d2d410SLiu Zhe         calc.waitForExistence(10, 2);
69b4d2d410SLiu Zhe         sleep(1);
70b4d2d410SLiu Zhe     }
71b4d2d410SLiu Zhe 
72b4d2d410SLiu Zhe     public static void newPresentation() {
73b4d2d410SLiu Zhe         app.dispatch("private:factory/simpress");
74b4d2d410SLiu Zhe         impress.waitForExistence(10, 2);
75b4d2d410SLiu Zhe         sleep(1);
76b4d2d410SLiu Zhe     }
77b4d2d410SLiu Zhe 
78b4d2d410SLiu Zhe     public static void newDrawing() {
79b4d2d410SLiu Zhe         app.dispatch("private:factory/sdraw");
80b4d2d410SLiu Zhe         draw.waitForExistence(10, 2);
81b4d2d410SLiu Zhe         sleep(1);
82b4d2d410SLiu Zhe     }
83b4d2d410SLiu Zhe 
84b4d2d410SLiu Zhe     public static void newFormula() {
85b4d2d410SLiu Zhe         app.dispatch("private:factory/smath");
86b4d2d410SLiu Zhe         mathEditWindow.waitForExistence(10, 2);
87b4d2d410SLiu Zhe         sleep(1);
88b4d2d410SLiu Zhe     }
89b4d2d410SLiu Zhe 
90b4d2d410SLiu Zhe     public static void open(String path) {
91b4d2d410SLiu Zhe         app.dispatch(".uno:Open");
92b4d2d410SLiu Zhe         submitOpenDlg(getPath(path));
93b4d2d410SLiu Zhe     }
94b4d2d410SLiu Zhe 
95b4d2d410SLiu Zhe     public static void saveAs(String path) {
96b4d2d410SLiu Zhe         app.dispatch(".uno:SaveAs");
97b4d2d410SLiu Zhe         submitSaveDlg(getPath(path));
98b4d2d410SLiu Zhe         if (alienFormatDlg.exists(3))
99b4d2d410SLiu Zhe             alienFormatDlg.ok();
100*f76c5999SHerbert Dürr         if( activeMsgBox.exists(1)) {
101*f76c5999SHerbert Dürr             String msg = activeMsgBox.getMessage();
102*f76c5999SHerbert Dürr             // #i123142# confirm overwriting of test files
103*f76c5999SHerbert Dürr             if( msg.indexOf( "overwrite changes") >= 0)
104*f76c5999SHerbert Dürr                 activeMsgBox.yes();
105*f76c5999SHerbert Dürr         }
106*f76c5999SHerbert Dürr         app.waitSlot(5 * 60); // 5 minutes
107b4d2d410SLiu Zhe     }
108b4d2d410SLiu Zhe 
109b4d2d410SLiu Zhe     public static void close() {
110b4d2d410SLiu Zhe         app.dispatch(".uno:CloseDoc");
111b4d2d410SLiu Zhe     }
112b4d2d410SLiu Zhe 
113c37bcbf4SLi Feng Wang     public static void saveAndReopen(String path){
114c37bcbf4SLi Feng Wang         saveAs(path);
115c37bcbf4SLi Feng Wang         close();
116c37bcbf4SLi Feng Wang         open(path);
117c37bcbf4SLi Feng Wang     }
118c37bcbf4SLi Feng Wang 
119b4d2d410SLiu Zhe     public static void discard() {
120b4d2d410SLiu Zhe         app.dispatch(".uno:CloseDoc");
121b4d2d410SLiu Zhe         if (activeMsgBox.exists(2))
122b4d2d410SLiu Zhe             activeMsgBox.no();
123b4d2d410SLiu Zhe     }
124b4d2d410SLiu Zhe 
125b4d2d410SLiu Zhe     public static void typeKeys(String keys) {
126b4d2d410SLiu Zhe         Tester.typeKeys(keys);
127b4d2d410SLiu Zhe     }
128b4d2d410SLiu Zhe 
129b4d2d410SLiu Zhe     public static void openStartcenter() {
130b4d2d410SLiu Zhe         if (startcenter.exists())
131b4d2d410SLiu Zhe             return;
132b4d2d410SLiu Zhe 
133b4d2d410SLiu Zhe         if (SystemUtil.isMac()) {
134b4d2d410SLiu Zhe             SystemUtil.execScript("osascript -e 'tell app \"OpenOffice.org\" to activate'");
135b4d2d410SLiu Zhe             typeKeys("<command n>");
136b4d2d410SLiu Zhe         }
137b4d2d410SLiu Zhe 
138b4d2d410SLiu Zhe     }
139b4d2d410SLiu Zhe 
140b4d2d410SLiu Zhe     public static String copyAll() {
141b4d2d410SLiu Zhe         app.setClipboard(".d.i.r.t.y.");
142b4d2d410SLiu Zhe         try {
143b4d2d410SLiu Zhe             app.dispatch(".uno:SelectAll");
144b4d2d410SLiu Zhe         } catch (Exception e) {
145b4d2d410SLiu Zhe             app.dispatch(".uno:Select");
146b4d2d410SLiu Zhe         }
147b4d2d410SLiu Zhe         app.dispatch(".uno:Copy");
148b4d2d410SLiu Zhe         return app.getClipboard();
149b4d2d410SLiu Zhe     }
150b4d2d410SLiu Zhe 
151b4d2d410SLiu Zhe     public static void submitOpenDlg(String path) {
152b4d2d410SLiu Zhe         filePickerPath.setText(path);
153b4d2d410SLiu Zhe         filePickerOpen.click();
154b4d2d410SLiu Zhe     }
155b4d2d410SLiu Zhe 
156b4d2d410SLiu Zhe     public static void submitSaveDlg(String path) {
157b4d2d410SLiu Zhe         fileSavePath.setText(path);
158b4d2d410SLiu Zhe         String extName = FileUtil.getFileExtName(path).toLowerCase();
159b4d2d410SLiu Zhe         String[] filters = fileSaveFileType.getItemsText();
160b4d2d410SLiu Zhe         int i = 0;
161b4d2d410SLiu Zhe         for (; i < filters.length; i++) {
162b4d2d410SLiu Zhe             String f = filters[i];
163b4d2d410SLiu Zhe             int dotIndex = f.lastIndexOf(".");
164b4d2d410SLiu Zhe             if (dotIndex == -1)
165b4d2d410SLiu Zhe                 continue;
166b4d2d410SLiu Zhe             if (extName.equals(f.substring(dotIndex + 1, f.length() - 1)))
167b4d2d410SLiu Zhe                 break;
168b4d2d410SLiu Zhe         }
169b4d2d410SLiu Zhe         if (i == filters.length)
170b4d2d410SLiu Zhe             throw new RuntimeException("Can't find the supported doc format!");
171b4d2d410SLiu Zhe 
172b4d2d410SLiu Zhe         fileSaveFileType.select(i);
173b4d2d410SLiu Zhe         fileSaveSave.click();
174b4d2d410SLiu Zhe     }
175b4d2d410SLiu Zhe 
176b4d2d410SLiu Zhe     public static void submitSaveDlg(String path, String ext) {
177b4d2d410SLiu Zhe         fileSavePath.setText(path);
178b4d2d410SLiu Zhe         if (ext != null) {
179b4d2d410SLiu Zhe             // change filter
180b4d2d410SLiu Zhe             String[] filters = fileSaveFileType.getItemsText();
181b4d2d410SLiu Zhe             int i = 0;
182b4d2d410SLiu Zhe             for (; i < filters.length; i++) {
183b4d2d410SLiu Zhe                 String f = filters[i];
184b4d2d410SLiu Zhe                 int dotIndex = f.lastIndexOf(".");
185b4d2d410SLiu Zhe                 if (dotIndex == -1)
186b4d2d410SLiu Zhe                     continue;
187b4d2d410SLiu Zhe                 if (ext.equals(f.substring(dotIndex + 1, f.length() - 1)))
188b4d2d410SLiu Zhe                     break;
189b4d2d410SLiu Zhe             }
190b4d2d410SLiu Zhe             if (i == filters.length)
191b4d2d410SLiu Zhe                 throw new RuntimeException("Can't find the supported doc format!");
192b4d2d410SLiu Zhe         }
193b4d2d410SLiu Zhe         fileSaveFileType.click();
194b4d2d410SLiu Zhe     }
195b4d2d410SLiu Zhe 
196b4d2d410SLiu Zhe     public static void handleBlocker(final VclWindow... windows) {
197b4d2d410SLiu Zhe         new Condition() {
198b4d2d410SLiu Zhe             @Override
199b4d2d410SLiu Zhe             public boolean value() {
200b4d2d410SLiu Zhe                 if (activeMsgBox.exists()) {
201b4d2d410SLiu Zhe                     try {
202b4d2d410SLiu Zhe                         activeMsgBox.ok();
203b4d2d410SLiu Zhe                     } catch (Exception e) {
204b4d2d410SLiu Zhe                         try {
205b4d2d410SLiu Zhe                             activeMsgBox.yes();
206b4d2d410SLiu Zhe                         } catch (Exception e1) {
207b4d2d410SLiu Zhe                         }
208b4d2d410SLiu Zhe                     }
209b4d2d410SLiu Zhe                 }
210b4d2d410SLiu Zhe 
211b4d2d410SLiu Zhe                 boolean shown = false;
212b4d2d410SLiu Zhe 
213b4d2d410SLiu Zhe                 for (VclWindow w : windows) {
214b4d2d410SLiu Zhe                     if (w.exists()) {
215b4d2d410SLiu Zhe                         shown = true;
216b4d2d410SLiu Zhe                         break;
217b4d2d410SLiu Zhe                     }
218b4d2d410SLiu Zhe                 }
219b4d2d410SLiu Zhe 
220b4d2d410SLiu Zhe                 if (!shown)
221b4d2d410SLiu Zhe                     return false;
222b4d2d410SLiu Zhe 
223b4d2d410SLiu Zhe                 if (activeMsgBox.exists(2)) {
224b4d2d410SLiu Zhe                     try {
225b4d2d410SLiu Zhe                         activeMsgBox.ok();
226b4d2d410SLiu Zhe                     } catch (Exception e) {
227b4d2d410SLiu Zhe                         try {
228b4d2d410SLiu Zhe                             activeMsgBox.yes();
229b4d2d410SLiu Zhe                         } catch (Exception e1) {
230b4d2d410SLiu Zhe                         }
231b4d2d410SLiu Zhe                     }
232b4d2d410SLiu Zhe                 }
233b4d2d410SLiu Zhe 
234b4d2d410SLiu Zhe                 return true;
235b4d2d410SLiu Zhe             }
236b4d2d410SLiu Zhe 
237b4d2d410SLiu Zhe         }.waitForTrue("Time out wait window to be active.", 120, 2);
238b4d2d410SLiu Zhe     }
239f46d12a6SLiu Zhe 
240f46d12a6SLiu Zhe     public static void insertPicture(String path) {
241f46d12a6SLiu Zhe         app.dispatch(".uno:InsertGraphic");
242f46d12a6SLiu Zhe         submitOpenDlg(getPath(path));
243f46d12a6SLiu Zhe     }
244b4d2d410SLiu Zhe }
245