Bladeren bron

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 jaren geleden
bovenliggende
commit
46d1a95f04

+ 17
- 0
server/src/main/java/com/vaadin/ui/Grid.java Bestand weergeven

} }
this.userId = id; this.userId = id;
getGrid().setColumnId(id, this); getGrid().setColumnId(id, this);
updateSortable();


return this; 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 * Gets the function used to produce the value for data in this column
* based on the row item. * based on the row item.
if (getDefaultHeaderRow() != null) { if (getDefaultHeaderRow() != null) {
getDefaultHeaderRow().getCell(column).setText(column.getCaption()); getDefaultHeaderRow().getCell(column).setText(column.getCaption());
} }

column.updateSortable();
} }


/** /**
order -> order.getSorted().getComparator(order.getDirection())) order -> order.getSorted().getComparator(order.getDirection()))
.reduce((x, y) -> 0, operator); .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 Bestand weergeven

int expandRatio = 83; int expandRatio = 83;
column2.setExpandRatio(expandRatio); column2.setExpandRatio(expandRatio);



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


col2.getMinimumWidth()); col2.getMinimumWidth());
assertEquals(baseError + "Expand ratio", col1.getExpandRatio(), assertEquals(baseError + "Expand ratio", col1.getExpandRatio(),
col2.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()); col2.isSortable());
}
assertEquals(baseError + "Editable", col1.isEditable(), assertEquals(baseError + "Editable", col1.isEditable(),
col2.isEditable()); col2.isEditable());
assertEquals(baseError + "Hidable", col1.isHidable(), assertEquals(baseError + "Hidable", col1.isHidable(),

+ 2
- 2
shared/src/main/java/com/vaadin/shared/ui/grid/ColumnState.java Bestand weergeven



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


/** The caption for the column hiding toggle. */ /** The caption for the column hiding toggle. */
public Connector renderer; public Connector renderer;
/** /**
* Whether the contents define the minimum width for this column. * Whether the contents define the minimum width for this column.
*
*
* @since 8.1 * @since 8.1
*/ */
public boolean minimumWidthFromContent = true; public boolean minimumWidthFromContent = true;

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

protected void init(VaadinRequest request) { protected void init(VaadinRequest request) {
Grid<Integer> grid = new Grid<>(); Grid<Integer> grid = new Grid<>();


int cols = 10;

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


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

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

import com.vaadin.server.VaadinRequest; import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractReindeerTestUI; import com.vaadin.tests.components.AbstractReindeerTestUI;
import com.vaadin.ui.Grid; import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.Column;
import com.vaadin.ui.Grid.SelectionMode; import com.vaadin.ui.Grid.SelectionMode;


public class GridSingleColumn extends AbstractReindeerTestUI { public class GridSingleColumn extends AbstractReindeerTestUI {


grid.setItems(IntStream.range(0, 100).mapToObj(indx -> "cell")); 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); addComponent(grid);
grid.scrollTo(50); grid.scrollTo(50);

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

/*
* 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 Bestand weergeven

*/ */
package com.vaadin.tests.components.grid; 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.ButtonElement;
import com.vaadin.testbench.elements.GridElement; import com.vaadin.testbench.elements.GridElement;
import com.vaadin.tests.tb3.SingleBrowserTest; import com.vaadin.tests.tb3.SingleBrowserTest;
import org.junit.Test;


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


private boolean containsRows(int rowcount) { 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 Bestand weergeven



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


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


GridEditorElement editor = updateField(index, grid, "foo"); GridEditorElement editor = updateField(index, grid, "foo");
editor.save(); 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 = updateField(index, grid, "bar");
editor.cancel(); 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, private GridEditorElement updateField(int index, GridElement grid,

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

import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;


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

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

/*
* 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 Bestand weergeven

import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.NoSuchElementException;
* awkward with two scroll commands back to back. * awkward with two scroll commands back to back.
*/ */
private static final int ALMOST_LAST_INDEX = 995; 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 @Override
@Before @Before

Laden…
Annuleren
Opslaan