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