]> source.dussan.org Git - vaadin-framework.git/blob
fc708db5c43fb3433761d438e4f7654e12d77799
[vaadin-framework.git] /
1 /*
2  * Copyright 2000-2016 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.assertTrue;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.openqa.selenium.By;
25 import org.openqa.selenium.Keys;
26 import org.openqa.selenium.WebElement;
27 import org.openqa.selenium.interactions.Actions;
28
29 import com.vaadin.testbench.TestBenchElement;
30 import com.vaadin.testbench.elements.GridElement.GridCellElement;
31
32 public class GridEditorUnbufferedTest extends GridEditorTest {
33
34     private static final String[] TOGGLE_EDITOR_BUFFERED = new String[] {
35             "Component", "Editor", "Buffered mode" };
36     private static final String[] CANCEL_EDIT = new String[] { "Component",
37             "Editor", "Cancel edit" };
38
39     @Override
40     @Before
41     public void setUp() {
42         super.setUp();
43         selectMenuPath(TOGGLE_EDITOR_BUFFERED);
44     }
45
46     @Test
47     public void testEditorShowsNoButtons() {
48         selectMenuPath(EDIT_ITEM_5);
49
50         assertEditorOpen();
51
52         assertFalse("Save button should not be visible in unbuffered mode.",
53                 isElementPresent(BY_EDITOR_SAVE));
54
55         assertFalse("Cancel button should not be visible in unbuffered mode.",
56                 isElementPresent(BY_EDITOR_CANCEL));
57     }
58
59     @Test
60     public void testToggleEditorUnbufferedWhileOpen() {
61         selectMenuPath(EDIT_ITEM_5);
62         assertEditorOpen();
63         selectMenuPath(TOGGLE_EDITOR_BUFFERED);
64         boolean thrown = logContainsText(
65                 "Exception occured, java.lang.IllegalStateException");
66         assertTrue("IllegalStateException thrown", thrown);
67     }
68
69     @Test
70     public void testEditorMoveWithMouse() {
71         selectMenuPath(EDIT_ITEM_5);
72
73         assertEditorOpen();
74
75         String firstFieldValue = getEditorWidgets().get(0)
76                 .getAttribute("value");
77         assertEquals("Editor should be at row 5", "(5, 0)", firstFieldValue);
78
79         getGridElement().getCell(10, 0).click();
80         firstFieldValue = getEditorWidgets().get(0).getAttribute("value");
81
82         assertEquals("Editor should be at row 10", "(10, 0)", firstFieldValue);
83     }
84
85     @Test
86     public void testEditorMoveWithKeyboard() throws InterruptedException {
87         selectMenuPath(EDIT_ITEM_100);
88
89         assertEditorOpen();
90
91         getEditorWidgets().get(0).click();
92         new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
93
94         String firstFieldValue = getEditorWidgets().get(0)
95                 .getAttribute("value");
96         assertEquals("Editor should move to row 101", "(101, 0)",
97                 firstFieldValue);
98
99         for (int i = 0; i < 10; i++) {
100             new Actions(getDriver()).keyDown(Keys.SHIFT).sendKeys(Keys.ENTER)
101                     .keyUp(Keys.SHIFT).perform();
102
103             firstFieldValue = getEditorWidgets().get(0).getAttribute("value");
104             int row = 100 - i;
105             assertEquals("Editor should move to row " + row, "(" + row + ", 0)",
106                     firstFieldValue);
107         }
108     }
109
110     @Test
111     public void testValidationErrorPreventsMove() throws InterruptedException {
112         // Because of "out of view" issues, we need to move this for easy access
113         selectMenuPath("Component", "Columns", "Column 7", "Column 7 Width",
114                 "50px");
115         for (int i = 0; i < 6; ++i) {
116             selectMenuPath("Component", "Columns", "Column 7", "Move left");
117         }
118
119         selectMenuPath(EDIT_ITEM_5);
120
121         getEditorWidgets().get(1).click();
122         String faultyInt = "not a number";
123         getEditorWidgets().get(1).sendKeys(faultyInt);
124
125         getGridElement().getCell(10, 0).click();
126
127         assertEquals("Editor should not move from row 5", "(5, 0)",
128                 getEditorWidgets().get(0).getAttribute("value"));
129
130         getEditorWidgets().get(1).sendKeys(Keys.chord(Keys.CONTROL, "a"));
131         getEditorWidgets().get(1).sendKeys("5");
132         // FIXME: Needs to trigger one extra validation round-trip for now
133         getGridElement().sendKeys(Keys.ENTER);
134
135         getGridElement().getCell(10, 0).click();
136
137         assertEquals("Editor should move to row 10", "(10, 0)",
138                 getEditorWidgets().get(0).getAttribute("value"));
139
140     }
141
142     @Test
143     public void testErrorMessageWrapperHidden() {
144         selectMenuPath(EDIT_ITEM_5);
145
146         assertEditorOpen();
147
148         WebElement editorFooter = getEditor()
149                 .findElement(By.className("v-grid-editor-footer"));
150
151         assertTrue("Editor footer should not be visible when there's no error",
152                 editorFooter.getCssValue("display").equalsIgnoreCase("none"));
153     }
154
155     @Test
156     public void testScrollEnabledOnProgrammaticOpen() {
157         int originalScrollPos = getGridVerticalScrollPos();
158
159         selectMenuPath(EDIT_ITEM_5);
160
161         scrollGridVerticallyTo(100);
162         assertGreater(
163                 "Grid should scroll vertically while editing in unbuffered mode",
164                 getGridVerticalScrollPos(), originalScrollPos);
165     }
166
167     @Test
168     public void testScrollEnabledOnMouseOpen() {
169         int originalScrollPos = getGridVerticalScrollPos();
170
171         GridCellElement cell_5_0 = getGridElement().getCell(5, 0);
172         new Actions(getDriver()).doubleClick(cell_5_0).perform();
173
174         scrollGridVerticallyTo(100);
175         assertGreater(
176                 "Grid should scroll vertically while editing in unbuffered mode",
177                 getGridVerticalScrollPos(), originalScrollPos);
178     }
179
180     @Test
181     public void testScrollEnabledOnKeyboardOpen() {
182         int originalScrollPos = getGridVerticalScrollPos();
183
184         GridCellElement cell_5_0 = getGridElement().getCell(5, 0);
185         cell_5_0.click();
186         new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
187
188         scrollGridVerticallyTo(100);
189         assertGreater(
190                 "Grid should scroll vertically while editing in unbuffered mode",
191                 getGridVerticalScrollPos(), originalScrollPos);
192     }
193
194     @Test
195     public void testEditorInDisabledGrid() {
196         selectMenuPath(EDIT_ITEM_5);
197
198         selectMenuPath("Component", "State", "Enabled");
199         assertEditorOpen();
200
201         assertTrue("Editor text field should be disabled",
202                 null != getEditorWidgets().get(2).getAttribute("disabled"));
203
204         selectMenuPath("Component", "State", "Enabled");
205         assertEditorOpen();
206
207         assertFalse("Editor text field should not be disabled",
208                 null != getEditorWidgets().get(2).getAttribute("disabled"));
209     }
210
211     @Test
212     public void testMouseOpeningClosing() {
213
214         getGridElement().getCell(4, 0).doubleClick();
215         assertEditorOpen();
216
217         selectMenuPath(CANCEL_EDIT);
218         selectMenuPath(TOGGLE_EDIT_ENABLED);
219
220         getGridElement().getCell(4, 0).doubleClick();
221         assertEditorClosed();
222     }
223
224     @Test
225     public void testProgrammaticOpeningWhenOpen() {
226         selectMenuPath(EDIT_ITEM_5);
227         assertEditorOpen();
228         assertEquals("Editor should edit row 5", "(5, 0)",
229                 getEditorWidgets().get(0).getAttribute("value"));
230
231         selectMenuPath(EDIT_ITEM_100);
232         assertEditorOpen();
233         assertEquals("Editor should edit row 100", "(100, 0)",
234                 getEditorWidgets().get(0).getAttribute("value"));
235     }
236
237     @Test
238     public void testExternalValueChangePassesToEditor() {
239         selectMenuPath(EDIT_ITEM_5);
240         assertEditorOpen();
241
242         selectMenuPath("Component", "State", "ReactiveValueChanger");
243
244         getEditorWidgets().get(0).click();
245         getEditorWidgets().get(0).sendKeys("changing value");
246
247         // Focus another field to cause the value to be sent to the server
248         getEditorWidgets().get(2).click();
249
250         assertEquals("Value of Column 2 in the editor was not changed",
251                 "Modified", getEditorWidgets().get(2).getAttribute("value"));
252     }
253
254     @Test
255     public void testEditorClosedOnUserSort() {
256         selectMenuPath(EDIT_ITEM_5);
257
258         getGridElement().getHeaderCell(0, 0).click();
259
260         assertEditorClosed();
261     }
262
263     @Test
264     public void testEditorSaveOnRowChange() {
265         // Double click sets the focus programmatically
266         getGridElement().getCell(5, 2).doubleClick();
267
268         TestBenchElement editor = getGridElement().getEditor().getField(2);
269         editor.clear();
270         // Click to ensure IE focus...
271         editor.click(5, 5);
272         editor.sendKeys("Foo", Keys.ENTER);
273
274         assertEquals("Editor did not move.", "(6, 0)",
275                 getGridElement().getEditor().getField(0).getAttribute("value"));
276         assertEquals("Editor field value did not update from server.", "(6, 2)",
277                 getGridElement().getEditor().getField(2).getAttribute("value"));
278
279         assertEquals("Edited value was not saved.", "Foo",
280                 getGridElement().getCell(5, 2).getText());
281     }
282 }