Browse Source

Grid column to be sortable when implemented/supported (Fixes #8792). (#10190)

* Grid column to be sortable when implemented/supported
* Fix GridDeclarativeTest
* Parameterize to Grid<Person>
* Revert Parameterize to Grid<Person>, JDK with generics.
* Assertions for other columns
* Fix test

Fixes #8792
tags/8.2.0.alpha3
Ahmed Ashour 6 years ago
parent
commit
46d1a95f04

+ 17
- 0
server/src/main/java/com/vaadin/ui/Grid.java View File

@@ -1157,10 +1157,16 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
}
this.userId = id;
getGrid().setColumnId(id, this);
updateSortable();

return this;
}

private void updateSortable() {
setSortable(getGrid().getDataProvider().isInMemory()
|| getSortOrder(SortDirection.ASCENDING).count() != 0);
}

/**
* Gets the function used to produce the value for data in this column
* based on the row item.
@@ -2711,6 +2717,8 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
if (getDefaultHeaderRow() != null) {
getDefaultHeaderRow().getCell(column).setText(column.getCaption());
}

column.updateSortable();
}

/**
@@ -4587,4 +4595,13 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
order -> order.getSorted().getComparator(order.getDirection()))
.reduce((x, y) -> 0, operator);
}

@Override
protected void internalSetDataProvider(DataProvider<T, ?> dataProvider) {
super.internalSetDataProvider(dataProvider);
for (Column<T, ?> column : getColumns()) {
column.updateSortable();
}
}

}

+ 14
- 3
server/src/test/java/com/vaadin/tests/server/component/grid/GridDeclarativeTest.java View File

@@ -200,13 +200,18 @@ public class GridDeclarativeTest extends AbstractListingDeclarativeTest<Grid> {
int expandRatio = 83;
column2.setExpandRatio(expandRatio);


String sortableSuffix = "";
if (sortable) {
sortableSuffix = "='true'";
}
String design = String.format("<%s><table><colgroup>"
+ "<col column-id='column0' sortable='%s' editable resizable='%s' hidable hidden>"
+ "<col column-id='column0' sortable%s editable resizable='%s' hidable hidden>"
+ "<col column-id='id' sortable hiding-toggle-caption='%s' width='%s' min-width='%s' max-width='%s' expand='%s'>"
+ "</colgroup><thead>"
+ "<tr default><th plain-text column-ids='column0'>%s</th>"
+ "<th plain-text column-ids='id'>%s</th>" + "</tr></thead>"
+ "</table></%s>", getComponentTag(), sortable, resizable,
+ "</table></%s>", getComponentTag(), sortableSuffix, resizable,
hidingToggleCaption, width, minWidth, maxWidth, expandRatio,
caption, "Id", getComponentTag());

@@ -694,8 +699,14 @@ public class GridDeclarativeTest extends AbstractListingDeclarativeTest<Grid> {
col2.getMinimumWidth());
assertEquals(baseError + "Expand ratio", col1.getExpandRatio(),
col2.getExpandRatio());
assertEquals(baseError + "Sortable", col1.isSortable(),

String id1 = col1.getId();
String id2 = col2.getId();
// column.getId() affects .isSortable()
if ((id1 != null && id2 != null) || (id1 == null && id2 == null)) {
assertEquals(baseError + "Sortable", col1.isSortable(),
col2.isSortable());
}
assertEquals(baseError + "Editable", col1.isEditable(),
col2.isEditable());
assertEquals(baseError + "Hidable", col1.isHidable(),

+ 2
- 2
shared/src/main/java/com/vaadin/shared/ui/grid/ColumnState.java View File

@@ -27,7 +27,7 @@ public class ColumnState extends AbstractGridExtensionState {

public String caption;
public String internalId;
public boolean sortable = true;
public boolean sortable = false;
public boolean editable = false;

/** The caption for the column hiding toggle. */
@@ -66,7 +66,7 @@ public class ColumnState extends AbstractGridExtensionState {
public Connector renderer;
/**
* Whether the contents define the minimum width for this column.
*
*
* @since 8.1
*/
public boolean minimumWidthFromContent = true;

+ 0
- 2
uitest/src/main/java/com/vaadin/tests/components/grid/GridResizeTerror.java View File

@@ -30,8 +30,6 @@ public class GridResizeTerror extends UI {
protected void init(VaadinRequest request) {
Grid<Integer> grid = new Grid<>();

int cols = 10;

IntStream.range(0, 10).forEach(i -> grid.addColumn(item -> "Data" + i));

grid.setItems(IntStream.range(0, 500).boxed());

+ 1
- 3
uitest/src/main/java/com/vaadin/tests/components/grid/GridSingleColumn.java View File

@@ -21,7 +21,6 @@ import com.vaadin.data.ValueProvider;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractReindeerTestUI;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.Column;
import com.vaadin.ui.Grid.SelectionMode;

public class GridSingleColumn extends AbstractReindeerTestUI {
@@ -33,8 +32,7 @@ public class GridSingleColumn extends AbstractReindeerTestUI {

grid.setItems(IntStream.range(0, 100).mapToObj(indx -> "cell"));

Column<String, String> column = grid.addColumn(ValueProvider.identity())
.setCaption("Header");
grid.addColumn(ValueProvider.identity()).setCaption("Header");

addComponent(grid);
grid.scrollTo(50);

+ 103
- 0
uitest/src/main/java/com/vaadin/tests/components/grid/GridSortWhenPossible.java View File

@@ -0,0 +1,103 @@
/*
* Copyright 2000-2016 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;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.vaadin.data.provider.CallbackDataProvider;
import com.vaadin.data.provider.QuerySortOrder;
import com.vaadin.server.VaadinRequest;
import com.vaadin.shared.data.sort.SortDirection;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.tests.data.bean.Person;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Grid;
import com.vaadin.ui.renderers.NumberRenderer;

public class GridSortWhenPossible extends AbstractTestUI {

private List<Person> persons;

@Override
protected void setup(VaadinRequest request) {
persons = Collections.unmodifiableList(Arrays.asList(
createPerson("a", 4, true), createPerson("b", 5, false),
createPerson("c", 3, false), createPerson("a", 6, false),
createPerson("a", 2, true), createPerson("c", 7, false),
createPerson("b", 1, true)));

CheckBox inMemoryCheckBox = new CheckBox("In memory");
addComponent(inMemoryCheckBox);
addComponent(new Button("Create Grid",
e -> addComponent(getGrid(inMemoryCheckBox.getValue()))));
}

private final Grid<Person> getGrid(boolean inMemory) {
Grid<Person> grid = new Grid<>();
grid.addColumn(Person::getFirstName).setId("name");
grid.addColumn(Person::getAge, new NumberRenderer()).setId("age");
grid.addColumn(Person::getDeceased);

if (inMemory) {
grid.setItems(persons);
} else {
grid.setDataProvider(new CallbackDataProvider<>(query -> {
List<Person> list = new ArrayList<>(persons);
if (!query.getSortOrders().isEmpty()) {
QuerySortOrder order = query.getSortOrders().get(0);

Comparator<Person> comparator;
if ("name".equals(order.getSorted())) {
comparator = Comparator.comparing(Person::getFirstName);
} else {
comparator = Comparator.comparing(Person::getAge);
}

if (order.getDirection() == SortDirection.DESCENDING) {
comparator = comparator.reversed();
}
Collections.sort(list, comparator);
}
return list.stream();
}, query -> persons.size()));
}
return grid;
}

private Person createPerson(String name, int age, boolean deceased) {
Person person = new Person();
person.setFirstName(name);
person.setAge(age);
person.setDeceased(deceased);
return person;

}

@Override
public String getTestDescription() {
return "Grid columns are sorted, only when sorting is implemented";
}

@Override
public Integer getTicketNumber() {
return 8792;
}
}

+ 6
- 2
uitest/src/test/java/com/vaadin/tests/components/grid/GridAriaRowcountTest.java View File

@@ -15,10 +15,13 @@
*/
package com.vaadin.tests.components.grid;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.GridElement;
import com.vaadin.tests.tb3.SingleBrowserTest;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@@ -77,6 +80,7 @@ public class GridAriaRowcountTest extends SingleBrowserTest {
}

private boolean containsRows(int rowcount) {
return grid.getHTML().contains("aria-rowcount=\"" + String.valueOf(rowcount) + "\"");
return grid.getHTML()
.contains("aria-rowcount=\"" + String.valueOf(rowcount) + "\"");
}
}

+ 4
- 9
uitest/src/test/java/com/vaadin/tests/components/grid/GridEditorEventsTest.java View File

@@ -17,7 +17,6 @@ package com.vaadin.tests.components.grid;

import static org.junit.Assert.assertEquals;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.WebElement;

@@ -41,18 +40,14 @@ public class GridEditorEventsTest extends MultiBrowserTest {
GridEditorElement editor = updateField(index, grid, "foo");
editor.save();

assertEquals((index * 4 + 1) + ". editor is opened",
getLogRow(1));
assertEquals((index * 4 + 2) + ". editor is saved",
getLogRow(0));
assertEquals((index * 4 + 1) + ". editor is opened", getLogRow(1));
assertEquals((index * 4 + 2) + ". editor is saved", getLogRow(0));

editor = updateField(index, grid, "bar");
editor.cancel();

assertEquals((index * 4 + 3) + ". editor is opened",
getLogRow(1));
assertEquals((index * 4 + 4) + ". editor is canceled",
getLogRow(0));
assertEquals((index * 4 + 3) + ". editor is opened", getLogRow(1));
assertEquals((index * 4 + 4) + ". editor is canceled", getLogRow(0));
}

private GridEditorElement updateField(int index, GridElement grid,

+ 0
- 1
uitest/src/test/java/com/vaadin/tests/components/grid/GridSelectionTest.java View File

@@ -6,7 +6,6 @@ import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.HashSet;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.openqa.selenium.Keys;

+ 175
- 0
uitest/src/test/java/com/vaadin/tests/components/grid/GridSortWhenPossibleTest.java View File

@@ -0,0 +1,175 @@
/*
* Copyright 2000-2016 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;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.CheckBoxElement;
import com.vaadin.testbench.elements.GridElement;
import com.vaadin.tests.tb3.MultiBrowserTest;

public class GridSortWhenPossibleTest extends MultiBrowserTest {

@Test
public void inMemory() throws InterruptedException {
openTestURL();
$(CheckBoxElement.class).first().click();
$(ButtonElement.class).first().click();

GridElement grid = $(GridElement.class).first();
assertRow(grid, 0, "a", "4", true);
assertRow(grid, 1, "b", "5", false);
assertRow(grid, 2, "c", "3", false);
assertRow(grid, 3, "a", "6", false);
assertRow(grid, 4, "a", "2", true);
assertRow(grid, 5, "c", "7", false);
assertRow(grid, 6, "b", "1", true);

grid.getHeaderCell(0, 0).click();
assertTrue("First column should be sorted ascending",
grid.getHeaderCell(0, 0).getAttribute("class")
.contains("sort-asc"));
assertFalse("Second column should not be sorted", grid
.getHeaderCell(0, 1).getAttribute("class").contains("sort-"));
assertFalse("Third column should not be sorted", grid
.getHeaderCell(0, 2).getAttribute("class").contains("sort-"));

assertRow(grid, 0, "a", "4", true);
assertRow(grid, 1, "a", "6", false);
assertRow(grid, 2, "a", "2", true);
assertRow(grid, 3, "b", "5", false);
assertRow(grid, 4, "b", "1", true);
assertRow(grid, 5, "c", "3", false);
assertRow(grid, 6, "c", "7", false);

grid.getHeaderCell(0, 1).click();

assertFalse("First column should not be sorted", grid
.getHeaderCell(0, 0).getAttribute("class").contains("sort-"));
assertTrue("Second column should be sorted ascending",
grid.getHeaderCell(0, 1).getAttribute("class")
.contains("sort-asc"));
assertFalse("Third column should not be sorted", grid
.getHeaderCell(0, 2).getAttribute("class").contains("sort-"));

assertRow(grid, 0, "b", "1", true);
assertRow(grid, 1, "a", "2", true);
assertRow(grid, 2, "c", "3", false);
assertRow(grid, 3, "a", "4", true);
assertRow(grid, 4, "b", "5", false);
assertRow(grid, 5, "a", "6", false);
assertRow(grid, 6, "c", "7", false);

grid.getHeaderCell(0, 2).click();

assertFalse("First column should not be sorted", grid
.getHeaderCell(0, 0).getAttribute("class").contains("sort-"));
assertFalse("Second column should not be sorted", grid
.getHeaderCell(0, 1).getAttribute("class").contains("sort-"));
assertTrue("Third column should be sorted ascending",
grid.getHeaderCell(0, 2).getAttribute("class")
.contains("sort-asc"));

assertRow(grid, 0, "b", "5", false);
assertRow(grid, 1, "c", "3", false);
assertRow(grid, 2, "a", "6", false);
assertRow(grid, 3, "c", "7", false);
assertRow(grid, 4, "a", "4", true);
assertRow(grid, 5, "a", "2", true);
assertRow(grid, 6, "b", "1", true);
}

@Test
public void lazyLoading() throws InterruptedException {
openTestURL();
$(ButtonElement.class).first().click();

GridElement grid = $(GridElement.class).first();
assertRow(grid, 0, "a", "4", true);
assertRow(grid, 1, "b", "5", false);
assertRow(grid, 2, "c", "3", false);
assertRow(grid, 3, "a", "6", false);
assertRow(grid, 4, "a", "2", true);
assertRow(grid, 5, "c", "7", false);
assertRow(grid, 6, "b", "1", true);

grid.getHeaderCell(0, 0).click();

assertTrue("First column should be sorted ascending",
grid.getHeaderCell(0, 0).getAttribute("class")
.contains("sort-asc"));
assertFalse("Second column should not be sorted", grid
.getHeaderCell(0, 1).getAttribute("class").contains("sort-"));
assertFalse("Third column should not be sorted", grid
.getHeaderCell(0, 2).getAttribute("class").contains("sort-"));

assertRow(grid, 0, "a", "4", true);
assertRow(grid, 1, "a", "6", false);
assertRow(grid, 2, "a", "2", true);
assertRow(grid, 3, "b", "5", false);
assertRow(grid, 4, "b", "1", true);
assertRow(grid, 5, "c", "3", false);
assertRow(grid, 6, "c", "7", false);

grid.getHeaderCell(0, 1).click();

assertFalse("First column should not be sorted", grid
.getHeaderCell(0, 0).getAttribute("class").contains("sort-"));
assertTrue("Second column should be sorted ascending",
grid.getHeaderCell(0, 1).getAttribute("class")
.contains("sort-asc"));
assertFalse("Third column should not be sorted", grid
.getHeaderCell(0, 2).getAttribute("class").contains("sort-"));

assertRow(grid, 0, "b", "1", true);
assertRow(grid, 1, "a", "2", true);
assertRow(grid, 2, "c", "3", false);
assertRow(grid, 3, "a", "4", true);
assertRow(grid, 4, "b", "5", false);
assertRow(grid, 5, "a", "6", false);
assertRow(grid, 6, "c", "7", false);

grid.getHeaderCell(0, 2).click();

assertFalse("First column should not be sorted", grid
.getHeaderCell(0, 0).getAttribute("class").contains("sort-"));
assertTrue("Second column should be sorted ascending",
grid.getHeaderCell(0, 1).getAttribute("class")
.contains("sort-asc"));
assertFalse("Third column should not be sorted", grid
.getHeaderCell(0, 2).getAttribute("class").contains("sort-"));

assertRow(grid, 0, "b", "1", true);
assertRow(grid, 1, "a", "2", true);
assertRow(grid, 2, "c", "3", false);
assertRow(grid, 3, "a", "4", true);
assertRow(grid, 4, "b", "5", false);
assertRow(grid, 5, "a", "6", false);
assertRow(grid, 6, "c", "7", false);
}

private void assertRow(GridElement grid, int row, Object... values) {
for (int col = 0; col < values.length; col++) {
assertEquals(String.valueOf(values[col]),
grid.getCell(row, col).getText());
}
}
}

+ 14
- 15
uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridBasicDetailsTest.java View File

@@ -6,7 +6,6 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.NoSuchElementException;
@@ -23,20 +22,20 @@ public class GridBasicDetailsTest extends GridBasicsTest {
* awkward with two scroll commands back to back.
*/
private static final int ALMOST_LAST_INDEX = 995;
private static final String[] OPEN_ALMOST_LAST_ITEM_DETAILS = {
"Component", "Details", "Open " + ALMOST_LAST_INDEX };
private static final String[] OPEN_FIRST_ITEM_DETAILS = {
"Component", "Details", "Open First" };
private static final String[] TOGGLE_FIRST_ITEM_DETAILS = {
"Component", "Details", "Toggle First" };
private static final String[] DETAILS_GENERATOR_NULL = {
"Component", "Details", "Generators", "NULL" };
private static final String[] DETAILS_GENERATOR_WATCHING = {
"Component", "Details", "Generators", "\"Watching\"" };
private static final String[] DETAILS_GENERATOR_PERSISTING = {
"Component", "Details", "Generators", "Persisting" };
private static final String[] CHANGE_HIERARCHY = { "Component",
"Details", "Generators", "- Change Component" };
private static final String[] OPEN_ALMOST_LAST_ITEM_DETAILS = { "Component",
"Details", "Open " + ALMOST_LAST_INDEX };
private static final String[] OPEN_FIRST_ITEM_DETAILS = { "Component",
"Details", "Open First" };
private static final String[] TOGGLE_FIRST_ITEM_DETAILS = { "Component",
"Details", "Toggle First" };
private static final String[] DETAILS_GENERATOR_NULL = { "Component",
"Details", "Generators", "NULL" };
private static final String[] DETAILS_GENERATOR_WATCHING = { "Component",
"Details", "Generators", "\"Watching\"" };
private static final String[] DETAILS_GENERATOR_PERSISTING = { "Component",
"Details", "Generators", "Persisting" };
private static final String[] CHANGE_HIERARCHY = { "Component", "Details",
"Generators", "- Change Component" };

@Override
@Before

Loading…
Cancel
Save