|
1 /* |
|
2 * Copyright (c) 2018, 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 */ |
|
23 |
|
24 import static com.sun.swingset3.demos.table.TableDemo.COLUMN1_NAME; |
|
25 import static com.sun.swingset3.demos.table.TableDemo.COLUMN2_NAME; |
|
26 import static com.sun.swingset3.demos.table.TableDemo.COLUMN3_NAME; |
|
27 import static com.sun.swingset3.demos.table.TableDemo.COLUMN4_NAME; |
|
28 import static com.sun.swingset3.demos.table.TableDemo.DEMO_TITLE; |
|
29 import static com.sun.swingset3.demos.table.TableDemo.ROW_HEIGHT; |
|
30 import static org.jemmy2ext.JemmyExt.EXACT_STRING_COMPARATOR; |
|
31 |
|
32 import java.util.ArrayList; |
|
33 import java.util.List; |
|
34 |
|
35 import javax.swing.JTable; |
|
36 |
|
37 import org.jtregext.GuiTestListener; |
|
38 import org.netbeans.jemmy.ClassReference; |
|
39 import org.netbeans.jemmy.operators.JCheckBoxOperator; |
|
40 import org.netbeans.jemmy.operators.JFrameOperator; |
|
41 import org.netbeans.jemmy.operators.JTableHeaderOperator; |
|
42 import org.netbeans.jemmy.operators.JTableOperator; |
|
43 import org.netbeans.jemmy.operators.JTextFieldOperator; |
|
44 import org.testng.annotations.Listeners; |
|
45 import org.testng.annotations.Test; |
|
46 |
|
47 import com.sun.swingset3.demos.table.OscarCandidate; |
|
48 import com.sun.swingset3.demos.table.OscarTableModel; |
|
49 import com.sun.swingset3.demos.table.TableDemo; |
|
50 |
|
51 /* |
|
52 * @test |
|
53 * @key headful |
|
54 * @summary Verifies SwingSet3 TableDemo page by checking different properties |
|
55 * of the JTable like number of row, number of columns and actions like |
|
56 * selection of cell, sorting based on column, filtering based on text and |
|
57 * moving of the column |
|
58 * |
|
59 * @library /sanity/client/lib/jemmy/src |
|
60 * @library /sanity/client/lib/Extensions/src |
|
61 * @library /sanity/client/lib/SwingSet3/src |
|
62 * @modules java.desktop |
|
63 * java.logging |
|
64 * @build org.jemmy2ext.JemmyExt |
|
65 * @build com.sun.swingset3.demos.table.TableDemo |
|
66 * @run testng TableDemoTest |
|
67 */ |
|
68 @Listeners(GuiTestListener.class) |
|
69 public class TableDemoTest { |
|
70 |
|
71 private final static int MAX_ROW_COUNT = 524; |
|
72 private final static int MAX_COL_COUNT = 4; |
|
73 |
|
74 private final static String FILTER_TEXT = "Sunrise"; |
|
75 private final static String FILTER_RESET_TEXT = ""; |
|
76 |
|
77 private final static int [] SELECT_ROW_INDICES ={10, 11, 18}; |
|
78 |
|
79 private final static int MOVE_COL_START_INDEX = 1; |
|
80 private final static int MOVE_COL_END_INDEX = 2; |
|
81 private final static String MOVE_COL_VAL_TEXT1 = "Sunrise"; |
|
82 private final static String MOVE_COL_VAL_TEXT2 = "Most Unique Artistic Picture"; |
|
83 private final static int MOVE_COL_VAL_ROW = 0; |
|
84 |
|
85 private final static int SORT_COL = 1; |
|
86 private final static int[] SORT_VAL_ROWS =new int[] {0, 250, 523}; |
|
87 private final static String[][] ASC_PRE_SORT_ROW_VAL = new String[][] { |
|
88 {"1928", "Best Actor", "The Way of All Flesh", "[Emil Jannings]"}, |
|
89 {"1933", "Best Director", "Cavalcade", "[Frank Lloyd]"}, |
|
90 {"1936", "Best Engineering Effects", "My Man Godfrey", "[Eric Hatch, Morris Ryskind]"}}; |
|
91 private final static String[][] ASC_POST_SORT_ROW_VAL = new String[][] { |
|
92 {"1928", "Best Actor", "The Way of All Flesh", "[Emil Jannings]"}, |
|
93 {"1936", "Best Director", "My Man Godfrey", "[Gregory La Cava]"}, |
|
94 {"1928", "Most Unique Artistic Picture", "The Crowd", "[]"}}; |
|
95 private final static String[][] DESC_POST_SORT_ROW_VAL = new String[][] { |
|
96 {"1928", "Most Unique Artistic Picture", "Sunrise", "[]"}, |
|
97 {"1934", "Best Engineering Effects", "Viva Villa!", "[Ben Hecht]"}, |
|
98 {"1936", "Best Actor", "San Francisco", "[Spencer Tracy]"}}; |
|
99 |
|
100 /** |
|
101 * Tests the different properties of JTable like number of rows, number |
|
102 * of columns and actions like selection of cell, sorting based on column, |
|
103 * filtering based on text and moving of the column. |
|
104 * |
|
105 * @throws Exception |
|
106 */ |
|
107 @Test |
|
108 public void test() throws Exception { |
|
109 |
|
110 new ClassReference(TableDemo.class.getCanonicalName()).startApplication(); |
|
111 |
|
112 JFrameOperator frameOperator = new JFrameOperator(DEMO_TITLE); |
|
113 frameOperator.setComparator(EXACT_STRING_COMPARATOR); |
|
114 frameOperator.setVerification(true); |
|
115 JTableOperator tableOperator = new JTableOperator(frameOperator); |
|
116 JTableHeaderOperator tableHeaderOperator = new JTableHeaderOperator(frameOperator); |
|
117 |
|
118 checkTableBasicProperties(tableOperator); |
|
119 checkCellSelection(tableOperator); |
|
120 checkSortTable(tableOperator, tableHeaderOperator); |
|
121 checkMoveColumn(tableOperator, tableHeaderOperator); |
|
122 checkFilterTable(frameOperator, tableOperator); |
|
123 } |
|
124 |
|
125 /** |
|
126 * Verifies the table basic properties number of columns, rows and row height |
|
127 * |
|
128 * @param tableOperator |
|
129 */ |
|
130 private void checkTableBasicProperties(JTableOperator tableOperator) { |
|
131 tableOperator.waitStateOnQueue(comp |
|
132 -> MAX_COL_COUNT == ((JTable)comp).getColumnCount()); |
|
133 waitRowCount(tableOperator, MAX_ROW_COUNT); |
|
134 tableOperator.waitStateOnQueue(comp |
|
135 -> ROW_HEIGHT == ((JTable)comp).getRowHeight()); |
|
136 } |
|
137 |
|
138 /** |
|
139 * Selects one table cell and verifies the selected cell's row number and column number |
|
140 * |
|
141 * @param tableOperator |
|
142 */ |
|
143 private void checkCellSelection(JTableOperator tableOperator) { |
|
144 int noOfColumns = tableOperator.getColumnCount(); |
|
145 for (int i = 0; i < SELECT_ROW_INDICES.length; i++) { |
|
146 int rowIndex = SELECT_ROW_INDICES[i]; |
|
147 for (int j = 0; j < noOfColumns; j++) { |
|
148 int colIndex = j; |
|
149 tableOperator.clickOnCell(rowIndex, colIndex); |
|
150 tableOperator.waitStateOnQueue(comp |
|
151 -> rowIndex == ((JTable)comp).getSelectedRow() && |
|
152 colIndex == ((JTable)comp).getSelectedColumn()); |
|
153 } |
|
154 } |
|
155 } |
|
156 |
|
157 /** |
|
158 * Filter table based on specific text and winners check box, and verifies row count |
|
159 * |
|
160 * @param frameOperator |
|
161 * @param tableOperator |
|
162 */ |
|
163 private void checkFilterTable(JFrameOperator frameOperator, |
|
164 JTableOperator tableOperator) { |
|
165 |
|
166 int [] filterRowCount = getFilteredCount(tableOperator, FILTER_TEXT); |
|
167 JTextFieldOperator filterField = new JTextFieldOperator(frameOperator); |
|
168 JCheckBoxOperator winnersCheckbox = new JCheckBoxOperator(frameOperator); |
|
169 |
|
170 // Filtering based on FILTER_TEXT |
|
171 filterField.setText(FILTER_TEXT); |
|
172 waitRowCount(tableOperator, filterRowCount[0]); |
|
173 |
|
174 // Filtering based on WinnersCheckbox |
|
175 winnersCheckbox.setSelected(true); |
|
176 waitRowCount(tableOperator, filterRowCount[1]); |
|
177 |
|
178 // Resets the winners check box |
|
179 winnersCheckbox.setSelected(false); |
|
180 waitRowCount(tableOperator, filterRowCount[0]); |
|
181 |
|
182 // Resets the filter text field |
|
183 filterField.setText(FILTER_RESET_TEXT); |
|
184 waitRowCount(tableOperator, MAX_ROW_COUNT); |
|
185 |
|
186 } |
|
187 |
|
188 private int[] getFilteredCount(JTableOperator tableOperator, String filterText){ |
|
189 OscarTableModel tableModel = (OscarTableModel)tableOperator.getModel(); |
|
190 int noOfRows = tableModel.getRowCount(); |
|
191 int filteredRowCount = 0; |
|
192 int filteredWinnersRowCount = 0; |
|
193 for (int i = 0; i < noOfRows; i++) { |
|
194 OscarCandidate candidate = tableModel.getCandidate(i); |
|
195 if(isMovieOrPersonsContainsText(candidate, filterText)){ |
|
196 filteredRowCount++; |
|
197 if(candidate.isWinner()) { |
|
198 filteredWinnersRowCount++; |
|
199 } |
|
200 } |
|
201 } |
|
202 return new int[] {filteredRowCount, filteredWinnersRowCount}; |
|
203 } |
|
204 |
|
205 private boolean isMovieOrPersonsContainsText( |
|
206 OscarCandidate candidate, String filterText){ |
|
207 String movie = candidate.getMovieTitle(); |
|
208 if(movie != null && movie.contains(filterText)) { |
|
209 return true; |
|
210 } else { |
|
211 List<String> persons = candidate.getPersons(); |
|
212 for (String person : persons) { |
|
213 if(person != null && person.contains(filterText)) { |
|
214 return true; |
|
215 } |
|
216 } |
|
217 } |
|
218 return false; |
|
219 } |
|
220 |
|
221 /** |
|
222 * Moves to swap the columns, move again to reset back, verify column name |
|
223 * and cell values in both the scenarios. |
|
224 * |
|
225 * @param tableOperator |
|
226 * @param tableHeaderOperator |
|
227 */ |
|
228 private void checkMoveColumn(JTableOperator tableOperator, |
|
229 JTableHeaderOperator tableHeaderOperator) { |
|
230 |
|
231 String[] columnNames = {COLUMN1_NAME, COLUMN3_NAME, COLUMN2_NAME, COLUMN4_NAME}; |
|
232 // Moving the column from 'start index' to 'end index' |
|
233 moveColumn(tableOperator, tableHeaderOperator, columnNames, |
|
234 MOVE_COL_START_INDEX, MOVE_COL_END_INDEX); |
|
235 |
|
236 // Resets the columns to original position(from 'end index' to 'start index') |
|
237 columnNames[1] = COLUMN2_NAME; |
|
238 columnNames[2] = COLUMN3_NAME; |
|
239 moveColumn(tableOperator, tableHeaderOperator, columnNames, |
|
240 MOVE_COL_END_INDEX, MOVE_COL_START_INDEX); |
|
241 } |
|
242 |
|
243 /** |
|
244 * Moves to swap the columns, verify column name and cell values. |
|
245 * |
|
246 * @param tableOperator |
|
247 * @param tableHeaderOperator |
|
248 * @param columnNames |
|
249 * @param moveCol |
|
250 * @param moveToCol |
|
251 */ |
|
252 private void moveColumn(JTableOperator tableOperator, JTableHeaderOperator tableHeaderOperator, |
|
253 String[] columnNames, int moveCol, int moveToCol){ |
|
254 |
|
255 tableHeaderOperator.moveColumn(moveCol, moveToCol); |
|
256 checkColumnNames(tableOperator, columnNames); |
|
257 tableOperator.waitCell(MOVE_COL_VAL_TEXT1, MOVE_COL_VAL_ROW, moveCol); |
|
258 tableOperator.waitCell(MOVE_COL_VAL_TEXT2, MOVE_COL_VAL_ROW, moveToCol); |
|
259 } |
|
260 |
|
261 private void checkColumnNames(JTableOperator tableOperator, String[] columnNames) { |
|
262 for (int i = 0; i < tableOperator.getColumnCount(); i++) { |
|
263 int columnIndex = i; |
|
264 tableOperator.waitStateOnQueue(comp -> columnNames[columnIndex].equals( |
|
265 ((JTable)comp).getColumnModel().getColumn(columnIndex).getHeaderValue())); |
|
266 } |
|
267 } |
|
268 |
|
269 /** |
|
270 * Sorts the table based on one particular column in ascending and descending order, |
|
271 * and verifies cell values |
|
272 * |
|
273 * @param tableOperator |
|
274 * @param tableHeaderOperator |
|
275 */ |
|
276 private void checkSortTable(JTableOperator tableOperator, |
|
277 JTableHeaderOperator tableHeaderOperator) { |
|
278 |
|
279 // Verifying the row values before sort |
|
280 checkTableRows(tableOperator, ASC_PRE_SORT_ROW_VAL); |
|
281 |
|
282 // Getting all award category values before stating the sort |
|
283 // to prepare the expected result |
|
284 ArrayList<String> awardCats = new ArrayList<>(); |
|
285 for (int i = 0; i < tableOperator.getRowCount(); i++) { |
|
286 awardCats.add((String) tableOperator.getValueAt(i, SORT_COL)); |
|
287 } |
|
288 // Sorting awardCats(expected result) in ascending order |
|
289 awardCats.sort((s1, s2) -> s1.compareTo(s2)); |
|
290 |
|
291 // Sorting table based on column 'Award Category' in ascending order |
|
292 sortTable(tableOperator, tableHeaderOperator, awardCats, |
|
293 ASC_POST_SORT_ROW_VAL); |
|
294 |
|
295 // Sorting awardCats(expected result) in descending order |
|
296 awardCats.sort((s1, s2) -> s2.compareTo(s1)); |
|
297 // Sorting table based on column 'Award Category' in descending order |
|
298 sortTable(tableOperator, tableHeaderOperator, awardCats, |
|
299 DESC_POST_SORT_ROW_VAL); |
|
300 |
|
301 } |
|
302 |
|
303 private void checkColumnSorted(JTableOperator tableOperator, |
|
304 ArrayList<String> awardCatExp){ |
|
305 ArrayList<String> awardCatActual = new ArrayList<>(); |
|
306 for (int i = 0; i < tableOperator.getRowCount(); i++) { |
|
307 awardCatActual.add((String) tableOperator.getValueAt(i, SORT_COL)); |
|
308 } |
|
309 tableOperator.waitStateOnQueue(comp -> awardCatExp.equals(awardCatActual)); |
|
310 } |
|
311 |
|
312 private void checkTableRows(JTableOperator tableOperator, String[][] rowValues) { |
|
313 for (int i = 0; i < SORT_VAL_ROWS.length; i++) { |
|
314 tableOperator.waitCell(rowValues[i][0], SORT_VAL_ROWS[i], 0); |
|
315 tableOperator.waitCell(rowValues[i][1], SORT_VAL_ROWS[i], 1); |
|
316 tableOperator.waitCell(rowValues[i][2], SORT_VAL_ROWS[i], 2); |
|
317 tableOperator.waitCell(rowValues[i][3], SORT_VAL_ROWS[i], 3); |
|
318 } |
|
319 } |
|
320 |
|
321 /** |
|
322 * Sorts the table based on one particular column and verifies cell values |
|
323 * |
|
324 * @param tableOperator |
|
325 * @param tableHeaderOperator |
|
326 * @param awardCatExp |
|
327 * @param rowValues |
|
328 */ |
|
329 private void sortTable(JTableOperator tableOperator, JTableHeaderOperator tableHeaderOperator, |
|
330 ArrayList<String> awardCatExp, String[][] rowValues) { |
|
331 |
|
332 tableHeaderOperator.selectColumn(SORT_COL); |
|
333 checkColumnSorted(tableOperator, awardCatExp); |
|
334 // Verifying the row values after sort |
|
335 checkTableRows(tableOperator, rowValues); |
|
336 } |
|
337 |
|
338 /** |
|
339 * Waits the number of rows on table equal to the count specified |
|
340 * |
|
341 * @param tableOperator |
|
342 * @param count |
|
343 */ |
|
344 private void waitRowCount(JTableOperator tableOperator, int count) { |
|
345 tableOperator.waitStateOnQueue(comp |
|
346 -> count == ((JTable)comp).getRowCount()); |
|
347 } |
|
348 |
|
349 } |