Fixes issue r3414 by making date box fire value change events reliably
Review by:jlabanca
git-svn-id: https://google-web-toolkit.googlecode.com/svn/releases/1.6@4898 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/reference/code-museum/src/com/google/gwt/museum/client/defaultmuseum/VisualsForDateBox.java b/reference/code-museum/src/com/google/gwt/museum/client/defaultmuseum/VisualsForDateBox.java
index 132d0f8..e9cab6e 100644
--- a/reference/code-museum/src/com/google/gwt/museum/client/defaultmuseum/VisualsForDateBox.java
+++ b/reference/code-museum/src/com/google/gwt/museum/client/defaultmuseum/VisualsForDateBox.java
@@ -24,6 +24,7 @@
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.museum.client.common.AbstractIssue;
+import com.google.gwt.museum.client.common.EventReporter;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
@@ -176,6 +177,12 @@
+ "\" End: \"" + (d2 == null ? "null" : f.format(d2)) + "\"");
}
}));
+
+ EventReporter<Date, DateBox> reporter = new EventReporter<Date, DateBox>();
+ start.addValueChangeHandler(reporter);
+ end.addValueChangeHandler(reporter);
+ reporter.report("Events are logged here");
+ v.add(reporter);
return v;
}
diff --git a/user/src/com/google/gwt/event/logical/shared/ValueChangeEvent.java b/user/src/com/google/gwt/event/logical/shared/ValueChangeEvent.java
index e5d8368..4b5513e 100644
--- a/user/src/com/google/gwt/event/logical/shared/ValueChangeEvent.java
+++ b/user/src/com/google/gwt/event/logical/shared/ValueChangeEvent.java
@@ -117,6 +117,11 @@
public I getValue() {
return value;
}
+
+ @Override
+ public String toDebugString() {
+ return super.toDebugString() + getValue();
+ }
@Override
protected void dispatch(ValueChangeHandler<I> handler) {
diff --git a/user/src/com/google/gwt/user/datepicker/client/DateBox.java b/user/src/com/google/gwt/user/datepicker/client/DateBox.java
index 1332561..334a7a0 100644
--- a/user/src/com/google/gwt/user/datepicker/client/DateBox.java
+++ b/user/src/com/google/gwt/user/datepicker/client/DateBox.java
@@ -223,7 +223,7 @@
}
public void onValueChange(ValueChangeEvent<Date> event) {
- setValue(event.getValue());
+ setValue(parseDate(false), event.getValue(),true);
hideDatePicker();
preventDatePickerPopup();
box.setFocus(true);
@@ -432,16 +432,7 @@
}
public void setValue(Date date, boolean fireEvents) {
- Date oldDate = parseDate(false);
- if (date != null) {
- picker.setCurrentMonth(date);
- }
- picker.setValue(date, false);
- setDate(date);
-
- if (fireEvents) {
- DateChangeEvent.fireIfNotEqualDates(this, oldDate, date);
- }
+ setValue(picker.getValue(), date, fireEvents);
}
/**
@@ -473,19 +464,23 @@
});
}
- /**
- * Does the actual work of setting the date. Performs no validation, fires no
- * events.
- */
- private void setDate(Date value) {
+ private void setValue(Date oldDate, Date date, boolean fireEvents) {
+ if (date != null) {
+ picker.setCurrentMonth(date);
+ }
+ picker.setValue(date, false);
format.reset(this, false);
- box.setText(getFormat().format(this, value));
+ box.setText(getFormat().format(this, date));
+
+ if (fireEvents) {
+ DateChangeEvent.fireIfNotEqualDates(this, oldDate, date);
+ }
}
private void updateDateFromTextBox() {
Date parsedDate = parseDate(true);
if (parsedDate != null) {
- setValue(parsedDate);
+ setValue(picker.getValue(), parsedDate,true);
}
}
}
diff --git a/user/test/com/google/gwt/user/client/ui/DateBoxTest.java b/user/test/com/google/gwt/user/client/ui/DateBoxTest.java
index f8ee4a7..624b0b9 100644
--- a/user/test/com/google/gwt/user/client/ui/DateBoxTest.java
+++ b/user/test/com/google/gwt/user/client/ui/DateBoxTest.java
@@ -15,13 +15,14 @@
*/
package com.google.gwt.user.client.ui;
-import com.google.gwt.junit.client.GWTTestCase;
+import com.google.gwt.dom.client.Document;
+import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.user.datepicker.client.DateBox;
/**
- * Tests DateBox.
+ * Tests {@link DateBox}.
*/
-public class DateBoxTest extends GWTTestCase {
+public class DateBoxTest extends WidgetTestBase {
@Override
public String getModuleName() {
return "com.google.gwt.user.User";
@@ -37,8 +38,32 @@
}
public void testValueChangeEvent() {
+
+ // Checks setValue(date, true);
DateBox db = new DateBox();
RootPanel.get().add(db);
new DateValueChangeTester(db).run();
+
+ // Check setting the text directly in the text box.
+ final DateBox db2 = new DateBox();
+ RootPanel.get().add(db2);
+ new DateValueChangeTester(db2) {
+ @Override
+ protected void fire(java.util.Date d) {
+ db2.getTextBox().setText(d.toString());
+ NativeEvent e = Document.get().createBlurEvent();
+ db2.getTextBox().getElement().dispatchEvent(e);
+ };
+ }.run();
+
+ // Checks that setting the date picker's date works correctly.
+ final DateBox db3 = new DateBox();
+ RootPanel.get().add(db3);
+ new DateValueChangeTester(db3) {
+ @Override
+ protected void fire(java.util.Date d) {
+ db3.getDatePicker().setValue(d, true);
+ };
+ }.run();
}
}
diff --git a/user/test/com/google/gwt/user/client/ui/DateValueChangeTester.java b/user/test/com/google/gwt/user/client/ui/DateValueChangeTester.java
index 0e9636f..5a42bac 100644
--- a/user/test/com/google/gwt/user/client/ui/DateValueChangeTester.java
+++ b/user/test/com/google/gwt/user/client/ui/DateValueChangeTester.java
@@ -23,7 +23,8 @@
import java.util.Date;
/**
- * Handy tool for testing classes that implement {@link HasValue<Date>}.
+ * Handy tool for testing classes that implement {@link HasValue} of
+ * {@link Date}.
*/
public class DateValueChangeTester {
static class Handler implements ValueChangeHandler<Date> {
@@ -34,7 +35,7 @@
}
}
- private final HasValue<Date> subject;
+ protected final HasValue<Date> subject;
/**
* The HasValue<Date> to be tested. It should have been freshly created before
@@ -51,6 +52,8 @@
*/
@SuppressWarnings("deprecation")
public void run() {
+
+ // Negative test cases.
TestCase.assertNull(subject.getValue());
DateValueChangeTester.Handler h = new Handler();
@@ -72,16 +75,17 @@
subject.setValue(baker);
TestCase.assertNull(h.received);
+ // Positive test cases.
+
// Value has not changed, so should not fire a change event even though
// fire event is true.
- subject.setValue(baker, true);
+ fire(baker);
TestCase.assertNull(h.received);
-
- subject.setValue(able, true);
+ fire(able);
TestCase.assertEquals(able, h.received);
TestCase.assertNotSame(able, h.received);
- subject.setValue(baker, true);
+ fire(baker);
TestCase.assertEquals(baker, h.received);
TestCase.assertNotSame(baker, h.received);
@@ -90,4 +94,9 @@
subject.setValue(baker, false);
TestCase.assertNull(h.received);
}
-}
\ No newline at end of file
+
+ protected void fire(Date d) {
+ subject.setValue(d, true);
+ }
+
+}
diff --git a/user/test/com/google/gwt/user/client/ui/WidgetTestBase.java b/user/test/com/google/gwt/user/client/ui/WidgetTestBase.java
new file mode 100644
index 0000000..6204ef4
--- /dev/null
+++ b/user/test/com/google/gwt/user/client/ui/WidgetTestBase.java
@@ -0,0 +1,40 @@
+/*
+ * 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.junit.client.GWTTestCase;
+
+/**
+ * Convenience class to use user module and a default tear down code for widget
+ * tests.
+ */
+public class WidgetTestBase extends GWTTestCase {
+ @Override
+ public String getModuleName() {
+ return "com.google.gwt.user.User";
+ }
+
+ /**
+ * A replacement for JUnit's {@link #tearDown()} method. This method runs once
+ * per test method in your subclass, just after your each test method runs and
+ * can be used to perform cleanup. Override this method instead of
+ * {@link #tearDown()}.
+ */
+ @Override
+ protected void gwtTearDown() throws Exception {
+ RootPanel.get().clear();
+ }
+}