選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

GridEditorTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. 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.shared.ui.grid.GridConstants;
  32. import com.vaadin.testbench.elements.GridElement.GridCellElement;
  33. import com.vaadin.testbench.elements.GridElement.GridEditorElement;
  34. import com.vaadin.testbench.elements.NotificationElement;
  35. import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeatures;
  36. import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeaturesTest;
  37. public class GridEditorTest extends GridBasicFeaturesTest {
  38. private static final String[] EDIT_ITEM_5 = new String[] { "Component",
  39. "Editor", "Edit item 5" };
  40. private static final String[] EDIT_ITEM_100 = new String[] { "Component",
  41. "Editor", "Edit item 100" };
  42. private static final String[] TOGGLE_EDIT_ENABLED = new String[] {
  43. "Component", "Editor", "Enabled" };
  44. @Before
  45. public void setUp() {
  46. setDebug(true);
  47. openTestURL();
  48. selectMenuPath(TOGGLE_EDIT_ENABLED);
  49. }
  50. @Test
  51. public void testProgrammaticOpeningClosing() {
  52. selectMenuPath(EDIT_ITEM_5);
  53. assertEditorOpen();
  54. selectMenuPath("Component", "Editor", "Cancel edit");
  55. assertEditorClosed();
  56. }
  57. @Test
  58. public void testProgrammaticOpeningWhenDisabled() {
  59. selectMenuPath(TOGGLE_EDIT_ENABLED);
  60. selectMenuPath(EDIT_ITEM_5);
  61. assertEditorClosed();
  62. boolean thrown = logContainsText("Exception occured, java.lang.IllegalStateException");
  63. assertTrue("IllegalStateException thrown", thrown);
  64. }
  65. @Test
  66. public void testDisablingWhileOpen() {
  67. selectMenuPath(EDIT_ITEM_5);
  68. selectMenuPath(TOGGLE_EDIT_ENABLED);
  69. assertEditorOpen();
  70. boolean thrown = logContainsText("Exception occured, java.lang.IllegalStateException");
  71. assertTrue("IllegalStateException thrown", thrown);
  72. }
  73. @Test
  74. public void testProgrammaticOpeningWithScroll() {
  75. selectMenuPath(EDIT_ITEM_100);
  76. assertEditorOpen();
  77. }
  78. @Test(expected = NoSuchElementException.class)
  79. public void testVerticalScrollLocking() {
  80. selectMenuPath(EDIT_ITEM_5);
  81. getGridElement().getCell(200, 0);
  82. }
  83. @Test
  84. public void testMouseOpeningClosing() {
  85. getGridElement().getCell(4, 0).doubleClick();
  86. assertEditorOpen();
  87. getCancelButton().click();
  88. assertEditorClosed();
  89. selectMenuPath(TOGGLE_EDIT_ENABLED);
  90. getGridElement().getCell(4, 0).doubleClick();
  91. assertEditorClosed();
  92. }
  93. @Test
  94. public void testKeyboardOpeningClosing() {
  95. getGridElement().getCell(4, 0).click();
  96. assertEditorClosed();
  97. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  98. assertEditorOpen();
  99. new Actions(getDriver()).sendKeys(Keys.ESCAPE).perform();
  100. assertEditorClosed();
  101. // Disable Editor
  102. selectMenuPath(TOGGLE_EDIT_ENABLED);
  103. getGridElement().getCell(5, 0).click();
  104. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  105. assertEditorClosed();
  106. }
  107. @Test
  108. public void testComponentBinding() {
  109. selectMenuPath(EDIT_ITEM_100);
  110. List<WebElement> widgets = getEditorWidgets();
  111. assertEquals("Number of widgets", GridBasicFeatures.EDITABLE_COLUMNS,
  112. widgets.size());
  113. assertEquals("(100, 0)", widgets.get(0).getAttribute("value"));
  114. assertEquals("(100, 1)", widgets.get(1).getAttribute("value"));
  115. assertEquals("(100, 2)", widgets.get(2).getAttribute("value"));
  116. assertEquals("<b>100</b>", widgets.get(8).getAttribute("value"));
  117. }
  118. @Test
  119. public void testSave() {
  120. selectMenuPath(EDIT_ITEM_100);
  121. WebElement textField = getEditorWidgets().get(0);
  122. textField.click();
  123. textField.sendKeys(" changed");
  124. WebElement saveButton = getEditor().findElement(
  125. By.className("v-grid-editor-save"));
  126. saveButton.click();
  127. assertEquals("(100, 0) changed", getGridElement().getCell(100, 0)
  128. .getText());
  129. }
  130. @Test
  131. public void testProgrammaticSave() {
  132. selectMenuPath(EDIT_ITEM_100);
  133. WebElement textField = getEditorWidgets().get(0);
  134. textField.click();
  135. textField.sendKeys(" changed");
  136. selectMenuPath("Component", "Editor", "Save");
  137. assertEquals("(100, 0) changed", getGridElement().getCell(100, 0)
  138. .getText());
  139. }
  140. @Test
  141. public void testCaptionChange() {
  142. selectMenuPath(EDIT_ITEM_5);
  143. assertEquals("Save button caption should've been \""
  144. + GridConstants.DEFAULT_SAVE_CAPTION + "\" to begin with",
  145. GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());
  146. assertEquals("Cancel button caption should've been \""
  147. + GridConstants.DEFAULT_CANCEL_CAPTION + "\" to begin with",
  148. GridConstants.DEFAULT_CANCEL_CAPTION, getCancelButton()
  149. .getText());
  150. selectMenuPath("Component", "Editor", "Change save caption");
  151. assertNotEquals(
  152. "Save button caption should've changed while editor is open",
  153. GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());
  154. getCancelButton().click();
  155. selectMenuPath("Component", "Editor", "Change cancel caption");
  156. selectMenuPath(EDIT_ITEM_5);
  157. assertNotEquals(
  158. "Cancel button caption should've changed while editor is closed",
  159. GridConstants.DEFAULT_CANCEL_CAPTION, getCancelButton()
  160. .getText());
  161. }
  162. private void assertEditorOpen() {
  163. assertNotNull("Editor is supposed to be open", getEditor());
  164. assertEquals("Unexpected number of widgets",
  165. GridBasicFeatures.EDITABLE_COLUMNS, getEditorWidgets().size());
  166. }
  167. private void assertEditorClosed() {
  168. assertNull("Editor is supposed to be closed", getEditor());
  169. }
  170. private List<WebElement> getEditorWidgets() {
  171. assertNotNull(getEditor());
  172. return getEditor().findElements(By.className("v-textfield"));
  173. }
  174. @Test
  175. public void testInvalidEdition() {
  176. selectMenuPath(EDIT_ITEM_5);
  177. assertFalse(logContainsText("Exception occured, java.lang.IllegalStateException"));
  178. GridEditorElement editor = getGridElement().getEditor();
  179. assertFalse(
  180. "Field 7 should not have been marked with an error before error",
  181. editor.isFieldErrorMarked(7));
  182. WebElement intField = editor.getField(7);
  183. intField.clear();
  184. intField.sendKeys("banana phone");
  185. editor.save();
  186. assertEquals("Column 7: Could not convert value to Integer",
  187. editor.getErrorMessage());
  188. assertTrue("Field 7 should have been marked with an error after error",
  189. editor.isFieldErrorMarked(7));
  190. editor.cancel();
  191. selectMenuPath(EDIT_ITEM_100);
  192. assertFalse("Exception should not exist",
  193. isElementPresent(NotificationElement.class));
  194. assertEquals("There should be no editor error message", null,
  195. getGridElement().getEditor().getErrorMessage());
  196. }
  197. @Test
  198. public void testNoScrollAfterProgrammaticOpen() {
  199. int originalScrollPos = getGridVerticalScrollPos();
  200. selectMenuPath(EDIT_ITEM_5);
  201. scrollGridVerticallyTo(100);
  202. assertEquals("Grid shouldn't scroll vertically while editing",
  203. originalScrollPos, getGridVerticalScrollPos());
  204. }
  205. @Test
  206. public void testNoScrollAfterMouseOpen() {
  207. int originalScrollPos = getGridVerticalScrollPos();
  208. GridCellElement cell_5_0 = getGridElement().getCell(5, 0);
  209. new Actions(getDriver()).doubleClick(cell_5_0).perform();
  210. scrollGridVerticallyTo(100);
  211. assertEquals("Grid shouldn't scroll vertically while editing",
  212. originalScrollPos, getGridVerticalScrollPos());
  213. }
  214. @Test
  215. public void testNoScrollAfterKeyboardOpen() {
  216. int originalScrollPos = getGridVerticalScrollPos();
  217. GridCellElement cell_5_0 = getGridElement().getCell(5, 0);
  218. cell_5_0.click();
  219. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  220. scrollGridVerticallyTo(100);
  221. assertEquals("Grid shouldn't scroll vertically while editing",
  222. originalScrollPos, getGridVerticalScrollPos());
  223. }
  224. @Test
  225. public void testEditorInDisabledGrid() {
  226. int originalScrollPos = getGridVerticalScrollPos();
  227. selectMenuPath(EDIT_ITEM_5);
  228. assertEditorOpen();
  229. selectMenuPath("Component", "State", "Enabled");
  230. assertEditorOpen();
  231. GridEditorElement editor = getGridElement().getEditor();
  232. editor.save();
  233. assertEditorOpen();
  234. editor.cancel();
  235. assertEditorOpen();
  236. selectMenuPath("Component", "State", "Enabled");
  237. scrollGridVerticallyTo(100);
  238. assertEquals("Grid shouldn't scroll vertically while editing",
  239. originalScrollPos, getGridVerticalScrollPos());
  240. }
  241. @Test
  242. public void testFocusOnMouseOpen() {
  243. GridCellElement cell = getGridElement().getCell(4, 2);
  244. cell.doubleClick();
  245. WebElement focused = getFocusedElement();
  246. assertEquals("", "input", focused.getTagName());
  247. assertEquals("", cell.getText(), focused.getAttribute("value"));
  248. }
  249. @Test
  250. public void testFocusOnKeyboardOpen() {
  251. GridCellElement cell = getGridElement().getCell(4, 2);
  252. cell.click();
  253. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  254. WebElement focused = getFocusedElement();
  255. assertEquals("", "input", focused.getTagName());
  256. assertEquals("", cell.getText(), focused.getAttribute("value"));
  257. }
  258. @Test
  259. public void testNoFocusOnProgrammaticOpen() {
  260. selectMenuPath(EDIT_ITEM_5);
  261. WebElement focused = getFocusedElement();
  262. assertEquals("Focus should remain in the menu", "menu",
  263. focused.getAttribute("id"));
  264. }
  265. @Override
  266. protected WebElement getFocusedElement() {
  267. return (WebElement) executeScript("return document.activeElement;");
  268. }
  269. @Test
  270. public void testUneditableColumn() {
  271. selectMenuPath(EDIT_ITEM_5);
  272. assertEditorOpen();
  273. GridEditorElement editor = getGridElement().getEditor();
  274. assertFalse("Uneditable column should not have an editor widget",
  275. editor.isEditable(3));
  276. assertEquals(
  277. "Not editable cell did not contain correct classname",
  278. "not-editable",
  279. editor.findElements(By.className("v-grid-editor-cells")).get(1)
  280. .findElements(By.xpath("./div")).get(3)
  281. .getAttribute("class"));
  282. }
  283. private WebElement getSaveButton() {
  284. return getDriver().findElement(By.className("v-grid-editor-save"));
  285. }
  286. private WebElement getCancelButton() {
  287. return getDriver().findElement(By.className("v-grid-editor-cancel"));
  288. }
  289. }