2 * Copyright 2000-2016 Vaadin Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
16 package com.vaadin.tests.components.table;
18 import static org.hamcrest.CoreMatchers.is;
19 import static org.hamcrest.MatcherAssert.assertThat;
21 import java.util.List;
23 import org.junit.Test;
24 import org.openqa.selenium.By;
25 import org.openqa.selenium.WebElement;
26 import org.openqa.selenium.interactions.Actions;
28 import com.vaadin.tests.tb3.MultiBrowserTest;
29 import com.vaadin.v7.testbench.customelements.TableElement;
32 * Tests that clicking on active fields doesn't change Table selection, nor does
37 public class TableClickAndDragOnIconAndComponentsTest extends MultiBrowserTest {
40 public void setup() throws Exception {
47 public void clickOnTextFieldDoesNotSelectRow() {
51 assertThatFocusTextFieldHasText("foo 2foo");
53 assertThat(getSelectedRowTextValue(), is(1));
57 public void clickOnReadOnlyTextFieldSelectsRow() {
60 clickOnReadOnlyTextField(2);
62 assertThat(getSelectedRowTextValue(), is(2));
66 public void clickOnLabelSelectsRow() {
71 assertThat(getSelectedRowTextValue(), is(2));
75 public void clickOnEmbeddedIconSelectsRow() {
78 clickOnEmbeddedIcon(2);
80 assertThat(getSelectedRowTextValue(), is(2));
84 public void dragAndDroppingRowDoesNotSelectRow() {
89 assertThat(getSelectedRowTextValue(), is(1));
90 assertThat(getSelectedRowIndex(), is(0));
94 public void dragAndDroppingSelectedRowStaysSelected() {
99 assertThat(getSelectedRowTextValue(), is(1));
100 assertThat(getSelectedRowIndex(), is(4));
103 private void assertThatFocusTextFieldHasText(String text) {
104 List<WebElement> focused = getTable()
105 .findElements(By.className("v-textfield-focus"));
107 assertThat(focused.get(0).getAttribute("value"), is(text));
110 private int getSelectedRowTextValue() {
111 WebElement selectedRow = getSelectedRow();
114 String text = getText(selectedRow, 2);
116 return Integer.parseInt(text.substring(4, 5));
119 private String getText(WebElement row, int column) {
120 List<WebElement> cellContents = getCellContents(row);
122 return cellContents.get(column).getText();
125 private List<WebElement> getCellContents(WebElement row) {
126 return row.findElements(By.className("v-table-cell-content"));
129 private WebElement getSelectedRow() {
130 return getTable().findElement(By.className("v-selected"));
133 private void clickOnTextField(int row) {
134 WebElement textField = getTextField(row, 0);
139 private void clickOnReadOnlyTextField(int row) {
140 WebElement textField = getTextField(row, 1);
145 private WebElement getTextField(int row, int index) {
146 return getElement(row, index, "v-textfield");
149 private WebElement getElement(int row, String className) {
150 return getElement(row, 0, className);
153 private WebElement getElement(int row, int index, String className) {
154 return getRows().get(row).findElements(By.className(className))
158 private List<WebElement> getRows() {
159 return getTable().findElement(By.className("v-table-body"))
160 .findElements(By.tagName("tr"));
163 private void selectRow(int row) {
164 TableElement table = getTable();
166 table.getCell(row, 2).click();
169 private TableElement getTable() {
170 return $(TableElement.class).first();
173 private void clickOnLabel(int row) {
174 WebElement label = getElement(row, "v-label");
178 private void clickOnEmbeddedIcon(int row) {
179 WebElement embeddedIcon = getElement(row, "v-embedded");
180 embeddedIcon.click();
183 private void moveRow(int from, int to) {
184 List<WebElement> rows = getRows();
185 List<WebElement> cellContents = getCellContents(rows.get(from));
187 new Actions(getDriver()).moveToElement(cellContents.get(2))
188 .clickAndHold().moveToElement(rows.get(to)).release().perform();
191 private int getSelectedRowIndex() {
192 List<WebElement> rows = getRows();
194 // Unfortunately rows.getIndexOf(getSelectedRow()) doesn't work.
195 for (WebElement r : rows) {
196 if (r.getAttribute("class").contains("v-selected")) {
197 return rows.indexOf(r);