Path: blob/master/test/jdk/sanity/client/SwingSet/src/TableDemoTest.java
41153 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import static com.sun.swingset3.demos.table.TableDemo.COLUMN1_NAME;24import static com.sun.swingset3.demos.table.TableDemo.COLUMN2_NAME;25import static com.sun.swingset3.demos.table.TableDemo.COLUMN3_NAME;26import static com.sun.swingset3.demos.table.TableDemo.COLUMN4_NAME;27import static com.sun.swingset3.demos.table.TableDemo.DEMO_TITLE;28import static com.sun.swingset3.demos.table.TableDemo.ROW_HEIGHT;29import static org.jemmy2ext.JemmyExt.EXACT_STRING_COMPARATOR;3031import java.util.ArrayList;32import java.util.List;3334import javax.swing.JTable;35import javax.swing.UIManager;3637import org.jtregext.GuiTestListener;38import org.netbeans.jemmy.ClassReference;39import org.netbeans.jemmy.operators.JCheckBoxOperator;40import org.netbeans.jemmy.operators.JFrameOperator;41import org.netbeans.jemmy.operators.JTableHeaderOperator;42import org.netbeans.jemmy.operators.JTableOperator;43import org.netbeans.jemmy.operators.JTextFieldOperator;44import org.testng.annotations.Listeners;45import org.testng.annotations.Test;4647import com.sun.swingset3.demos.table.OscarCandidate;48import com.sun.swingset3.demos.table.OscarTableModel;49import com.sun.swingset3.demos.table.TableDemo;5051/*52* @test53* @key headful54* @summary Verifies SwingSet3 TableDemo page by checking different properties55* of the JTable like number of row, number of columns and actions like56* selection of cell, sorting based on column, filtering based on text and57* moving of the column58*59* @library /sanity/client/lib/jemmy/src60* @library /sanity/client/lib/Extensions/src61* @library /sanity/client/lib/SwingSet3/src62* @modules java.desktop63* java.logging64* @build org.jemmy2ext.JemmyExt65* @build com.sun.swingset3.demos.table.TableDemo66* @run testng/timeout=600 TableDemoTest67*/68@Listeners(GuiTestListener.class)69public class TableDemoTest {7071private final static int MAX_ROW_COUNT = 524;72private final static int MAX_COL_COUNT = 4;7374private final static String FILTER_TEXT = "Sunrise";75private final static String FILTER_RESET_TEXT = "";7677private final static int [] SELECT_ROW_INDICES ={10, 11, 18};7879private final static int MOVE_COL_START_INDEX = 1;80private final static int MOVE_COL_END_INDEX = 2;81private final static String MOVE_COL_VAL_TEXT1 = "Sunrise";82private final static String MOVE_COL_VAL_TEXT2 = "Most Unique Artistic Picture";83private final static int MOVE_COL_VAL_ROW = 0;8485private final static int SORT_COL = 1;86private final static int[] SORT_VAL_ROWS =new int[] {0, 250, 523};87private 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]"}};91private 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", "[]"}};95private 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]"}};99100/**101* Tests the different properties of JTable like number of rows, number102* 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 Exception106*/107@Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)108public void test(String lookAndFeel) throws Exception {109UIManager.setLookAndFeel(lookAndFeel);110111new ClassReference(TableDemo.class.getCanonicalName()).startApplication();112113JFrameOperator frameOperator = new JFrameOperator(DEMO_TITLE);114frameOperator.setComparator(EXACT_STRING_COMPARATOR);115frameOperator.setVerification(true);116JTableOperator tableOperator = new JTableOperator(frameOperator);117JTableHeaderOperator tableHeaderOperator = new JTableHeaderOperator(frameOperator);118119checkTableBasicProperties(tableOperator);120checkCellSelection(tableOperator);121checkSortTable(tableOperator, tableHeaderOperator);122checkMoveColumn(tableOperator, tableHeaderOperator);123checkFilterTable(frameOperator, tableOperator);124}125126/**127* Verifies the table basic properties number of columns, rows and row height128*129* @param tableOperator130*/131private void checkTableBasicProperties(JTableOperator tableOperator) {132tableOperator.waitStateOnQueue(comp133-> MAX_COL_COUNT == ((JTable)comp).getColumnCount());134waitRowCount(tableOperator, MAX_ROW_COUNT);135tableOperator.waitStateOnQueue(comp136-> ROW_HEIGHT == ((JTable)comp).getRowHeight());137}138139/**140* Selects one table cell and verifies the selected cell's row number and column number141*142* @param tableOperator143*/144private void checkCellSelection(JTableOperator tableOperator) {145int noOfColumns = tableOperator.getColumnCount();146for (int i = 0; i < SELECT_ROW_INDICES.length; i++) {147int rowIndex = SELECT_ROW_INDICES[i];148for (int j = 0; j < noOfColumns; j++) {149int colIndex = j;150tableOperator.clickOnCell(rowIndex, colIndex);151tableOperator.waitStateOnQueue(comp152-> rowIndex == ((JTable)comp).getSelectedRow() &&153colIndex == ((JTable)comp).getSelectedColumn());154}155}156}157158/**159* Filter table based on specific text and winners check box, and verifies row count160*161* @param frameOperator162* @param tableOperator163*/164private void checkFilterTable(JFrameOperator frameOperator,165JTableOperator tableOperator) {166167int [] filterRowCount = getFilteredCount(tableOperator, FILTER_TEXT);168JTextFieldOperator filterField = new JTextFieldOperator(frameOperator);169JCheckBoxOperator winnersCheckbox = new JCheckBoxOperator(frameOperator);170171// Filtering based on FILTER_TEXT172filterField.setText(FILTER_TEXT);173waitRowCount(tableOperator, filterRowCount[0]);174175// Filtering based on WinnersCheckbox176winnersCheckbox.setSelected(true);177waitRowCount(tableOperator, filterRowCount[1]);178179// Resets the winners check box180winnersCheckbox.setSelected(false);181waitRowCount(tableOperator, filterRowCount[0]);182183// Resets the filter text field184filterField.setText(FILTER_RESET_TEXT);185waitRowCount(tableOperator, MAX_ROW_COUNT);186187}188189private int[] getFilteredCount(JTableOperator tableOperator, String filterText){190OscarTableModel tableModel = (OscarTableModel)tableOperator.getModel();191int noOfRows = tableModel.getRowCount();192int filteredRowCount = 0;193int filteredWinnersRowCount = 0;194for (int i = 0; i < noOfRows; i++) {195OscarCandidate candidate = tableModel.getCandidate(i);196if(isMovieOrPersonsContainsText(candidate, filterText)){197filteredRowCount++;198if(candidate.isWinner()) {199filteredWinnersRowCount++;200}201}202}203return new int[] {filteredRowCount, filteredWinnersRowCount};204}205206private boolean isMovieOrPersonsContainsText(207OscarCandidate candidate, String filterText){208String movie = candidate.getMovieTitle();209if(movie != null && movie.contains(filterText)) {210return true;211} else {212List<String> persons = candidate.getPersons();213for (String person : persons) {214if(person != null && person.contains(filterText)) {215return true;216}217}218}219return false;220}221222/**223* Moves to swap the columns, move again to reset back, verify column name224* and cell values in both the scenarios.225*226* @param tableOperator227* @param tableHeaderOperator228*/229private void checkMoveColumn(JTableOperator tableOperator,230JTableHeaderOperator tableHeaderOperator) {231232String[] columnNames = {COLUMN1_NAME, COLUMN3_NAME, COLUMN2_NAME, COLUMN4_NAME};233// Moving the column from 'start index' to 'end index'234moveColumn(tableOperator, tableHeaderOperator, columnNames,235MOVE_COL_START_INDEX, MOVE_COL_END_INDEX);236237// Resets the columns to original position(from 'end index' to 'start index')238columnNames[1] = COLUMN2_NAME;239columnNames[2] = COLUMN3_NAME;240moveColumn(tableOperator, tableHeaderOperator, columnNames,241MOVE_COL_END_INDEX, MOVE_COL_START_INDEX);242}243244/**245* Moves to swap the columns, verify column name and cell values.246*247* @param tableOperator248* @param tableHeaderOperator249* @param columnNames250* @param moveCol251* @param moveToCol252*/253private void moveColumn(JTableOperator tableOperator, JTableHeaderOperator tableHeaderOperator,254String[] columnNames, int moveCol, int moveToCol){255256tableHeaderOperator.moveColumn(moveCol, moveToCol);257checkColumnNames(tableOperator, columnNames);258tableOperator.waitCell(MOVE_COL_VAL_TEXT1, MOVE_COL_VAL_ROW, moveCol);259tableOperator.waitCell(MOVE_COL_VAL_TEXT2, MOVE_COL_VAL_ROW, moveToCol);260}261262private void checkColumnNames(JTableOperator tableOperator, String[] columnNames) {263for (int i = 0; i < tableOperator.getColumnCount(); i++) {264int columnIndex = i;265tableOperator.waitStateOnQueue(comp -> columnNames[columnIndex].equals(266((JTable)comp).getColumnModel().getColumn(columnIndex).getHeaderValue()));267}268}269270/**271* Sorts the table based on one particular column in ascending and descending order,272* and verifies cell values273*274* @param tableOperator275* @param tableHeaderOperator276*/277private void checkSortTable(JTableOperator tableOperator,278JTableHeaderOperator tableHeaderOperator) {279280// Verifying the row values before sort281checkTableRows(tableOperator, ASC_PRE_SORT_ROW_VAL);282283// Getting all award category values before stating the sort284// to prepare the expected result285ArrayList<String> awardCats = new ArrayList<>();286for (int i = 0; i < tableOperator.getRowCount(); i++) {287awardCats.add((String) tableOperator.getValueAt(i, SORT_COL));288}289// Sorting awardCats(expected result) in ascending order290awardCats.sort((s1, s2) -> s1.compareTo(s2));291292// Sorting table based on column 'Award Category' in ascending order293sortTable(tableOperator, tableHeaderOperator, awardCats,294ASC_POST_SORT_ROW_VAL);295296// Sorting awardCats(expected result) in descending order297awardCats.sort((s1, s2) -> s2.compareTo(s1));298// Sorting table based on column 'Award Category' in descending order299sortTable(tableOperator, tableHeaderOperator, awardCats,300DESC_POST_SORT_ROW_VAL);301302}303304private void checkColumnSorted(JTableOperator tableOperator,305ArrayList<String> awardCatExp){306ArrayList<String> awardCatActual = new ArrayList<>();307for (int i = 0; i < tableOperator.getRowCount(); i++) {308awardCatActual.add((String) tableOperator.getValueAt(i, SORT_COL));309}310tableOperator.waitStateOnQueue(comp -> awardCatExp.equals(awardCatActual));311}312313private void checkTableRows(JTableOperator tableOperator, String[][] rowValues) {314for (int i = 0; i < SORT_VAL_ROWS.length; i++) {315tableOperator.waitCell(rowValues[i][0], SORT_VAL_ROWS[i], 0);316tableOperator.waitCell(rowValues[i][1], SORT_VAL_ROWS[i], 1);317tableOperator.waitCell(rowValues[i][2], SORT_VAL_ROWS[i], 2);318tableOperator.waitCell(rowValues[i][3], SORT_VAL_ROWS[i], 3);319}320}321322/**323* Sorts the table based on one particular column and verifies cell values324*325* @param tableOperator326* @param tableHeaderOperator327* @param awardCatExp328* @param rowValues329*/330private void sortTable(JTableOperator tableOperator, JTableHeaderOperator tableHeaderOperator,331ArrayList<String> awardCatExp, String[][] rowValues) {332333tableHeaderOperator.selectColumn(SORT_COL);334checkColumnSorted(tableOperator, awardCatExp);335// Verifying the row values after sort336checkTableRows(tableOperator, rowValues);337}338339/**340* Waits the number of rows on table equal to the count specified341*342* @param tableOperator343* @param count344*/345private void waitRowCount(JTableOperator tableOperator, int count) {346tableOperator.waitStateOnQueue(comp347-> count == ((JTable)comp).getRowCount());348}349350}351352353