From: Artur Signell Date: Sun, 17 Oct 2010 18:13:33 +0000 (+0000) Subject: Tree test case X-Git-Tag: 6.7.0.beta1~971^2~8 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8122422ccfe581393f806dc4d8c58e1427a8e102;p=vaadin-framework.git Tree test case svn changeset:15585/svn branch:6.4 --- diff --git a/tests/src/com/vaadin/tests/components/MenuBasedComponentTestCase.java b/tests/src/com/vaadin/tests/components/MenuBasedComponentTestCase.java index 5bea6f2877..c8b6dd021c 100644 --- a/tests/src/com/vaadin/tests/components/MenuBasedComponentTestCase.java +++ b/tests/src/com/vaadin/tests/components/MenuBasedComponentTestCase.java @@ -1,9 +1,11 @@ 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.terminal.Resource; @@ -38,6 +40,11 @@ public abstract class MenuBasedComponentTestCase private Set parentOfSelectableMenuItem = new HashSet(); private MenuItem windowMenu; + /** + * Maps the category name to a menu item + */ + private Map categoryToMenuItem = new HashMap(); + protected static final String CATEGORY_STATE = "State"; protected static final String CATEGORY_SIZE = "Size"; protected static final String CATEGORY_SELECTION = "Selection"; @@ -47,8 +54,11 @@ public abstract class MenuBasedComponentTestCase @Override protected final void setup() { + setTheme("tests-components"); + // Create menu here so it appears before the components menu = new MenuBar(); + menu.setDebugId("menu"); mainMenu = menu.addItem("Component", null); windowMenu = menu.addItem("Test", null); addComponent(menu); @@ -71,6 +81,7 @@ public abstract class MenuBasedComponentTestCase @Override protected void initializeComponents() { component = constructComponent(); + component.setDebugId("testComponent"); addTestComponent(component); } @@ -225,19 +236,52 @@ public abstract class MenuBasedComponentTestCase doCommand(caption, command, initialState, data); } + protected void createClickAction(String caption, + String category, final Command command, DATATYPE value) { + createClickAction(caption, category, command, value, null); + } + + protected void createClickAction(String caption, + String category, final Command 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); + } + + /** + * Creates category "category" in parent category "parentCategory". Each + * category name must be globally unique. + * + * @param category + * @param parentCategory + * @return + */ + 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( @@ -254,6 +298,18 @@ public abstract class MenuBasedComponentTestCase }; } + private MenuBar.Command menuClickCommand( + final com.vaadin.tests.components.ComponentTestCase.Command 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); @@ -323,6 +379,56 @@ public abstract class MenuBasedComponentTestCase } + protected void createMultiClickAction( + String caption, + String category, + LinkedHashMap options, + com.vaadin.tests.components.ComponentTestCase.Command command, + Object data) { + + MenuItem categoryItem = getCategoryMenuItem(category); + MenuItem mainItem = categoryItem.addItem(caption, null); + + for (String option : options.keySet()) { + MenuBar.Command cmd = menuClickCommand(command, + options.get(option), data); + mainItem.addItem(option, cmd); + } + } + + protected void createMultiToggleAction( + String caption, + String category, + LinkedHashMap options, + com.vaadin.tests.components.ComponentTestCase.Command command, + boolean defaultValue) { + + LinkedHashMap defaultValues = new LinkedHashMap(); + + for (String option : options.keySet()) { + defaultValues.put(option, defaultValue); + } + + createMultiToggleAction(caption, category, options, command, + defaultValues); + } + + protected void createMultiToggleAction( + String caption, + String category, + LinkedHashMap options, + com.vaadin.tests.components.ComponentTestCase.Command command, + LinkedHashMap defaultValues) { + + createCategory(caption, category); + + for (String option : options.keySet()) { + createBooleanAction(option, caption, defaultValues.get(option), + command, options.get(option)); + + } + } + protected void createSelectAction( String caption, String category, diff --git a/tests/src/com/vaadin/tests/components/select/AbstractSelectTestCase.java b/tests/src/com/vaadin/tests/components/select/AbstractSelectTestCase.java index 8d63e7243c..50ed38d85e 100644 --- a/tests/src/com/vaadin/tests/components/select/AbstractSelectTestCase.java +++ b/tests/src/com/vaadin/tests/components/select/AbstractSelectTestCase.java @@ -5,20 +5,26 @@ 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 extends - AbstractFieldTestCase { + AbstractFieldTestCase implements ItemClickListener { protected static final String CATEGORY_CONTENT = "Contents"; + private int items = 0; + private int properties = 0; + @Override protected void createActions() { super.createActions(); createNullSelectAllowedCheckbox(CATEGORY_SELECTION); + createPropertiesInContainerSelect(CATEGORY_CONTENT); createItemsInContainerSelect(CATEGORY_CONTENT); - createColumnsInContainerSelect(CATEGORY_CONTENT); } @@ -40,19 +46,29 @@ public abstract class AbstractSelectTestCase extends } 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("Column " + i, String.class, ""); + 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("Column " + j).setValue( + item.getItemProperty("Property " + j).setValue( "Item " + i + "," + j); } } - return c; } protected void createItemsInContainerSelect(String category) { @@ -70,7 +86,7 @@ public abstract class AbstractSelectTestCase extends itemsInContainerCommand); } - protected void createColumnsInContainerSelect(String category) { + protected void createPropertiesInContainerSelect(String category) { LinkedHashMap options = new LinkedHashMap(); options.put("0", 0); for (int i = 0; i <= 10; i++) { @@ -80,8 +96,13 @@ public abstract class AbstractSelectTestCase extends options.put("100", 100); options.put("1000", 1000); - createSelectAction("Columns in container", category, options, "10", - columnsInContainerCommand); + createSelectAction("Properties in container", category, options, "10", + propertiesInContainerCommand); + } + + protected void createItemClickListener(String category) { + createBooleanAction("Item click listener", category, false, + itemClickListenerCommand); } /* COMMANDS */ @@ -103,19 +124,45 @@ public abstract class AbstractSelectTestCase extends protected Command itemsInContainerCommand = new Command() { public void execute(T t, Integer value, Object data) { - t.setContainerDataSource(createContainer(t.getContainerDataSource() - .getContainerPropertyIds().size(), value)); + items = value; + updateContainer(); } }; - protected Command columnsInContainerCommand = new Command() { + protected Command propertiesInContainerCommand = new Command() { public void execute(T t, Integer value, Object data) { - t.setContainerDataSource(createContainer(value, t - .getContainerDataSource().size())); + properties = value; + updateContainer(); } }; + protected Command itemClickListenerCommand = new Command() { + + 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()); + } } diff --git a/tests/src/com/vaadin/tests/components/table/Tables.java b/tests/src/com/vaadin/tests/components/table/Tables.java index 7173244c3f..15a273377c 100644 --- a/tests/src/com/vaadin/tests/components/table/Tables.java +++ b/tests/src/com/vaadin/tests/components/table/Tables.java @@ -5,7 +5,6 @@ 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.select.AbstractSelectTestCase; import com.vaadin.ui.AbstractSelect.MultiSelectMode; @@ -129,7 +128,7 @@ public class Tables extends AbstractSelectTestCase implements createSelectionModeSelect(CATEGORY_SELECTION); - createItemClickListenerCheckbox(CATEGORY_LISTENERS); + createItemClickListener(CATEGORY_LISTENERS); createColumnResizeListenerCheckbox(CATEGORY_LISTENERS); createHeaderClickListenerCheckbox(CATEGORY_LISTENERS); createFooterClickListenerCheckbox(CATEGORY_LISTENERS); @@ -168,10 +167,13 @@ public class Tables extends AbstractSelectTestCase
implements } private void createVisibleColumnsMultiToggle(String category) { + LinkedHashMap options = new LinkedHashMap(); 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) { @@ -229,22 +231,6 @@ public class Tables extends AbstractSelectTestCase
implements }); } - private void createItemClickListenerCheckbox(String category) { - Command itemClickListenerCommand = new Command() { - - 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, @@ -379,8 +365,4 @@ public class Tables extends AbstractSelectTestCase
implements + event.getButtonName()); } - public void itemClick(ItemClickEvent event) { - log("ItemClick on " + event.getItemId() + "/" + event.getPropertyId() - + " using " + event.getButtonName()); - } } diff --git a/tests/src/com/vaadin/tests/components/tree/Trees.java b/tests/src/com/vaadin/tests/components/tree/Trees.java new file mode 100644 index 0000000000..da6eb9fe87 --- /dev/null +++ b/tests/src/com/vaadin/tests/components/tree/Trees.java @@ -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 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 getTestClass() { + return Tree.class; + } + + @Override + protected void createActions() { + super.createActions(); + + // Causes container changes so doing this first.. + createRootItemSelectAction(CATEGORY_CONTENT); + + createExpandCollapseActions(CATEGORY_FEATURES); + createSelectionModeSelect(CATEGORY_SELECTION); + createChildrenAllowedAction(CATEGORY_CONTENT); + + createListeners(CATEGORY_LISTENERS); + createItemStyleGenerator(CATEGORY_FEATURES); + + // TODO: DropHandler + // TODO: DragMode + // TODO: ActionHandler + + } + + private void createItemStyleGenerator(String category) { + + LinkedHashMap options = new LinkedHashMap(); + + 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 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); + + createSelectAction("Selection Mode", category, options, + "Multi - ctrl/shift", new Command() { + + 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 itemIds = new ArrayList(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 options = new LinkedHashMap(); + 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 options = new LinkedHashMap(); + + 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 options = new LinkedHashMap(); + + for (Object id : getComponent().getItemIds()) { + options.put(id.toString(), id); + } + createMultiToggleAction("Children allowed", category, options, + setChildrenAllowedCommand, true); + + } + + /* + * COMMANDS + */ + private Command rootItemIdsCommand = new Command() { + + public void execute(Tree c, Integer value, Object data) { + rootItemIds = value; + updateContainer(); + } + }; + + private Command expandItemCommand = new Command() { + + public void execute(Tree c, Object itemId, Object data) { + c.expandItem(itemId); + } + }; + private Command expandItemRecursivelyCommand = new Command() { + + public void execute(Tree c, Object itemId, Object data) { + c.expandItemsRecursively(itemId); + } + }; + + private Command collapseItemCommand = new Command() { + + public void execute(Tree c, Object itemId, Object data) { + c.collapseItem(itemId); + } + }; + + private Command setChildrenAllowedCommand = new Command() { + + public void execute(Tree c, Boolean areChildrenAllowed, Object itemId) { + c.setChildrenAllowed(itemId, areChildrenAllowed); + } + }; + + private Command expandListenerCommand = new Command() { + public void execute(Tree c, Boolean value, Object data) { + if (value) { + c.addListener((ExpandListener) Trees.this); + } else { + c.removeListener((ExpandListener) Trees.this); + } + } + }; + + private Command collapseListenerCommand = new Command() { + public void execute(Tree c, Boolean value, Object data) { + if (value) { + c.addListener((CollapseListener) Trees.this); + } else { + c.removeListener((CollapseListener) Trees.this); + } + } + }; + + private Command itemStyleGeneratorCommand = new Command() { + + 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()); + } + +}