You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GridEditorClientTest.java 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertFalse;
  19. import static org.junit.Assert.assertNotEquals;
  20. import static org.junit.Assert.assertNotNull;
  21. import static org.junit.Assert.assertNull;
  22. import static org.junit.Assert.assertTrue;
  23. import java.util.List;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. import org.openqa.selenium.By;
  27. import org.openqa.selenium.Keys;
  28. import org.openqa.selenium.NoSuchElementException;
  29. import org.openqa.selenium.WebElement;
  30. import org.openqa.selenium.interactions.Actions;
  31. import com.vaadin.testbench.elements.GridElement.GridCellElement;
  32. import com.vaadin.testbench.elements.GridElement.GridEditorElement;
  33. import com.vaadin.testbench.parallel.BrowserUtil;
  34. import com.vaadin.tests.components.grid.basicfeatures.GridBasicClientFeaturesTest;
  35. import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeatures;
  36. import com.vaadin.v7.shared.ui.grid.GridConstants;
  37. public class GridEditorClientTest extends GridBasicClientFeaturesTest {
  38. private static final String[] EDIT_ROW_100 = new String[] { "Component",
  39. "Editor", "Edit row 100" };
  40. private static final String[] EDIT_ROW_5 = new String[] { "Component",
  41. "Editor", "Edit row 5" };
  42. @Before
  43. public void setUp() {
  44. openTestURL();
  45. selectMenuPath("Component", "Editor", "Enabled");
  46. }
  47. @Test
  48. public void testProgrammaticOpeningClosing() {
  49. selectMenuPath(EDIT_ROW_5);
  50. assertNotNull(getEditor());
  51. selectMenuPath("Component", "Editor", "Cancel edit");
  52. assertNull(getEditor());
  53. assertEquals("Row 5 edit cancelled",
  54. findElement(By.className("grid-editor-log")).getText());
  55. }
  56. @Test
  57. public void testProgrammaticOpeningWithScroll() {
  58. selectMenuPath(EDIT_ROW_100);
  59. assertNotNull(getEditor());
  60. }
  61. @Test(expected = NoSuchElementException.class)
  62. public void testVerticalScrollLocking() {
  63. selectMenuPath(EDIT_ROW_5);
  64. getGridElement().getCell(200, 0);
  65. }
  66. @Test
  67. public void testMouseOpeningClosing() {
  68. getGridElement().getCell(4, 0).doubleClick();
  69. assertNotNull(getEditor());
  70. // Move focus to the third input field
  71. getEditor().findElements(By.className("gwt-TextBox")).get(2).click();
  72. // Press save button
  73. getSaveButton().click();
  74. // Make sure the editor went away
  75. assertNull(getEditor());
  76. // Check that focus has moved to cell 4,2 - the last one that was
  77. // focused in Editor
  78. assertTrue(getGridElement().getCell(4, 2).isFocused());
  79. // Disable editor
  80. selectMenuPath("Component", "Editor", "Enabled");
  81. getGridElement().getCell(4, 0).doubleClick();
  82. assertNull(getEditor());
  83. }
  84. @Test
  85. public void testKeyboardOpeningClosing() {
  86. getGridElement().getCell(4, 0).click();
  87. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  88. assertNotNull(getEditor());
  89. new Actions(getDriver()).sendKeys(Keys.ESCAPE).perform();
  90. assertNull(getEditor());
  91. assertEquals("Row 4 edit cancelled",
  92. findElement(By.className("grid-editor-log")).getText());
  93. // Disable editor
  94. selectMenuPath("Component", "Editor", "Enabled");
  95. getGridElement().getCell(5, 0).click();
  96. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  97. assertNull(getEditor());
  98. }
  99. @Test
  100. public void testWidgetBinding() throws Exception {
  101. selectMenuPath(EDIT_ROW_100);
  102. WebElement editor = getEditor();
  103. List<WebElement> widgets = editor
  104. .findElements(By.className("gwt-TextBox"));
  105. assertEquals(GridBasicFeatures.EDITABLE_COLUMNS, widgets.size());
  106. assertEquals("(100, 0)", widgets.get(0).getAttribute("value"));
  107. assertEquals("(100, 1)", widgets.get(1).getAttribute("value"));
  108. assertEquals("(100, 2)", widgets.get(2).getAttribute("value"));
  109. assertEquals("100", widgets.get(6).getAttribute("value"));
  110. assertEquals("<b>100</b>", widgets.get(8).getAttribute("value"));
  111. }
  112. @Test
  113. public void testWithSelectionColumn() throws Exception {
  114. selectMenuPath("Component", "State", "Selection mode", "multi");
  115. selectMenuPath("Component", "State", "Frozen column count",
  116. "-1 columns");
  117. selectMenuPath(EDIT_ROW_5);
  118. WebElement editorCells = findElements(
  119. By.className("v-grid-editor-cells")).get(1);
  120. List<WebElement> selectorDivs = editorCells
  121. .findElements(By.cssSelector("div"));
  122. assertFalse("selector column cell should've had contents",
  123. selectorDivs.get(0).getAttribute("innerHTML").isEmpty());
  124. assertFalse("normal column cell shoul've had contents",
  125. selectorDivs.get(1).getAttribute("innerHTML").isEmpty());
  126. }
  127. @Test
  128. public void testSave() {
  129. selectMenuPath(EDIT_ROW_100);
  130. WebElement textField = getEditor()
  131. .findElements(By.className("gwt-TextBox")).get(0);
  132. textField.clear();
  133. textField.sendKeys("Changed");
  134. WebElement saveButton = getEditor()
  135. .findElement(By.className("v-grid-editor-save"));
  136. saveButton.click();
  137. assertEquals("Changed", getGridElement().getCell(100, 0).getText());
  138. }
  139. @Test
  140. public void testProgrammaticSave() {
  141. selectMenuPath(EDIT_ROW_100);
  142. WebElement textField = getEditor()
  143. .findElements(By.className("gwt-TextBox")).get(0);
  144. textField.clear();
  145. textField.sendKeys("Changed");
  146. selectMenuPath("Component", "Editor", "Save");
  147. assertEquals("Changed", getGridElement().getCell(100, 0).getText());
  148. }
  149. @Test
  150. public void testCaptionChange() {
  151. selectMenuPath(EDIT_ROW_5);
  152. assertEquals("Save button caption should've been \""
  153. + GridConstants.DEFAULT_SAVE_CAPTION + "\" to begin with",
  154. GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());
  155. assertEquals("Cancel button caption should've been \""
  156. + GridConstants.DEFAULT_CANCEL_CAPTION + "\" to begin with",
  157. GridConstants.DEFAULT_CANCEL_CAPTION,
  158. getCancelButton().getText());
  159. selectMenuPath("Component", "Editor", "Change Save Caption");
  160. assertNotEquals(
  161. "Save button caption should've changed while editor is open",
  162. GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());
  163. getCancelButton().click();
  164. selectMenuPath("Component", "Editor", "Change Cancel Caption");
  165. selectMenuPath(EDIT_ROW_5);
  166. assertNotEquals(
  167. "Cancel button caption should've changed while editor is closed",
  168. GridConstants.DEFAULT_CANCEL_CAPTION,
  169. getCancelButton().getText());
  170. }
  171. @Test
  172. public void testUneditableColumn() {
  173. selectMenuPath("Component", "Editor", "Edit row 5");
  174. assertFalse("Uneditable column should not have an editor widget",
  175. getGridElement().getEditor().isEditable(3));
  176. }
  177. @Test
  178. public void testErrorField() {
  179. selectMenuPath(EDIT_ROW_5);
  180. GridEditorElement editor = getGridElement().getEditor();
  181. assertTrue("No errors should be present",
  182. editor.findElements(By.className("error")).isEmpty());
  183. assertEquals("No error message should be present", null,
  184. editor.getErrorMessage());
  185. selectMenuPath("Component", "Editor", "Toggle second editor error");
  186. getSaveButton().click();
  187. assertEquals("Unexpected amount of error fields", 1,
  188. editor.findElements(By.className("error")).size());
  189. assertEquals("Unexpedted error message",
  190. "Syntethic fail of editor in column 2. "
  191. + "This message is so long that it doesn't fit into its box",
  192. editor.getErrorMessage());
  193. }
  194. @Test
  195. public void testFocusOnMouseOpen() {
  196. GridCellElement cell = getGridElement().getCell(4, 2);
  197. cell.doubleClick();
  198. WebElement focused = getFocusedElement();
  199. assertEquals("", "input", focused.getTagName());
  200. assertEquals("", cell.getText(), focused.getAttribute("value"));
  201. }
  202. @Test
  203. public void testFocusOnKeyboardOpen() {
  204. GridCellElement cell = getGridElement().getCell(4, 2);
  205. cell.click();
  206. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  207. WebElement focused = getFocusedElement();
  208. assertEquals("", "input", focused.getTagName());
  209. assertEquals("", cell.getText(), focused.getAttribute("value"));
  210. }
  211. @Test
  212. public void testNoFocusOnProgrammaticOpen() {
  213. selectMenuPath(EDIT_ROW_5);
  214. WebElement focused = getFocusedElement();
  215. if (BrowserUtil.isIE(getDesiredCapabilities())) {
  216. assertEquals("Focus should be nowhere", null, focused);
  217. } else {
  218. // GWT menubar loses focus after clicking a menuitem
  219. assertEquals("Focus should be in body", "body",
  220. focused.getTagName());
  221. }
  222. }
  223. protected WebElement getSaveButton() {
  224. return getEditor().findElement(By.className("v-grid-editor-save"));
  225. }
  226. protected WebElement getCancelButton() {
  227. return getEditor().findElement(By.className("v-grid-editor-cancel"));
  228. }
  229. }