]> source.dussan.org Git - vaadin-framework.git/blob
abbd954b9b03ead42cafa7b9b673c902c2455476
[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.client;
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.assertNotNull;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertTrue;
24
25 import java.util.List;
26
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;
34
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;
41
42 public class GridEditorClientTest extends GridBasicClientFeaturesTest {
43
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" };
48
49     @Before
50     public void setUp() {
51         openTestURL();
52         selectMenuPath("Component", "Editor", "Enabled");
53     }
54
55     @Test
56     public void testProgrammaticOpeningClosing() {
57         selectMenuPath(EDIT_ROW_5);
58         assertNotNull(getEditor());
59
60         selectMenuPath("Component", "Editor", "Cancel edit");
61         assertNull(getEditor());
62         assertEquals("Row 5 edit cancelled",
63                 findElement(By.className("grid-editor-log")).getText());
64     }
65
66     @Test
67     public void testProgrammaticOpeningWithScroll() {
68         selectMenuPath(EDIT_ROW_100);
69         assertNotNull(getEditor());
70     }
71
72     @Test(expected = NoSuchElementException.class)
73     public void testVerticalScrollLocking() {
74         selectMenuPath(EDIT_ROW_5);
75         getGridElement().getCell(200, 0);
76     }
77
78     @Test
79     public void testMouseOpeningClosing() {
80
81         getGridElement().getCell(4, 0).doubleClick();
82         assertNotNull(getEditor());
83
84         // Move focus to the third input field
85         getEditor().findElements(By.className("gwt-TextBox")).get(2).click();
86
87         // Press save button
88         getSaveButton().click();
89
90         // Make sure the editor went away
91         assertNull(getEditor());
92
93         // Check that focus has moved to cell 4,2 - the last one that was
94         // focused in Editor
95         assertTrue(getGridElement().getCell(4, 2).isFocused());
96
97         // Disable editor
98         selectMenuPath("Component", "Editor", "Enabled");
99
100         getGridElement().getCell(4, 0).doubleClick();
101         assertNull(getEditor());
102     }
103
104     @Test
105     public void testKeyboardOpeningClosing() {
106
107         getGridElement().getCell(4, 0).click();
108
109         new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
110
111         assertNotNull(getEditor());
112
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());
117
118         // Disable editor
119         selectMenuPath("Component", "Editor", "Enabled");
120
121         getGridElement().getCell(5, 0).click();
122         new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
123         assertNull(getEditor());
124     }
125
126     @Test
127     public void testWidgetBinding() throws Exception {
128         selectMenuPath(EDIT_ROW_100);
129         WebElement editor = getEditor();
130
131         List<WebElement> widgets = editor
132                 .findElements(By.className("gwt-TextBox"));
133
134         assertEquals(GridBasicFeatures.EDITABLE_COLUMNS, widgets.size());
135
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"));
139
140         assertEquals("100", widgets.get(6).getAttribute("value"));
141         assertEquals("<b>100</b>", widgets.get(8).getAttribute("value"));
142     }
143
144     @Test
145     public void testWithSelectionColumn() throws Exception {
146         selectMenuPath("Component", "State", "Selection mode", "multi");
147         selectMenuPath("Component", "State", "Frozen column count",
148                 "-1 columns");
149         selectMenuPath(EDIT_ROW_5);
150
151         WebElement editorCells = findElements(
152                 By.className("v-grid-editor-cells")).get(1);
153         List<WebElement> selectorDivs = editorCells
154                 .findElements(By.cssSelector("div"));
155
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());
160     }
161
162     @Test
163     public void testSave() {
164         selectMenuPath(EDIT_ROW_100);
165
166         WebElement textField = getEditor()
167                 .findElements(By.className("gwt-TextBox")).get(0);
168
169         textField.clear();
170         textField.sendKeys("Changed");
171
172         WebElement saveButton = getEditor()
173                 .findElement(By.className("v-grid-editor-save"));
174
175         saveButton.click();
176
177         assertEquals("Changed", getGridElement().getCell(100, 0).getText());
178     }
179
180     @Test
181     public void testProgrammaticSave() {
182         selectMenuPath(EDIT_ROW_100);
183
184         WebElement textField = getEditor()
185                 .findElements(By.className("gwt-TextBox")).get(0);
186
187         textField.clear();
188         textField.sendKeys("Changed");
189
190         selectMenuPath("Component", "Editor", "Save");
191
192         assertEquals("Changed", getGridElement().getCell(100, 0).getText());
193     }
194
195     @Test
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());
205
206         selectMenuPath("Component", "Editor", "Change Save Caption");
207         assertNotEquals(
208                 "Save button caption should've changed while editor is open",
209                 GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());
210
211         getCancelButton().click();
212
213         selectMenuPath("Component", "Editor", "Change Cancel Caption");
214         selectMenuPath(EDIT_ROW_5);
215         assertNotEquals(
216                 "Cancel button caption should've changed while editor is closed",
217                 GridConstants.DEFAULT_CANCEL_CAPTION,
218                 getCancelButton().getText());
219     }
220
221     @Test
222     public void testUneditableColumn() {
223         selectMenuPath("Component", "Editor", "Edit row 5");
224
225         assertFalse("Uneditable column should not have an editor widget",
226                 getGridElement().getEditor().isEditable(3));
227     }
228
229     @Test
230     public void testErrorField() {
231         selectMenuPath(EDIT_ROW_5);
232
233         GridEditorElement editor = getGridElement().getEditor();
234
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());
239
240         selectMenuPath("Component", "Editor", "Toggle second editor error");
241         getSaveButton().click();
242
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());
249     }
250
251     @Test
252     public void testFocusOnMouseOpen() {
253
254         GridCellElement cell = getGridElement().getCell(4, 2);
255
256         cell.doubleClick();
257
258         WebElement focused = getFocusedElement();
259
260         assertEquals("", "input", focused.getTagName());
261         assertEquals("", cell.getText(), focused.getAttribute("value"));
262     }
263
264     @Test
265     public void testFocusOnKeyboardOpen() {
266
267         GridCellElement cell = getGridElement().getCell(4, 2);
268
269         cell.click();
270         new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
271
272         WebElement focused = getFocusedElement();
273
274         assertEquals("", "input", focused.getTagName());
275         assertEquals("", cell.getText(), focused.getAttribute("value"));
276     }
277
278     @Test
279     public void testNoFocusOnProgrammaticOpen() {
280
281         selectMenuPath(EDIT_ROW_5);
282
283         WebElement focused = getFocusedElement();
284
285         if (BrowserUtil.isIE(getDesiredCapabilities())) {
286             assertEquals("Focus should be nowhere", null, focused);
287         } else {
288             // GWT menubar loses focus after clicking a menuitem
289             assertEquals("Focus should be in body", "body",
290                     focused.getTagName());
291         }
292     }
293
294     protected WebElement getSaveButton() {
295         return getEditor().findElement(By.className("v-grid-editor-save"));
296     }
297
298     protected WebElement getCancelButton() {
299         return getEditor().findElement(By.className("v-grid-editor-cancel"));
300     }
301 }