From 2cfa64db4f709e74b0bbb0a83d3a31d754fd874e Mon Sep 17 00:00:00 2001 From: Juuso Valli Date: Wed, 23 Apr 2014 15:46:16 +0300 Subject: [PATCH] Fix Table.sort(...) to update the sort indicator (#8978) Change-Id: I2df7de7648a8a311a913267ef0d0d0e57f52f19d --- server/src/com/vaadin/ui/Table.java | 10 +- .../table/TableSortingIndicator.java | 129 ++++++++++++++++++ .../table/TableSortingIndicatorTest.java | 65 +++++++++ 3 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 uitest/src/com/vaadin/tests/components/table/TableSortingIndicator.java create mode 100644 uitest/src/com/vaadin/tests/components/table/TableSortingIndicatorTest.java diff --git a/server/src/com/vaadin/ui/Table.java b/server/src/com/vaadin/ui/Table.java index e23a1bf688..85019a979c 100644 --- a/server/src/com/vaadin/ui/Table.java +++ b/server/src/com/vaadin/ui/Table.java @@ -4751,7 +4751,15 @@ public class Table extends AbstractSelect implements Action.Container, if (refreshingPreviouslyEnabled) { enableContentRefreshing(true); } - + if (propertyId.length > 0 && ascending.length > 0) { + // The first propertyId is the primary sorting criterion, + // therefore the sort indicator should be there + sortAscending = ascending[0]; + sortContainerPropertyId = propertyId[0]; + } else { + sortAscending = true; + sortContainerPropertyId = null; + } } else if (c != null) { throw new UnsupportedOperationException( "Underlying Data does not allow sorting"); diff --git a/uitest/src/com/vaadin/tests/components/table/TableSortingIndicator.java b/uitest/src/com/vaadin/tests/components/table/TableSortingIndicator.java new file mode 100644 index 0000000000..93fe8d7d9e --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/table/TableSortingIndicator.java @@ -0,0 +1,129 @@ +/* + * Copyright 2000-2013 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.table; + +import java.util.Random; + +import com.vaadin.data.Container; +import com.vaadin.data.util.BeanItemContainer; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Button.ClickListener; +import com.vaadin.ui.Table; + +/** + * Test if the table sorting indicators update correctly when the table is + * sorted serverside + * + * @author Vaadin Ltd + */ +public class TableSortingIndicator extends AbstractTestUI { + + /* + * (non-Javadoc) + * + * @see com.vaadin.tests.components.AbstractTestUI#setup(com.vaadin.server. + * VaadinRequest) + */ + @Override + protected void setup(VaadinRequest request) { + final Table table = new Table("Test table", buildContainer()); + table.setSizeFull(); + addComponent(table); + Button sortButton = new Button("Sort", new ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + table.sort(new Object[] { "val1" }, new boolean[] { false }); + } + }); + addComponent(sortButton); + } + + private Container buildContainer() { + BeanItemContainer container = new BeanItemContainer( + TestBean.class); + for (int i = 0; i < 100; ++i) { + TestBean item = new TestBean(); + item.setVal1(i); + item.setVal2(randomWord()); + item.setVal3(randomWord()); + container.addBean(item); + } + return container; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.tests.components.AbstractTestUI#getTestDescription() + */ + @Override + protected String getTestDescription() { + return "The table should have visible sorting indicators."; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.tests.components.AbstractTestUI#getTicketNumber() + */ + @Override + protected Integer getTicketNumber() { + return 8978; + } + + public class TestBean { + private Integer val1; + private String val2; + private String val3; + + public Integer getVal1() { + return val1; + } + + public void setVal1(Integer val1) { + this.val1 = val1; + } + + public String getVal2() { + return val2; + } + + public void setVal2(String val2) { + this.val2 = val2; + } + + public String getVal3() { + return val3; + } + + public void setVal3(String val3) { + this.val3 = val3; + } + } + + private String randomWord() { + Random rng = new Random(); + char[] word = new char[3 + rng.nextInt(10)]; + for (int i = 0; i < word.length; ++i) { + word[i] = (char) ('a' + rng.nextInt(26)); + } + return new String(word); + } +} diff --git a/uitest/src/com/vaadin/tests/components/table/TableSortingIndicatorTest.java b/uitest/src/com/vaadin/tests/components/table/TableSortingIndicatorTest.java new file mode 100644 index 0000000000..36a51b35e3 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/table/TableSortingIndicatorTest.java @@ -0,0 +1,65 @@ +/* + * Copyright 2000-2013 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.table; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.WebElement; + +import com.vaadin.testbench.By; +import com.vaadin.tests.tb3.MultiBrowserTest; + +/** + * Tests if the sort indicator is visible after the table has been sorted from + * the serverside. + * + * @author Vaadin Ltd + */ +public class TableSortingIndicatorTest extends MultiBrowserTest { + private static final String TABLE_HEADER_DESC_INDICATOR = "v-table-header-cell-desc"; + private static final String TABLE_HEADER_ASC_INDICATOR = "v-table-header-cell-asc"; + + @Test + public void testTableSortingIndicatorIsVisibleAfterServersideSort() { + openTestURL(); + + Assert.assertFalse("Descending indicator was prematurely visible", + isElementPresent(By.className(TABLE_HEADER_DESC_INDICATOR))); + Assert.assertFalse("Ascending indicator was prematurely visible", + isElementPresent(By.className(TABLE_HEADER_ASC_INDICATOR))); + WebElement button = driver.findElement(By + .vaadin("//Button[caption=\"Sort\"]")); + button.click(); + Assert.assertTrue("Indicator did not become visible", + isElementPresent(By.className(TABLE_HEADER_DESC_INDICATOR))); + + Assert.assertFalse("Ascending sort indicator was wrongly visible", + isElementPresent(By.className(TABLE_HEADER_ASC_INDICATOR))); + WebElement manualSort = driver.findElement(By + .className(TABLE_HEADER_DESC_INDICATOR)); + manualSort.click(); + Assert.assertFalse("Table sort indicator didn't change", + isElementPresent(By.className(TABLE_HEADER_DESC_INDICATOR))); + Assert.assertTrue("Ascending sort indicator didn't become visible", + isElementPresent(By.className(TABLE_HEADER_ASC_INDICATOR))); + button.click(); + Assert.assertTrue( + "Descending sort indicator didn't appear on the second serverside sort.", + isElementPresent(By.className(TABLE_HEADER_DESC_INDICATOR))); + Assert.assertFalse("Ascending sort indicator didn't disappear", + isElementPresent(By.className(TABLE_HEADER_ASC_INDICATOR))); + } +} -- 2.39.5