Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

RadioButtonGroupTest.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.radiobuttongroup;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertTrue;
  19. import java.util.List;
  20. import org.junit.Assert;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import org.openqa.selenium.WebElement;
  24. import com.vaadin.icons.VaadinIcons;
  25. import com.vaadin.testbench.By;
  26. import com.vaadin.testbench.elements.RadioButtonGroupElement;
  27. import com.vaadin.tests.components.radiobutton.RadioButtonGroupTestUI;
  28. import com.vaadin.tests.tb3.MultiBrowserTest;
  29. /**
  30. * Test for RadioButtonGroup
  31. *
  32. * @author Vaadin Ltd
  33. * @since 8.0
  34. */
  35. public class RadioButtonGroupTest extends MultiBrowserTest {
  36. @Before
  37. public void setUp() throws Exception {
  38. openTestURL();
  39. }
  40. @Test
  41. public void initialLoad_containsCorrectItems() {
  42. assertItems(20);
  43. }
  44. @Test
  45. public void initialItems_reduceItemCount_containsCorrectItems() {
  46. selectMenuPath("Component", "Data provider", "Items", "5");
  47. assertItems(5);
  48. }
  49. @Test
  50. public void disabled_reduceItemCount_containsCorrectItems() {
  51. selectMenuPath("Component", "State", "Enabled");
  52. selectMenuPath("Component", "Data provider", "Items", "5");
  53. assertItems(5);
  54. }
  55. @Test
  56. public void initialItems_increaseItemCount_containsCorrectItems() {
  57. selectMenuPath("Component", "Data provider", "Items", "100");
  58. assertItems(100);
  59. }
  60. @Test
  61. public void disabled_increaseItemCountWithinPushRows_containsCorrectItems() {
  62. selectMenuPath("Component", "Data provider", "Items", "5");
  63. selectMenuPath("Component", "State", "Enabled");
  64. selectMenuPath("Component", "Data provider", "Items", "20");
  65. assertItems(20);
  66. }
  67. @Test
  68. public void disabled_increaseItemCountBeyondPushRows_containsCorrectItems() {
  69. selectMenuPath("Component", "Data provider", "Items", "5");
  70. selectMenuPath("Component", "State", "Enabled");
  71. selectMenuPath("Component", "Data provider", "Items", "100");
  72. assertItems(100);
  73. }
  74. @Test
  75. public void clickToSelect() {
  76. selectMenuPath("Component", "Listeners", "Selection listener");
  77. getSelect().selectByText("Item 4");
  78. Assert.assertEquals("1. Selected: Optional[Item 4]", getLogRow(0));
  79. getSelect().selectByText("Item 2");
  80. Assert.assertEquals("2. Selected: Optional[Item 2]", getLogRow(0));
  81. getSelect().selectByText("Item 4");
  82. Assert.assertEquals("3. Selected: Optional[Item 4]", getLogRow(0));
  83. }
  84. @Test
  85. public void disabled_clickToSelect() {
  86. selectMenuPath("Component", "State", "Enabled");
  87. Assert.assertTrue(getSelect().findElements(By.tagName("input")).stream()
  88. .allMatch(element -> element.getAttribute("disabled") != null));
  89. selectMenuPath("Component", "Listeners", "Selection listener");
  90. String lastLogRow = getLogRow(0);
  91. getSelect().selectByText("Item 4");
  92. Assert.assertEquals(lastLogRow, getLogRow(0));
  93. getSelect().selectByText("Item 2");
  94. Assert.assertEquals(lastLogRow, getLogRow(0));
  95. getSelect().selectByText("Item 4");
  96. Assert.assertEquals(lastLogRow, getLogRow(0));
  97. }
  98. @Test
  99. public void itemIconGenerator() {
  100. selectMenuPath("Component", "Item Icon Generator",
  101. "Use Item Icon Generator");
  102. assertItemsSuffices(20);
  103. List<WebElement> icons = getSelect()
  104. .findElements(By.cssSelector(".v-select-optiongroup .v-icon"));
  105. assertEquals(20, icons.size());
  106. for (int i = 0; i < icons.size(); i++) {
  107. Assert.assertEquals(VaadinIcons.values()[i + 1].getCodepoint(),
  108. icons.get(i).getText().charAt(0));
  109. }
  110. }
  111. @Test
  112. public void clickToSelect_reenable() {
  113. selectMenuPath("Component", "State", "Enabled");
  114. selectMenuPath("Component", "Listeners", "Selection listener");
  115. getSelect().selectByText("Item 4");
  116. selectMenuPath("Component", "State", "Enabled");
  117. getSelect().selectByText("Item 5");
  118. Assert.assertEquals("3. Selected: Optional[Item 5]", getLogRow(0));
  119. getSelect().selectByText("Item 2");
  120. Assert.assertEquals("4. Selected: Optional[Item 2]", getLogRow(0));
  121. getSelect().selectByText("Item 4");
  122. Assert.assertEquals("5. Selected: Optional[Item 4]", getLogRow(0));
  123. }
  124. @Test
  125. public void itemCaptionGenerator() {
  126. selectMenuPath("Component", "Item Caption Generator",
  127. "Item Caption Generator", "Custom Caption Generator");
  128. assertItems(20, " Caption");
  129. }
  130. @Test
  131. public void nullItemCaptionGenerator() {
  132. selectMenuPath("Component", "Item Caption Generator",
  133. "Item Caption Generator", "Null Caption Generator");
  134. for (String text : getSelect().getOptions()) {
  135. Assert.assertEquals("", text);
  136. }
  137. }
  138. @Test
  139. public void selectProgramatically() {
  140. selectMenuPath("Component", "Listeners", "Selection listener");
  141. selectMenuPath("Component", "Selection", "Toggle Item 5");
  142. Assert.assertEquals("2. Selected: Optional[Item 5]", getLogRow(0));
  143. assertSelected("Item 5");
  144. selectMenuPath("Component", "Selection", "Toggle Item 1");
  145. Assert.assertEquals("4. Selected: Optional[Item 1]", getLogRow(0));
  146. // DOM order
  147. assertSelected("Item 1");
  148. selectMenuPath("Component", "Selection", "Toggle Item 5");
  149. Assert.assertEquals("6. Selected: Optional[Item 5]", getLogRow(0));
  150. assertSelected("Item 5");
  151. }
  152. private void assertSelected(String expectedSelection) {
  153. Assert.assertEquals(expectedSelection, getSelect().getValue());
  154. }
  155. @Override
  156. protected Class<?> getUIClass() {
  157. return RadioButtonGroupTestUI.class;
  158. }
  159. protected RadioButtonGroupElement getSelect() {
  160. return $(RadioButtonGroupElement.class).first();
  161. }
  162. protected void assertItems(int count) {
  163. assertItems(count, "");
  164. }
  165. protected void assertItems(int count, String suffix) {
  166. int i = 0;
  167. for (String text : getSelect().getOptions()) {
  168. assertEquals("Item " + i + suffix, text);
  169. i++;
  170. }
  171. assertEquals("Number of items", count, i);
  172. }
  173. protected void assertItemsSuffices(int count) {
  174. int i = 0;
  175. for (String text : getSelect().getOptions()) {
  176. assertTrue(text.endsWith("Item " + i));
  177. i++;
  178. }
  179. assertEquals("Number of items", count, i);
  180. }
  181. }