]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fire value change events from Label when content changes (#12414)
authorMaciej Przepióra <matthew@vaadin.com>
Mon, 24 Feb 2014 01:56:59 +0000 (03:56 +0200)
committerVaadin Code Review <review@vaadin.com>
Mon, 10 Mar 2014 11:56:20 +0000 (11:56 +0000)
Change-Id: I9656025ebde0a80c913713856f5c0374e0f92c27

server/src/com/vaadin/ui/Label.java
server/tests/src/com/vaadin/tests/server/component/label/LabelListeners.java

index 3aa83de42078402cb65e3355a8a8f93f64d0e6d4..71d54fc29adc152bea8763cb2ae4e2074109ffe5 100644 (file)
@@ -186,7 +186,8 @@ public class Label extends AbstractComponent implements Property<String>,
 
     /**
      * Set the value of the label. Value of the label is the XML contents of the
-     * label.
+     * label. Since Vaadin 7.2, changing the value of Label instance with that
+     * method will fire ValueChangeEvent.
      * 
      * @param newStringValue
      *            the New value of the label.
@@ -194,7 +195,13 @@ public class Label extends AbstractComponent implements Property<String>,
     @Override
     public void setValue(String newStringValue) {
         if (getPropertyDataSource() == null) {
-            getState().text = newStringValue;
+
+            LabelState state = (LabelState) getState(false);
+            String oldTextValue = state.text;
+            if (!SharedUtil.equals(oldTextValue, newStringValue)) {
+                getState().text = newStringValue;
+                fireValueChange();
+            }
         } else {
             throw new IllegalStateException(
                     "Label is only a Property.Viewer and cannot update its data source");
@@ -223,7 +230,8 @@ public class Label extends AbstractComponent implements Property<String>,
     }
 
     /**
-     * Sets the property as data-source for viewing.
+     * Sets the property as data-source for viewing. Since Vaadin 7.2 a
+     * ValueChangeEvent is fired if the new value is different from previous.
      * 
      * @param newDataSource
      *            the new data source Property
@@ -253,7 +261,7 @@ public class Label extends AbstractComponent implements Property<String>,
         if (dataSource != null) {
             // Update the value from the data source. If data source was set to
             // null, retain the old value
-            getState().text = getDataSourceValue();
+            updateValueFromDataSource();
         }
 
         // Listens the new data source if possible
@@ -404,7 +412,8 @@ public class Label extends AbstractComponent implements Property<String>,
     private void updateValueFromDataSource() {
         // Update the internal value from the data source
         String newConvertedValue = getDataSourceValue();
-        if (!SharedUtil.equals(newConvertedValue, getState().text)) {
+        if (!SharedUtil.equals(newConvertedValue,
+                ((LabelState) getState(false)).text)) {
             getState().text = newConvertedValue;
             fireValueChange();
         }
index 3ed79f5010e34a690f7c38627fe8b8f8fdc82a84..9bb4c53ba2af8801a57c90093ef280f1e1aed13e 100644 (file)
@@ -1,5 +1,14 @@
 package com.vaadin.tests.server.component.label;
 
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.createStrictMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+
+import org.easymock.EasyMock;
+
+import com.vaadin.data.Property;
 import com.vaadin.data.Property.ValueChangeListener;
 import com.vaadin.tests.server.component.AbstractListenerMethodsTest;
 import com.vaadin.ui.Label;
@@ -10,4 +19,74 @@ public class LabelListeners extends AbstractListenerMethodsTest {
         testListenerAddGetRemove(Label.class, ValueChangeEvent.class,
                 ValueChangeListener.class);
     }
+
+    public void testValueChangeFiredWhenSettingValue() {
+        Label underTest = new Label();
+
+        // setup the mock listener
+        ValueChangeListener mockListener = createStrictMock(ValueChangeListener.class);
+        // record
+        mockListener
+                .valueChange(anyObject(com.vaadin.data.Property.ValueChangeEvent.class));
+
+        // test
+        underTest.addValueChangeListener(mockListener);
+
+        replay(mockListener);
+        underTest.setValue("A new value");
+
+        verify(mockListener);
+
+    }
+
+    public void testValueChangeFiredWhenSettingPropertyDataSource() {
+        // setup
+        Label underTest = new Label();
+
+        Property mockProperty = EasyMock.createMock(Property.class);
+
+        ValueChangeListener mockListener = createStrictMock(ValueChangeListener.class);
+        // record
+        mockListener
+                .valueChange(anyObject(com.vaadin.data.Property.ValueChangeEvent.class));
+
+        expect(mockProperty.getType()).andReturn(String.class).atLeastOnce();
+        expect(mockProperty.getValue()).andReturn("Any").atLeastOnce();
+
+        // test
+
+        replay(mockListener, mockProperty);
+        underTest.addValueChangeListener(mockListener);
+        underTest.setPropertyDataSource(mockProperty);
+
+        verify(mockListener);
+
+    }
+
+    public void testValueChangeNotFiredWhenNotSettingValue() {
+        Label underTest = new Label();
+        // setup the mock listener
+        ValueChangeListener mockListener = createStrictMock(ValueChangeListener.class);
+        // record: nothing to record
+
+        // test
+        underTest.addValueChangeListener(mockListener);
+        replay(mockListener);
+        verify(mockListener);
+    }
+
+    public void testNoValueChangeFiredWhenSettingPropertyDataSourceToNull() {
+        Label underTest = new Label();
+        // setup the mock Listener
+        ValueChangeListener mockListener = createStrictMock(ValueChangeListener.class);
+        // record: nothing to record
+
+        // test
+        underTest.addValueChangeListener(mockListener);
+        underTest.setPropertyDataSource(null);
+
+        replay(mockListener);
+        verify(mockListener);
+    }
+
 }