--- /dev/null
+/*
+ * 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;
+ }
+}
--- /dev/null
+/*
+ * 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());
+ }
+ }
+}