1 | package org.greenstone.gsdl3.testing;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.util.Map;
|
---|
5 | import java.util.regex.*;
|
---|
6 | import java.awt.Component;
|
---|
7 |
|
---|
8 | // only JUnit and JAssert-Swing, no Selenium in this class
|
---|
9 | import org.junit.Assert;
|
---|
10 | import org.assertj.swing.fixture.*;
|
---|
11 | import org.assertj.swing.timing.Timeout ;
|
---|
12 |
|
---|
13 |
|
---|
14 | // GLI imports
|
---|
15 | import org.greenstone.gatherer.Gatherer;
|
---|
16 | import org.greenstone.gatherer.GathererProg; // main GLI class we'll be testing
|
---|
17 | import org.greenstone.gatherer.Dictionary; // access to display strings
|
---|
18 |
|
---|
19 | /*
|
---|
20 | * Utility class containing helper functions for GLI GUI testing using AsssertJ Swing
|
---|
21 | */
|
---|
22 |
|
---|
23 | public class GSGUITestingUtil
|
---|
24 | {
|
---|
25 | public static final boolean PAUSE_ON = true;
|
---|
26 |
|
---|
27 | public static final int SECOND = 1000; // 1000 ms
|
---|
28 |
|
---|
29 | public static final String DOWNLOAD_PANE = "Download";
|
---|
30 | public static final String GATHER_PANE = "Gather";
|
---|
31 | public static final String ENRICH_PANE = "Enrich";
|
---|
32 | public static final String DESIGN_PANE = "Design";
|
---|
33 | public static final String CREATE_PANE = "Create";
|
---|
34 | public static final String FORMAT_PANE = "Format";
|
---|
35 |
|
---|
36 |
|
---|
37 | /************** NEEDED FOR TESTING *************/
|
---|
38 | // There's now a new method in GLI: GUIManager.setNamesRecursively()
|
---|
39 | // that attempts to recursively call setName() on visible GUI components so that we
|
---|
40 | // can have easier access to those GUI components when testing here through their names
|
---|
41 |
|
---|
42 | /************************ GENERAL *******************************/
|
---|
43 | public static void PAUSE() {
|
---|
44 | PAUSE(5);
|
---|
45 | }
|
---|
46 | public static void PAUSE(int seconds) {
|
---|
47 | if(!PAUSE_ON) return; // ignore calls to PAUSE
|
---|
48 |
|
---|
49 | // wait a couple of seconds again?
|
---|
50 | try{
|
---|
51 | Thread.sleep(seconds*SECOND);
|
---|
52 | } catch(Exception e) {
|
---|
53 | e.printStackTrace();
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | //https://joel-costigliola.github.io/assertj/swing/api/org/assertj/swing/fixture/FrameFixture.html
|
---|
59 | public static void switchToPane(FrameFixture window, String pane) {
|
---|
60 | String paneLabel = Dictionary.get("GUI." + pane); // e.g. GUI.Enrich
|
---|
61 | //JPanelFixture tab = window.panel(pane); // this just gets us the JPanel of controls within
|
---|
62 | JTabbedPaneFixture tab = window.tabbedPane("GUIManager.tab_pane");
|
---|
63 | tab.selectTab(paneLabel); // select tab by its title
|
---|
64 | }
|
---|
65 | public static void openMenuItem(FrameFixture window, String menu, String subMenu) {
|
---|
66 | /*String menuLabel = Dictionary.get("Menu." + menu); // e.g. Menu.File
|
---|
67 | window.menuItem(menuLabel).click(); //JMenuItemFixture.click()
|
---|
68 | String subMenuLabel = Dictionary.get(menuLabel + "_" + subMenu); // e.g. Menu.File_Open
|
---|
69 | window.menuItem(subMenuLabel).click();*/
|
---|
70 | window.menuItem("MenuBar."+menu).click();
|
---|
71 | window.menuItem("MenuBar."+menu+"_"+subMenu).click();
|
---|
72 | }
|
---|
73 |
|
---|
74 | /*
|
---|
75 | public static void stealAnyLock(FrameFixture window) {
|
---|
76 | DialogFixture dialog = window.dialog("LockFileDialog");
|
---|
77 | if(dialog != null) {
|
---|
78 | dialog.button("LockFileDialog.ok_button").click();
|
---|
79 | }
|
---|
80 | }*/
|
---|
81 |
|
---|
82 |
|
---|
83 | // e.g. pane = Enrich,view=collection; pane = Gather, view = workspace (or collection)
|
---|
84 | public static void getFolderPath(String pane, String view, String folderPath) {}
|
---|
85 | public static void getFilePath(String pane, String view, String filePath) {}
|
---|
86 |
|
---|
87 | /**********************FILE MENU*******************************/
|
---|
88 | public static void setPrefs(FrameFixture window, String tab, Map params) {
|
---|
89 | openMenuItem(window, "file", "options");
|
---|
90 |
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * @param toMode must be the dictionary key for Preferences mode.
|
---|
95 | * Choose from Preferences.Mode.Assistant, Preferences.Mode.Librarian, Preferences.Mode.Expert
|
---|
96 | */
|
---|
97 | public static void changeUserMode(FrameFixture window, String toMode) {
|
---|
98 | openMenuItem(window, "file", "options"); // click MenuBar.file -> MenuBar.file_options
|
---|
99 |
|
---|
100 | // switch to Mode tab
|
---|
101 | String paneLabel = Dictionary.get("Preferences.Mode");
|
---|
102 | DialogFixture dialog = window.dialog("Preferences");
|
---|
103 | JTabbedPaneFixture tab = window.tabbedPane("Preferences.tab_pane");
|
---|
104 | tab.selectTab(paneLabel); // select tab by its title
|
---|
105 |
|
---|
106 | String mode = "assistant"; // library assistant
|
---|
107 | if(toMode.contains("xpert")) {
|
---|
108 | mode = "expert";
|
---|
109 | } else if (toMode.endsWith("ibrarian")) {
|
---|
110 | mode = "librarian";
|
---|
111 | }
|
---|
112 | dialog.radioButton("Preferences."+mode+"_mode_radio_button").check(true);
|
---|
113 |
|
---|
114 | // apply and close dialog
|
---|
115 | dialog.button("Preferences.apply_button").click();
|
---|
116 | dialog.button("Preferences.ok_button").click();
|
---|
117 | }
|
---|
118 |
|
---|
119 | public static void exitGLI(FrameFixture window) {
|
---|
120 | openMenuItem(window, "file", "exit");
|
---|
121 | PAUSE(5);
|
---|
122 | }
|
---|
123 |
|
---|
124 | public static void closeCollection(FrameFixture window){
|
---|
125 | // Through componennt names, clicks on MenuBar.file then MenuBar.file_close
|
---|
126 | openMenuItem(window, "file", "close");
|
---|
127 | }
|
---|
128 |
|
---|
129 | public static void deleteCollection(FrameFixture window, String collName) {
|
---|
130 | openMenuItem(window, "file", "delete");
|
---|
131 | DialogFixture dialog = window.dialog("DeleteCollectionPrompt"); // could call window.dialg() too as there will be only one, unless GLI run in debug mode?
|
---|
132 |
|
---|
133 | JListFixture collection_list = dialog.list("DeleteCollectionPrompt.list");
|
---|
134 |
|
---|
135 | Pattern collNameRegex = Pattern.compile(".*"+collName+".*");
|
---|
136 | collection_list.selectItem(collNameRegex);
|
---|
137 |
|
---|
138 | collection_list.requireSelection(collNameRegex); // assert it exists and is selectable. Can't select (or delete) coll if it's open
|
---|
139 |
|
---|
140 | dialog.checkBox("DeleteCollectionPrompt.confirmation").check(true); // check checkbox to be sure to delete
|
---|
141 | dialog.button("DeleteCollectionPrompt.ok_button").click(); // delete button
|
---|
142 |
|
---|
143 | // A close confirmation optionPane dialog appears - click OK in it
|
---|
144 | dialog.optionPane().okButton().click();
|
---|
145 |
|
---|
146 | dialog.button("DeleteCollectionPrompt.close_button").click(); // close deleteColl dialog
|
---|
147 | }
|
---|
148 |
|
---|
149 | public static void saveCollection(FrameFixture window) {
|
---|
150 | openMenuItem(window, "file", "save"); // MenuBar.file -> MenuBar.file_save
|
---|
151 | }
|
---|
152 |
|
---|
153 | public static void createCollection(FrameFixture window,
|
---|
154 | String collTitle, String collDescription, String baseColl)
|
---|
155 | {
|
---|
156 | openMenuItem(window, "file", "new");
|
---|
157 | DialogFixture dialog = window.dialog("col"); // GLI is unable to setName() of this dialog
|
---|
158 | // to NewCollectioNDetailsPrompt. Not sure why.
|
---|
159 |
|
---|
160 | dialog.textBox("NewCollectionDetailsPrompt.title").enterText(collTitle); // JTextField
|
---|
161 | if(collDescription != null && !collDescription.equals("")) {
|
---|
162 | // JTextArea description
|
---|
163 | dialog.textBox("NewCollectionDetailsPrompt.description").enterText(collDescription);
|
---|
164 | }
|
---|
165 |
|
---|
166 | // JComboBox base_collection
|
---|
167 | if(baseColl != null && !baseColl.equals("")) {
|
---|
168 | JComboBoxFixture baseCollBox = dialog.comboBox("NewCollectionDetailsPrompt.base_collection");
|
---|
169 | Pattern collNameRegex = Pattern.compile(".*"+baseColl+".*"); // comboBox should CONTAIN this string
|
---|
170 | baseCollBox.selectItem(collNameRegex);
|
---|
171 | baseCollBox.requireSelection(collNameRegex); // assert the baseColl name exists
|
---|
172 | }
|
---|
173 | dialog.button("NewCollectionDetailsPrompt.create_button").click();
|
---|
174 | }
|
---|
175 |
|
---|
176 | public static void loadCollection(FrameFixture window, String collName) {
|
---|
177 | openMenuItem(window, "file", "open");
|
---|
178 | DialogFixture dialog = window.dialog("OpenCollectionDialog");
|
---|
179 | JListFixture collection_list = dialog.list("OpenCollectionDialog.collection_list");
|
---|
180 |
|
---|
181 | // See section 4.2 of http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
|
---|
182 | // Apparently, testing Pattern "collName" expects it to match in entirety
|
---|
183 | // whereas testing ".*collName.*" will test the string contains this Pattern
|
---|
184 | Pattern collNameRegex = Pattern.compile(".*"+collName+".*");
|
---|
185 |
|
---|
186 | /*String[] items = collection_list.contents();
|
---|
187 | for(String item : items) {
|
---|
188 | System.err.println("@@@ ITEM: " + item);
|
---|
189 | }*/
|
---|
190 |
|
---|
191 |
|
---|
192 | //JListItemFixture collItem = collection_list.item(collNameRegex);
|
---|
193 | //collItem.click();
|
---|
194 | //collection_list.requireSelection(collNameRegex); // assert that the requested collection exists
|
---|
195 | //collItem.doubleClick();
|
---|
196 | collection_list.selectItem(collNameRegex);
|
---|
197 | collection_list.requireSelection(collNameRegex); // assert that the requested collection exists
|
---|
198 |
|
---|
199 |
|
---|
200 | /*String[] selections = collection_list.selection();
|
---|
201 | for(String s : selections) {
|
---|
202 | System.err.println("@@@ opening collection " + s);
|
---|
203 | }*/
|
---|
204 |
|
---|
205 | JListItemFixture selectedColl = collection_list.item(collNameRegex);
|
---|
206 | //collection_list.doubleClick(); // will double-click on last item regardless of what's selected
|
---|
207 | selectedColl.doubleClick();
|
---|
208 |
|
---|
209 | //steal any lock
|
---|
210 | /*
|
---|
211 | DialogFixture dialog = window.dialog("LockFileDialog");
|
---|
212 | if(dialog != null) {
|
---|
213 | dialog.button("LockFileDialog.ok_button").click();
|
---|
214 | }
|
---|
215 | */
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * @param exportColl has to be internal GS collection name, e.g. lucene-jdbm-demo
|
---|
220 | * not public collection name like "Demo Collection".
|
---|
221 | */
|
---|
222 | public static void exportCollection(FrameFixture window, String exportToFormat, String exportColl)
|
---|
223 | {
|
---|
224 | openMenuItem(window, "file", "exportas"); // Clicks MenuBar.file -> MenuBar.file_exportas
|
---|
225 |
|
---|
226 | DialogFixture dialog = window.dialog("ExportAsPrompt");
|
---|
227 | dialog.comboBox("ExportAsPrompt.saveas_combobox").selectItem(exportToFormat);
|
---|
228 |
|
---|
229 | Pattern collNameRegex = Pattern.compile(".*"+exportColl+".*");
|
---|
230 | JListFixture collection_list = dialog.list("ExportAsPrompt.list");
|
---|
231 | collection_list.selectItem(collNameRegex); //JList of collections
|
---|
232 | collection_list.requireSelection(collNameRegex); // check it exists
|
---|
233 |
|
---|
234 | dialog.button("ExportAsPrompt.ok_button").click(); // do export
|
---|
235 |
|
---|
236 | // first we see a progress dialog
|
---|
237 |
|
---|
238 | // Wait for subsequent export complete dialog
|
---|
239 | DialogFixture exportCompleteDialog = window.dialog("SimpleResultDialog", Timeout.timeout(30*SECOND));
|
---|
240 |
|
---|
241 | // non-modal dialog, so give it focus, so we can close it through its Close button
|
---|
242 | exportCompleteDialog.focus();
|
---|
243 |
|
---|
244 | //exportCompleteDialog.button().click();
|
---|
245 | exportCompleteDialog.button("SimpleResultDialog.GLIButton."+Dictionary.get("General.Close")).click(); // it only has a single button: "Close", still want to future proof if more buttons get added by specifying the particular button
|
---|
246 |
|
---|
247 |
|
---|
248 | // Close outer dialog too - cancel doubles as close button now that export is complete
|
---|
249 | dialog.button("ExportAsPrompt.cancel_button").click();
|
---|
250 |
|
---|
251 | File file = new File(System.getenv("GSDLHOME") + File.separator + "tmp" + File.separator + "exported_"+exportColl+"_"+exportToFormat);
|
---|
252 |
|
---|
253 | Assert.assertTrue(file.exists());
|
---|
254 | Assert.assertTrue(file.isDirectory());
|
---|
255 | }
|
---|
256 |
|
---|
257 | /*************** DESIGN ******************************/
|
---|
258 | public static void changeIndexer(String toIndexer) {}
|
---|
259 | public static void changeDB(String toDB) {}
|
---|
260 |
|
---|
261 | public static void configurePartitionIndex(String tab, Map params) {}
|
---|
262 |
|
---|
263 | public static void configurePlugin(String pluginName, Map params) {
|
---|
264 | }
|
---|
265 |
|
---|
266 | public static void configureClassifier(String classifierName, Map params) {}
|
---|
267 |
|
---|
268 | public static void configurePlugout(String plugoutName, Map params) {}
|
---|
269 |
|
---|
270 | // https://www.journaldev.com/1257/java-varargs
|
---|
271 | // https://www.geeksforgeeks.org/variable-arguments-varargs-in-java/
|
---|
272 | public static void keepOnlyPlugins(String ... plug_n) {}
|
---|
273 |
|
---|
274 | public static void removePlugs(String ... plug_n) {}
|
---|
275 |
|
---|
276 | /********************** CREATE PANEL ********************/
|
---|
277 | public static void buildOpenCollection() {}
|
---|
278 | public static void buildOpenCollection(boolean minimalRebuild) {}
|
---|
279 | public static void configureImportOptions(Map params) {}
|
---|
280 | public static void configureBuildOptions(Map params) {}
|
---|
281 |
|
---|
282 | public static void buildOutputContains(String ... n) {}
|
---|
283 |
|
---|
284 | /******************** GATHER PANE ********************/
|
---|
285 | public static void createWorkspaceShortcut(){}
|
---|
286 |
|
---|
287 | // in current open collection
|
---|
288 | public static void createCollectionSubfolder() {}
|
---|
289 |
|
---|
290 | public static void dragNDrop(String workspacePath, String collPath) {}
|
---|
291 |
|
---|
292 | /********************* ENRICH PANE ******************/
|
---|
293 | // docPath can be just docName or collectionSubfolder/Subfolder2/docName
|
---|
294 | public static void addMeta(String docPath, Map metanamesToValues) {}
|
---|
295 | public static void addFolderLevelMeta(String folderPath, Map metanamesToValues) {}
|
---|
296 |
|
---|
297 | /********************* DOWNLOAD PANE ******************/
|
---|
298 | // TODO: more functions needed here: e.g. serverInfo?
|
---|
299 | public static void download(String downloader, Map params) {}
|
---|
300 | public static void clearCache() {}
|
---|
301 | public static String serverInfo() {
|
---|
302 | return "";
|
---|
303 | }
|
---|
304 |
|
---|
305 | /********************* DOWNLOAD PANE ******************/
|
---|
306 | public static void formatGeneral(){}
|
---|
307 | public static void formatSearch() {}
|
---|
308 | public static void formatFeature(String featureName, String filename) {}
|
---|
309 | public static boolean isFormatFeatureXMLValid() {
|
---|
310 | return true;
|
---|
311 | }
|
---|
312 |
|
---|
313 | // Moving these working bits of code here, in case I can use them to right
|
---|
314 | // general get methods that make use of the GenericTypeMatcher method of accessing components
|
---|
315 | // See https://joel-costigliola.github.io/assertj/assertj-swing-lookup.html
|
---|
316 |
|
---|
317 | /*window = findFrame(new GenericTypeMatcher<JFrame>(JFrame.class) {
|
---|
318 | protected boolean isMatching(JFrame window) {
|
---|
319 | return window.getTitle().trim().startsWith(expectedWindowTitle)
|
---|
320 | && window.isShowing();
|
---|
321 | }
|
---|
322 | }).using(robot());
|
---|
323 | */
|
---|
324 | /*
|
---|
325 | JTabbedPaneFixture tab = window.tabbedPane(new GenericTypeMatcher<JTabbedPane>(JTabbedPane.class) {
|
---|
326 | @Override protected boolean isMatching(JTabbedPane tabPane) {
|
---|
327 | System.err.println("### trying for match");
|
---|
328 | //int index = GuiActionRunner.execute(() -> tabPane.getSelectedIndex());
|
---|
329 | int index = tabPane.getSelectedIndex();
|
---|
330 | String selectedTabTitle = tabPane.getTitleAt(index); //GuiActionRunner.execute(() -> tabPane.getTitleAt(index));
|
---|
331 | System.err.println("### GOT TITLE: " + selectedTabTitle);
|
---|
332 | return gatherPaneLabel.equals(selectedTabTitle);
|
---|
333 | }
|
---|
334 | });
|
---|
335 | */
|
---|
336 | }
|
---|