Browse Source

Merged test updates from 6.4

svn changeset:15590/svn branch:6.5
tags/6.7.0.beta1
Artur Signell 13 years ago
parent
commit
90d1e732a8

+ 85
- 7
tests/src/com/vaadin/tests/components/AbstractComponentTestCase.java View File

@@ -1,8 +1,12 @@
package com.vaadin.tests.components;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.vaadin.terminal.Resource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.terminal.UserError;
import com.vaadin.tests.util.Log;
import com.vaadin.ui.AbstractComponent;
@@ -12,10 +16,27 @@ import com.vaadin.ui.Layout.SpacingHandler;
public abstract class AbstractComponentTestCase<T extends AbstractComponent>
extends TestBase {

protected static final ThemeResource ICON_16_USER_PNG_CACHEABLE = cacheableThemeResource("../runo/icons/16/user.png");
protected static final ThemeResource ICON_16_USER_PNG_UNCACHEABLE = uncacheableThemeResource("../runo/icons/16/user.png");
protected static final ThemeResource ICON_32_ATTENTION_PNG_CACHEABLE = cacheableThemeResource("../runo/icons/32/attention.png");
protected static final ThemeResource ICON_32_ATTENTION_PNG_UNCACHEABLE = uncacheableThemeResource("../runo/icons/32/attention.png");
protected static final ThemeResource ICON_64_EMAIL_REPLY_PNG_CACHEABLE = cacheableThemeResource("../runo/icons/64/email-reply.png");
protected static final ThemeResource ICON_64_EMAIL_REPLY_PNG_UNCACHEABLE = uncacheableThemeResource("../runo/icons/64/email-reply.png");

private List<T> testComponents = new ArrayList<T>();

abstract protected Class<T> getTestClass();

protected static ThemeResource uncacheableThemeResource(
String resourceLocation) {
return new ThemeResource(resourceLocation + "?" + new Date().getTime());
}

protected static ThemeResource cacheableThemeResource(
String resourceLocation) {
return new ThemeResource(resourceLocation);
}

abstract protected void initializeComponents();

private Log log = null;
@@ -75,6 +96,32 @@ public abstract class AbstractComponentTestCase<T extends AbstractComponent>
}
};

protected Command<T, Boolean> errorIndicatorCommand = new Command<T, Boolean>() {

public void execute(T c, Boolean enabled, Object data) {
if (enabled) {
c.setComponentError(new UserError(errorMessage));
} else {
c.setComponentError(null);

}
}
};
private String errorMessage = null;

protected Command<T, String> errorMessageCommand = new Command<T, String>() {

public void execute(T c, String value, Object data) {
errorMessage = value;
if (c.getComponentError() != null) {
errorIndicatorCommand.execute(c, true, null);
}

}

};

// TODO Move to AbstractFieldTestCase
protected Command<T, Boolean> requiredCommand = new Command<T, Boolean>() {

public void execute(T c, Boolean enabled, Object data) {
@@ -86,16 +133,17 @@ public abstract class AbstractComponentTestCase<T extends AbstractComponent>
}
}
};
protected Command<T, String> requiredErrorMessageCommand = new Command<T, String>() {

protected Command<T, Boolean> errorIndicatorCommand = new Command<T, Boolean>() {
public void execute(T c, String value, Object data) {
((Field) c).setRequiredError(value);
}

public void execute(T c, Boolean enabled, Object data) {
if (enabled) {
c.setComponentError(new UserError("It failed!"));
} else {
c.setComponentError(null);
};

}
protected Command<T, String> descriptionCommand = new Command<T, String>() {
public void execute(T c, String value, Object data) {
c.setDescription(value);
}
};

@@ -106,6 +154,36 @@ public abstract class AbstractComponentTestCase<T extends AbstractComponent>
}
};

protected Command<T, Boolean> visibleCommand = new Command<T, Boolean>() {

public void execute(T c, Boolean enabled, Object data) {
c.setVisible(enabled);
}
};

protected Command<T, Resource> iconCommand = new Command<T, Resource>() {

public void execute(T c, Resource value, Object data) {
c.setIcon(value);
}

};
protected Command<T, String> captionCommand = new Command<T, String>() {

public void execute(T c, String value, Object data) {
c.setCaption(value);
}

};

protected Command<T, Locale> localeCommand = new Command<T, Locale>() {

public void execute(T c, Locale value, Object data) {
c.setLocale(value);
}

};

protected <VALUET> void doCommand(Command<T, VALUET> command, VALUET value) {
doCommand(command, value, null);
}

+ 189
- 119
tests/src/com/vaadin/tests/components/MenuBasedComponentTestCase.java View File

@@ -1,24 +1,31 @@
package com.vaadin.tests.components;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.tests.util.LoremIpsum;
import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.Field;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.MenuItem;
//TODO swap the inheritance order so AbstractComponentTestCase refers to AbstractComponent and this is the base class. Can only be done when all old tests are converted to use this.
public abstract class MenuBasedComponentTestCase<T extends AbstractComponent>
extends AbstractComponentTestCase<T> {
protected static final String TEXT_SHORT = "Short";
protected static final String TEXT_MEDIUM = "This is a semi-long text that might wrap.";
protected static final String TEXT_LONG = "This is a long text. "
+ LoremIpsum.get(500);
protected static final String TEXT_VERY_LONG = "This is a very, very long text. "
+ LoremIpsum.get(5000);
private static final Resource SELECTED_ICON = new ThemeResource(
"../runo/icons/16/ok.png");
@@ -31,18 +38,29 @@ public abstract class MenuBasedComponentTestCase<T extends AbstractComponent>
// Used to determine if a menuItem should be selected and the other
// unselected on click
private Set<MenuItem> parentOfSelectableMenuItem = new HashSet<MenuItem>();
private MenuItem windowMenu;
/**
* Maps the category name to a menu item
*/
private Map<String, MenuItem> categoryToMenuItem = new HashMap<String, MenuItem>();
protected static final String CATEGORY_STATE = "State";
protected static final String CATEGORY_SIZE = "Size";
protected static final String CATEGORY_SELECTION = "Selection";
protected static final String CATEGORY_CONTENT = "Contents";
protected static final String CATEGORY_LISTENERS = "Listeners";
protected static final String CATEGORY_FEATURES = "Features";
protected static final String CATEGORY_DECORATIONS = "Decorations";
@Override
protected final void setup() {
setTheme("tests-components");
// Create menu here so it appears before the components
menu = new MenuBar();
mainMenu = menu.addItem("Settings", null);
menu.setDebugId("menu");
mainMenu = menu.addItem("Component", null);
windowMenu = menu.addItem("Test", null);
addComponent(menu);
getLayout().setSizeFull();
@@ -50,7 +68,7 @@ public abstract class MenuBasedComponentTestCase<T extends AbstractComponent>
super.setup();
// Create menu actions and trigger default actions
populateMenu();
createActions();
// Clear initialization log messages
clearLog();
@@ -63,6 +81,7 @@ public abstract class MenuBasedComponentTestCase<T extends AbstractComponent>
@Override
protected void initializeComponents() {
component = constructComponent();
component.setDebugId("testComponent");
addTestComponent(component);
}
@@ -94,96 +113,68 @@ public abstract class MenuBasedComponentTestCase<T extends AbstractComponent>
}
}
private void populateMenu() {
createDefaultActions();
createCustomActions();
}
private void createDefaultActions() {
/**
* Create actions for the component. Remember to call super.createActions()
* when overriding.
*/
protected void createActions() {
createBooleanAction("Immediate", CATEGORY_STATE, true, immediateCommand);
createBooleanAction("Enabled", CATEGORY_STATE, true, enabledCommand);
createBooleanAction("Readonly", CATEGORY_STATE, false, readonlyCommand);
createBooleanAction("Visible", CATEGORY_STATE, true, visibleCommand);
createBooleanAction("Error indicator", CATEGORY_STATE, false,
errorIndicatorCommand);
createLocaleSelect(CATEGORY_STATE);
createErrorMessageSelect(CATEGORY_DECORATIONS);
createDescriptionSelect(CATEGORY_DECORATIONS);
createCaptionSelect(CATEGORY_DECORATIONS);
createIconSelect(CATEGORY_DECORATIONS);
if (component instanceof Field) {
createBooleanAction("Required", CATEGORY_STATE, false,
requiredCommand);
}
createWidthSelect(CATEGORY_SIZE);
createHeightSelect(CATEGORY_SIZE);
if (component instanceof AbstractSelect) {
createNullSelectAllowedCheckbox(CATEGORY_SELECTION);
createItemsInContainerSelect(CATEGORY_CONTENT);
createColumnsInContainerSelect(CATEGORY_CONTENT);
}
}
// TODO Style name
@SuppressWarnings("unchecked")
protected void createItemsInContainerSelect(String category) {
LinkedHashMap<String, Integer> options = new LinkedHashMap<String, Integer>();
options.put("0", 0);
options.put("20", 20);
options.put("100", 100);
options.put("1000", 1000);
options.put("10000", 10000);
options.put("100000", 100000);
createSelectAction("Items in container", category, options, "20",
(Command) itemsInContainerCommand);
}
@SuppressWarnings("unchecked")
protected void createColumnsInContainerSelect(String category) {
LinkedHashMap<String, Integer> options = new LinkedHashMap<String, Integer>();
options.put("0", 0);
options.put("5", 5);
options.put("10", 10);
options.put("50", 50);
options.put("100", 100);
options.put("1000", 1000);
createSelectAction("Columns in container", category, options, "10",
(Command) columnsInContainerCommand);
}
private Container createContainer(int properties, int items) {
IndexedContainer c = new IndexedContainer();
for (int i = 1; i <= properties; i++) {
c.addContainerProperty("Column " + i, String.class, "");
}
for (int i = 1; i <= items; i++) {
Item item = c.addItem("Item " + i);
for (int j = 1; j <= properties; j++) {
item.getItemProperty("Column " + j).setValue(
"Item " + i + "," + j);
}
}
private void createErrorMessageSelect(String category) {
LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
options.put("-", null);
options.put(TEXT_SHORT, TEXT_SHORT);
options.put("Medium", TEXT_MEDIUM);
options.put("Long", TEXT_LONG);
options.put("Very long", TEXT_VERY_LONG);
createSelectAction("Error message", category, options, "-",
errorMessageCommand);
return c;
}
@SuppressWarnings("unchecked")
protected void createNullSelectAllowedCheckbox(String category) {
createBooleanAction("Null Selection Allowed", category, false,
(Command) nullSelectionAllowedCommand);
private void createDescriptionSelect(String category) {
LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
options.put("-", null);
options.put(TEXT_SHORT, TEXT_SHORT);
options.put("Medium", TEXT_MEDIUM);
options.put("Long", TEXT_LONG);
options.put("Very long", TEXT_VERY_LONG);
createSelectAction("Description / tooltip", category, options, "-",
descriptionCommand);
}
@SuppressWarnings("unchecked")
protected void createNullSelectItemId(String category) {
LinkedHashMap<String, Object> options = new LinkedHashMap<String, Object>();
options.put("- None -", null);
for (Object id : ((AbstractSelect) component).getContainerDataSource()
.getContainerPropertyIds()) {
options.put(id.toString(), id);
}
createSelectAction("Null Selection Item Id", category, options,
"- None -", (Command) nullSelectItemIdCommand);
private void createCaptionSelect(String category) {
LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
options.put("-", null);
options.put("Short", TEXT_SHORT);
options.put("Medium", TEXT_MEDIUM);
options.put("Long", TEXT_LONG);
options.put("Very long", TEXT_VERY_LONG);
createSelectAction("Caption", category, options, "Short",
captionCommand);
}
protected void createWidthSelect(String category) {
private void createWidthSelect(String category) {
LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
options.put("Undefined", null);
options.put("50%", "50%");
@@ -196,7 +187,29 @@ public abstract class MenuBasedComponentTestCase<T extends AbstractComponent>
widthCommand, null);
}
protected void createHeightSelect(String category) {
private void createIconSelect(String category) {
LinkedHashMap<String, Resource> options = new LinkedHashMap<String, Resource>();
options.put("-", null);
options.put("16x16", ICON_16_USER_PNG_CACHEABLE);
options.put("32x32", ICON_32_ATTENTION_PNG_CACHEABLE);
options.put("64x64", ICON_64_EMAIL_REPLY_PNG_CACHEABLE);
createSelectAction("Icon", category, options, "-", iconCommand, null);
}
private void createLocaleSelect(String category) {
LinkedHashMap<String, Locale> options = new LinkedHashMap<String, Locale>();
options.put("-", null);
options.put("fi_FI", new Locale("fi", "FI"));
options.put("en_US", Locale.US);
options.put("zh_CN", Locale.SIMPLIFIED_CHINESE);
options.put("fr_FR", Locale.FRANCE);
createSelectAction("Locale", category, options, "-", localeCommand,
null);
}
private void createHeightSelect(String category) {
LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
options.put("Undefined", null);
options.put("50%", "50%");
@@ -224,25 +237,53 @@ public abstract class MenuBasedComponentTestCase<T extends AbstractComponent>
doCommand(caption, command, initialState, data);
}
protected <DATATYPE> void createClickAction(String caption,
String category, final Command<T, DATATYPE> command, DATATYPE value) {
createClickAction(caption, category, command, value, null);
}
protected <DATATYPE> void createClickAction(String caption,
String category, final Command<T, DATATYPE> command,
DATATYPE value, Object data) {
MenuItem categoryItem = getCategoryMenuItem(category);
categoryItem.addItem(caption, menuClickCommand(command, value, data));
doCommand(caption, command, value, data);
}
private MenuItem getCategoryMenuItem(String category) {
if (category == null) {
return getCategoryMenuItem("Misc");
}
if (mainMenu.getChildren() != null) {
for (MenuItem i : mainMenu.getChildren()) {
if (i.getText().equals(category)) {
return i;
}
}
MenuItem item = categoryToMenuItem.get(category);
if (item != null) {
return item;
}
return mainMenu.addItem(category, null);
return createCategory(category, null);
}
/**
* Provide custom actions for the test case by creating them in this method.
* Creates category "category" in parent category "parentCategory". Each
* category name must be globally unique.
*
* @param category
* @param parentCategory
* @return
*/
protected abstract void createCustomActions();
protected MenuItem createCategory(String category, String parentCategory) {
if (categoryToMenuItem.containsKey(category)) {
return categoryToMenuItem.get(category);
}
MenuItem item;
if (parentCategory == null) {
item = mainMenu.addItem(category, null);
} else {
item = getCategoryMenuItem(parentCategory).addItem(category, null);
}
categoryToMenuItem.put(category, item);
return item;
}
private MenuBar.Command menuBooleanCommand(
final com.vaadin.tests.components.ComponentTestCase.Command<T, Boolean> booleanCommand,
@@ -258,6 +299,18 @@ public abstract class MenuBasedComponentTestCase<T extends AbstractComponent>
};
}
private <DATATYPE> MenuBar.Command menuClickCommand(
final com.vaadin.tests.components.ComponentTestCase.Command<T, DATATYPE> command,
final DATATYPE value, final Object data) {
return new MenuBar.Command() {
public void menuSelected(MenuItem selectedItem) {
doCommand(getText(selectedItem), command, value, data);
}
};
}
protected void setSelected(MenuItem item, boolean selected) {
if (selected) {
item.setIcon(SELECTED_ICON);
@@ -327,59 +380,76 @@ public abstract class MenuBasedComponentTestCase<T extends AbstractComponent>
}
protected <TYPE> void createSelectAction(
protected <TYPE> void createMultiClickAction(
String caption,
String category,
LinkedHashMap<String, TYPE> options,
String initialValue,
com.vaadin.tests.components.ComponentTestCase.Command<T, TYPE> command,
Object data) {
MenuItem parentItem = getCategoryMenuItem(category);
MenuItem mainItem = parentItem.addItem(caption, null);
MenuItem categoryItem = getCategoryMenuItem(category);
MenuItem mainItem = categoryItem.addItem(caption, null);
parentOfSelectableMenuItem.add(mainItem);
for (String option : options.keySet()) {
MenuBar.Command cmd = singleSelectMenuCommand(command,
MenuBar.Command cmd = menuClickCommand(command,
options.get(option), data);
MenuItem item = mainItem.addItem(option, cmd);
if (option.equals(initialValue)) {
cmd.menuSelected(item);
}
mainItem.addItem(option, cmd);
}
}
/* COMMANDS */
protected <TYPE> void createMultiToggleAction(
String caption,
String category,
LinkedHashMap<String, TYPE> options,
com.vaadin.tests.components.ComponentTestCase.Command<T, Boolean> command,
boolean defaultValue) {
protected Command<AbstractSelect, Boolean> nullSelectionAllowedCommand = new Command<AbstractSelect, Boolean>() {
LinkedHashMap<String, Boolean> defaultValues = new LinkedHashMap<String, Boolean>();
public void execute(AbstractSelect c, Boolean value, Object data) {
(c).setNullSelectionAllowed(value);
for (String option : options.keySet()) {
defaultValues.put(option, defaultValue);
}
};
protected Command<AbstractSelect, Object> nullSelectItemIdCommand = new Command<AbstractSelect, Object>() {
createMultiToggleAction(caption, category, options, command,
defaultValues);
}
public void execute(AbstractSelect c, Object value, Object data) {
c.setNullSelectionItemId(value);
}
};
protected <TYPE> void createMultiToggleAction(
String caption,
String category,
LinkedHashMap<String, TYPE> options,
com.vaadin.tests.components.ComponentTestCase.Command<T, Boolean> command,
LinkedHashMap<String, Boolean> defaultValues) {
protected Command<AbstractSelect, Integer> itemsInContainerCommand = new Command<AbstractSelect, Integer>() {
createCategory(caption, category);
for (String option : options.keySet()) {
createBooleanAction(option, caption, defaultValues.get(option),
command, options.get(option));
public void execute(AbstractSelect t, Integer value, Object data) {
t.setContainerDataSource(createContainer(t.getContainerDataSource()
.getContainerPropertyIds().size(), value));
}
};
}
protected <TYPE> void createSelectAction(
String caption,
String category,
LinkedHashMap<String, TYPE> options,
String initialValue,
com.vaadin.tests.components.ComponentTestCase.Command<T, TYPE> command,
Object data) {
protected Command<AbstractSelect, Integer> columnsInContainerCommand = new Command<AbstractSelect, Integer>() {
MenuItem parentItem = getCategoryMenuItem(category);
MenuItem mainItem = parentItem.addItem(caption, null);
public void execute(AbstractSelect t, Integer value, Object data) {
t.setContainerDataSource(createContainer(value, t
.getContainerDataSource().size()));
parentOfSelectableMenuItem.add(mainItem);
for (String option : options.keySet()) {
MenuBar.Command cmd = singleSelectMenuCommand(command,
options.get(option), data);
MenuItem item = mainItem.addItem(option, cmd);
if (option.equals(initialValue)) {
cmd.menuSelected(item);
}
}
};
}
/* COMMANDS END */
}

+ 124
- 0
tests/src/com/vaadin/tests/components/abstractfield/AbstractFieldTestCase.java View File

@@ -0,0 +1,124 @@
package com.vaadin.tests.components.abstractfield;
import java.util.LinkedHashMap;
import com.vaadin.data.Property.ReadOnlyStatusChangeEvent;
import com.vaadin.data.Property.ReadOnlyStatusChangeListener;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.event.FieldEvents.BlurEvent;
import com.vaadin.event.FieldEvents.BlurListener;
import com.vaadin.event.FieldEvents.BlurNotifier;
import com.vaadin.event.FieldEvents.FocusEvent;
import com.vaadin.event.FieldEvents.FocusListener;
import com.vaadin.event.FieldEvents.FocusNotifier;
import com.vaadin.tests.components.MenuBasedComponentTestCase;
import com.vaadin.ui.AbstractField;
public abstract class AbstractFieldTestCase<T extends AbstractField> extends
MenuBasedComponentTestCase<T> implements ValueChangeListener,
ReadOnlyStatusChangeListener, FocusListener, BlurListener {
@Override
protected void createActions() {
super.createActions();
createBooleanAction("Required", CATEGORY_STATE, false, requiredCommand);
createRequiredErrorSelect(CATEGORY_DECORATIONS);
createValueChangeListener(CATEGORY_LISTENERS);
createReadOnlyStatusChangeListener(CATEGORY_LISTENERS);
}
private void createRequiredErrorSelect(String category) {
LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
options.put("-", null);
options.put(TEXT_SHORT, TEXT_SHORT);
options.put("Medium", TEXT_MEDIUM);
options.put("Long", TEXT_LONG);
options.put("Very long", TEXT_VERY_LONG);
createSelectAction("Required error message", category, options, "-",
requiredErrorMessageCommand);
}
private void createValueChangeListener(String category) {
createBooleanAction("Value change listener", category, false,
valueChangeListenerCommand);
}
private void createReadOnlyStatusChangeListener(String category) {
createBooleanAction("Read only status change listener", category,
false, readonlyStatusChangeListenerCommand);
}
protected void createFocusListener(String category) {
createBooleanAction("Focus listener", category, false,
focusListenerCommand);
}
protected void createBlurListener(String category) {
createBooleanAction("Blur listener", category, false,
blurListenerCommand);
}
protected Command<T, Boolean> valueChangeListenerCommand = new Command<T, Boolean>() {
public void execute(T c, Boolean value, Object data) {
if (value) {
c.addListener((ValueChangeListener) AbstractFieldTestCase.this);
} else {
c.removeListener((ValueChangeListener) AbstractFieldTestCase.this);
}
}
};
protected Command<T, Boolean> readonlyStatusChangeListenerCommand = new Command<T, Boolean>() {
public void execute(T c, Boolean value, Object data) {
if (value) {
c.addListener((ReadOnlyStatusChangeListener) AbstractFieldTestCase.this);
} else {
c.removeListener((ReadOnlyStatusChangeListener) AbstractFieldTestCase.this);
}
}
};
protected Command<T, Boolean> focusListenerCommand = new Command<T, Boolean>() {
public void execute(T c, Boolean value, Object data) {
if (value) {
((FocusNotifier) c).addListener(AbstractFieldTestCase.this);
} else {
((FocusNotifier) c).removeListener(AbstractFieldTestCase.this);
}
}
};
protected Command<T, Boolean> blurListenerCommand = new Command<T, Boolean>() {
public void execute(T c, Boolean value, Object data) {
if (value) {
((BlurNotifier) c).addListener(AbstractFieldTestCase.this);
} else {
((BlurNotifier) c).removeListener(AbstractFieldTestCase.this);
}
}
};
public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
log(event.getClass().getSimpleName() + ", new value: "
+ event.getProperty().getValue());
};
public void readOnlyStatusChange(ReadOnlyStatusChangeEvent event) {
log(event.getClass().getSimpleName());
}
public void focus(FocusEvent event) {
log(event.getClass().getSimpleName());
}
public void blur(BlurEvent event) {
log(event.getClass().getSimpleName());
}
}

+ 56
- 0
tests/src/com/vaadin/tests/components/button/Buttons2.java View File

@@ -0,0 +1,56 @@
package com.vaadin.tests.components.button;
import com.vaadin.tests.components.abstractfield.AbstractFieldTestCase;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
public class Buttons2 extends AbstractFieldTestCase<Button> implements
ClickListener {
private Command<Button, Boolean> switchModeCommand = new Command<Button, Boolean>() {
public void execute(Button c, Boolean value, Object data) {
c.setSwitchMode(value);
}
};
private Command<Button, Boolean> clickListenerCommand = new Command<Button, Boolean>() {
public void execute(Button c, Boolean value, Object data) {
if (value) {
c.addListener((ClickListener) Buttons2.this);
} else {
c.removeListener((ClickListener) Buttons2.this);
}
}
};
@Override
protected Class<Button> getTestClass() {
return Button.class;
}
@Override
protected void createActions() {
super.createActions();
createFocusListener(CATEGORY_LISTENERS);
createBlurListener(CATEGORY_LISTENERS);
createBooleanAction("Switch mode", CATEGORY_FEATURES, false,
switchModeCommand);
addClickListener(CATEGORY_LISTENERS);
}
private void addClickListener(String category) {
createBooleanAction("Click listener", category, false,
clickListenerCommand);
}
public void buttonClick(ClickEvent event) {
log(event.getClass().getSimpleName());
}
}

+ 57
- 0
tests/src/com/vaadin/tests/components/checkbox/CheckBoxes2.java View File

@@ -0,0 +1,57 @@
package com.vaadin.tests.components.checkbox;
import com.vaadin.tests.components.abstractfield.AbstractFieldTestCase;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
public class CheckBoxes2 extends AbstractFieldTestCase<CheckBox> implements
ClickListener {
// cannot extend Buttons2 because of Switch mode problems
@Override
protected Class<CheckBox> getTestClass() {
return CheckBox.class;
}
private Command<CheckBox, Boolean> switchModeCommand = new Command<CheckBox, Boolean>() {
public void execute(CheckBox c, Boolean value, Object data) {
c.setSwitchMode(value);
}
};
private Command<CheckBox, Boolean> clickListenerCommand = new Command<CheckBox, Boolean>() {
public void execute(CheckBox c, Boolean value, Object data) {
if (value) {
c.addListener((ClickListener) CheckBoxes2.this);
} else {
c.removeListener((ClickListener) CheckBoxes2.this);
}
}
};
@Override
protected void createActions() {
super.createActions();
createFocusListener(CATEGORY_LISTENERS);
createBlurListener(CATEGORY_LISTENERS);
createBooleanAction("Switch mode", CATEGORY_FEATURES, true,
switchModeCommand);
addClickListener(CATEGORY_LISTENERS);
}
private void addClickListener(String category) {
createBooleanAction("Click listener", category, false,
clickListenerCommand);
}
public void buttonClick(ClickEvent event) {
log(event.getClass().getSimpleName());
}
}

+ 45
- 0
tests/src/com/vaadin/tests/components/combobox/ComboBoxes2.java View File

@@ -0,0 +1,45 @@
package com.vaadin.tests.components.combobox;

import java.util.LinkedHashMap;

import com.vaadin.terminal.Resource;
import com.vaadin.tests.components.select.AbstractSelectTestCase;
import com.vaadin.ui.ComboBox;

public class ComboBoxes2 extends AbstractSelectTestCase<ComboBox> {

@Override
protected Class<ComboBox> getTestClass() {
return ComboBox.class;
}

@Override
protected void createActions() {
super.createActions();
createItemIconSelect(CATEGORY_DATA_SOURCE);
}

private void createItemIconSelect(String category) {

LinkedHashMap<String, Resource> options = new LinkedHashMap<String, Resource>();
options.put("-", null);
options.put("16x16", ICON_16_USER_PNG_UNCACHEABLE);
options.put("32x32", ICON_32_ATTENTION_PNG_UNCACHEABLE);
options.put("64x64", ICON_64_EMAIL_REPLY_PNG_UNCACHEABLE);

createSelectAction("Icon", category, options, "-",
new Command<ComboBox, Resource>() {

public void execute(ComboBox c, Resource value, Object data) {
for (Object id : c.getItemIds()) {
if (value == null) {
c.setItemIcon(id, null);
} else {
c.setItemIcon(id, value);
}
}
}
});
}

}

+ 108
- 0
tests/src/com/vaadin/tests/components/datefield/DateFieldTestCase.java View File

@@ -0,0 +1,108 @@
package com.vaadin.tests.components.datefield;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.Locale;
import com.vaadin.tests.components.abstractfield.AbstractFieldTestCase;
import com.vaadin.ui.DateField;
public abstract class DateFieldTestCase<T extends DateField> extends
AbstractFieldTestCase<T> {
@Override
protected void createActions() {
super.createActions();
createResolutionSelectAction(CATEGORY_FEATURES);
createBooleanAction("Lenient", CATEGORY_FEATURES, false, lenientCommand);
createBooleanAction("Show week numbers", CATEGORY_FEATURES, false,
weekNumberCommand);
createDateFormatSelectAction(CATEGORY_FEATURES);
createFocusListener(CATEGORY_LISTENERS);
createBlurListener(CATEGORY_LISTENERS);
};
private void createDateFormatSelectAction(String category) {
LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
options.put("-", null);
options.put("d M yyyy", "d M yyyy");
options.put("d MM yyyy", "d MM yyyy");
options.put("d MMM yyyy", "d MMM yyyy");
options.put("d MMMM yyyy", "d MMMM yyyy");
options.put("dd M yyyy", "dd M yyyy");
options.put("ddd M yyyy", "ddd M yyyy");
options.put("d M y", "d M y");
options.put("d M yy", "d M yy");
options.put("d M yyy", "d M yyy");
options.put("d M yyyy", "d M yyyy");
options.put("d M 'custom text' yyyy", "d M 'custom text' yyyy");
options.put("'day:'d', month:'M', year: 'yyyy",
"'day:'d', month:'M', year: 'yyyy");
options.put(getDatePattern(new Locale("fi", "FI"), DateFormat.LONG),
getDatePattern(new Locale("fi", "FI"), DateFormat.LONG));
options.put(getDatePattern(new Locale("fi", "FI"), DateFormat.MEDIUM),
getDatePattern(new Locale("fi", "FI"), DateFormat.MEDIUM));
options.put(getDatePattern(new Locale("fi", "FI"), DateFormat.SHORT),
getDatePattern(new Locale("fi", "FI"), DateFormat.SHORT));
createSelectAction("Date format", category, options, "-",
dateFormatCommand);
}
private String getDatePattern(Locale locale, int dateStyle) {
DateFormat dateFormat = DateFormat.getDateInstance(dateStyle, locale);
if (dateFormat instanceof SimpleDateFormat) {
String pattern = ((SimpleDateFormat) dateFormat).toPattern();
return pattern;
}
return null;
}
private void createResolutionSelectAction(String category) {
LinkedHashMap<String, Integer> options = new LinkedHashMap<String, Integer>();
options.put("Year", DateField.RESOLUTION_YEAR);
options.put("Month", DateField.RESOLUTION_MONTH);
options.put("Day", DateField.RESOLUTION_DAY);
options.put("Hour", DateField.RESOLUTION_HOUR);
options.put("Min", DateField.RESOLUTION_MIN);
options.put("Sec", DateField.RESOLUTION_SEC);
options.put("Msec", DateField.RESOLUTION_MSEC);
createSelectAction("Resolution", category, options, "Year",
resolutionCommand);
}
private Command<T, Integer> resolutionCommand = new Command<T, Integer>() {
public void execute(T c, Integer value, Object data) {
c.setResolution(value);
}
};
private Command<T, Boolean> lenientCommand = new Command<T, Boolean>() {
public void execute(T c, Boolean value, Object data) {
c.setLenient(false);
}
};
private Command<T, Boolean> weekNumberCommand = new Command<T, Boolean>() {
public void execute(T c, Boolean value, Object data) {
c.setShowISOWeekNumbers(value);
}
};
private Command<T, String> dateFormatCommand = new Command<T, String>() {
public void execute(T c, String value, Object data) {
c.setDateFormat(value);
}
};
}

+ 12
- 0
tests/src/com/vaadin/tests/components/datefield/InlineDateFields2.java View File

@@ -0,0 +1,12 @@
package com.vaadin.tests.components.datefield;
import com.vaadin.ui.InlineDateField;
public class InlineDateFields2 extends DateFieldTestCase<InlineDateField> {
@Override
protected Class<InlineDateField> getTestClass() {
return InlineDateField.class;
}
}

+ 38
- 0
tests/src/com/vaadin/tests/components/datefield/PopupDateFields2.java View File

@@ -0,0 +1,38 @@
package com.vaadin.tests.components.datefield;
import java.util.LinkedHashMap;
import com.vaadin.ui.PopupDateField;
public class PopupDateFields2 extends DateFieldTestCase<PopupDateField> {
@Override
protected Class<PopupDateField> getTestClass() {
return PopupDateField.class;
}
@Override
protected void createActions() {
super.createActions();
createInputPromptSelectAction(CATEGORY_FEATURES);
}
private void createInputPromptSelectAction(String category) {
LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
options.put("<none>", null);
options.put("Please enter date", "Please enter date");
options.put("åäöÅÄÖ", "åäöÅÄÖ");
createSelectAction("Input prompt", category, options, "<none>",
new Command<PopupDateField, String>() {
public void execute(PopupDateField c, String value,
Object data) {
c.setInputPrompt(value);
}
});
}
}

+ 65
- 0
tests/src/com/vaadin/tests/components/listselect/ListSelects.java View File

@@ -0,0 +1,65 @@
package com.vaadin.tests.components.listselect;
import java.util.LinkedHashMap;
import com.vaadin.tests.components.select.AbstractSelectTestCase;
import com.vaadin.ui.ListSelect;
public class ListSelects extends AbstractSelectTestCase<ListSelect> {
@Override
protected Class<ListSelect> getTestClass() {
return ListSelect.class;
}
@Override
protected void createActions() {
super.createActions();
createColumnSelectAction();
createRowSelectAction();
}
private void createColumnSelectAction() {
LinkedHashMap<String, Integer> options = new LinkedHashMap<String, Integer>();
options.put("-", 0);
for (int i = 1; i <= 10; i++) {
options.put(String.valueOf(i), i);
}
options.put("50", 50);
options.put("100", 100);
options.put("1000", 1000);
super.createSelectAction("Columns", CATEGORY_DATA_SOURCE, options, "-",
columnsAction);
}
private void createRowSelectAction() {
LinkedHashMap<String, Integer> options = new LinkedHashMap<String, Integer>();
options.put("-", 0);
for (int i = 1; i <= 10; i++) {
options.put(String.valueOf(i), i);
}
options.put("50", 50);
options.put("100", 100);
options.put("1000", 1000);
super.createSelectAction("Rows", CATEGORY_DATA_SOURCE, options, "-",
rowsAction);
}
private Command<ListSelect, Integer> columnsAction = new Command<ListSelect, Integer>() {
public void execute(ListSelect c, Integer value, Object data) {
c.setColumns(value);
}
};
private Command<ListSelect, Integer> rowsAction = new Command<ListSelect, Integer>() {
public void execute(ListSelect c, Integer value, Object data) {
c.setRows(value);
}
};
}

+ 37
- 0
tests/src/com/vaadin/tests/components/optiongroup/OptionGroups.java View File

@@ -0,0 +1,37 @@
package com.vaadin.tests.components.optiongroup;
import com.vaadin.tests.components.select.AbstractSelectTestCase;
import com.vaadin.ui.OptionGroup;
public class OptionGroups extends AbstractSelectTestCase<OptionGroup> {
@Override
protected Class<OptionGroup> getTestClass() {
return OptionGroup.class;
}
@Override
protected void createActions() {
super.createActions();
createFocusListener(CATEGORY_LISTENERS);
createBlurListener(CATEGORY_LISTENERS);
createDisabledItemsMultiToggle("Disabled items");
}
private void createDisabledItemsMultiToggle(String category) {
for (Object id : getComponent().getItemIds()) {
createBooleanAction(id.toString() + " - enabled", category, true,
enabledItemCommand, id);
}
}
private Command<OptionGroup, Boolean> enabledItemCommand = new Command<OptionGroup, Boolean>() {
public void execute(OptionGroup c, Boolean value, Object data) {
c.setItemEnabled(data, value);
}
};
}

+ 168
- 0
tests/src/com/vaadin/tests/components/select/AbstractSelectTestCase.java View File

@@ -0,0 +1,168 @@
package com.vaadin.tests.components.select;
import java.util.LinkedHashMap;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.event.ItemClickEvent.ItemClickSource;
import com.vaadin.tests.components.abstractfield.AbstractFieldTestCase;
import com.vaadin.ui.AbstractSelect;
public abstract class AbstractSelectTestCase<T extends AbstractSelect> extends
AbstractFieldTestCase<T> implements ItemClickListener {
protected static final String CATEGORY_DATA_SOURCE = "Data source";
private int items = 0;
private int properties = 0;
@Override
protected void createActions() {
super.createActions();
createNullSelectAllowedCheckbox(CATEGORY_SELECTION);
createPropertiesInContainerSelect(CATEGORY_DATA_SOURCE);
createItemsInContainerSelect(CATEGORY_DATA_SOURCE);
}
protected void createNullSelectAllowedCheckbox(String category) {
createBooleanAction("Null Selection Allowed", category, false,
nullSelectionAllowedCommand);
}
protected void createNullSelectItemId(String category) {
LinkedHashMap<String, Object> options = new LinkedHashMap<String, Object>();
options.put("- None -", null);
for (Object id : (getComponent()).getContainerDataSource()
.getContainerPropertyIds()) {
options.put(id.toString(), id);
}
createSelectAction("Null Selection Item Id", category, options,
"- None -", nullSelectItemIdCommand);
}
protected Container createContainer(int properties, int items) {
return createIndexedContainer(properties, items);
}
private Container createIndexedContainer(int properties, int items) {
IndexedContainer c = new IndexedContainer();
populateContainer(c, properties, items);
return c;
}
protected void populateContainer(Container c, int properties, int items) {
c.removeAllItems();
for (int i = 1; i <= properties; i++) {
c.addContainerProperty("Property " + i, String.class, "");
}
for (int i = 1; i <= items; i++) {
Item item = c.addItem("Item " + i);
for (int j = 1; j <= properties; j++) {
item.getItemProperty("Property " + j).setValue(
"Item " + i + "," + j);
}
}
}
protected void createItemsInContainerSelect(String category) {
LinkedHashMap<String, Integer> options = new LinkedHashMap<String, Integer>();
for (int i = 0; i <= 10; i++) {
options.put(String.valueOf(i), i);
}
options.put("20", 20);
options.put("100", 100);
options.put("1000", 1000);
options.put("10000", 10000);
options.put("100000", 100000);
createSelectAction("Items in container", category, options, "20",
itemsInContainerCommand);
}
protected void createPropertiesInContainerSelect(String category) {
LinkedHashMap<String, Integer> options = new LinkedHashMap<String, Integer>();
options.put("0", 0);
for (int i = 0; i <= 10; i++) {
options.put(String.valueOf(i), i);
}
options.put("50", 50);
options.put("100", 100);
options.put("1000", 1000);
createSelectAction("Properties in container", category, options, "10",
propertiesInContainerCommand);
}
protected void createItemClickListener(String category) {
createBooleanAction("Item click listener", category, false,
itemClickListenerCommand);
}
/* COMMANDS */
protected Command<T, Boolean> nullSelectionAllowedCommand = new Command<T, Boolean>() {
public void execute(T c, Boolean value, Object data) {
(c).setNullSelectionAllowed(value);
}
};
protected Command<T, Object> nullSelectItemIdCommand = new Command<T, Object>() {
public void execute(T c, Object value, Object data) {
c.setNullSelectionItemId(value);
}
};
protected Command<T, Integer> itemsInContainerCommand = new Command<T, Integer>() {
public void execute(T t, Integer value, Object data) {
items = value;
updateContainer();
}
};
protected Command<T, Integer> propertiesInContainerCommand = new Command<T, Integer>() {
public void execute(T t, Integer value, Object data) {
properties = value;
updateContainer();
}
};
protected Command<T, Boolean> itemClickListenerCommand = new Command<T, Boolean>() {
public void execute(T c, Boolean value, Object data) {
if (value) {
((ItemClickSource) c).addListener(AbstractSelectTestCase.this);
} else {
((ItemClickSource) c)
.removeListener(AbstractSelectTestCase.this);
}
}
};
protected void setContainer(Container newContainer) {
getComponent().setContainerDataSource(newContainer);
}
protected void updateContainer() {
setContainer(createContainer(properties, items));
}
/* COMMANDS END */
public void itemClick(ItemClickEvent event) {
log("ItemClick on itemId: " + event.getItemId() + ", propertyId: "
+ event.getPropertyId() + " using " + event.getButtonName());
}
}

+ 18
- 35
tests/src/com/vaadin/tests/components/table/Tables.java View File

@@ -5,9 +5,8 @@ import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.tests.components.MenuBasedComponentTestCase;
import com.vaadin.tests.components.select.AbstractSelectTestCase;
import com.vaadin.ui.AbstractSelect.MultiSelectMode;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.ColumnResizeEvent;
@@ -17,16 +16,20 @@ import com.vaadin.ui.Table.FooterClickListener;
import com.vaadin.ui.Table.HeaderClickEvent;
import com.vaadin.ui.Table.HeaderClickListener;
public class Tables extends MenuBasedComponentTestCase<Table> implements
public class Tables extends AbstractSelectTestCase<Table> implements
ItemClickListener, HeaderClickListener, FooterClickListener,
ColumnResizeListener {
protected static final String CATEGORY_ROWS = "Rows";
private static final String CATEGORY_HEADER = "Header";
private static final String CATEGORY_FOOTER = "Footer";
private static final String CATEGORY_FEATURE_TOGGLES = "Features";
private static final String CATEGORY_VISIBLE_COLUMNS = "Visible columns";
@Override
protected Class<Table> getTestClass() {
return Table.class;
}
/* COMMANDS */
private Command<Table, Boolean> visibleColumnCommand = new Command<Table, Boolean>() {
public void execute(Table c, Boolean visible, Object propertyId) {
@@ -118,22 +121,19 @@ public class Tables extends MenuBasedComponentTestCase<Table> implements
/* COMMANDS END */
@Override
protected Class<Table> getTestClass() {
return Table.class;
}
protected void createActions() {
super.createActions();
@Override
protected void createCustomActions() {
createPageLengthSelect(CATEGORY_SIZE);
createSelectionModeSelect(CATEGORY_SELECTION);
createItemClickListenerCheckbox(CATEGORY_LISTENERS);
createItemClickListener(CATEGORY_LISTENERS);
createColumnResizeListenerCheckbox(CATEGORY_LISTENERS);
createHeaderClickListenerCheckbox(CATEGORY_LISTENERS);
createFooterClickListenerCheckbox(CATEGORY_LISTENERS);
createRowHeaderModeSelect(CATEGORY_CONTENT);
createRowHeaderModeSelect(CATEGORY_DATA_SOURCE);
createHeaderVisibilitySelect(CATEGORY_HEADER);
createHeaderTextCheckbox(CATEGORY_HEADER);
@@ -141,8 +141,8 @@ public class Tables extends MenuBasedComponentTestCase<Table> implements
createFooterVisibilityCheckbox(CATEGORY_FOOTER);
createFooterTextSelect(CATEGORY_FOOTER);
createColumnReorderingAllowedCheckbox(CATEGORY_FEATURE_TOGGLES);
createColumnCollapsingAllowedCheckbox(CATEGORY_FEATURE_TOGGLES);
createColumnReorderingAllowedCheckbox(CATEGORY_FEATURES);
createColumnCollapsingAllowedCheckbox(CATEGORY_FEATURES);
createVisibleColumnsMultiToggle(CATEGORY_VISIBLE_COLUMNS);
@@ -167,10 +167,13 @@ public class Tables extends MenuBasedComponentTestCase<Table> implements
}
private void createVisibleColumnsMultiToggle(String category) {
LinkedHashMap<String, Object> options = new LinkedHashMap<String, Object>();
for (Object id : getComponent().getContainerPropertyIds()) {
createBooleanAction(id.toString() + " - visible", category, true,
visibleColumnCommand, id);
options.put(id.toString(), id);
}
createMultiToggleAction("Visible columns", category, options,
visibleColumnCommand, true);
}
private void createRowHeaderModeSelect(String category) {
@@ -228,22 +231,6 @@ public class Tables extends MenuBasedComponentTestCase<Table> implements
});
}
private void createItemClickListenerCheckbox(String category) {
Command<Table, Boolean> itemClickListenerCommand = new Command<Table, Boolean>() {
public void execute(Table c, Boolean value, Object data) {
if (value) {
c.addListener((ItemClickListener) Tables.this);
} else {
c.removeListener((ItemClickListener) Tables.this);
}
}
};
createBooleanAction("Item click listener", category, false,
itemClickListenerCommand);
}
private void createHeaderClickListenerCheckbox(String category) {
createBooleanAction("Header click listener", category, false,
@@ -378,8 +365,4 @@ public class Tables extends MenuBasedComponentTestCase<Table> implements
+ event.getButtonName());
}
public void itemClick(ItemClickEvent event) {
log("ItemClick on " + event.getItemId() + "/" + event.getPropertyId()
+ " using " + event.getButtonName());
}
}

+ 353
- 0
tests/src/com/vaadin/tests/components/tree/Trees.java View File

@@ -0,0 +1,353 @@
package com.vaadin.tests.components.tree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import com.vaadin.data.Container;
import com.vaadin.data.Container.Hierarchical;
import com.vaadin.data.util.HierarchicalContainer;
import com.vaadin.tests.components.select.AbstractSelectTestCase;
import com.vaadin.ui.AbstractSelect.MultiSelectMode;
import com.vaadin.ui.Tree;
import com.vaadin.ui.Tree.CollapseEvent;
import com.vaadin.ui.Tree.CollapseListener;
import com.vaadin.ui.Tree.ExpandEvent;
import com.vaadin.ui.Tree.ExpandListener;
import com.vaadin.ui.Tree.ItemStyleGenerator;
public class Trees extends AbstractSelectTestCase<Tree> implements
ExpandListener, CollapseListener {
private int rootItemIds = 3;
private ItemStyleGenerator rootGreenSecondLevelRed = new com.vaadin.ui.Tree.ItemStyleGenerator() {
public String getStyle(Object itemId) {
Hierarchical c = (Container.Hierarchical) getComponent()
.getContainerDataSource();
if (c.isRoot(itemId)) {
return "green";
}
Object parent = c.getParent(itemId);
if (!c.isRoot(parent)) {
return "red";
}
return null;
}
@Override
public String toString() {
return "Root green, second level red";
};
};
private ItemStyleGenerator evenItemsBold = new com.vaadin.ui.Tree.ItemStyleGenerator() {
public String getStyle(Object itemId) {
Hierarchical c = (Container.Hierarchical) getComponent()
.getContainerDataSource();
int idx = 0;
for (Iterator<?> i = c.getItemIds().iterator(); i.hasNext();) {
Object id = i.next();
if (id == itemId) {
if (idx % 2 == 1) {
return "bold";
} else {
return null;
}
}
idx++;
}
return null;
}
@Override
public String toString() {
return "Even items bold";
};
};
@Override
protected Class<Tree> getTestClass() {
return Tree.class;
}
@Override
protected void createActions() {
super.createActions();
// Causes container changes so doing this first..
createRootItemSelectAction(CATEGORY_DATA_SOURCE);
createExpandCollapseActions(CATEGORY_FEATURES);
createSelectionModeSelect(CATEGORY_SELECTION);
createChildrenAllowedAction(CATEGORY_DATA_SOURCE);
createListeners(CATEGORY_LISTENERS);
createItemStyleGenerator(CATEGORY_FEATURES);
// TODO: DropHandler
// TODO: DragMode
// TODO: ActionHandler
}
private void createItemStyleGenerator(String category) {
LinkedHashMap<String, com.vaadin.ui.Tree.ItemStyleGenerator> options = new LinkedHashMap<String, com.vaadin.ui.Tree.ItemStyleGenerator>();
options.put("-", null);
options.put(rootGreenSecondLevelRed.toString(), rootGreenSecondLevelRed);
options.put(evenItemsBold.toString(), evenItemsBold);
createSelectAction("Item Style generator", category, options, "-",
itemStyleGeneratorCommand);
}
private void createListeners(String category) {
createBooleanAction("Expand listener", category, false,
expandListenerCommand);
createBooleanAction("Collapse listener", category, false,
collapseListenerCommand);
createBooleanAction("Item click listener", category, false,
itemClickListenerCommand);
}
private enum SelectMode {
NONE, SINGLE, MULTI_SIMPLE, MULTI;
}
protected void createSelectionModeSelect(String category) {
LinkedHashMap<String, SelectMode> options = new LinkedHashMap<String, SelectMode>();
options.put("None", SelectMode.NONE);
options.put("Single", SelectMode.SINGLE);
options.put("Multi - simple", SelectMode.MULTI_SIMPLE);
options.put("Multi - ctrl/shift", SelectMode.MULTI);
createSelectAction("Selection Mode", category, options,
"Multi - ctrl/shift", new Command<Tree, SelectMode>() {
public void execute(Tree t, SelectMode value, Object data) {
switch (value) {
case NONE:
t.setSelectable(false);
break;
case SINGLE:
t.setMultiSelect(false);
t.setSelectable(true);
break;
case MULTI_SIMPLE:
t.setSelectable(true);
t.setMultiSelect(true);
t.setMultiselectMode(MultiSelectMode.SIMPLE);
break;
case MULTI:
t.setSelectable(true);
t.setMultiSelect(true);
t.setMultiselectMode(MultiSelectMode.DEFAULT);
break;
}
}
});
}
@Override
protected Container createContainer(int properties, int items) {
return createHierarchicalContainer(properties, items, rootItemIds);
}
private Container.Hierarchical createHierarchicalContainer(int properties,
int items, int roots) {
Container.Hierarchical c = new HierarchicalContainer();
populateContainer(c, properties, items);
if (items <= roots) {
return c;
}
// "roots" roots, each with
// "firstLevel" children, two with no children (one with childAllowed,
// one without)
// ("firstLevel"-2)*"secondLevel" children ("secondLevel"/2 with
// childAllowed, "secondLevel"/2 without)
// N*M+N*(M-2)*C = items
// items=N(M+MC-2C)
// Using secondLevel=firstLevel/2 =>
// items = roots*(firstLevel+firstLevel*firstLevel/2-2*firstLevel/2)
// =roots*(firstLevel+firstLevel^2/2-firstLevel)
// = roots*firstLevel^2/2
// => firstLevel = sqrt(items/roots*2)
int firstLevel = (int) Math.ceil(Math.sqrt(items / roots * 2.0));
int secondLevel = firstLevel / 2;
while (roots * (1 + 2 + (firstLevel - 2) * secondLevel) < items) {
// Increase something so we get enough items
secondLevel++;
}
List<Object> itemIds = new ArrayList<Object>(c.getItemIds());
int nextItemId = roots;
for (int rootIndex = 0; rootIndex < roots; rootIndex++) {
// roots use items 0..roots-1
Object rootItemId = itemIds.get(rootIndex);
// force roots to be roots even though they automatically should be
c.setParent(rootItemId, null);
for (int firstLevelIndex = 0; firstLevelIndex < firstLevel; firstLevelIndex++) {
if (nextItemId >= items) {
break;
}
Object firstLevelItemId = itemIds.get(nextItemId++);
c.setParent(firstLevelItemId, rootItemId);
if (firstLevelIndex < 2) {
continue;
}
// firstLevelChildren 2.. have child nodes
for (int secondLevelIndex = 0; secondLevelIndex < secondLevel; secondLevelIndex++) {
if (nextItemId >= items) {
break;
}
Object secondLevelItemId = itemIds.get(nextItemId++);
c.setParent(secondLevelItemId, firstLevelItemId);
}
}
}
return c;
}
private void createRootItemSelectAction(String category) {
LinkedHashMap<String, Integer> options = new LinkedHashMap<String, Integer>();
for (int i = 1; i <= 10; i++) {
options.put(String.valueOf(i), i);
}
options.put("20", 20);
options.put("50", 50);
options.put("100", 100);
createSelectAction("Number of root items", category, options, "3",
rootItemIdsCommand);
}
private void createExpandCollapseActions(String category) {
LinkedHashMap<String, Object> options = new LinkedHashMap<String, Object>();
for (Object id : getComponent().getItemIds()) {
options.put(id.toString(), id);
}
createMultiClickAction("Expand", category, options, expandItemCommand,
null);
createMultiClickAction("Expand recursively", category, options,
expandItemRecursivelyCommand, null);
createMultiClickAction("Collapse", category, options,
collapseItemCommand, null);
}
private void createChildrenAllowedAction(String category) {
LinkedHashMap<String, Object> options = new LinkedHashMap<String, Object>();
for (Object id : getComponent().getItemIds()) {
options.put(id.toString(), id);
}
createMultiToggleAction("Children allowed", category, options,
setChildrenAllowedCommand, true);
}
/*
* COMMANDS
*/
private Command<Tree, Integer> rootItemIdsCommand = new Command<Tree, Integer>() {
public void execute(Tree c, Integer value, Object data) {
rootItemIds = value;
updateContainer();
}
};
private Command<Tree, Object> expandItemCommand = new Command<Tree, Object>() {
public void execute(Tree c, Object itemId, Object data) {
c.expandItem(itemId);
}
};
private Command<Tree, Object> expandItemRecursivelyCommand = new Command<Tree, Object>() {
public void execute(Tree c, Object itemId, Object data) {
c.expandItemsRecursively(itemId);
}
};
private Command<Tree, Object> collapseItemCommand = new Command<Tree, Object>() {
public void execute(Tree c, Object itemId, Object data) {
c.collapseItem(itemId);
}
};
private Command<Tree, Boolean> setChildrenAllowedCommand = new Command<Tree, Boolean>() {
public void execute(Tree c, Boolean areChildrenAllowed, Object itemId) {
c.setChildrenAllowed(itemId, areChildrenAllowed);
}
};
private Command<Tree, Boolean> expandListenerCommand = new Command<Tree, Boolean>() {
public void execute(Tree c, Boolean value, Object data) {
if (value) {
c.addListener((ExpandListener) Trees.this);
} else {
c.removeListener((ExpandListener) Trees.this);
}
}
};
private Command<Tree, Boolean> collapseListenerCommand = new Command<Tree, Boolean>() {
public void execute(Tree c, Boolean value, Object data) {
if (value) {
c.addListener((CollapseListener) Trees.this);
} else {
c.removeListener((CollapseListener) Trees.this);
}
}
};
private Command<Tree, com.vaadin.ui.Tree.ItemStyleGenerator> itemStyleGeneratorCommand = new Command<Tree, com.vaadin.ui.Tree.ItemStyleGenerator>() {
public void execute(Tree c,
com.vaadin.ui.Tree.ItemStyleGenerator value, Object data) {
c.setItemStyleGenerator(value);
}
};
public void nodeCollapse(CollapseEvent event) {
log(event.getClass().getSimpleName() + ": " + event.getItemId());
}
public void nodeExpand(ExpandEvent event) {
log(event.getClass().getSimpleName() + ": " + event.getItemId());
}
}

+ 64
- 0
tests/src/com/vaadin/tests/components/twincolselect/TwinColSelects.java View File

@@ -0,0 +1,64 @@
package com.vaadin.tests.components.twincolselect;
import java.util.LinkedHashMap;
import com.vaadin.tests.components.select.AbstractSelectTestCase;
import com.vaadin.ui.TwinColSelect;
public class TwinColSelects extends AbstractSelectTestCase<TwinColSelect> {
@Override
protected Class<TwinColSelect> getTestClass() {
return TwinColSelect.class;
}
@Override
protected void createActions() {
super.createActions();
createColumnSelectAction();
createRowSelectAction();
}
private void createColumnSelectAction() {
LinkedHashMap<String, Integer> options = new LinkedHashMap<String, Integer>();
options.put("-", 0);
for (int i = 1; i <= 10; i++) {
options.put(String.valueOf(i), i);
}
options.put("50", 50);
options.put("100", 100);
options.put("1000", 1000);
super.createSelectAction("Columns", CATEGORY_DATA_SOURCE, options, "-",
columnsAction);
}
private void createRowSelectAction() {
LinkedHashMap<String, Integer> options = new LinkedHashMap<String, Integer>();
options.put("-", 0);
for (int i = 1; i <= 10; i++) {
options.put(String.valueOf(i), i);
}
options.put("50", 50);
options.put("100", 100);
options.put("1000", 1000);
super.createSelectAction("Rows", CATEGORY_DATA_SOURCE, options, "-",
rowsAction);
}
private Command<TwinColSelect, Integer> columnsAction = new Command<TwinColSelect, Integer>() {
public void execute(TwinColSelect c, Integer value, Object data) {
c.setColumns(value);
}
};
private Command<TwinColSelect, Integer> rowsAction = new Command<TwinColSelect, Integer>() {
public void execute(TwinColSelect c, Integer value, Object data) {
c.setRows(value);
}
};
}

Loading…
Cancel
Save