Browse Source

Fix Table.sort(...) to update the sort indicator (#8978)

Change-Id: I2df7de7648a8a311a913267ef0d0d0e57f52f19d
tags/7.2.0
Juuso Valli 10 years ago
parent
commit
2cfa64db4f

+ 9
- 1
server/src/com/vaadin/ui/Table.java View File

@@ -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");

+ 129
- 0
uitest/src/com/vaadin/tests/components/table/TableSortingIndicator.java View File

@@ -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<TestBean> container = new BeanItemContainer<TestBean>(
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);
}
}

+ 65
- 0
uitest/src/com/vaadin/tests/components/table/TableSortingIndicatorTest.java View File

@@ -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)));
}
}

Loading…
Cancel
Save