aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/com/vaadin/ui/MenuBar.java2
-rw-r--r--tests/src/com/vaadin/tests/server/component/menubar/MenuBarIds.java97
2 files changed, 98 insertions, 1 deletions
diff --git a/src/com/vaadin/ui/MenuBar.java b/src/com/vaadin/ui/MenuBar.java
index f437243a0e..3a1ee020b2 100644
--- a/src/com/vaadin/ui/MenuBar.java
+++ b/src/com/vaadin/ui/MenuBar.java
@@ -31,7 +31,7 @@ public class MenuBar extends AbstractComponent {
private final List<MenuItem> menuItems;
// Number of items in this menu
- private static int numberOfItems = 0;
+ private int numberOfItems = 0;
/**
* @deprecated
diff --git a/tests/src/com/vaadin/tests/server/component/menubar/MenuBarIds.java b/tests/src/com/vaadin/tests/server/component/menubar/MenuBarIds.java
new file mode 100644
index 0000000000..9ce55361bd
--- /dev/null
+++ b/tests/src/com/vaadin/tests/server/component/menubar/MenuBarIds.java
@@ -0,0 +1,97 @@
+package com.vaadin.tests.server.component.menubar;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+import com.vaadin.ui.MenuBar;
+import com.vaadin.ui.MenuBar.Command;
+import com.vaadin.ui.MenuBar.MenuItem;
+
+public class MenuBarIds extends TestCase implements Command {
+
+ private MenuItem lastSelectedItem;
+ private MenuItem menuFile;
+ private MenuItem menuEdit;
+ private MenuItem menuEditCopy;
+ private MenuItem menuEditCut;
+ private MenuItem menuEditPaste;
+ private MenuItem menuEditFind;
+ private MenuItem menuFileOpen;
+ private MenuItem menuFileSave;
+ private MenuItem menuFileExit;
+ private Set<MenuItem> menuItems = new HashSet<MenuItem>();
+
+ private MenuBar menuBar;
+
+ @Override
+ public void setUp() {
+ menuBar = new MenuBar();
+ menuFile = menuBar.addItem("File", this);
+ menuEdit = menuBar.addItem("Edit", this);
+ menuEditCopy = menuEdit.addItem("Copy", this);
+ menuEditCut = menuEdit.addItem("Cut", this);
+ menuEditPaste = menuEdit.addItem("Paste", this);
+ menuEdit.addSeparator();
+ menuEditFind = menuEdit.addItem("Find...", this);
+ menuFileOpen = menuFile.addItem("Open", this);
+ menuFileSave = menuFile.addItem("Save", this);
+ menuFile.addSeparator();
+ menuFileExit = menuFile.addItem("Exit", this);
+
+ menuItems.add(menuFile);
+ menuItems.add(menuEdit);
+ menuItems.add(menuEditCopy);
+ menuItems.add(menuEditCut);
+ menuItems.add(menuEditPaste);
+ menuItems.add(menuEditFind);
+ menuItems.add(menuFileOpen);
+ menuItems.add(menuFileSave);
+ menuItems.add(menuFileExit);
+ }
+
+ public void testMenubarIdUniqueness() {
+ // Ids within a menubar must be unique
+ assertUniqueIds(menuBar);
+
+ menuBar.removeItem(menuFile);
+ MenuItem file2 = menuBar.addItem("File2", this);
+ MenuItem file3 = menuBar.addItem("File3", this);
+ MenuItem file2sub = file2.addItem("File2 sub menu", this);
+ menuItems.add(file2);
+ menuItems.add(file2sub);
+ menuItems.add(file3);
+
+ assertUniqueIds(menuBar);
+ }
+
+ private static void assertUniqueIds(MenuBar menuBar) {
+
+ Set<Object> ids = new HashSet<Object>();
+
+ for (MenuItem item : menuBar.getItems()) {
+ assertUniqueIds(ids, item);
+ }
+ }
+
+ private static void assertUniqueIds(Set<Object> ids, MenuItem item) {
+ int id = item.getId();
+ System.out.println("Item " + item.getText() + ", id: " + id);
+ assertFalse(ids.contains(id));
+ ids.add(id);
+ if (item.getChildren() != null) {
+ for (MenuItem subItem : item.getChildren()) {
+ assertUniqueIds(ids, subItem);
+ }
+ }
+ }
+
+ public void menuSelected(MenuItem selectedItem) {
+ assertNull("lastSelectedItem was not cleared before selecting an item",
+ lastSelectedItem);
+
+ lastSelectedItem = selectedItem;
+
+ }
+}