From 3c02d73b33d9a9eeda62e54c4195a144dae9fac9 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Wed, 13 Oct 2010 20:31:50 +0000 Subject: [PATCH] Split part of ComponentTestCase to AbstractComponentTestCase and added a MenuBasedComponentTestCase. Updated Tables test case to use MenuBasedComponentTestCase. Should be backwards compatible for existing tests svn changeset:15540/svn branch:6.4 --- .../components/AbstractComponentTestCase.java | 160 +++++++ .../tests/components/ComponentTestCase.java | 115 +---- .../MenuBasedComponentTestCase.java | 387 +++++++++++++++ .../tests/components/combobox/Comboboxes.java | 2 +- .../datefield/InlineDateFields.java | 6 +- .../components/datefield/PopupDateFields.java | 6 +- .../optiongroup/DisabledOptionGroupItems.java | 6 +- .../components/select/NativeSelects.java | 5 +- .../vaadin/tests/components/table/Tables.java | 440 +++++++++--------- .../upload/TestUploadAndDisableOnSuccess.java | 2 +- 10 files changed, 788 insertions(+), 341 deletions(-) create mode 100644 tests/src/com/vaadin/tests/components/AbstractComponentTestCase.java create mode 100644 tests/src/com/vaadin/tests/components/MenuBasedComponentTestCase.java diff --git a/tests/src/com/vaadin/tests/components/AbstractComponentTestCase.java b/tests/src/com/vaadin/tests/components/AbstractComponentTestCase.java new file mode 100644 index 0000000000..9fad108b73 --- /dev/null +++ b/tests/src/com/vaadin/tests/components/AbstractComponentTestCase.java @@ -0,0 +1,160 @@ +package com.vaadin.tests.components; + +import java.util.ArrayList; +import java.util.List; + +import com.vaadin.terminal.UserError; +import com.vaadin.tests.util.Log; +import com.vaadin.ui.AbstractComponent; +import com.vaadin.ui.Field; +import com.vaadin.ui.Layout.SpacingHandler; + +public abstract class AbstractComponentTestCase + extends TestBase { + + private List testComponents = new ArrayList(); + + abstract protected Class getTestClass(); + + abstract protected void initializeComponents(); + + private Log log = null; + + @Override + protected void setup() { + ((SpacingHandler) getLayout()).setSpacing(true); + + // Create Components + initializeComponents(); + } + + @Override + protected Integer getTicketNumber() { + return null; + } + + protected void addTestComponent(T c) { + testComponents.add(c); + addComponent(c); + } + + protected List getTestComponents() { + return testComponents; + } + + public interface Command { + public void execute(T c, VALUETYPE value, Object data); + } + + // public interface Command { + // public void execute(T c, VALUETYPE value, Object data); + // } + + /* COMMANDS */ + + protected Command widthCommand = new Command() { + + @Override + public void execute(T t, String value, Object data) { + t.setWidth(value); + } + }; + protected Command heightCommand = new Command() { + + @Override + public void execute(T t, String value, Object data) { + t.setHeight(value); + } + }; + + protected Command enabledCommand = new Command() { + + @Override + public void execute(T c, Boolean enabled, Object data) { + c.setEnabled(enabled); + } + }; + + protected Command requiredCommand = new Command() { + @Override + public void execute(T c, Boolean enabled, Object data) { + if (c instanceof Field) { + ((Field) c).setRequired(enabled); + } else { + throw new IllegalArgumentException(c.getClass().getName() + + " is not a field and cannot be set to required"); + } + } + }; + + protected Command errorIndicatorCommand = new Command() { + @Override + public void execute(T c, Boolean enabled, Object data) { + if (enabled) { + c.setComponentError(new UserError("It failed!")); + } else { + c.setComponentError(null); + + } + } + }; + + protected Command readonlyCommand = new Command() { + @Override + public void execute(T c, Boolean enabled, Object data) { + c.setReadOnly(enabled); + } + }; + + protected void doCommand(Command command, VALUET value) { + doCommand(command, value, null); + } + + protected void doCommand(Command command, VALUET value, + Object data) { + for (T c : getTestComponents()) { + command.execute(c, value, data); + } + } + + protected void doCommand(String commandName, + Command command, VALUET value) { + doCommand(command, value, null); + if (hasLog()) { + log(commandName + ": " + value); + } + } + + protected void doCommand(String commandName, + Command command, VALUET value, Object data) { + doCommand(command, value, data); + if (hasLog()) { + log(commandName + ": " + value); + } + } + + protected boolean hasLog() { + return log != null; + } + + @Override + protected String getDescription() { + return "Generic test case for " + getTestClass().getSimpleName(); + } + + protected void enableLog() { + if (log == null) { + log = new Log(5).setNumberLogRows(true); + getLayout().addComponent(log, 1); + } + + } + + protected void log(String msg) { + if (log == null) { + throw new IllegalStateException( + "Use enableLog() before calling log()"); + } + log.log(msg); + } +} diff --git a/tests/src/com/vaadin/tests/components/ComponentTestCase.java b/tests/src/com/vaadin/tests/components/ComponentTestCase.java index 580662ef0d..1e15f7a47b 100644 --- a/tests/src/com/vaadin/tests/components/ComponentTestCase.java +++ b/tests/src/com/vaadin/tests/components/ComponentTestCase.java @@ -7,8 +7,6 @@ import java.util.List; import com.vaadin.data.Item; import com.vaadin.data.Property; import com.vaadin.data.Property.ValueChangeEvent; -import com.vaadin.terminal.UserError; -import com.vaadin.tests.util.Log; import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.Alignment; import com.vaadin.ui.Button; @@ -17,49 +15,37 @@ import com.vaadin.ui.CheckBox; import com.vaadin.ui.Component; import com.vaadin.ui.Field; import com.vaadin.ui.HorizontalLayout; -import com.vaadin.ui.Layout.SpacingHandler; import com.vaadin.ui.NativeSelect; public abstract class ComponentTestCase extends - TestBase { + AbstractComponentTestCase { protected static final Object CAPTION = "caption"; - private List testComponents = new ArrayList(); private HorizontalLayout actionLayout; - abstract protected Class getTestClass(); - - abstract protected void initializeComponents(); - - private Log log = null; - @Override protected final void setup() { - ((SpacingHandler) getLayout()).setSpacing(true); - // Create action layout so it appears before the components actionLayout = createActionLayout(); addComponent(actionLayout); - // Create Components - initializeComponents(); + super.setup(); // Create actions and add to layout populateActionLayout(); } - private void populateActionLayout() { + protected void populateActionLayout() { for (Component c : createActions()) { - actionLayout.addComponent(c); - actionLayout.setComponentAlignment(c, Alignment.BOTTOM_LEFT); + addAction(c); } } - @Override - protected Integer getTicketNumber() { - return null; + private void addAction(Component c) { + actionLayout.addComponent(c); + actionLayout.setComponentAlignment(c, Alignment.BOTTOM_LEFT); } /** @@ -104,69 +90,24 @@ public abstract class ComponentTestCase extends return actionLayout; } - protected void addTestComponent(T c) { - testComponents.add(c); - addComponent(c); - } - - protected List getTestComponents() { - return testComponents; - } - - public interface Command { - public void execute(T c, VALUETYPE value); - - } - protected Component createErrorIndicatorAction(boolean initialState) { - return createCheckboxAction("Error indicators", initialState, - new Command() { - public void execute(T c, Boolean enabled) { - if (enabled) { - c.setComponentError(new UserError("It failed!")); - } else { - c.setComponentError(null); - - } - } - - }); + return createBooleanAction("Error indicators", initialState, + errorIndicatorCommand); } protected Component createEnabledAction(boolean initialState) { - return createCheckboxAction("Enabled", initialState, - new Command() { - public void execute(T c, Boolean enabled) { - c.setEnabled(enabled); - } - }); + return createBooleanAction("Enabled", initialState, enabledCommand); } protected Component createReadonlyAction(boolean initialState) { - return createCheckboxAction("Readonly", initialState, - new Command() { - public void execute(T c, Boolean enabled) { - c.setReadOnly(enabled); - } - }); + return createBooleanAction("Readonly", initialState, readonlyCommand); } protected Component createRequiredAction(boolean initialState) { - return createCheckboxAction("Required", initialState, - new Command() { - public void execute(T c, Boolean enabled) { - if (c instanceof Field) { - ((Field) c).setRequired(enabled); - } else { - throw new IllegalArgumentException( - c.getClass().getName() - + " is not a field and cannot be set to required"); - } - } - }); + return createBooleanAction("Required", initialState, requiredCommand); } - protected Component createCheckboxAction(String caption, + protected Component createBooleanAction(String caption, boolean initialState, final Command command) { CheckBox checkBox = new CheckBox(caption); @@ -204,16 +145,6 @@ public abstract class ComponentTestCase extends return button; } - protected void doCommand(Command command, VALUET value) { - for (T c : getTestComponents()) { - if (c == null) { - continue; - } - - command.execute(c, value); - } - } - protected Component createSelectAction(String caption, LinkedHashMap options, String initialValue, final Command command) { @@ -255,24 +186,4 @@ public abstract class ComponentTestCase extends return select; } - @Override - protected String getDescription() { - return "Generic test case for " + getTestClass().getSimpleName(); - } - - protected void enableLog() { - if (log == null) { - log = new Log(5).setNumberLogRows(true); - getLayout().addComponent(log, 1); - } - - } - - protected void log(String msg) { - if (log == null) { - throw new IllegalStateException( - "Use enableLog() before calling log()"); - } - log.log(msg); - } } diff --git a/tests/src/com/vaadin/tests/components/MenuBasedComponentTestCase.java b/tests/src/com/vaadin/tests/components/MenuBasedComponentTestCase.java new file mode 100644 index 0000000000..ef566fba7a --- /dev/null +++ b/tests/src/com/vaadin/tests/components/MenuBasedComponentTestCase.java @@ -0,0 +1,387 @@ +package com.vaadin.tests.components; + +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +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.ui.AbstractComponent; +import com.vaadin.ui.AbstractSelect; +import com.vaadin.ui.Field; +import com.vaadin.ui.MenuBar; +import com.vaadin.ui.MenuBar.MenuItem; + +public abstract class MenuBasedComponentTestCase + extends AbstractComponentTestCase { + + private static final Resource SELECTED_ICON = new ThemeResource( + "../runo/icons/16/ok.png"); + + private MenuItem mainMenu; + + private MenuBar menu; + + private T component; + + // Used to determine if a menuItem should be selected and the other + // unselected on click + private Set parentOfSelectableMenuItem = new HashSet(); + + 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"; + + @Override + protected final void setup() { + // Create menu here so it appears before the components + menu = new MenuBar(); + mainMenu = menu.addItem("Settings", null); + addComponent(menu); + + getLayout().setSizeFull(); + enableLog(); + super.setup(); + + // Create menu actions and trigger default actions + populateMenu(); + } + + /** + * By default initializes just one instance of {@link #getTestClass()} using + * {@link #constructComponent()}. + */ + @Override + protected void initializeComponents() { + component = constructComponent(); + addTestComponent(component); + } + + public T getComponent() { + return component; + } + + @Override + protected void addTestComponent(T c) { + super.addTestComponent(c); + getLayout().setExpandRatio(c, 1); + + }; + + /** + * Construct the component that is to be tested. This method uses a no-arg + * constructor by default. Override to customize. + * + * @return Instance of the component that is to be tested. + * @throws IllegalAccessException + * @throws InstantiationException + */ + protected T constructComponent() { + try { + return getTestClass().newInstance(); + } catch (Exception e) { + throw new RuntimeException("Failed to instantiate " + + getTestClass(), e); + } + } + + private void populateMenu() { + createDefaultActions(); + createCustomActions(); + } + + private void createDefaultActions() { + createBooleanAction("Enabled", CATEGORY_STATE, true, enabledCommand); + createBooleanAction("Readonly", CATEGORY_STATE, false, readonlyCommand); + createBooleanAction("Error indicator", CATEGORY_STATE, false, + errorIndicatorCommand); + + 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); + } + } + + @SuppressWarnings("unchecked") + protected void createItemsInContainerSelect(String category) { + LinkedHashMap options = new LinkedHashMap(); + 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 options = new LinkedHashMap(); + 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); + } + } + + return c; + } + + @SuppressWarnings("unchecked") + protected void createNullSelectAllowedCheckbox(String category) { + createBooleanAction("Null Selection Allowed", category, false, + (Command) nullSelectionAllowedCommand); + + } + + @SuppressWarnings("unchecked") + protected void createNullSelectItemId(String category) { + LinkedHashMap options = new LinkedHashMap(); + 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); + } + + protected void createWidthSelect(String category) { + LinkedHashMap options = new LinkedHashMap(); + options.put("Undefined", null); + options.put("50%", "50%"); + options.put("100%", "100%"); + for (int w = 200; w < 1000; w += 100) { + options.put(w + "px", w + "px"); + } + + createSelectAction("Width", category, options, "Undefined", + widthCommand, null); + } + + protected void createHeightSelect(String category) { + LinkedHashMap options = new LinkedHashMap(); + options.put("Undefined", null); + options.put("50%", "50%"); + options.put("100%", "100%"); + for (int w = 200; w < 1000; w += 100) { + options.put(w + "px", w + "px"); + } + + createSelectAction("Height", category, options, "Undefined", + heightCommand, null); + } + + protected void createBooleanAction(String caption, String category, + boolean initialState, final Command command) { + createBooleanAction(caption, category, initialState, command, null); + } + + protected void createBooleanAction(String caption, + String category, boolean initialState, + final Command command, Object data) { + MenuItem categoryItem = getCategoryMenuItem(category); + MenuItem item = categoryItem.addItem(caption, + menuBooleanCommand(command, data)); + setSelected(item, initialState); + doCommand(caption, command, initialState, 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; + } + } + } + return mainMenu.addItem(category, null); + } + + /** + * Provide custom actions for the test case by creating them in this method. + */ + protected abstract void createCustomActions(); + + private MenuBar.Command menuBooleanCommand( + final com.vaadin.tests.components.ComponentTestCase.Command booleanCommand, + final Object data) { + + return new MenuBar.Command() { + @Override + public void menuSelected(MenuItem selectedItem) { + boolean selected = !isSelected(selectedItem); + doCommand(getText(selectedItem), booleanCommand, selected, data); + setSelected(selectedItem, selected); + } + + }; + } + + protected void setSelected(MenuItem item, boolean selected) { + if (selected) { + item.setIcon(SELECTED_ICON); + } else { + item.setIcon(null); + } + } + + protected boolean isSelected(MenuItem item) { + return (item.getIcon() != null); + } + + private MenuBar.Command singleSelectMenuCommand( + final com.vaadin.tests.components.ComponentTestCase.Command cmd, + final VALUETYPE object, final Object data) { + return new MenuBar.Command() { + @Override + public void menuSelected(MenuItem selectedItem) { + doCommand(getText(selectedItem), cmd, object, data); + + if (parentOfSelectableMenuItem.contains(selectedItem + .getParent())) { + unselectChildren(selectedItem.getParent()); + setSelected(selectedItem, true); + } + } + + }; + + } + + /** + * Unselect all child menu items + * + * @param parent + */ + protected void unselectChildren(MenuItem parent) { + List children = parent.getChildren(); + if (children == null) { + return; + } + + for (MenuItem child : children) { + setSelected(child, false); + } + } + + protected String getText(MenuItem item) { + if (!isCategory(item.getParent())) { + return item.getParent().getText(); + } else { + return item.getText(); + } + } + + private boolean isCategory(MenuItem item) { + return item.getParent() == mainMenu; + } + + protected void createSelectAction( + String caption, + String category, + LinkedHashMap options, + String initialValue, + com.vaadin.tests.components.ComponentTestCase.Command command) { + createSelectAction(caption, category, options, initialValue, command, + null); + + } + + protected void createSelectAction( + String caption, + String category, + LinkedHashMap options, + String initialValue, + com.vaadin.tests.components.ComponentTestCase.Command command, + Object data) { + + MenuItem parentItem = getCategoryMenuItem(category); + MenuItem mainItem = parentItem.addItem(caption, null); + + 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 */ + + protected Command nullSelectionAllowedCommand = new Command() { + + @Override + public void execute(AbstractSelect c, Boolean value, Object data) { + (c).setNullSelectionAllowed(value); + } + }; + + protected Command nullSelectItemIdCommand = new Command() { + + @Override + public void execute(AbstractSelect c, Object value, Object data) { + c.setNullSelectionItemId(value); + } + }; + + protected Command itemsInContainerCommand = new Command() { + + @Override + public void execute(AbstractSelect t, Integer value, Object data) { + t.setContainerDataSource(createContainer(t.getContainerDataSource() + .getContainerPropertyIds().size(), value)); + } + }; + + protected Command columnsInContainerCommand = new Command() { + + @Override + public void execute(AbstractSelect t, Integer value, Object data) { + t.setContainerDataSource(createContainer(value, t + .getContainerDataSource().size())); + } + }; + + /* COMMANDS END */ +} diff --git a/tests/src/com/vaadin/tests/components/combobox/Comboboxes.java b/tests/src/com/vaadin/tests/components/combobox/Comboboxes.java index 5d41fae785..94ab20df43 100644 --- a/tests/src/com/vaadin/tests/components/combobox/Comboboxes.java +++ b/tests/src/com/vaadin/tests/components/combobox/Comboboxes.java @@ -136,7 +136,7 @@ public class Comboboxes extends ComponentTestCase { return createSelectAction("Icon", options, "", new Command() { - public void execute(ComboBox c, String value) { + public void execute(ComboBox c, String value, Object data) { for (Object id : c.getItemIds()) { if (value == null) { c.setItemIcon(id, null); diff --git a/tests/src/com/vaadin/tests/components/datefield/InlineDateFields.java b/tests/src/com/vaadin/tests/components/datefield/InlineDateFields.java index e0fb41abdd..083e8f0190 100644 --- a/tests/src/com/vaadin/tests/components/datefield/InlineDateFields.java +++ b/tests/src/com/vaadin/tests/components/datefield/InlineDateFields.java @@ -77,7 +77,8 @@ public class InlineDateFields extends ComponentTestCase { return createSelectAction("Resolution", options, "Year", new Command() { - public void execute(InlineDateField c, Integer value) { + public void execute(InlineDateField c, Integer value, + Object data) { c.setResolution(value); } @@ -92,7 +93,8 @@ public class InlineDateFields extends ComponentTestCase { return createSelectAction("Locale", options, LOCALES[0].toString(), new Command() { - public void execute(InlineDateField c, Locale value) { + public void execute(InlineDateField c, Locale value, + Object data) { c.setCaption(c.getCaption().replaceAll( c.getLocale().toString(), value.toString())); c.setLocale(value); diff --git a/tests/src/com/vaadin/tests/components/datefield/PopupDateFields.java b/tests/src/com/vaadin/tests/components/datefield/PopupDateFields.java index fd661f55c6..8b9cb68d7b 100644 --- a/tests/src/com/vaadin/tests/components/datefield/PopupDateFields.java +++ b/tests/src/com/vaadin/tests/components/datefield/PopupDateFields.java @@ -77,7 +77,8 @@ public class PopupDateFields extends ComponentTestCase { return createSelectAction("Resolution", options, "Year", new Command() { - public void execute(PopupDateField c, Integer value) { + public void execute(PopupDateField c, Integer value, + Object data) { c.setResolution(value); } @@ -93,7 +94,8 @@ public class PopupDateFields extends ComponentTestCase { return createSelectAction("Input prompt", options, "", new Command() { - public void execute(PopupDateField c, String value) { + public void execute(PopupDateField c, String value, + Object data) { c.setInputPrompt(value); } diff --git a/tests/src/com/vaadin/tests/components/optiongroup/DisabledOptionGroupItems.java b/tests/src/com/vaadin/tests/components/optiongroup/DisabledOptionGroupItems.java index 8ca1059e20..4668ef5ab8 100644 --- a/tests/src/com/vaadin/tests/components/optiongroup/DisabledOptionGroupItems.java +++ b/tests/src/com/vaadin/tests/components/optiongroup/DisabledOptionGroupItems.java @@ -52,7 +52,8 @@ public class DisabledOptionGroupItems extends ComponentTestCase { return createButtonAction("Toggle selection mode", new Command() { - public void execute(OptionGroup og, Boolean value) { + public void execute(OptionGroup og, Boolean value, + Object data) { if (og.isMultiSelect()) { og.setMultiSelect(false); og.setNullSelectionItemId(NULL_SELECTION_ID); @@ -68,7 +69,8 @@ public class DisabledOptionGroupItems extends ComponentTestCase { return createButtonAction("Invert disabled items", new Command() { - public void execute(OptionGroup c, Boolean value) { + public void execute(OptionGroup c, Boolean value, + Object data) { for (Object itemId : c.getItemIds()) { c.setItemEnabled(itemId, !c.isItemEnabled(itemId)); } diff --git a/tests/src/com/vaadin/tests/components/select/NativeSelects.java b/tests/src/com/vaadin/tests/components/select/NativeSelects.java index 8955c4e146..d5577e213c 100644 --- a/tests/src/com/vaadin/tests/components/select/NativeSelects.java +++ b/tests/src/com/vaadin/tests/components/select/NativeSelects.java @@ -105,10 +105,11 @@ public class NativeSelects extends ComponentTestCase { @Override protected void createCustomActions(List actions) { - actions.add(createCheckboxAction("Null selection allowed", false, + actions.add(createBooleanAction("Null selection allowed", false, new Command() { - public void execute(NativeSelect c, Boolean value) { + public void execute(NativeSelect c, Boolean value, + Object data) { c.setNullSelectionAllowed(value); } })); diff --git a/tests/src/com/vaadin/tests/components/table/Tables.java b/tests/src/com/vaadin/tests/components/table/Tables.java index a9dee06adf..617532b975 100644 --- a/tests/src/com/vaadin/tests/components/table/Tables.java +++ b/tests/src/com/vaadin/tests/components/table/Tables.java @@ -1,16 +1,14 @@ package com.vaadin.tests.components.table; +import java.util.ArrayList; +import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; -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.tests.components.ComponentTestCase; +import com.vaadin.tests.components.MenuBasedComponentTestCase; import com.vaadin.ui.AbstractSelect.MultiSelectMode; -import com.vaadin.ui.Component; import com.vaadin.ui.Table; import com.vaadin.ui.Table.ColumnResizeEvent; import com.vaadin.ui.Table.ColumnResizeListener; @@ -19,68 +17,165 @@ import com.vaadin.ui.Table.FooterClickListener; import com.vaadin.ui.Table.HeaderClickEvent; import com.vaadin.ui.Table.HeaderClickListener; -public class Tables extends ComponentTestCase implements +public class Tables extends MenuBasedComponentTestCase
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"; + + /* COMMANDS */ + private Command visibleColumnCommand = new Command() { + public void execute(Table c, Boolean visible, Object propertyId) { + List visibleColumns = new ArrayList(Arrays.asList(c + .getVisibleColumns())); + if (visible) { + // Table should really check this... Completely fails without + // the check (# + if (!visibleColumns.contains(propertyId)) { + visibleColumns.add(propertyId); + } + } else { + visibleColumns.remove(propertyId); + } + c.setVisibleColumns(visibleColumns.toArray()); + } + }; + + protected Command columnResizeListenerCommand = new Command() { + @Override + public void execute(Table c, Boolean value, Object data) { + if (value) { + c.addListener((ColumnResizeListener) Tables.this); + } else { + c.removeListener((ColumnResizeListener) Tables.this); + } + } + }; + + protected Command headerClickListenerCommand = new Command() { + @Override + public void execute(Table c, Boolean value, Object data) { + if (value) { + c.addListener((HeaderClickListener) Tables.this); + } else { + c.removeListener((HeaderClickListener) Tables.this); + } + } + }; + + protected Command footerClickListenerCommand = new Command() { + @Override + public void execute(Table c, Boolean value, Object data) { + if (value) { + c.addListener((FooterClickListener) Tables.this); + } else { + c.removeListener((FooterClickListener) Tables.this); + } + } + }; + + protected Command rowHeaderModeCommand = new Command() { + @Override + public void execute(Table c, Integer value, Object data) { + if (value == Table.ROW_HEADER_MODE_PROPERTY) { + c.setItemCaptionPropertyId("Column 3"); + } + c.setRowHeaderMode(value); + } + }; + + protected Command footerTextCommand = new Command() { + @Override + public void execute(Table c, String value, Object data) { + for (Object propertyId : c.getContainerPropertyIds()) { + if (value != null) { + c.setColumnFooter(propertyId, + value.replace("{id}", propertyId.toString())); + } else { + c.setColumnFooter(propertyId, null); + } + } + } + }; + + public class Alignments { + + } + + protected Command columnAlignmentCommand = new Command() { + @Override + public void execute(Table c, Alignments value, Object data) { + for (Object propertyId : c.getContainerPropertyIds()) { + // TODO + } + } + }; + + /* COMMANDS END */ + @Override protected Class
getTestClass() { return Table.class; } @Override - protected void initializeComponents() { - addTestComponent(createTable()); - enableLog(); + protected void createCustomActions() { + createPageLengthSelect(CATEGORY_SIZE); - } + createSelectionModeSelect(CATEGORY_SELECTION); - private Table createTable() { - Table t = new Table(); - return t; - } + createItemClickListenerCheckbox(CATEGORY_LISTENERS); + createColumnResizeListenerCheckbox(CATEGORY_LISTENERS); + createHeaderClickListenerCheckbox(CATEGORY_LISTENERS); + createFooterClickListenerCheckbox(CATEGORY_LISTENERS); - 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); - } - } + createRowHeaderModeSelect(CATEGORY_CONTENT); + + createHeaderVisibilitySelect(CATEGORY_HEADER); + createHeaderTextCheckbox(CATEGORY_HEADER); + + createFooterVisibilityCheckbox(CATEGORY_FOOTER); + createFooterTextSelect(CATEGORY_FOOTER); + + createColumnReorderingAllowedCheckbox(CATEGORY_FEATURE_TOGGLES); + createColumnCollapsingAllowedCheckbox(CATEGORY_FEATURE_TOGGLES); + + createVisibleColumnsMultiToggle(CATEGORY_VISIBLE_COLUMNS); - return c; } - @Override - protected void createCustomActions(List actions) { - actions.add(createNullSelectCheckbox()); - actions.add(createWidthSelect()); - actions.add(createHeightSelect()); - actions.add(createPageLengthSelect()); - actions.add(createItemsInContainerSelect()); - actions.add(createColumnsInContainerSelect()); - actions.add(createSelectionModeSelect()); - actions.add(createItemClickListenerCheckbox()); - actions.add(createColumnResizeListenerCheckbox()); - - actions.add(createRowHeaderModeSelect()); - - actions.add(createHeaderVisibilitySelect()); - actions.add(createHeaderClickListenerCheckbox()); - actions.add(createHeaderTextCheckbox()); - - actions.add(createFooterVisibilityCheckbox()); - actions.add(createFooterClickListenerCheckbox()); - actions.add(createFooterTextCheckbox()); + private void createColumnReorderingAllowedCheckbox(String category) { + createBooleanAction("Column reordering allowed", category, true, + new Command() { + @Override + public void execute(Table c, Boolean value, Object data) { + c.setColumnReorderingAllowed(value); + } + }); + } + + private void createColumnCollapsingAllowedCheckbox(String category) { + createBooleanAction("Column collapsing allowed", category, true, + new Command() { + @Override + public void execute(Table c, Boolean value, Object data) { + c.setColumnCollapsingAllowed(value); + } + }); + } + private void createVisibleColumnsMultiToggle(String category) { + for (Object id : getComponent().getContainerPropertyIds()) { + createBooleanAction(id.toString() + " - visible", category, true, + visibleColumnCommand, id); + } } - private Component createRowHeaderModeSelect() { + private void createRowHeaderModeSelect(String category) { LinkedHashMap options = new LinkedHashMap(); options.put("Explicit", Table.ROW_HEADER_MODE_EXPLICIT); options.put("Explicit defaults id", @@ -92,50 +187,42 @@ public class Tables extends ComponentTestCase
implements options.put("Item", Table.ROW_HEADER_MODE_ITEM); options.put("'Column 3' property", Table.ROW_HEADER_MODE_PROPERTY); - return super.createSelectAction("Row header mode", options, "Hidden", - new Command() { - - public void execute(Table c, Integer value) { - if (value == Table.ROW_HEADER_MODE_PROPERTY) { - c.setItemCaptionPropertyId("Column 3"); - } - c.setRowHeaderMode(value); - - } - }); + createSelectAction("Row header mode", category, options, "Hidden", + rowHeaderModeCommand); } - private Component createFooterTextCheckbox() { - return super.createCheckboxAction("Texts in footer", false, - new Command() { - - public void execute(Table c, Boolean value) { - for (Object propertyId : c.getContainerPropertyIds()) { - if (value) { - c.setColumnFooter(propertyId, "Footer: " - + propertyId); - } else { - c.setColumnFooter(propertyId, null); - - } - } + private void createFooterTextSelect(String category) { + LinkedHashMap options = new LinkedHashMap(); + options.put("None", null); + options.put("Footer X", "Footer {id}"); + options.put("X", "{id}"); - } - }); + createSelectAction("Texts in footer", category, options, "None", + footerTextCommand); } - private Component createHeaderTextCheckbox() { - return super.createCheckboxAction("Texts in header", false, - new Command() { + private void createHeaderTextCheckbox(String category) { + LinkedHashMap options = new LinkedHashMap(); + options.put("None", null); + options.put("Col: {id}", "Col: {id}"); + options.put("Header {id} - every second", "Header {id}"); - public void execute(Table c, Boolean value) { + createSelectAction("Texts in header", category, options, "None", + new Command() { + public void execute(Table c, String value, Object data) { + int nr = 0; for (Object propertyId : c.getContainerPropertyIds()) { - if (value) { - c.setColumnHeader(propertyId, "Header: " - + propertyId); + nr++; + if (value != null && value.equals("Header {id}") + && nr % 2 == 0) { + c.setColumnHeader(propertyId, null); + } else if (value != null) { + c.setColumnHeader( + propertyId, + value.replace("{id}", + propertyId.toString())); } else { c.setColumnHeader(propertyId, null); - } } @@ -143,77 +230,48 @@ public class Tables extends ComponentTestCase
implements }); } - private Component createItemClickListenerCheckbox() { - return super.createCheckboxAction("Item click listener", false, - new Command() { + private void createItemClickListenerCheckbox(String category) { + Command itemClickListenerCommand = new Command() { - public void execute(Table c, Boolean value) { - if (value) { - c.addListener((ItemClickListener) Tables.this); - } else { - c.removeListener((ItemClickListener) Tables.this); - } + @Override + 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 Component createHeaderClickListenerCheckbox() { - return super.createCheckboxAction("Header click listener", false, - new Command() { - - public void execute(Table c, Boolean value) { - if (value) { - c.addListener((HeaderClickListener) Tables.this); - } else { - c.removeListener((HeaderClickListener) Tables.this); - } + private void createHeaderClickListenerCheckbox(String category) { - } - }); + createBooleanAction("Header click listener", category, false, + headerClickListenerCommand); } - private Component createFooterClickListenerCheckbox() { - return super.createCheckboxAction("Footer click listener", false, - new Command() { - - public void execute(Table c, Boolean value) { - if (value) { - c.addListener((FooterClickListener) Tables.this); - } else { - c.removeListener((FooterClickListener) Tables.this); - } + private void createFooterClickListenerCheckbox(String category) { - } - }); + createBooleanAction("Footer click listener", category, false, + footerClickListenerCommand); } - private Component createColumnResizeListenerCheckbox() { - return super.createCheckboxAction("Column resize listener", false, - new Command() { - - public void execute(Table c, Boolean value) { - if (value) { - c.addListener((ColumnResizeListener) Tables.this); - } else { - c.removeListener((ColumnResizeListener) Tables.this); - } + private void createColumnResizeListenerCheckbox(String category) { - } - }); + createBooleanAction("Column resize listener", category, false, + columnResizeListenerCommand); } // TODO: // Visible columns - // Column headers - // Column footers // Column icons // Column alignments // Column width // Column expand ratio // Column collapse - // Column reordering allowed - // Column collapsing allowed // setCurrentPageFirstItemIndex() // setColumnHeaderMode(int) // setRowHeaderMode(int) @@ -224,27 +282,19 @@ public class Tables extends ComponentTestCase
implements // Cache rate // CurrentPageFirstItemId - private Component createNullSelectCheckbox() { - return super.createCheckboxAction("NullSelection", false, - new Command() { - - public void execute(Table c, Boolean value) { - c.setNullSelectionAllowed(value); - } - }); - } - private Component createFooterVisibilityCheckbox() { - return createCheckboxAction("Footer visible", true, + protected void createFooterVisibilityCheckbox(String category) { + createBooleanAction("Footer visible", category, true, new Command() { - public void execute(Table c, Boolean value) { + @Override + public void execute(Table c, Boolean value, Object data) { c.setFooterVisible(value); } }); } - private Component createHeaderVisibilitySelect() { + protected void createHeaderVisibilitySelect(String category) { LinkedHashMap options = new LinkedHashMap(); options.put("Explicit", Table.COLUMN_HEADER_MODE_EXPLICIT); options.put("Explicit defaults id", @@ -252,49 +302,18 @@ public class Tables extends ComponentTestCase
implements options.put("Id", Table.COLUMN_HEADER_MODE_ID); options.put("Hidden", Table.COLUMN_HEADER_MODE_HIDDEN); - return createSelectAction("Header mode", options, + createSelectAction("Header mode", category, options, "Explicit defaults id", new Command() { - public void execute(Table c, Integer value) { + @Override + public void execute(Table c, Integer value, Object data) { c.setColumnHeaderMode(value); } }); } - protected Component createWidthSelect() { - LinkedHashMap options = new LinkedHashMap(); - options.put("Undefined", null); - options.put("200px", "200px"); - options.put("500px", "500px"); - options.put("800px", "800px"); - - return super.createSelectAction("Width", options, "Undefined", - new Command() { - - public void execute(Table t, String value) { - t.setWidth(value); - } - }); - } - - protected Component createHeightSelect() { - LinkedHashMap options = new LinkedHashMap(); - options.put("Undefined", null); - options.put("200px", "200px"); - options.put("500px", "500px"); - options.put("800px", "800px"); - - return super.createSelectAction("Height", options, "Undefined", - new Command() { - - public void execute(Table t, String value) { - t.setHeight(value); - } - }); - } - - protected Component createPageLengthSelect() { + protected void createPageLengthSelect(String category) { LinkedHashMap options = new LinkedHashMap(); options.put("0", 0); options.put("5", 5); @@ -302,69 +321,32 @@ public class Tables extends ComponentTestCase
implements options.put("20", 20); options.put("50", 50); - return super.createSelectAction("PageLength", options, "10", + createSelectAction("PageLength", category, options, "10", new Command() { - public void execute(Table t, Integer value) { + @Override + public void execute(Table t, Integer value, Object data) { t.setPageLength(value); } }); } - protected Component createItemsInContainerSelect() { - LinkedHashMap options = new LinkedHashMap(); - options.put("0", 0); - options.put("20", 20); - options.put("100", 100); - options.put("1000", 1000); - options.put("10000", 10000); - options.put("100000", 100000); - - return super.createSelectAction("Items in container", options, "20", - new Command() { - - public void execute(Table t, Integer value) { - t.setContainerDataSource(createContainer(t - .getContainerDataSource() - .getContainerPropertyIds().size(), value)); - } - }); - } - - protected Component createColumnsInContainerSelect() { - LinkedHashMap options = new LinkedHashMap(); - options.put("0", 0); - options.put("5", 5); - options.put("10", 10); - options.put("50", 50); - options.put("100", 100); - options.put("1000", 1000); - - return super.createSelectAction("Columns in container", options, "10", - new Command() { - - public void execute(Table t, Integer value) { - t.setContainerDataSource(createContainer(value, t - .getContainerDataSource().size())); - } - }); - } - private enum SelectMode { NONE, SINGLE, MULTI_SIMPLE, MULTI; } - protected Component createSelectionModeSelect() { + protected void createSelectionModeSelect(String category) { LinkedHashMap options = new LinkedHashMap(); options.put("None", SelectMode.NONE); options.put("Single", SelectMode.SINGLE); options.put("Multi - simple", SelectMode.MULTI_SIMPLE); options.put("Multi - ctrl/shift", SelectMode.MULTI); - return super.createSelectAction("Selection Mode", options, + createSelectAction("Selection Mode", category, options, "Multi - ctrl/shift", new Command() { - public void execute(Table t, SelectMode value) { + @Override + public void execute(Table t, SelectMode value, Object data) { switch (value) { case NONE: t.setSelectable(false); @@ -404,7 +386,7 @@ public class Tables extends ComponentTestCase
implements } public void itemClick(ItemClickEvent event) { - log("ItemClick on " + event.getPropertyId() + " using " - + event.getButtonName()); + log("ItemClick on " + event.getItemId() + "/" + event.getPropertyId() + + " using " + event.getButtonName()); } } diff --git a/tests/src/com/vaadin/tests/components/upload/TestUploadAndDisableOnSuccess.java b/tests/src/com/vaadin/tests/components/upload/TestUploadAndDisableOnSuccess.java index 26e169c715..009cc63168 100644 --- a/tests/src/com/vaadin/tests/components/upload/TestUploadAndDisableOnSuccess.java +++ b/tests/src/com/vaadin/tests/components/upload/TestUploadAndDisableOnSuccess.java @@ -81,7 +81,7 @@ public class TestUploadAndDisableOnSuccess extends ComponentTestCase actions.add(createButtonAction("Toggle Enabled", new Command() { - public void execute(Upload c, Boolean value) { + public void execute(Upload c, Boolean value, Object data) { c.setEnabled(!c.isEnabled()); } })); -- 2.39.5