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 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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.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.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.parallel.BrowserUtil;
  35. import com.vaadin.tests.components.grid.basicfeatures.GridBasicClientFeaturesTest;
  36. import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeatures;
  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. getCancelButton().click();
  71. assertNull(getEditor());
  72. // Disable editor
  73. selectMenuPath("Component", "Editor", "Enabled");
  74. getGridElement().getCell(4, 0).doubleClick();
  75. assertNull(getEditor());
  76. }
  77. @Test
  78. public void testKeyboardOpeningClosing() {
  79. getGridElement().getCell(4, 0).click();
  80. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  81. assertNotNull(getEditor());
  82. new Actions(getDriver()).sendKeys(Keys.ESCAPE).perform();
  83. assertNull(getEditor());
  84. assertEquals("Row 4 edit cancelled",
  85. findElement(By.className("grid-editor-log")).getText());
  86. // Disable editor
  87. selectMenuPath("Component", "Editor", "Enabled");
  88. getGridElement().getCell(5, 0).click();
  89. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  90. assertNull(getEditor());
  91. }
  92. @Test
  93. public void testWidgetBinding() throws Exception {
  94. selectMenuPath(EDIT_ROW_100);
  95. WebElement editor = getEditor();
  96. List<WebElement> widgets = editor.findElements(By
  97. .className("gwt-TextBox"));
  98. assertEquals(GridBasicFeatures.EDITABLE_COLUMNS, widgets.size());
  99. assertEquals("(100, 0)", widgets.get(0).getAttribute("value"));
  100. assertEquals("(100, 1)", widgets.get(1).getAttribute("value"));
  101. assertEquals("(100, 2)", widgets.get(2).getAttribute("value"));
  102. assertEquals("100", widgets.get(6).getAttribute("value"));
  103. assertEquals("<b>100</b>", widgets.get(8).getAttribute("value"));
  104. }
  105. @Test
  106. public void testWithSelectionColumn() throws Exception {
  107. selectMenuPath("Component", "State", "Selection mode", "multi");
  108. selectMenuPath("Component", "State", "Frozen column count",
  109. "-1 columns");
  110. selectMenuPath(EDIT_ROW_5);
  111. WebElement editorCells = findElements(
  112. By.className("v-grid-editor-cells")).get(1);
  113. List<WebElement> selectorDivs = editorCells.findElements(By
  114. .cssSelector("div"));
  115. assertTrue("selector column cell should've been empty", selectorDivs
  116. .get(0).getAttribute("innerHTML").isEmpty());
  117. assertFalse("normal column cell shoul've had contents", selectorDivs
  118. .get(1).getAttribute("innerHTML").isEmpty());
  119. }
  120. @Test
  121. public void testSave() {
  122. selectMenuPath(EDIT_ROW_100);
  123. WebElement textField = getEditor().findElements(
  124. By.className("gwt-TextBox")).get(0);
  125. textField.clear();
  126. textField.sendKeys("Changed");
  127. WebElement saveButton = getEditor().findElement(
  128. By.className("v-grid-editor-save"));
  129. saveButton.click();
  130. assertEquals("Changed", getGridElement().getCell(100, 0).getText());
  131. }
  132. @Test
  133. public void testProgrammaticSave() {
  134. selectMenuPath(EDIT_ROW_100);
  135. WebElement textField = getEditor().findElements(
  136. By.className("gwt-TextBox")).get(0);
  137. textField.clear();
  138. textField.sendKeys("Changed");
  139. selectMenuPath("Component", "Editor", "Save");
  140. assertEquals("Changed", getGridElement().getCell(100, 0).getText());
  141. }
  142. @Test
  143. public void testCaptionChange() {
  144. selectMenuPath(EDIT_ROW_5);
  145. assertEquals("Save button caption should've been \""
  146. + GridConstants.DEFAULT_SAVE_CAPTION + "\" to begin with",
  147. GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());
  148. assertEquals("Cancel button caption should've been \""
  149. + GridConstants.DEFAULT_CANCEL_CAPTION + "\" to begin with",
  150. GridConstants.DEFAULT_CANCEL_CAPTION, getCancelButton()
  151. .getText());
  152. selectMenuPath("Component", "Editor", "Change Save Caption");
  153. assertNotEquals(
  154. "Save button caption should've changed while editor is open",
  155. GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());
  156. getCancelButton().click();
  157. selectMenuPath("Component", "Editor", "Change Cancel Caption");
  158. selectMenuPath(EDIT_ROW_5);
  159. assertNotEquals(
  160. "Cancel button caption should've changed while editor is closed",
  161. GridConstants.DEFAULT_CANCEL_CAPTION, getCancelButton()
  162. .getText());
  163. }
  164. @Test
  165. public void testUneditableColumn() {
  166. selectMenuPath("Component", "Editor", "Edit row 5");
  167. assertFalse("Uneditable column should not have an editor widget",
  168. getGridElement().getEditor().isEditable(3));
  169. }
  170. @Test
  171. public void testErrorField() {
  172. selectMenuPath(EDIT_ROW_5);
  173. GridEditorElement editor = getGridElement().getEditor();
  174. assertTrue("No errors should be present",
  175. editor.findElements(By.className("error")).isEmpty());
  176. assertEquals("No error message should be present", null,
  177. editor.getErrorMessage());
  178. selectMenuPath("Component", "Editor", "Toggle second editor error");
  179. getSaveButton().click();
  180. assertEquals("Unexpected amount of error fields", 1, editor
  181. .findElements(By.className("error")).size());
  182. assertEquals(
  183. "Unexpedted error message",
  184. "Syntethic fail of editor in column 2. "
  185. + "This message is so long that it doesn't fit into its box",
  186. editor.getErrorMessage());
  187. }
  188. @Test
  189. public void testFocusOnMouseOpen() {
  190. GridCellElement cell = getGridElement().getCell(4, 2);
  191. cell.doubleClick();
  192. WebElement focused = getFocusedElement();
  193. assertEquals("", "input", focused.getTagName());
  194. assertEquals("", cell.getText(), focused.getAttribute("value"));
  195. }
  196. @Test
  197. public void testFocusOnKeyboardOpen() {
  198. GridCellElement cell = getGridElement().getCell(4, 2);
  199. cell.click();
  200. new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
  201. WebElement focused = getFocusedElement();
  202. assertEquals("", "input", focused.getTagName());
  203. assertEquals("", cell.getText(), focused.getAttribute("value"));
  204. }
  205. @Test
  206. public void testNoFocusOnProgrammaticOpen() {
  207. selectMenuPath(EDIT_ROW_5);
  208. WebElement focused = getFocusedElement();
  209. if (BrowserUtil.isIE8(getDesiredCapabilities())) {
  210. assertEquals("Focus should be in html", "html",
  211. focused.getTagName());
  212. } else if (BrowserUtil.isIE(getDesiredCapabilities())) {
  213. assertEquals("Focus should be nowhere", null, focused);
  214. } else {
  215. // GWT menubar loses focus after clicking a menuitem
  216. assertEquals("Focus should be in body", "body",
  217. focused.getTagName());
  218. }
  219. }
  220. protected WebElement getSaveButton() {
  221. return getEditor().findElement(By.className("v-grid-editor-save"));
  222. }
  223. protected WebElement getCancelButton() {
  224. return getEditor().findElement(By.className("v-grid-editor-cancel"));
  225. }
  226. }