package com.vaadin.tests.server.component.abstractfield;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.vaadin.data.Property;
import com.vaadin.data.util.AbstractProperty;
import com.vaadin.data.util.converter.Converter.ConversionException;
import com.vaadin.ui.AbstractField;

public class RemoveListenersOnDetach {

    int numValueChanges = 0;
    int numReadOnlyChanges = 0;

    AbstractField field = new AbstractField() {
        @Override
        public Class<?> getType() {
            return null;
        }

        @Override
        public void valueChange(Property.ValueChangeEvent event) {
            super.valueChange(event);
            numValueChanges++;
        }

        @Override
        public void readOnlyStatusChange(
                Property.ReadOnlyStatusChangeEvent event) {
            super.readOnlyStatusChange(event);
            numReadOnlyChanges++;
        }
    };

    Property property = new AbstractProperty() {
        public Object getValue() {
            return null;
        }

        public void setValue(Object newValue) throws ReadOnlyException,
                ConversionException {
            fireValueChange();
        }

        public Class<?> getType() {
            return null;
        }
    };

    @Test
    public void testAttachDetach() {
        field.setPropertyDataSource(property);

        property.setValue(null);
        property.setReadOnly(true);
        assertEquals(1, numValueChanges);
        assertEquals(1, numReadOnlyChanges);

        field.attach();
        property.setValue(null);
        property.setReadOnly(false);
        assertEquals(2, numValueChanges);
        assertEquals(2, numReadOnlyChanges);

        field.detach();
        property.setValue(null);
        property.setReadOnly(true);
        assertEquals(2, numValueChanges);
        assertEquals(2, numReadOnlyChanges);
    }
}

@@ -291,24 +291,94 @@
 din.
-ui.AbstractField
+terminal.WrappedRequest;%0Aimport com.vaadin.ui.AbstractField;%0Aimport com.vaadin.ui.Root
 ;%0A%0Ap
@@ -522,24 +522,174 @@
 ctField() %7B%0A
+        private Root root = new Root() %7B%0A%0A            @Override%0A            protected void init(WrappedRequest request) %7B%0A%0A            %7D%0A%0A        %7D;%0A%0A
         @Ove
@@ -1124,32 +1124,32 @@
 dOnlyChanges++;%0A
-
         %7D%0A    %7D;
@@ -1133,32 +1133,133 @@
 ges++;%0A        %7D
+%0A%0A        @Override%0A        public com.vaadin.ui.Root getRoot() %7B%0A            return root;%0A        %7D;
 %0A    %7D;%0A%0A    Pro

Provide a dummy Root to avoid NPE