]> source.dussan.org Git - vaadin-framework.git/blob
34d46eae78c6c738be04204cee99d9eb8dbaa860
[vaadin-framework.git] /
1 /*
2  * Copyright 2000-2014 Vaadin Ltd.
3  *
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
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
14  * the License.
15  */
16 package com.vaadin.tests.components.grid.basicfeatures.server;
17
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;
22
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;
30
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;
36
37 public class GridEditorBufferedTest extends GridEditorTest {
38
39     @Override
40     @Before
41     public void setUp() {
42         super.setUp();
43     }
44
45     @Test
46     public void testKeyboardSave() {
47         selectMenuPath(EDIT_ITEM_100);
48
49         WebElement textField = getEditorWidgets().get(0);
50
51         textField.click();
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();
55
56         textField.sendKeys(" changed");
57
58         // Save from keyboard
59         new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
60
61         assertEditorClosed();
62         assertEquals("(100, 0) changed",
63                 getGridElement().getCell(100, 0).getText());
64     }
65
66     @Test
67     public void testKeyboardSaveWithInvalidEdition() {
68         makeInvalidEdition();
69
70         GridEditorElement editor = getGridElement().getEditor();
71         TestBenchElement field = editor.getField(7);
72
73         field.click();
74         new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
75
76         assertEditorOpen();
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));
81
82         editor.cancel();
83
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());
89     }
90
91     @Test
92     public void testSave() {
93         selectMenuPath(EDIT_ITEM_100);
94
95         WebElement textField = getEditorWidgets().get(0);
96
97         textField.click();
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();
101
102         textField.sendKeys(" changed");
103
104         WebElement saveButton = getEditor()
105                 .findElement(By.className("v-grid-editor-save"));
106
107         saveButton.click();
108
109         assertEquals("(100, 0) changed",
110                 getGridElement().getCell(100, 0).getText());
111     }
112
113     @Test
114     public void testProgrammaticSave() {
115         selectMenuPath(EDIT_ITEM_100);
116
117         WebElement textField = getEditorWidgets().get(0);
118
119         textField.click();
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();
123
124         textField.sendKeys(" changed");
125
126         selectMenuPath("Component", "Editor", "Save");
127
128         assertEquals("(100, 0) changed",
129                 getGridElement().getCell(100, 0).getText());
130     }
131
132     @Test
133     public void testInvalidEdition() {
134         makeInvalidEdition();
135
136         GridEditorElement editor = getGridElement().getEditor();
137         editor.save();
138
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));
143         editor.cancel();
144
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());
150     }
151
152     private void makeInvalidEdition() {
153         selectMenuPath(EDIT_ITEM_5);
154         assertFalse(logContainsText(
155                 "Exception occured, java.lang.IllegalStateException"));
156
157         GridEditorElement editor = getGridElement().getEditor();
158
159         assertFalse(
160                 "Field 7 should not have been marked with an error before error",
161                 editor.isFieldErrorMarked(7));
162
163         WebElement intField = editor.getField(7);
164         intField.clear();
165         intField.sendKeys("banana phone");
166     }
167
168     @Test
169     public void testEditorInDisabledGrid() {
170         int originalScrollPos = getGridVerticalScrollPos();
171
172         selectMenuPath(EDIT_ITEM_5);
173         assertEditorOpen();
174
175         selectMenuPath("Component", "State", "Enabled");
176         assertEditorOpen();
177
178         GridEditorElement editor = getGridElement().getEditor();
179         editor.save();
180         assertEditorOpen();
181
182         editor.cancel();
183         assertEditorOpen();
184
185         selectMenuPath("Component", "State", "Enabled");
186
187         scrollGridVerticallyTo(100);
188         assertEquals(
189                 "Grid shouldn't scroll vertically while editing in buffered mode",
190                 originalScrollPos, getGridVerticalScrollPos());
191     }
192
193     @Test
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());
203
204         selectMenuPath("Component", "Editor", "Change save caption");
205         assertNotEquals(
206                 "Save button caption should've changed while editor is open",
207                 GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());
208
209         getCancelButton().click();
210
211         selectMenuPath("Component", "Editor", "Change cancel caption");
212         selectMenuPath(EDIT_ITEM_5);
213         assertNotEquals(
214                 "Cancel button caption should've changed while editor is closed",
215                 GridConstants.DEFAULT_CANCEL_CAPTION,
216                 getCancelButton().getText());
217     }
218
219     @Test(expected = NoSuchElementException.class)
220     public void testVerticalScrollLocking() {
221         selectMenuPath(EDIT_ITEM_5);
222         getGridElement().getCell(200, 0);
223     }
224
225     @Test
226     public void testScrollDisabledOnProgrammaticOpen() {
227         int originalScrollPos = getGridVerticalScrollPos();
228
229         selectMenuPath(EDIT_ITEM_5);
230
231         scrollGridVerticallyTo(100);
232         assertEquals(
233                 "Grid shouldn't scroll vertically while editing in buffered mode",
234                 originalScrollPos, getGridVerticalScrollPos());
235     }
236
237     @Test
238     public void testScrollDisabledOnMouseOpen() {
239         int originalScrollPos = getGridVerticalScrollPos();
240
241         GridCellElement cell_5_0 = getGridElement().getCell(5, 0);
242         new Actions(getDriver()).doubleClick(cell_5_0).perform();
243
244         scrollGridVerticallyTo(100);
245         assertEquals(
246                 "Grid shouldn't scroll vertically while editing in buffered mode",
247                 originalScrollPos, getGridVerticalScrollPos());
248     }
249
250     @Test
251     public void testScrollDisabledOnKeyboardOpen() {
252         int originalScrollPos = getGridVerticalScrollPos();
253
254         GridCellElement cell_5_0 = getGridElement().getCell(5, 0);
255         cell_5_0.click();
256         new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
257
258         scrollGridVerticallyTo(100);
259         assertEquals(
260                 "Grid shouldn't scroll vertically while editing in buffered mode",
261                 originalScrollPos, getGridVerticalScrollPos());
262     }
263
264     @Test
265     public void testMouseOpeningClosing() {
266
267         getGridElement().getCell(4, 0).doubleClick();
268         assertEditorOpen();
269
270         getCancelButton().click();
271         assertEditorClosed();
272
273         selectMenuPath(TOGGLE_EDIT_ENABLED);
274         getGridElement().getCell(4, 0).doubleClick();
275         assertEditorClosed();
276     }
277
278     @Test
279     public void testMouseOpeningDisabledWhenOpen() {
280         selectMenuPath(EDIT_ITEM_5);
281
282         getGridElement().getCell(4, 0).doubleClick();
283
284         assertEquals("Editor should still edit row 5", "(5, 0)",
285                 getEditorWidgets().get(0).getAttribute("value"));
286     }
287
288     @Test
289     public void testProgrammaticOpeningDisabledWhenOpen() {
290         selectMenuPath(EDIT_ITEM_5);
291         assertEditorOpen();
292         assertEquals("Editor should edit row 5", "(5, 0)",
293                 getEditorWidgets().get(0).getAttribute("value"));
294
295         selectMenuPath(EDIT_ITEM_100);
296         boolean thrown = logContainsText(
297                 "Exception occured, java.lang.IllegalStateException");
298         assertTrue("IllegalStateException thrown", thrown);
299
300         assertEditorOpen();
301         assertEquals("Editor should still edit row 5", "(5, 0)",
302                 getEditorWidgets().get(0).getAttribute("value"));
303     }
304
305     @Test
306     public void testUserSortDisabledWhenOpen() {
307         selectMenuPath(EDIT_ITEM_5);
308
309         getGridElement().getHeaderCell(0, 0).click();
310
311         assertEditorOpen();
312         assertEquals("(2, 0)", getGridElement().getCell(2, 0).getText());
313     }
314 }