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

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.vaadin.Application;
import com.vaadin.data.Property;
import com.vaadin.data.util.AbstractProperty;
import com.vaadin.data.util.converter.Converter.ConversionException;
import com.vaadin.server.WrappedRequest;
import com.vaadin.ui.AbstractField;
import com.vaadin.ui.UI;

public class RemoveListenersOnDetach {

    int numValueChanges = 0;
    int numReadOnlyChanges = 0;

    AbstractField field = new AbstractField() {
        private UI uI = new UI() {

            @Override
            protected void init(WrappedRequest request) {

            }

        };
        private Application application = new Application() {

        };

        @Override
        public Class<?> getType() {
            return String.class;
        }

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

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

        @Override
        public com.vaadin.ui.UI getUI() {
            return uI;
        };

        @Override
        public Application getApplication() {
            return application;
        };
    };

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

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

        @Override
        public Class<?> getType() {
            return String.class;
        }
    };

    @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);
    }
}

@@ -549,24 +549,104 @@
 ctField() %7B%0A
+        final private Application application = new Application() %7B%0A%0A        %7D;%0A
         priv
@@ -773,34 +773,48 @@
         
-%7D;%0A        private
+    @Override%0A            public
  Applica
@@ -810,33 +810,36 @@
 lic Application 
-a
+getA
 pplication = new
@@ -832,38 +832,70 @@
 lication
- = new Application() %7B
+() %7B%0A                return application;%0A            %7D
 %0A%0A      

Update test to make UI.getApplication() return an instance (#9419)