Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

GridEditorTest.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.basics;
  17. import org.junit.Assert;
  18. import org.junit.Before;
  19. import org.junit.Ignore;
  20. import org.junit.Test;
  21. import org.openqa.selenium.Keys;
  22. import org.openqa.selenium.WebElement;
  23. import org.openqa.selenium.interactions.Actions;
  24. import com.vaadin.testbench.By;
  25. import com.vaadin.testbench.TestBenchElement;
  26. import com.vaadin.testbench.elements.GridElement.GridCellElement;
  27. import com.vaadin.testbench.elements.GridElement.GridEditorElement;
  28. import static org.junit.Assert.*;
  29. public abstract class GridEditorTest extends GridBasicsTest {
  30. protected static final org.openqa.selenium.By BY_EDITOR_CANCEL = By
  31. .className("v-grid-editor-cancel");
  32. protected static final org.openqa.selenium.By BY_EDITOR_SAVE = By
  33. .className("v-grid-editor-save");
  34. protected static final String[] TOGGLE_EDIT_ENABLED = { "Component",
  35. "Editor", "Enabled" };
  36. @Override
  37. @Before
  38. public void setUp() {
  39. setDebug(true);
  40. openTestURL();
  41. selectMenuPath(TOGGLE_EDIT_ENABLED);
  42. }
  43. @Test
  44. public void testProgrammaticClosing() {
  45. editRow(5);
  46. assertEditorOpen();
  47. selectMenuPath("Component", "Editor", "Cancel edit");
  48. assertEditorClosed();
  49. }
  50. public void testEditorReopenAfterHide() {
  51. editRow(5);
  52. assertEditorOpen();
  53. selectMenuPath("Component", "Editor", "Hide grid");
  54. selectMenuPath("Component", "Editor", "Show grid");
  55. assertEditorClosed();
  56. editRow(5);
  57. assertEditorOpen();
  58. }
  59. @Test
  60. public void testKeyboardOpeningClosing() {
  61. getGridElement().getCell(4, 0).click();
  62. assertEditorClosed();
  63. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  64. assertEditorOpen();
  65. new Actions(getDriver()).sendKeys(Keys.ESCAPE).perform();
  66. assertEditorClosed();
  67. // Disable Editor
  68. selectMenuPath(TOGGLE_EDIT_ENABLED);
  69. getGridElement().getCell(5, 0).click();
  70. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  71. assertEditorClosed();
  72. }
  73. protected void assertEditorOpen() {
  74. waitUntil(driver -> getGridElement()
  75. .isElementPresent(By.vaadin("#editor")));
  76. }
  77. protected void assertEditorClosed() {
  78. assertFalse("Editor is supposed to be closed",
  79. getGridElement().isElementPresent(By.vaadin("#editor")));
  80. }
  81. @Test
  82. public void testFocusOnMouseOpen() {
  83. GridCellElement cell = getGridElement().getCell(4, 0);
  84. cell.doubleClick();
  85. WebElement focused = getFocusedElement();
  86. assertEquals("", "input", focused.getTagName());
  87. assertEquals("", cell.getText(), focused.getAttribute("value"));
  88. }
  89. @Test
  90. public void testFocusOnKeyboardOpen() {
  91. GridCellElement cell = getGridElement().getCell(4, 0);
  92. cell.click();
  93. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  94. WebElement focused = getFocusedElement();
  95. assertEquals("", "input", focused.getTagName());
  96. assertEquals("", cell.getText(), focused.getAttribute("value"));
  97. }
  98. @Test
  99. public void testUneditableColumn() {
  100. editRow(5);
  101. assertEditorOpen();
  102. GridEditorElement editor = getGridElement().getEditor();
  103. assertFalse("Uneditable column should not have an editor widget",
  104. editor.isEditable(2));
  105. String classNames = editor
  106. .findElements(By.className("v-grid-editor-cells")).get(1)
  107. .findElements(By.xpath("./div")).get(2).getAttribute("class");
  108. assertTrue("Noneditable cell should contain not-editable classname",
  109. classNames.contains("not-editable"));
  110. assertTrue("Noneditable cell should contain v-grid-cell classname",
  111. classNames.contains("v-grid-cell"));
  112. assertNoErrorNotifications();
  113. }
  114. @Test
  115. public void testNoOpenFromHeaderOrFooter() {
  116. selectMenuPath("Component", "Footer", "Append footer row");
  117. getGridElement().getHeaderCell(0, 0).doubleClick();
  118. assertEditorClosed();
  119. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  120. assertEditorClosed();
  121. getGridElement().getFooterCell(0, 0).doubleClick();
  122. assertEditorClosed();
  123. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  124. assertEditorClosed();
  125. }
  126. public void testEditorMoveOnResize() {
  127. selectMenuPath("Component", "Size", "Height", "500px");
  128. getGridElement().getCell(22, 0).doubleClick();
  129. assertEditorOpen();
  130. GridEditorElement editor = getGridElement().getEditor();
  131. TestBenchElement tableWrapper = getGridElement().getTableWrapper();
  132. int tableWrapperBottom = tableWrapper.getLocation().getY()
  133. + tableWrapper.getSize().getHeight();
  134. int editorBottom = editor.getLocation().getY()
  135. + editor.getSize().getHeight();
  136. assertTrue("Editor should not be initially outside grid",
  137. tableWrapperBottom - editorBottom <= 2);
  138. selectMenuPath("Component", "Size", "Height", "300px");
  139. assertEditorOpen();
  140. tableWrapperBottom = tableWrapper.getLocation().getY()
  141. + tableWrapper.getSize().getHeight();
  142. editorBottom = editor.getLocation().getY()
  143. + editor.getSize().getHeight();
  144. assertTrue("Editor should not be outside grid after resize",
  145. tableWrapperBottom - editorBottom <= 2);
  146. }
  147. public void testEditorDoesNotMoveOnResizeIfNotNeeded() {
  148. selectMenuPath("Component", "Size", "Height", "500px");
  149. editRow(5);
  150. assertEditorOpen();
  151. GridEditorElement editor = getGridElement().getEditor();
  152. int editorPos = editor.getLocation().getY();
  153. selectMenuPath("Component", "Size", "Height", "300px");
  154. assertEditorOpen();
  155. assertTrue("Editor should not have moved due to resize",
  156. editorPos == editor.getLocation().getY());
  157. }
  158. @Test
  159. public void testEditorClosedOnSort() {
  160. editRow(5);
  161. selectMenuPath("Component", "Columns", "Column 0", "Sort ASC");
  162. assertEditorClosed();
  163. }
  164. @Ignore("Needs programmatic filtering")
  165. @Test
  166. public void testEditorClosedOnFilter() {
  167. editRow(5);
  168. selectMenuPath("Component", "Filter", "Column 1 starts with \"(23\"");
  169. assertEditorClosed();
  170. }
  171. @Test
  172. public void testEditorOpeningFromServer() {
  173. selectMenuPath("Component", "Editor", "Edit row 5");
  174. assertEditorOpen();
  175. Assert.assertEquals("Unexpected editor field content", "5",
  176. getEditor().getField(3).getAttribute("value"));
  177. Assert.assertEquals("Unexpected not-editable column content", "(5, 1)",
  178. getEditor().findElement(By.className("not-editable"))
  179. .getText());
  180. }
  181. @Test
  182. public void testEditorOpenWithScrollFromServer() {
  183. selectMenuPath("Component", "Editor", "Edit last row");
  184. assertEditorOpen();
  185. Assert.assertEquals("Unexpected editor field content", "999",
  186. getEditor().getField(3).getAttribute("value"));
  187. Assert.assertEquals("Unexpected not-editable column content",
  188. "(999, 1)", getEditor()
  189. .findElement(By.className("not-editable")).getText());
  190. }
  191. protected WebElement getSaveButton() {
  192. return getDriver().findElement(BY_EDITOR_SAVE);
  193. }
  194. protected WebElement getCancelButton() {
  195. return getDriver().findElement(BY_EDITOR_CANCEL);
  196. }
  197. protected void editRow(int rowIndex) {
  198. getGridElement().getCell(rowIndex, 0).doubleClick();
  199. assertEditorOpen();
  200. }
  201. protected boolean isEditorCellErrorMarked(int colIndex) {
  202. WebElement editorCell = getGridElement().getEditor()
  203. .findElement(By.xpath("./div/div[" + (colIndex + 1) + "]"));
  204. return editorCell.getAttribute("class").contains("error");
  205. }
  206. }