123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- /*
- * Copyright 2000-2014 Vaadin Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
- package com.vaadin.tests.components.grid.basicfeatures.server;
-
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertFalse;
- import static org.junit.Assert.assertTrue;
- import static org.junit.Assert.fail;
-
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
-
- import org.junit.Test;
- import org.openqa.selenium.Keys;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.interactions.Actions;
-
- import com.vaadin.shared.data.sort.SortDirection;
- import com.vaadin.testbench.By;
- import com.vaadin.testbench.elements.GridElement;
- import com.vaadin.testbench.elements.GridElement.GridCellElement;
- import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeatures;
- import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeaturesTest;
-
- public class GridSortingTest extends GridBasicFeaturesTest {
-
- private static class SortInfo {
- public final int sortOrder;
- public final SortDirection sortDirection;
-
- private SortInfo(int sortOrder, SortDirection sortDirection) {
- this.sortOrder = sortOrder;
- this.sortDirection = sortDirection;
- }
- }
-
- private static class SortInfoWithColumn extends SortInfo {
- public final int columnIndex;
-
- private SortInfoWithColumn(int columnIndex, int sortOrder,
- SortDirection sortDirection) {
- super(sortOrder, sortDirection);
- this.columnIndex = columnIndex;
- }
- }
-
- private static SortInfo _(int sortOrder, SortDirection sortDirection) {
- return new SortInfo(sortOrder, sortDirection);
- }
-
- private static SortInfoWithColumn _(int columnIndex, int sortOrder,
- SortDirection sortDirection) {
- return new SortInfoWithColumn(columnIndex, sortOrder, sortDirection);
- }
-
- @Test
- public void testProgrammaticSorting() throws Exception {
- openTestURL();
-
- // Sorting by column 9 is sorting by row index that is represented as a
- // String.
- // First cells for first 3 rows are (9, 0), (99, 0) and (999, 0)
- sortBy("Column 9, DESC");
- assertLastSortIsUserOriginated(false);
-
- // Verify that programmatic sorting calls are identified as originating
- // from API
- assertColumnsAreSortedAs(_(9, 1, SortDirection.DESCENDING));
-
- String row = "";
- for (int i = 0; i < 3; ++i) {
- row += "9";
- String expected = "(" + row + ", 0)";
- String cellValue = getGridElement().getCell(i, 0).getText();
- assertEquals("Grid is not sorted by Column 9 "
- + "using descending direction.", expected, cellValue);
- }
-
- // Column 10 is random numbers from Random with seed 13334
- sortBy("Column 10, ASC");
-
- assertFalse("Column 9 should no longer have the sort-desc stylename",
- getGridElement().getHeaderCell(0, 9).getAttribute("class")
- .contains("sort-desc"));
-
- assertColumnsAreSortedAs(_(10, 1, SortDirection.ASCENDING));
-
- for (int i = 0; i < 5; ++i) {
- Integer firstRow = Integer.valueOf(getGridElement().getCell(i + 1,
- 10).getText());
- Integer secondRow = Integer.valueOf(getGridElement().getCell(i, 10)
- .getText());
- assertGreater("Grid is not sorted by Column 10 using"
- + " ascending direction", firstRow, secondRow);
-
- }
-
- // Column 7 is row index as a number. Last three row are original rows
- // 2, 1 and 0.
- sortBy("Column 7, DESC");
- for (int i = 0; i < 3; ++i) {
- String expected = "(" + i + ", 0)";
- String cellContent = getGridElement().getCell(
- GridBasicFeatures.ROWS - (i + 1), 0).getText();
- assertEquals("Grid is not sorted by Column 7 using "
- + "descending direction", expected, cellContent);
- }
-
- assertFalse("Column 10 should no longer have the sort-asc stylename",
- getGridElement().getHeaderCell(0, 10).getAttribute("class")
- .contains("sort-asc"));
-
- assertColumnsAreSortedAs(_(7, 1, SortDirection.DESCENDING));
- }
-
- @Test
- public void testMouseSorting() throws Exception {
- setDebug(true);
- openTestURL();
-
- GridElement grid = getGridElement();
-
- selectMenuPath("Component", "Columns", "Column 9", "Column 9 Width",
- "Auto");
-
- // Sorting by column 9 is sorting by row index that is represented as a
- // String.
-
- // Click header twice to sort descending
- clickHeader(grid.getHeaderCell(0, 9));
-
- assertLastSortIsUserOriginated(true);
-
- assertColumnsAreSortedAs(_(9, 1, SortDirection.ASCENDING));
- clickHeader(grid.getHeaderCell(0, 9));
- assertColumnsAreSortedAs(_(9, 1, SortDirection.DESCENDING));
-
- // First cells for first 3 rows are (9, 0), (99, 0) and (999, 0)
- String row = "";
- for (int i = 0; i < 3; ++i) {
- row += "9";
- String expected = "(" + row + ", 0)";
- String actual = grid.getCell(i, 0).getText();
- assertEquals("Grid is not sorted by Column 9"
- + " using descending direction.", expected, actual);
- }
-
- selectMenuPath("Component", "Columns", "Column 10", "Column 10 Width",
- "Auto");
- // Column 10 is random numbers from Random with seed 13334
- // Click header to sort ascending
- clickHeader(grid.getHeaderCell(0, 10));
- assertColumnsAreSortedAs(_(10, 1, SortDirection.ASCENDING));
-
- for (int i = 0; i < 5; ++i) {
- Integer firstRow = Integer.valueOf(grid.getCell(i + 1, 10)
- .getText());
- Integer secondRow = Integer.valueOf(grid.getCell(i, 10).getText());
- assertGreater(
- "Grid is not sorted by Column 10 using ascending direction",
- firstRow, secondRow);
-
- }
-
- selectMenuPath("Component", "Columns", "Column 7", "Column 7 Width",
- "Auto");
- // Column 7 is row index as a number. Last three row are original rows
- // 2, 1 and 0.
- // Click header twice to sort descending
- clickHeader(grid.getHeaderCell(0, 7));
- assertColumnsAreSortedAs(_(7, 1, SortDirection.ASCENDING));
- clickHeader(grid.getHeaderCell(0, 7));
- assertColumnsAreSortedAs(_(7, 1, SortDirection.DESCENDING));
-
- for (int i = 0; i < 3; ++i) {
- assertEquals(
- "Grid is not sorted by Column 7 using descending direction",
- "(" + i + ", 0)",
- grid.getCell(GridBasicFeatures.ROWS - (i + 1), 0).getText());
- }
-
- }
-
- private void clickHeader(GridCellElement headerCell) {
- new Actions(getDriver()).moveToElement(headerCell, 5, 5).click()
- .perform();
- }
-
- private void sendKey(Keys seq) {
- new Actions(getDriver()).sendKeys(seq).perform();
- }
-
- private void holdKey(Keys key) {
- new Actions(getDriver()).keyDown(key).perform();
- }
-
- private void releaseKey(Keys key) {
- new Actions(getDriver()).keyUp(key).perform();
- }
-
- @Test
- public void testKeyboardSorting() {
- openTestURL();
-
- /*
- * We can't click on the header directly, since it will sort the header
- * immediately. We need to focus some other column first, and only then
- * navigate there.
- */
- getGridElement().getCell(0, 0).click();
- sendKey(Keys.ARROW_UP);
-
- // Sort ASCENDING on first column
- sendKey(Keys.ENTER);
- assertLastSortIsUserOriginated(true);
- assertColumnsAreSortedAs(_(1, SortDirection.ASCENDING));
-
- // Move to next column
- sendKey(Keys.RIGHT);
-
- // Add this column to the existing sorting group
- holdKey(Keys.SHIFT);
- sendKey(Keys.ENTER);
- releaseKey(Keys.SHIFT);
- assertColumnsAreSortedAs(_(1, SortDirection.ASCENDING),
- _(2, SortDirection.ASCENDING));
-
- // Move to next column
- sendKey(Keys.RIGHT);
-
- // Add a third column to the sorting group
- holdKey(Keys.SHIFT);
- sendKey(Keys.ENTER);
- releaseKey(Keys.SHIFT);
- assertColumnsAreSortedAs(_(1, SortDirection.ASCENDING),
- _(2, SortDirection.ASCENDING), _(3, SortDirection.ASCENDING));
-
- // Move back to the second column
- sendKey(Keys.LEFT);
-
- // Change sort direction of the second column to DESCENDING
- holdKey(Keys.SHIFT);
- sendKey(Keys.ENTER);
- releaseKey(Keys.SHIFT);
- assertColumnsAreSortedAs(_(1, SortDirection.ASCENDING),
- _(2, SortDirection.DESCENDING), _(3, SortDirection.ASCENDING));
-
- // Move back to the third column
- sendKey(Keys.RIGHT);
-
- // Set sorting to third column, ASCENDING
- sendKey(Keys.ENTER);
- assertColumnsAreSortedAs(_(2, 1, SortDirection.ASCENDING));
-
- // Move to the fourth column
- sendKey(Keys.RIGHT);
-
- // Make sure that single-column sorting also works as expected
- sendKey(Keys.ENTER);
- assertColumnsAreSortedAs(_(3, 1, SortDirection.ASCENDING));
-
- }
-
- private void assertColumnsAreSortedAs(SortInfoWithColumn... sortInfos) {
- for (SortInfoWithColumn sortInfo : sortInfos) {
- assertSort(sortInfo, sortInfo.columnIndex,
- onlyOneColumnIsSorted(sortInfos));
- }
- }
-
- /**
- * @param sortDirections
- * <code>null</code> if not interested in that index, otherwise a
- * direction that the column needs to be sorted as
- */
- private void assertColumnsAreSortedAs(SortInfo... sortInfos) {
- for (int column = 0; column < sortInfos.length; column++) {
- SortInfo sortInfo = sortInfos[column];
- assertSort(sortInfo, column, onlyOneColumnIsSorted(sortInfos));
- }
- }
-
- private void assertSort(SortInfo sortInfo, int column,
- boolean onlyOneColumnIsSorted) {
- if (sortInfo == null) {
- return;
- }
-
- GridCellElement headerCell = getGridElement().getHeaderCell(0, column);
- String classValue = headerCell.getAttribute("class");
-
- boolean isSortedAscending = sortInfo.sortDirection == SortDirection.ASCENDING
- && classValue.contains("sort-asc");
- boolean isSortedDescending = sortInfo.sortDirection == SortDirection.DESCENDING
- && classValue.contains("sort-desc");
-
- if (isSortedAscending || isSortedDescending) {
- String sortOrderAttribute = headerCell.getAttribute("sort-order");
-
- if (sortOrderAttribute == null) {
- if (!(sortInfo.sortOrder == 1 && onlyOneColumnIsSorted)) {
- fail("missing sort-order element attribute from column "
- + column);
- }
- } else {
- assertEquals("sort order was not as expected",
- String.valueOf(sortInfo.sortOrder), sortOrderAttribute);
- }
- } else {
- fail("column index " + column + " was not sorted as "
- + sortInfo.sortDirection + " (class: " + classValue + ")");
- }
- }
-
- private static boolean onlyOneColumnIsSorted(SortInfo[] sortInfos) {
-
- boolean foundSortedColumn = false;
- for (SortInfo sortInfo : sortInfos) {
- if (sortInfo == null) {
- continue;
- }
-
- if (!foundSortedColumn) {
- foundSortedColumn = true;
- } else {
- // two columns were sorted
- return false;
- }
- }
- return foundSortedColumn;
- }
-
- private void sortBy(String column) {
- selectMenuPath("Component", "State", "Sort by column", column);
- }
-
- private void assertLastSortIsUserOriginated(boolean isUserOriginated) {
- List<WebElement> userOriginatedMessages = getDriver()
- .findElements(
- By.xpath("//*[contains(text(),'SortOrderChangeEvent: isUserOriginated')]"));
-
- Collections.sort(userOriginatedMessages, new Comparator<WebElement>() {
- @Override
- public int compare(WebElement o1, WebElement o2) {
- return o1.getText().compareTo(o2.getText());
- }
- });
-
- String newestEntry = userOriginatedMessages.get(
- userOriginatedMessages.size() - 1).getText();
-
- String[] parts = newestEntry.split(" ");
- boolean wasUserOriginated = Boolean
- .parseBoolean(parts[parts.length - 1]);
- if (isUserOriginated) {
- assertTrue("expected the sort to be user originated, but wasn't",
- wasUserOriginated);
- } else {
- assertFalse(
- "expected the sort not to be user originated, but it was",
- wasUserOriginated);
- }
- }
- }
|