2 * Copyright 2000-2014 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.server;
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.assertTrue;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.openqa.selenium.By;
26 import org.openqa.selenium.Keys;
27 import org.openqa.selenium.NoSuchElementException;
28 import org.openqa.selenium.WebElement;
29 import org.openqa.selenium.interactions.Actions;
31 import com.vaadin.shared.ui.grid.GridConstants;
32 import com.vaadin.testbench.TestBenchElement;
33 import com.vaadin.testbench.elements.GridElement.GridCellElement;
34 import com.vaadin.testbench.elements.GridElement.GridEditorElement;
35 import com.vaadin.testbench.elements.NotificationElement;
37 public class GridEditorBufferedTest extends GridEditorTest {
46 public void testKeyboardSave() {
47 selectMenuPath(EDIT_ITEM_100);
49 WebElement textField = getEditorWidgets().get(0);
52 // without this, the click in the middle of the field might not be after
53 // the old text on some browsers
54 new Actions(getDriver()).sendKeys(Keys.END).perform();
56 textField.sendKeys(" changed");
59 new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
62 assertEquals("(100, 0) changed",
63 getGridElement().getCell(100, 0).getText());
67 public void testKeyboardSaveWithInvalidEdition() {
70 GridEditorElement editor = getGridElement().getEditor();
71 TestBenchElement field = editor.getField(7);
74 new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
77 assertEquals("Column 7: Could not convert value to Integer",
78 editor.getErrorMessage());
79 assertTrue("Field 7 should have been marked with an error after error",
80 editor.isFieldErrorMarked(7));
84 selectMenuPath(EDIT_ITEM_100);
85 assertFalse("Exception should not exist",
86 isElementPresent(NotificationElement.class));
87 assertEquals("There should be no editor error message", null,
88 getGridElement().getEditor().getErrorMessage());
92 public void testSave() {
93 selectMenuPath(EDIT_ITEM_100);
95 WebElement textField = getEditorWidgets().get(0);
98 // without this, the click in the middle of the field might not be after
99 // the old text on some browsers
100 new Actions(getDriver()).sendKeys(Keys.END).perform();
102 textField.sendKeys(" changed");
104 WebElement saveButton = getEditor()
105 .findElement(By.className("v-grid-editor-save"));
109 assertEquals("(100, 0) changed",
110 getGridElement().getCell(100, 0).getText());
114 public void testProgrammaticSave() {
115 selectMenuPath(EDIT_ITEM_100);
117 WebElement textField = getEditorWidgets().get(0);
120 // without this, the click in the middle of the field might not be after
121 // the old text on some browsers
122 new Actions(getDriver()).sendKeys(Keys.END).perform();
124 textField.sendKeys(" changed");
126 selectMenuPath("Component", "Editor", "Save");
128 assertEquals("(100, 0) changed",
129 getGridElement().getCell(100, 0).getText());
133 public void testInvalidEdition() {
134 makeInvalidEdition();
136 GridEditorElement editor = getGridElement().getEditor();
139 assertEquals("Column 7: Could not convert value to Integer",
140 editor.getErrorMessage());
141 assertTrue("Field 7 should have been marked with an error after error",
142 editor.isFieldErrorMarked(7));
145 selectMenuPath(EDIT_ITEM_100);
146 assertFalse("Exception should not exist",
147 isElementPresent(NotificationElement.class));
148 assertEquals("There should be no editor error message", null,
149 getGridElement().getEditor().getErrorMessage());
152 private void makeInvalidEdition() {
153 selectMenuPath(EDIT_ITEM_5);
154 assertFalse(logContainsText(
155 "Exception occured, java.lang.IllegalStateException"));
157 GridEditorElement editor = getGridElement().getEditor();
160 "Field 7 should not have been marked with an error before error",
161 editor.isFieldErrorMarked(7));
163 WebElement intField = editor.getField(7);
165 intField.sendKeys("banana phone");
169 public void testEditorInDisabledGrid() {
170 int originalScrollPos = getGridVerticalScrollPos();
172 selectMenuPath(EDIT_ITEM_5);
175 selectMenuPath("Component", "State", "Enabled");
178 GridEditorElement editor = getGridElement().getEditor();
185 selectMenuPath("Component", "State", "Enabled");
187 scrollGridVerticallyTo(100);
189 "Grid shouldn't scroll vertically while editing in buffered mode",
190 originalScrollPos, getGridVerticalScrollPos());
194 public void testCaptionChange() {
195 selectMenuPath(EDIT_ITEM_5);
196 assertEquals("Save button caption should've been \""
197 + GridConstants.DEFAULT_SAVE_CAPTION + "\" to begin with",
198 GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());
199 assertEquals("Cancel button caption should've been \""
200 + GridConstants.DEFAULT_CANCEL_CAPTION + "\" to begin with",
201 GridConstants.DEFAULT_CANCEL_CAPTION,
202 getCancelButton().getText());
204 selectMenuPath("Component", "Editor", "Change save caption");
206 "Save button caption should've changed while editor is open",
207 GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());
209 getCancelButton().click();
211 selectMenuPath("Component", "Editor", "Change cancel caption");
212 selectMenuPath(EDIT_ITEM_5);
214 "Cancel button caption should've changed while editor is closed",
215 GridConstants.DEFAULT_CANCEL_CAPTION,
216 getCancelButton().getText());
219 @Test(expected = NoSuchElementException.class)
220 public void testVerticalScrollLocking() {
221 selectMenuPath(EDIT_ITEM_5);
222 getGridElement().getCell(200, 0);
226 public void testScrollDisabledOnProgrammaticOpen() {
227 int originalScrollPos = getGridVerticalScrollPos();
229 selectMenuPath(EDIT_ITEM_5);
231 scrollGridVerticallyTo(100);
233 "Grid shouldn't scroll vertically while editing in buffered mode",
234 originalScrollPos, getGridVerticalScrollPos());
238 public void testScrollDisabledOnMouseOpen() {
239 int originalScrollPos = getGridVerticalScrollPos();
241 GridCellElement cell_5_0 = getGridElement().getCell(5, 0);
242 new Actions(getDriver()).doubleClick(cell_5_0).perform();
244 scrollGridVerticallyTo(100);
246 "Grid shouldn't scroll vertically while editing in buffered mode",
247 originalScrollPos, getGridVerticalScrollPos());
251 public void testScrollDisabledOnKeyboardOpen() {
252 int originalScrollPos = getGridVerticalScrollPos();
254 GridCellElement cell_5_0 = getGridElement().getCell(5, 0);
256 new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
258 scrollGridVerticallyTo(100);
260 "Grid shouldn't scroll vertically while editing in buffered mode",
261 originalScrollPos, getGridVerticalScrollPos());
265 public void testMouseOpeningClosing() {
267 getGridElement().getCell(4, 0).doubleClick();
270 getCancelButton().click();
271 assertEditorClosed();
273 selectMenuPath(TOGGLE_EDIT_ENABLED);
274 getGridElement().getCell(4, 0).doubleClick();
275 assertEditorClosed();
279 public void testMouseOpeningDisabledWhenOpen() {
280 selectMenuPath(EDIT_ITEM_5);
282 getGridElement().getCell(4, 0).doubleClick();
284 assertEquals("Editor should still edit row 5", "(5, 0)",
285 getEditorWidgets().get(0).getAttribute("value"));
289 public void testProgrammaticOpeningDisabledWhenOpen() {
290 selectMenuPath(EDIT_ITEM_5);
292 assertEquals("Editor should edit row 5", "(5, 0)",
293 getEditorWidgets().get(0).getAttribute("value"));
295 selectMenuPath(EDIT_ITEM_100);
296 boolean thrown = logContainsText(
297 "Exception occured, java.lang.IllegalStateException");
298 assertTrue("IllegalStateException thrown", thrown);
301 assertEquals("Editor should still edit row 5", "(5, 0)",
302 getEditorWidgets().get(0).getAttribute("value"));
306 public void testUserSortDisabledWhenOpen() {
307 selectMenuPath(EDIT_ITEM_5);
309 getGridElement().getHeaderCell(0, 0).click();
312 assertEquals("(2, 0)", getGridElement().getCell(2, 0).getText());