From 7fd0e6e1d4f1895de65858e09e26c26f49244211 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 9 Nov 2010 14:02:55 +0000 Subject: [PATCH] Generic menubar test case svn changeset:15935/svn branch:6.5 --- .../MenuBasedComponentTestCase.java | 35 ++ .../tests/components/menubar/MenuBars2.java | 299 ++++++++++++++++++ 2 files changed, 334 insertions(+) create mode 100644 tests/src/com/vaadin/tests/components/menubar/MenuBars2.java diff --git a/tests/src/com/vaadin/tests/components/MenuBasedComponentTestCase.java b/tests/src/com/vaadin/tests/components/MenuBasedComponentTestCase.java index dfe1b675a6..b26166b167 100644 --- a/tests/src/com/vaadin/tests/components/MenuBasedComponentTestCase.java +++ b/tests/src/com/vaadin/tests/components/MenuBasedComponentTestCase.java @@ -488,6 +488,41 @@ public abstract class MenuBasedComponentTestCase } } + protected LinkedHashMap createIntegerOptions(int max) { + LinkedHashMap options = new LinkedHashMap(); + for (int i = 0; i <= 9 && i <= max; i++) { + options.put(String.valueOf(i), i); + } + for (int i = 10; i <= max; i *= 10) { + options.put(String.valueOf(i), i); + if (2 * i <= max) { + options.put(String.valueOf(2 * i), 2 * i); + } + if (5 * i <= max) { + options.put(String.valueOf(5 * i), 5 * i); + } + } + + return options; + } + + protected LinkedHashMap createIconOptions( + boolean cacheable) { + LinkedHashMap options = new LinkedHashMap(); + options.put("-", null); + if (cacheable) { + 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); + } else { + 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); + + } + return options; + } + protected void log(String msg) { log.log(msg); } diff --git a/tests/src/com/vaadin/tests/components/menubar/MenuBars2.java b/tests/src/com/vaadin/tests/components/menubar/MenuBars2.java new file mode 100644 index 0000000000..29155df22e --- /dev/null +++ b/tests/src/com/vaadin/tests/components/menubar/MenuBars2.java @@ -0,0 +1,299 @@ +package com.vaadin.tests.components.menubar; + +import java.util.Date; +import java.util.LinkedHashMap; + +import com.vaadin.terminal.Resource; +import com.vaadin.terminal.ThemeResource; +import com.vaadin.tests.components.MenuBasedComponentTestCase; +import com.vaadin.ui.MenuBar; +import com.vaadin.ui.MenuBar.MenuItem; + +public class MenuBars2 extends MenuBasedComponentTestCase { + + private static final String CATEGORY_MENU_ITEMS = "Menu items"; + private static final String CATEGORY_MENU_ITEM_STATES = "Menu item states"; + + private int rootItems = -1; + private int subItems = -1; + private int subLevels = -1; + private int subMenuDensity = -1; + private Integer subMenuSeparatorDensity = null; + private int iconInterval = -1; + private Integer iconSize; + private Integer disabledDensity; + private Integer invisibleDensity; + + @Override + protected Class getTestClass() { + return MenuBar.class; + } + + @Override + protected void createActions() { + super.createActions(); + createRootMenuItemSelect(CATEGORY_MENU_ITEMS); + createSubMenuItemSelect(CATEGORY_MENU_ITEMS); + createSubMenuLevelsSelect(CATEGORY_MENU_ITEMS); + createSubMenuDensitySelect(CATEGORY_MENU_ITEMS); + createSubMenuSeparatorDensitySelect(CATEGORY_MENU_ITEMS); + + createMenuItemIconIntervalSelect(CATEGORY_MENU_ITEM_STATES); + createMenuIconsSizeSelect(CATEGORY_MENU_ITEM_STATES); + createMenuItemDisabledDensitySelect(CATEGORY_MENU_ITEM_STATES); + createMenuItemInvisibleDensitySelect(CATEGORY_MENU_ITEM_STATES); + + } + + private void createRootMenuItemSelect(String category) { + createSelectAction("Root menu items", category, + createIntegerOptions(100), "10", createRootMenuItems); + } + + private void createSubMenuItemSelect(String category) { + createSelectAction("Sub menu items", category, + createIntegerOptions(100), "10", createSubMenuItems); + } + + private void createSubMenuLevelsSelect(String category) { + createSelectAction("Sub menu levels", category, + createIntegerOptions(100), "2", setSubMenuLevels); + } + + private void createMenuIconsSizeSelect(String category) { + LinkedHashMap options = new LinkedHashMap(); + options.put("16x16", 16); + options.put("32x32", 32); + options.put("64x64", 64); + createSelectAction("Icon size", category, options, "16x16", selectIcon); + } + + private void createMenuItemIconIntervalSelect(String category) { + LinkedHashMap options = new LinkedHashMap(); + options.put("None", 0); + options.put("All", 1); + options.put("Every second", 2); + options.put("Every third", 3); + + createSelectAction("Icons", category, options, "None", setMenuIcons); + } + + private void createSubMenuDensitySelect(String category) { + LinkedHashMap options = new LinkedHashMap(); + options.put("All", 1); + options.put("Every second", 2); + options.put("Every third", 3); + + createSelectAction("Sub sub menus", category, options, "Every third", + setSubMenuDensity); + } + + private void createSubMenuSeparatorDensitySelect(String category) { + LinkedHashMap options = new LinkedHashMap(); + options.put("No separators", null); + options.put("Between all", 1); + options.put("Between every second", 2); + options.put("Between every third", 3); + + createSelectAction("Sub menu separators", category, options, + "No separators", setSubMenuSeparatorDensity); + } + + private void createMenuItemDisabledDensitySelect(String category) { + LinkedHashMap options = new LinkedHashMap(); + options.put("All enabled", null); + options.put("All disabled", 1); + options.put("Every second", 2); + options.put("Every third", 3); + + createSelectAction("Enabled", category, options, "All enabled", + setMenuItemDisabledDensity); + } + + private void createMenuItemInvisibleDensitySelect(String category) { + LinkedHashMap options = new LinkedHashMap(); + options.put("All visible", null); + options.put("All invisible", 1); + options.put("Every second", 2); + options.put("Every third", 3); + + createSelectAction("Visible", category, options, "All visible", + setMenuItemInvisibleDensity); + } + + /* COMMANDS */ + Command createRootMenuItems = new Command() { + + public void execute(MenuBar c, Integer value, Object data) { + rootItems = value; + createRootItems(c); + } + }; + + Command createSubMenuItems = new Command() { + + public void execute(MenuBar c, Integer value, Object data) { + subItems = value; + createSubItems(c); + } + }; + + Command setSubMenuLevels = new Command() { + + public void execute(MenuBar c, Integer value, Object data) { + subLevels = value; + createSubItems(c); + } + }; + private Command setMenuIcons = new Command() { + + public void execute(MenuBar c, Integer value, Object data) { + iconInterval = value; + updateIcons(c); + } + }; + + private Command setSubMenuDensity = new Command() { + + public void execute(MenuBar c, Integer value, Object data) { + subMenuDensity = value; + createSubItems(c); + } + }; + + private Command setMenuItemDisabledDensity = new Command() { + + public void execute(MenuBar c, Integer value, Object data) { + disabledDensity = value; + createRootItems(c); + } + }; + + private Command setMenuItemInvisibleDensity = new Command() { + + public void execute(MenuBar c, Integer value, Object data) { + invisibleDensity = value; + createRootItems(c); + } + }; + + private Command setSubMenuSeparatorDensity = new Command() { + + public void execute(MenuBar c, Integer value, Object data) { + subMenuSeparatorDensity = value; + createSubItems(c); + } + }; + + private Command selectIcon = new Command() { + + public void execute(MenuBar c, Integer value, Object data) { + iconSize = value; + updateIcons(c); + } + }; + + /* End of commands */ + + private MenuBar.Command menuCommand = new MenuBar.Command() { + + public void menuSelected(MenuItem selectedItem) { + log("Menu item '" + selectedItem.getText() + "' selected"); + + } + }; + + protected void createSubItems(MenuBar c) { + for (MenuItem rootItem : c.getItems()) { + createSubItems(rootItem, 1); + } + updateIcons(c); + + } + + private void createSubItems(MenuItem parent, int level) { + if (level > subLevels) { + return; + } + + parent.removeChildren(); + for (int i = 0; i < subItems; i++) { + if (subMenuSeparatorDensity != null && i > 0 + && i % subMenuSeparatorDensity == 0) { + parent.addSeparator(); + } + + MenuItem subMenuItem = parent.addItem("Sub menu " + parent.getId() + + "/" + (i + 1), menuCommand); + + if (disabledDensity != null && i % disabledDensity == 0) { + subMenuItem.setEnabled(false); + } + if (invisibleDensity != null && i % invisibleDensity == 0) { + subMenuItem.setVisible(false); + } + + if (i % subMenuDensity == 0) { + subMenuItem.setCommand(null); + createSubItems(subMenuItem, level + 1); + } + } + + } + + protected void updateIcons(MenuBar c) { + int idx = 0; + for (MenuItem rootItem : c.getItems()) { + updateIcons(rootItem, idx++); + } + } + + private void updateIcons(MenuItem item, int idx) { + if (iconInterval > 0 && idx % iconInterval == 0) { + item.setIcon(getIcon()); + } else { + item.setIcon(null); + } + if (item.getChildren() != null) { + int i = 0; + for (MenuItem child : item.getChildren()) { + updateIcons(child, i++); + } + } + } + + private long iconCacheIndex = new Date().getTime(); + + private Resource getIcon() { + String resourceID = null; + if (iconSize == 16) { + resourceID = "../runo/icons/16/user.png"; + } else if (iconSize == 32) { + resourceID = "../runo/icons/32/user.png"; + } else if (iconSize == 64) { + resourceID = "../runo/icons/64/user.png"; + } + + if (resourceID != null) { + return new ThemeResource(resourceID + "?" + iconCacheIndex++); + } + return null; + } + + protected void createRootItems(MenuBar c) { + // Remove all existing items + c.removeItems(); + for (int i = 0; i < rootItems; i++) { + MenuItem rootItem = c.addItem("Root menu " + (i + 1), null); + if (disabledDensity != null && i % disabledDensity == 0) { + rootItem.setEnabled(false); + } + if (invisibleDensity != null && i % invisibleDensity == 0) { + rootItem.setVisible(false); + } + } + createSubItems(c); + + } + +} -- 2.39.5