Fixes issue 3378 by adding getHandlerCount()
Review by:rjrjr
git-svn-id: https://google-web-toolkit.googlecode.com/svn/releases/1.6@4885 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/client/ui/Widget.java b/user/src/com/google/gwt/user/client/ui/Widget.java
index 5933672..f3aa9d9 100644
--- a/user/src/com/google/gwt/user/client/ui/Widget.java
+++ b/user/src/com/google/gwt/user/client/ui/Widget.java
@@ -22,6 +22,7 @@
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.event.shared.HasHandlers;
+import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventListener;
@@ -196,6 +197,16 @@
}
/**
+ * Gets the number of handlers listening to the event type.
+ *
+ * @param type the event type
+ * @return the number of registered handlers
+ */
+ protected int getHandlerCount(Type<?> type) {
+ return handlerManager == null ? 0 : handlerManager.getHandlerCount(type);
+ }
+
+ /**
* Has this widget ever been attached?
*
* @return true if this widget ever been attached to the DOM, false otherwise
diff --git a/user/test/com/google/gwt/user/UISuite.java b/user/test/com/google/gwt/user/UISuite.java
index d043f86..f5b3da7 100644
--- a/user/test/com/google/gwt/user/UISuite.java
+++ b/user/test/com/google/gwt/user/UISuite.java
@@ -22,6 +22,7 @@
import com.google.gwt.user.client.WindowTest;
import com.google.gwt.user.client.ui.AbsolutePanelTest;
import com.google.gwt.user.client.ui.AnchorTest;
+import com.google.gwt.user.client.ui.WidgetTest;
import com.google.gwt.user.client.ui.ButtonTest;
import com.google.gwt.user.client.ui.CaptionPanelTest;
import com.google.gwt.user.client.ui.CheckBoxTest;
@@ -164,6 +165,7 @@
suite.addTestSuite(ClassInitTest.class);
suite.addTestSuite(DateChangeEventTest.class);
suite.addTestSuite(CreateEventTest.class);
+ suite.addTestSuite(WidgetTest.class);
return suite;
}
}
diff --git a/user/test/com/google/gwt/user/client/ui/WidgetTest.java b/user/test/com/google/gwt/user/client/ui/WidgetTest.java
new file mode 100644
index 0000000..109ce8e
--- /dev/null
+++ b/user/test/com/google/gwt/user/client/ui/WidgetTest.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.user.client.ui;
+
+import com.google.gwt.event.dom.client.ChangeEvent;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.event.shared.HandlerRegistration;
+import com.google.gwt.junit.client.GWTTestCase;
+
+/**
+ * Tests the basic widget infrastructure.
+ *
+ */
+public class WidgetTest extends GWTTestCase {
+
+ ClickHandler handlerA = new ClickHandler() {
+
+ public void onClick(ClickEvent event) {
+ }
+ };
+
+ ClickHandler handlerB = new ClickHandler() {
+
+ public void onClick(ClickEvent event) {
+ }
+ };
+
+ @Override
+ public String getModuleName() {
+ return "com.google.gwt.user.User";
+ }
+
+ public void testHandlerCount() {
+ Widget a = new Widget();
+ assertEquals(0, a.getHandlerCount(ClickEvent.getType()));
+ HandlerRegistration r1 = a.addDomHandler(handlerA, ClickEvent.getType());
+ assertEquals(1, a.getHandlerCount(ClickEvent.getType()));
+ HandlerRegistration r2 = a.addHandler(handlerB, ClickEvent.getType());
+ assertEquals(2, a.getHandlerCount(ClickEvent.getType()));
+
+ assertEquals(0, a.getHandlerCount(ChangeEvent.getType()));
+ r1.removeHandler();
+ r2.removeHandler();
+ assertEquals(0, a.getHandlerCount(ClickEvent.getType()));
+ }
+
+}