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.grid.basicfeatures.client;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNotEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertTrue;
25 import java.util.List;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.openqa.selenium.By;
30 import org.openqa.selenium.Keys;
31 import org.openqa.selenium.NoSuchElementException;
32 import org.openqa.selenium.WebElement;
33 import org.openqa.selenium.interactions.Actions;
35 import com.vaadin.testbench.elements.GridElement.GridCellElement;
36 import com.vaadin.testbench.elements.GridElement.GridEditorElement;
37 import com.vaadin.testbench.parallel.BrowserUtil;
38 import com.vaadin.tests.components.grid.basicfeatures.GridBasicClientFeaturesTest;
39 import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeatures;
40 import com.vaadin.v7.shared.ui.grid.GridConstants;
42 public class GridEditorClientTest extends GridBasicClientFeaturesTest {
44 private static final String[] EDIT_ROW_100 = new String[] { "Component",
45 "Editor", "Edit row 100" };
46 private static final String[] EDIT_ROW_5 = new String[] { "Component",
47 "Editor", "Edit row 5" };
52 selectMenuPath("Component", "Editor", "Enabled");
56 public void testProgrammaticOpeningClosing() {
57 selectMenuPath(EDIT_ROW_5);
58 assertNotNull(getEditor());
60 selectMenuPath("Component", "Editor", "Cancel edit");
61 assertNull(getEditor());
62 assertEquals("Row 5 edit cancelled",
63 findElement(By.className("grid-editor-log")).getText());
67 public void testProgrammaticOpeningWithScroll() {
68 selectMenuPath(EDIT_ROW_100);
69 assertNotNull(getEditor());
72 @Test(expected = NoSuchElementException.class)
73 public void testVerticalScrollLocking() {
74 selectMenuPath(EDIT_ROW_5);
75 getGridElement().getCell(200, 0);
79 public void testMouseOpeningClosing() {
81 getGridElement().getCell(4, 0).doubleClick();
82 assertNotNull(getEditor());
84 // Move focus to the third input field
85 getEditor().findElements(By.className("gwt-TextBox")).get(2).click();
88 getSaveButton().click();
90 // Make sure the editor went away
91 assertNull(getEditor());
93 // Check that focus has moved to cell 4,2 - the last one that was
95 assertTrue(getGridElement().getCell(4, 2).isFocused());
98 selectMenuPath("Component", "Editor", "Enabled");
100 getGridElement().getCell(4, 0).doubleClick();
101 assertNull(getEditor());
105 public void testKeyboardOpeningClosing() {
107 getGridElement().getCell(4, 0).click();
109 new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
111 assertNotNull(getEditor());
113 new Actions(getDriver()).sendKeys(Keys.ESCAPE).perform();
114 assertNull(getEditor());
115 assertEquals("Row 4 edit cancelled",
116 findElement(By.className("grid-editor-log")).getText());
119 selectMenuPath("Component", "Editor", "Enabled");
121 getGridElement().getCell(5, 0).click();
122 new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
123 assertNull(getEditor());
127 public void testWidgetBinding() throws Exception {
128 selectMenuPath(EDIT_ROW_100);
129 WebElement editor = getEditor();
131 List<WebElement> widgets = editor
132 .findElements(By.className("gwt-TextBox"));
134 assertEquals(GridBasicFeatures.EDITABLE_COLUMNS, widgets.size());
136 assertEquals("(100, 0)", widgets.get(0).getAttribute("value"));
137 assertEquals("(100, 1)", widgets.get(1).getAttribute("value"));
138 assertEquals("(100, 2)", widgets.get(2).getAttribute("value"));
140 assertEquals("100", widgets.get(6).getAttribute("value"));
141 assertEquals("<b>100</b>", widgets.get(8).getAttribute("value"));
145 public void testWithSelectionColumn() throws Exception {
146 selectMenuPath("Component", "State", "Selection mode", "multi");
147 selectMenuPath("Component", "State", "Frozen column count",
149 selectMenuPath(EDIT_ROW_5);
151 WebElement editorCells = findElements(
152 By.className("v-grid-editor-cells")).get(1);
153 List<WebElement> selectorDivs = editorCells
154 .findElements(By.cssSelector("div"));
156 assertFalse("selector column cell should've had contents",
157 selectorDivs.get(0).getAttribute("innerHTML").isEmpty());
158 assertFalse("normal column cell shoul've had contents",
159 selectorDivs.get(1).getAttribute("innerHTML").isEmpty());
163 public void testSave() {
164 selectMenuPath(EDIT_ROW_100);
166 WebElement textField = getEditor()
167 .findElements(By.className("gwt-TextBox")).get(0);
170 textField.sendKeys("Changed");
172 WebElement saveButton = getEditor()
173 .findElement(By.className("v-grid-editor-save"));
177 assertEquals("Changed", getGridElement().getCell(100, 0).getText());
181 public void testProgrammaticSave() {
182 selectMenuPath(EDIT_ROW_100);
184 WebElement textField = getEditor()
185 .findElements(By.className("gwt-TextBox")).get(0);
188 textField.sendKeys("Changed");
190 selectMenuPath("Component", "Editor", "Save");
192 assertEquals("Changed", getGridElement().getCell(100, 0).getText());
196 public void testCaptionChange() {
197 selectMenuPath(EDIT_ROW_5);
198 assertEquals("Save button caption should've been \""
199 + GridConstants.DEFAULT_SAVE_CAPTION + "\" to begin with",
200 GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());
201 assertEquals("Cancel button caption should've been \""
202 + GridConstants.DEFAULT_CANCEL_CAPTION + "\" to begin with",
203 GridConstants.DEFAULT_CANCEL_CAPTION,
204 getCancelButton().getText());
206 selectMenuPath("Component", "Editor", "Change Save Caption");
208 "Save button caption should've changed while editor is open",
209 GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());
211 getCancelButton().click();
213 selectMenuPath("Component", "Editor", "Change Cancel Caption");
214 selectMenuPath(EDIT_ROW_5);
216 "Cancel button caption should've changed while editor is closed",
217 GridConstants.DEFAULT_CANCEL_CAPTION,
218 getCancelButton().getText());
222 public void testUneditableColumn() {
223 selectMenuPath("Component", "Editor", "Edit row 5");
225 assertFalse("Uneditable column should not have an editor widget",
226 getGridElement().getEditor().isEditable(3));
230 public void testErrorField() {
231 selectMenuPath(EDIT_ROW_5);
233 GridEditorElement editor = getGridElement().getEditor();
235 assertTrue("No errors should be present",
236 editor.findElements(By.className("error")).isEmpty());
237 assertEquals("No error message should be present", null,
238 editor.getErrorMessage());
240 selectMenuPath("Component", "Editor", "Toggle second editor error");
241 getSaveButton().click();
243 assertEquals("Unexpected amount of error fields", 1,
244 editor.findElements(By.className("error")).size());
245 assertEquals("Unexpedted error message",
246 "Syntethic fail of editor in column 2. "
247 + "This message is so long that it doesn't fit into its box",
248 editor.getErrorMessage());
252 public void testFocusOnMouseOpen() {
254 GridCellElement cell = getGridElement().getCell(4, 2);
258 WebElement focused = getFocusedElement();
260 assertEquals("", "input", focused.getTagName());
261 assertEquals("", cell.getText(), focused.getAttribute("value"));
265 public void testFocusOnKeyboardOpen() {
267 GridCellElement cell = getGridElement().getCell(4, 2);
270 new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
272 WebElement focused = getFocusedElement();
274 assertEquals("", "input", focused.getTagName());
275 assertEquals("", cell.getText(), focused.getAttribute("value"));
279 public void testNoFocusOnProgrammaticOpen() {
281 selectMenuPath(EDIT_ROW_5);
283 WebElement focused = getFocusedElement();
285 if (BrowserUtil.isIE(getDesiredCapabilities())) {
286 assertEquals("Focus should be nowhere", null, focused);
288 // GWT menubar loses focus after clicking a menuitem
289 assertEquals("Focus should be in body", "body",
290 focused.getTagName());
294 protected WebElement getSaveButton() {
295 return getEditor().findElement(By.className("v-grid-editor-save"));
298 protected WebElement getCancelButton() {
299 return getEditor().findElement(By.className("v-grid-editor-cancel"));