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.

CheckBoxGroupTest.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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.checkboxgroup;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertFalse;
  19. import static org.junit.Assert.assertNotNull;
  20. import static org.junit.Assert.assertNull;
  21. import static org.junit.Assert.assertTrue;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.List;
  25. import java.util.Locale;
  26. import java.util.stream.Collectors;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import org.openqa.selenium.By;
  30. import org.openqa.selenium.WebElement;
  31. import com.vaadin.icons.VaadinIcons;
  32. import com.vaadin.testbench.TestBenchElement;
  33. import com.vaadin.testbench.elements.CheckBoxGroupElement;
  34. import com.vaadin.tests.components.checkbox.CheckBoxGroupTestUI;
  35. import com.vaadin.tests.tb3.MultiBrowserTest;
  36. /**
  37. * Test for CheckBoxGroup
  38. *
  39. * @author Vaadin Ltd
  40. * @since 8.0
  41. */
  42. public class CheckBoxGroupTest extends MultiBrowserTest {
  43. @Before
  44. public void setUp() throws Exception {
  45. openTestURL();
  46. }
  47. @Test
  48. public void initialLoad_containsCorrectItems() {
  49. assertItems(20);
  50. }
  51. @Test
  52. public void initialItems_reduceItemCount_containsCorrectItems() {
  53. selectMenuPath("Component", "Data provider", "Items", "5");
  54. assertItems(5);
  55. }
  56. @Test
  57. public void disabled_reduceItemCount_containsCorrectItems() {
  58. selectMenuPath("Component", "State", "Enabled");
  59. selectMenuPath("Component", "Data provider", "Items", "5");
  60. assertItems(5);
  61. }
  62. @Test
  63. public void initialItems_increaseItemCount_containsCorrectItems() {
  64. selectMenuPath("Component", "Data provider", "Items", "100");
  65. assertItems(100);
  66. }
  67. @Test
  68. public void disabled_increaseItemCountWithinPushRows_containsCorrectItems() {
  69. selectMenuPath("Component", "Data provider", "Items", "5");
  70. selectMenuPath("Component", "State", "Enabled");
  71. selectMenuPath("Component", "Data provider", "Items", "20");
  72. assertItems(20);
  73. }
  74. @Test
  75. public void disabled_increaseItemCountBeyondPushRows_containsCorrectItems() {
  76. selectMenuPath("Component", "Data provider", "Items", "5");
  77. selectMenuPath("Component", "State", "Enabled");
  78. selectMenuPath("Component", "Data provider", "Items", "100");
  79. assertItems(100);
  80. }
  81. @Test
  82. public void clickToSelect() {
  83. selectMenuPath("Component", "Listeners", "Selection listener");
  84. getSelect().selectByText("Item 4");
  85. assertEquals("1. Selected: [Item 4]", getLogRow(0));
  86. getSelect().selectByText("Item 2");
  87. // Selection order (most recently selected is last)
  88. assertEquals("2. Selected: [Item 4, Item 2]", getLogRow(0));
  89. getSelect().selectByText("Item 4");
  90. assertEquals("3. Selected: [Item 2]", getLogRow(0));
  91. }
  92. @Test
  93. public void disabled_clickToSelect() {
  94. selectMenuPath("Component", "State", "Enabled");
  95. assertTrue(getSelect().findElements(By.tagName("input")).stream()
  96. .allMatch(element -> element.getAttribute("disabled") != null));
  97. selectMenuPath("Component", "Listeners", "Selection listener");
  98. String lastLogRow = getLogRow(0);
  99. getSelect().selectByText("Item 4");
  100. assertEquals(lastLogRow, getLogRow(0));
  101. getSelect().selectByText("Item 2");
  102. // Selection order (most recently selected is last)
  103. assertEquals(lastLogRow, getLogRow(0));
  104. getSelect().selectByText("Item 4");
  105. assertEquals(lastLogRow, getLogRow(0));
  106. }
  107. @Test
  108. public void clickToSelect_reenable() {
  109. selectMenuPath("Component", "State", "Enabled");
  110. selectMenuPath("Component", "Listeners", "Selection listener");
  111. getSelect().selectByText("Item 4");
  112. selectMenuPath("Component", "State", "Enabled");
  113. getSelect().selectByText("Item 5");
  114. assertEquals("3. Selected: [Item 5]", getLogRow(0));
  115. getSelect().selectByText("Item 2");
  116. assertEquals("4. Selected: [Item 5, Item 2]", getLogRow(0));
  117. getSelect().selectByText("Item 5");
  118. assertEquals("5. Selected: [Item 2]", getLogRow(0));
  119. }
  120. @Test
  121. public void itemCaptionGenerator() {
  122. selectMenuPath("Component", "Item Generator", "Item Caption Generator",
  123. "Custom Caption Generator");
  124. assertItems(20, " Caption");
  125. }
  126. @Test
  127. public void nullItemCaptionGenerator() {
  128. selectMenuPath("Component", "Item Generator", "Item Caption Generator",
  129. "Null Caption Generator");
  130. for (String text : getSelect().getOptions()) {
  131. assertEquals("", text);
  132. }
  133. }
  134. @Test
  135. public void itemIconGenerator() {
  136. selectMenuPath("Component", "Item Generator",
  137. "Use Item Icon Generator");
  138. assertItemSuffices(20);
  139. List<WebElement> icons = getSelect()
  140. .findElements(By.cssSelector(".v-select-optiongroup .v-icon"));
  141. assertFalse(icons.isEmpty());
  142. for (int i = 0; i < icons.size(); i++) {
  143. assertEquals(VaadinIcons.values()[i + 1].getCodepoint(),
  144. icons.get(i).getText().charAt(0));
  145. }
  146. }
  147. @Test
  148. public void selectProgramatically() {
  149. selectMenuPath("Component", "Listeners", "Selection listener");
  150. selectMenuPath("Component", "Selection", "Toggle Item 5");
  151. assertEquals("2. Selected: [Item 5]", getLogRow(0));
  152. assertSelected("Item 5");
  153. selectMenuPath("Component", "Selection", "Toggle Item 1");
  154. // Selection order (most recently selected is last)
  155. assertEquals("4. Selected: [Item 5, Item 1]", getLogRow(0));
  156. // DOM order
  157. assertSelected("Item 1", "Item 5");
  158. selectMenuPath("Component", "Selection", "Toggle Item 5");
  159. assertEquals("6. Selected: [Item 1]", getLogRow(0));
  160. assertSelected("Item 1");
  161. }
  162. @Test
  163. public void testItemDescriptionGenerators() {
  164. TestBenchElement label;
  165. selectMenuPath("Component", "Item Description Generator",
  166. "Item Description Generator", "Default Description Generator");
  167. label = (TestBenchElement) findElements(By.tagName("label")).get(5);
  168. label.showTooltip();
  169. assertEquals("Tooltip should contain the same text as caption",
  170. label.getText(), getTooltipElement().getText());
  171. selectMenuPath("Component", "Item Description Generator",
  172. "Item Description Generator", "Custom Description Generator");
  173. label = (TestBenchElement) findElements(By.tagName("label")).get(5);
  174. label.showTooltip();
  175. assertEquals("Tooltip should contain caption + ' Description'",
  176. label.getText() + " Description",
  177. getTooltipElement().getText());
  178. }
  179. private void assertSelected(String... expectedSelection) {
  180. assertEquals(Arrays.asList(expectedSelection), getSelect().getValue());
  181. }
  182. @Override
  183. protected Class<?> getUIClass() {
  184. return CheckBoxGroupTestUI.class;
  185. }
  186. protected CheckBoxGroupElement getSelect() {
  187. return $(CheckBoxGroupElement.class).first();
  188. }
  189. protected void assertItems(int count) {
  190. assertItems(count, "");
  191. }
  192. protected void assertItems(int count, String suffix) {
  193. int i = 0;
  194. for (String text : getSelect().getOptions()) {
  195. assertEquals("Item " + i + suffix, text);
  196. i++;
  197. }
  198. assertEquals("Number of items", count, i);
  199. }
  200. protected void assertItemSuffices(int count) {
  201. int i = 0;
  202. for (String text : getSelect().getOptions()) {
  203. assertTrue(text.endsWith("Item " + i));
  204. i++;
  205. }
  206. assertEquals("Number of items", count, i);
  207. }
  208. @Test
  209. public void testDisabled() {
  210. List<String> optionsCssClasses = getSelect().getOptionElements()
  211. .stream().map(element -> element.getAttribute("class"))
  212. .collect(Collectors.toList());
  213. for (int i = 0; i < optionsCssClasses.size(); i++) {
  214. String cssClassList = optionsCssClasses.get(i);
  215. if (i == 10) {
  216. assertTrue("10th item should be disabled", cssClassList
  217. .toLowerCase(Locale.ROOT).contains("disabled"));
  218. } else {
  219. assertFalse("Only 10th item should be disabled", cssClassList
  220. .toLowerCase(Locale.ROOT).contains("disabled"));
  221. }
  222. }
  223. }
  224. @Test
  225. public void testIconUrl() {
  226. List<String> optionsIcons = new ArrayList<>();
  227. for (WebElement option : getSelect().getOptionElements()) {
  228. List<WebElement> images = option.findElements(By.tagName("img"));
  229. if (!images.isEmpty()) {
  230. optionsIcons.add(images.get(0).getAttribute("src"));
  231. } else {
  232. optionsIcons.add(null);
  233. }
  234. }
  235. for (int i = 0; i < optionsIcons.size(); i++) {
  236. String icon = optionsIcons.get(i);
  237. if (i == 2) {
  238. assertNotNull("2nd item should have icon", icon);
  239. } else {
  240. assertNull("Only 2nd item should have icon", icon);
  241. }
  242. }
  243. }
  244. // needed to make tooltips work in IE tests
  245. @Override
  246. protected boolean requireWindowFocusForIE() {
  247. return true;
  248. }
  249. }