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.

GridEditorBufferedTest.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. 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.assertTrue;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import org.openqa.selenium.By;
  24. import org.openqa.selenium.Keys;
  25. import org.openqa.selenium.NoSuchElementException;
  26. import org.openqa.selenium.WebElement;
  27. import org.openqa.selenium.interactions.Actions;
  28. import com.vaadin.testbench.TestBenchElement;
  29. import com.vaadin.testbench.elements.GridElement.GridCellElement;
  30. import com.vaadin.testbench.elements.GridElement.GridEditorElement;
  31. import com.vaadin.v7.shared.ui.grid.GridConstants;
  32. import com.vaadin.testbench.elements.NotificationElement;
  33. public class GridEditorBufferedTest extends GridEditorTest {
  34. @Override
  35. @Before
  36. public void setUp() {
  37. super.setUp();
  38. }
  39. @Test
  40. public void testKeyboardSave() {
  41. selectMenuPath(EDIT_ITEM_100);
  42. WebElement textField = getEditorWidgets().get(0);
  43. textField.click();
  44. // without this, the click in the middle of the field might not be after
  45. // the old text on some browsers
  46. new Actions(getDriver()).sendKeys(Keys.END).perform();
  47. textField.sendKeys(" changed");
  48. // Save from keyboard
  49. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  50. assertEditorClosed();
  51. assertEquals("(100, 0) changed",
  52. getGridElement().getCell(100, 0).getText());
  53. }
  54. @Test
  55. public void testKeyboardSaveWithInvalidEdition() {
  56. makeInvalidEdition();
  57. GridEditorElement editor = getGridElement().getEditor();
  58. TestBenchElement field = editor.getField(7);
  59. field.click();
  60. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  61. assertEditorOpen();
  62. assertEquals("Column 7: Could not convert value to Integer",
  63. editor.getErrorMessage());
  64. assertTrue("Field 7 should have been marked with an error after error",
  65. editor.isFieldErrorMarked(7));
  66. editor.cancel();
  67. selectMenuPath(EDIT_ITEM_100);
  68. assertFalse("Exception should not exist",
  69. isElementPresent(NotificationElement.class));
  70. assertEquals("There should be no editor error message", null,
  71. getGridElement().getEditor().getErrorMessage());
  72. }
  73. @Test
  74. public void testSave() {
  75. selectMenuPath(EDIT_ITEM_100);
  76. WebElement textField = getEditorWidgets().get(0);
  77. textField.click();
  78. // without this, the click in the middle of the field might not be after
  79. // the old text on some browsers
  80. new Actions(getDriver()).sendKeys(Keys.END).perform();
  81. textField.sendKeys(" changed");
  82. WebElement saveButton = getEditor()
  83. .findElement(By.className("v-grid-editor-save"));
  84. saveButton.click();
  85. assertEquals("(100, 0) changed",
  86. getGridElement().getCell(100, 0).getText());
  87. }
  88. @Test
  89. public void testProgrammaticSave() {
  90. selectMenuPath(EDIT_ITEM_100);
  91. WebElement textField = getEditorWidgets().get(0);
  92. textField.click();
  93. // without this, the click in the middle of the field might not be after
  94. // the old text on some browsers
  95. new Actions(getDriver()).sendKeys(Keys.END).perform();
  96. textField.sendKeys(" changed");
  97. selectMenuPath("Component", "Editor", "Save");
  98. assertEquals("(100, 0) changed",
  99. getGridElement().getCell(100, 0).getText());
  100. }
  101. @Test
  102. public void testInvalidEdition() {
  103. makeInvalidEdition();
  104. GridEditorElement editor = getGridElement().getEditor();
  105. editor.save();
  106. assertEquals("Column 7: Could not convert value to Integer",
  107. editor.getErrorMessage());
  108. assertTrue("Field 7 should have been marked with an error after error",
  109. editor.isFieldErrorMarked(7));
  110. editor.cancel();
  111. selectMenuPath(EDIT_ITEM_100);
  112. assertFalse("Exception should not exist",
  113. isElementPresent(NotificationElement.class));
  114. assertEquals("There should be no editor error message", null,
  115. getGridElement().getEditor().getErrorMessage());
  116. }
  117. private void makeInvalidEdition() {
  118. selectMenuPath(EDIT_ITEM_5);
  119. assertFalse(logContainsText(
  120. "Exception occured, java.lang.IllegalStateException"));
  121. GridEditorElement editor = getGridElement().getEditor();
  122. assertFalse(
  123. "Field 7 should not have been marked with an error before error",
  124. editor.isFieldErrorMarked(7));
  125. WebElement intField = editor.getField(7);
  126. intField.clear();
  127. intField.sendKeys("banana phone");
  128. }
  129. @Test
  130. public void testEditorInDisabledGrid() {
  131. int originalScrollPos = getGridVerticalScrollPos();
  132. selectMenuPath(EDIT_ITEM_5);
  133. assertEditorOpen();
  134. selectMenuPath("Component", "State", "Enabled");
  135. assertEditorOpen();
  136. GridEditorElement editor = getGridElement().getEditor();
  137. editor.save();
  138. assertEditorOpen();
  139. editor.cancel();
  140. assertEditorOpen();
  141. selectMenuPath("Component", "State", "Enabled");
  142. scrollGridVerticallyTo(100);
  143. assertEquals(
  144. "Grid shouldn't scroll vertically while editing in buffered mode",
  145. originalScrollPos, getGridVerticalScrollPos());
  146. }
  147. @Test
  148. public void testCaptionChange() {
  149. selectMenuPath(EDIT_ITEM_5);
  150. assertEquals("Save button caption should've been \""
  151. + GridConstants.DEFAULT_SAVE_CAPTION + "\" to begin with",
  152. GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());
  153. assertEquals("Cancel button caption should've been \""
  154. + GridConstants.DEFAULT_CANCEL_CAPTION + "\" to begin with",
  155. GridConstants.DEFAULT_CANCEL_CAPTION,
  156. getCancelButton().getText());
  157. selectMenuPath("Component", "Editor", "Change save caption");
  158. assertNotEquals(
  159. "Save button caption should've changed while editor is open",
  160. GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());
  161. getCancelButton().click();
  162. selectMenuPath("Component", "Editor", "Change cancel caption");
  163. selectMenuPath(EDIT_ITEM_5);
  164. assertNotEquals(
  165. "Cancel button caption should've changed while editor is closed",
  166. GridConstants.DEFAULT_CANCEL_CAPTION,
  167. getCancelButton().getText());
  168. }
  169. @Test(expected = NoSuchElementException.class)
  170. public void testVerticalScrollLocking() {
  171. selectMenuPath(EDIT_ITEM_5);
  172. getGridElement().getCell(200, 0);
  173. }
  174. @Test
  175. public void testScrollDisabledOnProgrammaticOpen() {
  176. int originalScrollPos = getGridVerticalScrollPos();
  177. selectMenuPath(EDIT_ITEM_5);
  178. scrollGridVerticallyTo(100);
  179. assertEquals(
  180. "Grid shouldn't scroll vertically while editing in buffered mode",
  181. originalScrollPos, getGridVerticalScrollPos());
  182. }
  183. @Test
  184. public void testScrollDisabledOnMouseOpen() {
  185. int originalScrollPos = getGridVerticalScrollPos();
  186. GridCellElement cell_5_0 = getGridElement().getCell(5, 0);
  187. new Actions(getDriver()).doubleClick(cell_5_0).perform();
  188. scrollGridVerticallyTo(100);
  189. assertEquals(
  190. "Grid shouldn't scroll vertically while editing in buffered mode",
  191. originalScrollPos, getGridVerticalScrollPos());
  192. }
  193. @Test
  194. public void testScrollDisabledOnKeyboardOpen() {
  195. int originalScrollPos = getGridVerticalScrollPos();
  196. GridCellElement cell_5_0 = getGridElement().getCell(5, 0);
  197. cell_5_0.click();
  198. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  199. scrollGridVerticallyTo(100);
  200. assertEquals(
  201. "Grid shouldn't scroll vertically while editing in buffered mode",
  202. originalScrollPos, getGridVerticalScrollPos());
  203. }
  204. @Test
  205. public void testMouseOpeningClosing() {
  206. getGridElement().getCell(4, 0).doubleClick();
  207. assertEditorOpen();
  208. getCancelButton().click();
  209. assertEditorClosed();
  210. selectMenuPath(TOGGLE_EDIT_ENABLED);
  211. getGridElement().getCell(4, 0).doubleClick();
  212. assertEditorClosed();
  213. }
  214. @Test
  215. public void testMouseOpeningDisabledWhenOpen() {
  216. selectMenuPath(EDIT_ITEM_5);
  217. getGridElement().getCell(4, 0).doubleClick();
  218. assertEquals("Editor should still edit row 5", "(5, 0)",
  219. getEditorWidgets().get(0).getAttribute("value"));
  220. }
  221. @Test
  222. public void testProgrammaticOpeningDisabledWhenOpen() {
  223. selectMenuPath(EDIT_ITEM_5);
  224. assertEditorOpen();
  225. assertEquals("Editor should edit row 5", "(5, 0)",
  226. getEditorWidgets().get(0).getAttribute("value"));
  227. selectMenuPath(EDIT_ITEM_100);
  228. boolean thrown = logContainsText(
  229. "Exception occured, java.lang.IllegalStateException");
  230. assertTrue("IllegalStateException thrown", thrown);
  231. assertEditorOpen();
  232. assertEquals("Editor should still edit row 5", "(5, 0)",
  233. getEditorWidgets().get(0).getAttribute("value"));
  234. }
  235. @Test
  236. public void testUserSortDisabledWhenOpen() {
  237. selectMenuPath(EDIT_ITEM_5);
  238. getGridElement().getHeaderCell(0, 0).click();
  239. assertEditorOpen();
  240. assertEquals("(2, 0)", getGridElement().getCell(2, 0).getText());
  241. }
  242. }