*/
package com.vaadin.tests.components.checkbox;
+import java.util.function.Function;
import java.util.stream.IntStream;
+import com.vaadin.server.FontAwesome;
+import com.vaadin.server.Resource;
import com.vaadin.shared.data.selection.SelectionModel.Multi;
import com.vaadin.tests.components.abstractlisting.AbstractListingTestUI;
import com.vaadin.ui.CheckBoxGroup;
private final String selectionCategory = "Selection";
+ private static final Function<Object, Resource> DEFAULT_ICON_PROVIDER = item -> "Item 2"
+ .equals(item) ? ICON_16_HELP_PNG_CACHEABLE : null;
+
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected Class<CheckBoxGroup<Object>> getTestClass() {
return (Class) CheckBoxGroup.class;
}
-
@Override
protected CheckBoxGroup<Object> constructComponent() {
CheckBoxGroup<Object> checkBoxGroup = super.constructComponent();
- checkBoxGroup.setItemIconProvider(
- item -> "Item 2".equals(item) ? ICON_16_HELP_PNG_CACHEABLE :
- null);
+ checkBoxGroup.setItemIconProvider(DEFAULT_ICON_PROVIDER);
checkBoxGroup.setItemEnabledProvider(item -> !"Item 10".equals(item));
return checkBoxGroup;
}
super.createActions();
createListenerMenu();
createSelectionMenu();
+ createItemProviderMenu();
}
protected void createSelectionMenu() {
}
}
+ private void createItemProviderMenu() {
+ createBooleanAction("Use Item Caption Provider", "Item Provider", false,
+ this::useItemCaptionProvider);
+ createBooleanAction("Use Item Icon Provider", "Item Provider", false,
+ this::useItemIconProvider);
+ }
+
+ private void useItemCaptionProvider(CheckBoxGroup<Object> group,
+ boolean activate, Object data) {
+ if (activate) {
+ group.setItemCaptionProvider(item -> item.toString() + " Caption");
+ } else {
+ group.setItemCaptionProvider(item -> item.toString());
+ }
+ group.getDataSource().refreshAll();
+ }
+
+ private void useItemIconProvider(CheckBoxGroup<Object> group,
+ boolean activate, Object data) {
+ if (activate) {
+ group.setItemIconProvider(
+ item -> FontAwesome.values()[getIndex(item) + 1]);
+ } else {
+ group.setItemIconProvider(DEFAULT_ICON_PROVIDER);
+ }
+ group.getDataSource().refreshAll();
+ }
+
protected void createListenerMenu() {
createListenerAction("Selection listener", "Listeners",
c -> c.addSelectionListener(
e -> log("Selected: " + e.getNewSelection())));
}
+
+ private int getIndex(Object item) {
+ int index = item.toString().indexOf(' ');
+ if (index < 0) {
+ return 0;
+ }
+ String postfix = item.toString().substring(index + 1);
+ index = postfix.indexOf(' ');
+ if (index >= 0) {
+ postfix = postfix.substring(0, index);
+ }
+ try {
+ return Integer.parseInt(postfix);
+ } catch (NumberFormatException e) {
+ return 0;
+ }
+ }
}
*/
package com.vaadin.tests.components.radiobutton;
+import java.util.stream.IntStream;
+
import com.vaadin.shared.data.selection.SelectionModel;
import com.vaadin.tests.components.abstractlisting.AbstractListingTestUI;
import com.vaadin.ui.RadioButtonGroup;
-import java.util.stream.IntStream;
-
/**
* Test UI for RadioButtonGroup component
*
super.createActions();
createListenerMenu();
createSelectionMenu();
+ createItemProviderMenu();
}
protected void createSelectionMenu() {
item, data) -> toggleSelection(item);
IntStream.of(0, 1, 5, 10, 25).mapToObj(i -> "Item " + i)
- .forEach(item -> createClickAction("Toggle " + item, selectionCategory,
- toggleSelection, item));
+ .forEach(item -> createClickAction("Toggle " + item,
+ selectionCategory, toggleSelection, item));
+ }
+
+ private void createItemProviderMenu() {
+ createBooleanAction("Use Item Caption Provider", "Item Provider", false,
+ this::useItemCaptionProvider);
+ }
+
+ private void useItemCaptionProvider(RadioButtonGroup<Object> group,
+ boolean activate, Object data) {
+ if (activate) {
+ group.setItemCaptionProvider(item -> item.toString() + " Caption");
+ } else {
+ group.setItemCaptionProvider(item -> item.toString());
+ }
+ group.getDataSource().refreshAll();
}
private void toggleSelection(String item) {
- SelectionModel.Single<Object> selectionModel = getComponent().getSelectionModel();
+ SelectionModel.Single<Object> selectionModel = getComponent()
+ .getSelectionModel();
if (selectionModel.isSelected(item)) {
selectionModel.deselect(item);
} else {
c -> c.addSelectionListener(
e -> log("Selected: " + e.getSelectedItem())));
}
+
}
package com.vaadin.tests.components.checkboxgroup;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
import java.util.Arrays;
+import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.openqa.selenium.By;
+import org.openqa.selenium.WebElement;
+import com.vaadin.server.FontAwesome;
import com.vaadin.testbench.customelements.CheckBoxGroupElement;
import com.vaadin.tests.components.checkbox.CheckBoxGroupTestUI;
import com.vaadin.tests.tb3.MultiBrowserTest;
-import org.junit.Before;
-import org.junit.Test;
-
-import java.util.List;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
/**
* Test for CheckBoxGroup
Assert.assertEquals("3. Selected: [Item 2]", getLogRow(0));
}
+ @Test
+ public void disabled_clickToSelect() {
+ selectMenuPath("Component", "State", "Enabled");
+
+ Assert.assertTrue(getSelect().findElements(By.tagName("input")).stream()
+ .allMatch(element -> element.getAttribute("disabled") != null));
+
+ selectMenuPath("Component", "Listeners", "Selection listener");
+
+ String lastLogRow = getLogRow(0);
+
+ getSelect().selectByText("Item 4");
+ Assert.assertEquals(lastLogRow, getLogRow(0));
+
+ getSelect().selectByText("Item 2");
+ // Selection order (most recently selected is last)
+ Assert.assertEquals(lastLogRow, getLogRow(0));
+
+ getSelect().selectByText("Item 4");
+ Assert.assertEquals(lastLogRow, getLogRow(0));
+ }
+
+ @Test
+ public void clickToSelect_reenable() {
+ selectMenuPath("Component", "State", "Enabled");
+ selectMenuPath("Component", "Listeners", "Selection listener");
+
+ getSelect().selectByText("Item 4");
+
+ selectMenuPath("Component", "State", "Enabled");
+
+ getSelect().selectByText("Item 5");
+ Assert.assertEquals("3. Selected: [Item 5]", getLogRow(0));
+
+ getSelect().selectByText("Item 2");
+ Assert.assertEquals("4. Selected: [Item 5, Item 2]", getLogRow(0));
+
+ getSelect().selectByText("Item 5");
+ Assert.assertEquals("5. Selected: [Item 2]", getLogRow(0));
+ }
+
+ @Test
+ public void itemCaptionProvider() {
+ selectMenuPath("Component", "Item Provider",
+ "Use Item Caption Provider");
+ assertItems(20, " Caption");
+ }
+
+ @Test
+ public void itemIconProvider() {
+ selectMenuPath("Component", "Item Provider", "Use Item Icon Provider");
+ assertItemSuffices(20);
+ List<WebElement> icons = getSelect()
+ .findElements(By.cssSelector(".v-icon.FontAwesome"));
+ for (int i = 0; i < icons.size(); i++) {
+ Assert.assertEquals(FontAwesome.values()[i + 1].getCodepoint(),
+ icons.get(i).getText().charAt(0));
+ }
+ }
+
@Test
public void selectProgramatically() {
selectMenuPath("Component", "Listeners", "Selection listener");
}
protected void assertItems(int count) {
+ assertItems(count, "");
+ }
+
+ protected void assertItems(int count, String suffix) {
+ int i = 0;
+ for (String text : getSelect().getOptions()) {
+ assertEquals("Item " + i + suffix, text);
+ i++;
+ }
+ assertEquals("Number of items", count, i);
+ }
+
+ protected void assertItemSuffices(int count) {
int i = 0;
for (String text : getSelect().getOptions()) {
- assertEquals("Item " + i, text);
+ assertTrue(text.endsWith("Item " + i));
i++;
}
assertEquals("Number of items", count, i);
String cssClassList = optionsCssClasses.get(i);
if (i == 10) {
assertTrue("10th item should be disabled",
- cssClassList.toLowerCase().contains("disabled"));
+ cssClassList.toLowerCase().contains("disabled"));
} else {
assertFalse("Only 10th item should be disabled",
- cssClassList.toLowerCase().contains("disabled"));
+ cssClassList.toLowerCase().contains("disabled"));
}
}
}
}
}
}
+
}
*/
package com.vaadin.tests.components.radiobutton;
-import com.vaadin.testbench.customelements.RadioButtonGroupElement;
-import com.vaadin.tests.tb3.MultiBrowserTest;
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.openqa.selenium.By;
-import java.util.Arrays;
-
-import static org.junit.Assert.assertEquals;
+import com.vaadin.testbench.customelements.RadioButtonGroupElement;
+import com.vaadin.tests.tb3.MultiBrowserTest;
/**
* Test for RadioButtonGroup
Assert.assertEquals("3. Selected: Optional[Item 4]", getLogRow(0));
}
+ @Test
+ public void disabled_clickToSelect() {
+ selectMenuPath("Component", "State", "Enabled");
+
+ Assert.assertTrue(getSelect().findElements(By.tagName("input")).stream()
+ .allMatch(element -> element.getAttribute("disabled") != null));
+
+ selectMenuPath("Component", "Listeners", "Selection listener");
+
+ String lastLogRow = getLogRow(0);
+
+ getSelect().selectByText("Item 4");
+ Assert.assertEquals(lastLogRow, getLogRow(0));
+
+ getSelect().selectByText("Item 2");
+ Assert.assertEquals(lastLogRow, getLogRow(0));
+
+ getSelect().selectByText("Item 4");
+ Assert.assertEquals(lastLogRow, getLogRow(0));
+ }
+
+ @Test
+ public void clickToSelect_reenable() {
+ selectMenuPath("Component", "State", "Enabled");
+ selectMenuPath("Component", "Listeners", "Selection listener");
+
+ getSelect().selectByText("Item 4");
+
+ selectMenuPath("Component", "State", "Enabled");
+
+ getSelect().selectByText("Item 5");
+ Assert.assertEquals("3. Selected: Optional[Item 5]", getLogRow(0));
+
+ getSelect().selectByText("Item 2");
+ Assert.assertEquals("4. Selected: Optional[Item 2]", getLogRow(0));
+
+ getSelect().selectByText("Item 4");
+ Assert.assertEquals("5. Selected: Optional[Item 4]", getLogRow(0));
+ }
+
+ @Test
+ public void itemCaptionProvider() {
+ selectMenuPath("Component", "Item Provider",
+ "Use Item Caption Provider");
+ assertItems(20, " Caption");
+ }
+
@Test
public void selectProgramatically() {
selectMenuPath("Component", "Listeners", "Selection listener");
}
protected void assertItems(int count) {
+ assertItems(count, "");
+ }
+
+ protected void assertItems(int count, String suffix) {
int i = 0;
for (String text : getSelect().getOptions()) {
- assertEquals("Item " + i, text);
+ assertEquals("Item " + i + suffix, text);
i++;
}
assertEquals("Number of items", count, i);
}
+
}