]> source.dussan.org Git - vaadin-framework.git/commitdiff
#8170 Updated to work with Vaadin 7
authorLeif Åstrand <leif@vaadin.com>
Fri, 6 Jan 2012 12:41:07 +0000 (14:41 +0200)
committerLeif Åstrand <leif@vaadin.com>
Fri, 6 Jan 2012 12:41:07 +0000 (14:41 +0200)
src/com/vaadin/ui/Root.java
src/com/vaadin/ui/Window.java
tests/server-side/com/vaadin/tests/server/component/window/AttachDetachWindow.java
tests/testbench/com/vaadin/tests/components/window/AttachShouldBeCalledForSubWindows.java

index dec2efcb9f548cf40be8f7b6b98d217f70455669..7fd4a79458b6361e03081d6f30647cf04bc29977 100644 (file)
@@ -641,15 +641,17 @@ public abstract class Root extends AbstractComponentContainer implements
      * @see #getApplication()
      */
     public void setApplication(Application application) {
-        if (application == null) {
-            throw new NullPointerException("application");
-        } else if (this.application != null) {
+        if ((application == null) == (this.application == null)) {
             throw new IllegalStateException("Application has already been set");
         } else {
             this.application = application;
         }
 
-        attach();
+        if (application != null) {
+            attach();
+        } else {
+            detach();
+        }
     }
 
     /**
@@ -1509,4 +1511,29 @@ public abstract class Root extends AbstractComponentContainer implements
     public int getBrowserWindowWidth() {
         return browserWindowWidth;
     }
+
+    /**
+     * Notifies the child components and windows that the root is attached to
+     * the application.
+     */
+    @Override
+    public void attach() {
+        super.attach();
+        for (Window w : windows) {
+            w.attach();
+        }
+    }
+
+    /**
+     * Notifies the child components and windows that the root is detached from
+     * the application.
+     */
+    @Override
+    public void detach() {
+        super.detach();
+        for (Window w : windows) {
+            w.detach();
+        }
+    }
+
 }
index 1b880bcb706c7d46aa149febfaaea7f656d683c7..a860d371b086aa1188890b0c25a1a92257c50c35 100644 (file)
@@ -898,28 +898,4 @@ public class Window extends Panel implements FocusNotifier, BlurNotifier {
         bringToFront();
     }
 
-    /**
-     * Notifies the child components and subwindows that the window is attached
-     * to the application.
-     */
-    @Override
-    public void attach() {
-        super.attach();
-        for (Window w : subwindows) {
-            w.attach();
-        }
-    }
-
-    /**
-     * Notifies the child components and subwindows that the window is detached
-     * from the application.
-     */
-    @Override
-    public void detach() {
-        super.detach();
-        for (Window w : subwindows) {
-            w.detach();
-        }
-    }
-
 }
index 8daee1ba65df28a9cac4085cc5aedb50806d8dec..db97c7e6f20c9e48db6b195f0131033301e006ff 100644 (file)
@@ -3,61 +3,36 @@ package com.vaadin.tests.server.component.window;
 import static org.junit.Assert.assertSame;\r
 import static org.junit.Assert.assertTrue;\r
 \r
+import org.junit.Test;\r
+\r
 import com.vaadin.Application;\r
-import com.vaadin.ui.Component;\r
+import com.vaadin.terminal.WrappedRequest;\r
 import com.vaadin.ui.Label;\r
+import com.vaadin.ui.Root;\r
 import com.vaadin.ui.VerticalLayout;\r
 import com.vaadin.ui.Window;\r
 \r
-import org.junit.Test;\r
-\r
 public class AttachDetachWindow {\r
 \r
-    private Application testApp = new Application() {\r
-        @Override\r
-        public void init() {\r
-        }\r
-    };\r
+    private Application testApp = new Application();\r
+\r
+    private interface TestContainer {\r
+        public boolean attachCalled();\r
+\r
+        public boolean detachCalled();\r
 \r
-    private class TestWindow extends Window {\r
+        public TestContent getTestContent();\r
+\r
+        public Application getApplication();\r
+    }\r
+\r
+    private class TestWindow extends Window implements TestContainer {\r
         boolean windowAttachCalled = false;\r
-        boolean contentAttachCalled = false;\r
-        boolean childAttachCalled = false;\r
         boolean windowDetachCalled = false;\r
-        boolean contentDetachCalled = false;\r
-        boolean childDetachCalled = false;\r
+        private TestContent testContent = new TestContent();;\r
 \r
         TestWindow() {\r
-            setContent(new VerticalLayout() {\r
-                @Override\r
-                public void attach() {\r
-                    super.attach();\r
-                    contentAttachCalled = true;\r
-                }\r
-\r
-                @Override\r
-                public void detach() {\r
-                    super.detach();\r
-                    contentDetachCalled = true;\r
-                }\r
-            });\r
-            addComponent(new Label() {\r
-                @Override\r
-                public void attach() {\r
-                    super.attach();\r
-                    childAttachCalled = true;\r
-                }\r
-\r
-                @Override\r
-                public void detach() {\r
-                    super.detach();\r
-                    childDetachCalled = true;\r
-                }\r
-            });\r
-        }\r
-\r
-        Component getChild() {\r
-            return getComponentIterator().next();\r
+            setContent(testContent);\r
         }\r
 \r
         @Override\r
@@ -71,9 +46,97 @@ public class AttachDetachWindow {
             super.detach();\r
             windowDetachCalled = true;\r
         }\r
+\r
+        public boolean attachCalled() {\r
+            return windowAttachCalled;\r
+        }\r
+\r
+        public boolean detachCalled() {\r
+            return windowDetachCalled;\r
+        }\r
+\r
+        public TestContent getTestContent() {\r
+            return testContent;\r
+        }\r
     }\r
 \r
-    TestWindow main = new TestWindow();\r
+    private class TestContent extends VerticalLayout {\r
+        boolean contentDetachCalled = false;\r
+        boolean childDetachCalled = false;\r
+        boolean contentAttachCalled = false;\r
+        boolean childAttachCalled = false;\r
+\r
+        private Label child = new Label() {\r
+            @Override\r
+            public void attach() {\r
+                super.attach();\r
+                childAttachCalled = true;\r
+            }\r
+\r
+            @Override\r
+            public void detach() {\r
+                super.detach();\r
+                childDetachCalled = true;\r
+            }\r
+        };\r
+\r
+        public TestContent() {\r
+            addComponent(child);\r
+        }\r
+\r
+        @Override\r
+        public void attach() {\r
+            super.attach();\r
+            contentAttachCalled = true;\r
+        }\r
+\r
+        @Override\r
+        public void detach() {\r
+            super.detach();\r
+            contentDetachCalled = true;\r
+        }\r
+    }\r
+\r
+    private class TestRoot extends Root implements TestContainer {\r
+        boolean rootAttachCalled = false;\r
+        boolean rootDetachCalled = false;\r
+        private TestContent testContent = new TestContent();;\r
+\r
+        public TestRoot() {\r
+            setContent(testContent);\r
+        }\r
+\r
+        @Override\r
+        protected void init(WrappedRequest request) {\r
+            // Do nothing\r
+        }\r
+\r
+        public boolean attachCalled() {\r
+            return rootAttachCalled;\r
+        }\r
+\r
+        public boolean detachCalled() {\r
+            return rootDetachCalled;\r
+        }\r
+\r
+        public TestContent getTestContent() {\r
+            return testContent;\r
+        }\r
+\r
+        @Override\r
+        public void attach() {\r
+            super.attach();\r
+            rootAttachCalled = true;\r
+        }\r
+\r
+        @Override\r
+        public void detach() {\r
+            super.detach();\r
+            rootDetachCalled = true;\r
+        }\r
+    }\r
+\r
+    TestRoot main = new TestRoot();\r
     TestWindow sub = new TestWindow();\r
 \r
     @Test\r
@@ -86,7 +149,7 @@ public class AttachDetachWindow {
         assertUnattached(sub);\r
 \r
         // attaching main should recurse to sub\r
-        testApp.setMainWindow(main);\r
+        main.setApplication(testApp);\r
         assertAttached(main);\r
         assertAttached(sub);\r
     }\r
@@ -96,7 +159,7 @@ public class AttachDetachWindow {
         assertUnattached(main);\r
         assertUnattached(sub);\r
 \r
-        testApp.setMainWindow(main);\r
+        main.setApplication(testApp);\r
         assertAttached(main);\r
         assertUnattached(sub);\r
 \r
@@ -108,7 +171,7 @@ public class AttachDetachWindow {
 \r
     @Test\r
     public void removeSubWindowBeforeDetachingMainWindow() {\r
-        testApp.addWindow(main);\r
+        main.setApplication(testApp);\r
         main.addWindow(sub);\r
 \r
         // sub should be detached when removing from attached main\r
@@ -117,18 +180,18 @@ public class AttachDetachWindow {
         assertDetached(sub);\r
 \r
         // main detach should recurse to sub\r
-        testApp.removeWindow(main);\r
+        main.setApplication(null);\r
         assertDetached(main);\r
         assertDetached(sub);\r
     }\r
 \r
     @Test\r
     public void removeSubWindowAfterDetachingMainWindow() {\r
-        testApp.addWindow(main);\r
+        main.setApplication(testApp);\r
         main.addWindow(sub);\r
 \r
         // main detach should recurse to sub\r
-        testApp.removeWindow(main);\r
+        main.setApplication(null);\r
         assertDetached(main);\r
         assertDetached(sub);\r
 \r
@@ -141,27 +204,31 @@ public class AttachDetachWindow {
      * Asserts that win and its children are attached to testApp and their\r
      * attach() methods have been called.\r
      */\r
-    private void assertAttached(TestWindow win) {\r
-        assertTrue("window attach not called", win.windowAttachCalled);\r
-        assertTrue("window content attach not called", win.contentAttachCalled);\r
-        assertTrue("window child attach not called", win.childAttachCalled);\r
+    private void assertAttached(TestContainer win) {\r
+        TestContent testContent = win.getTestContent();\r
+        \r
+        assertTrue("window attach not called", win.attachCalled());\r
+        assertTrue("window content attach not called",\r
+                testContent.contentAttachCalled);\r
+        assertTrue("window child attach not called",\r
+                testContent.childAttachCalled);\r
 \r
         assertSame("window not attached", win.getApplication(), testApp);\r
-        assertSame("window content not attached", win.getContent()\r
-                .getApplication(), testApp);\r
-        assertSame("window children not attached", win.getChild()\r
-                .getApplication(), testApp);\r
+        assertSame("window content not attached", testContent.getApplication(),\r
+                testApp);\r
+        assertSame("window children not attached",\r
+                testContent.child.getApplication(), testApp);\r
     }\r
 \r
     /**\r
      * Asserts that win and its children are not attached.\r
      */\r
-    private void assertUnattached(TestWindow win) {\r
+    private void assertUnattached(TestContainer win) {\r
         assertSame("window not detached", win.getApplication(), null);\r
-        assertSame("window content not detached", win.getContent()\r
-                .getApplication(), null);\r
-        assertSame("window children not detached", win.getChild()\r
+        assertSame("window content not detached", win.getTestContent()\r
                 .getApplication(), null);\r
+        assertSame("window children not detached",\r
+                win.getTestContent().child.getApplication(), null);\r
     }\r
 \r
     /**\r
@@ -170,10 +237,12 @@ public class AttachDetachWindow {
      * \r
      * @param win\r
      */\r
-    private void assertDetached(TestWindow win) {\r
+    private void assertDetached(TestContainer win) {\r
         assertUnattached(win);\r
-        assertTrue("window detach not called", win.windowDetachCalled);\r
-        assertTrue("window content detach not called", win.contentDetachCalled);\r
-        assertTrue("window child detach not called", win.childDetachCalled);\r
+        assertTrue("window detach not called", win.detachCalled());\r
+        assertTrue("window content detach not called",\r
+                win.getTestContent().contentDetachCalled);\r
+        assertTrue("window child detach not called",\r
+                win.getTestContent().childDetachCalled);\r
     }\r
 }\r
index a6040c06d3c4dfb014bdf793c487462fea9fcacc..44d80390547bc8b9653b8fb41689c31fc79b21d5 100644 (file)
@@ -12,6 +12,7 @@ import com.vaadin.ui.Button.ClickEvent;
 import com.vaadin.ui.Button.ClickListener;\r
 import com.vaadin.ui.Component;\r
 import com.vaadin.ui.Label;\r
+import com.vaadin.ui.Root;\r
 import com.vaadin.ui.Window;\r
 \r
 public class AttachShouldBeCalledForSubWindows extends AbstractTestCase\r
@@ -25,7 +26,7 @@ public class AttachShouldBeCalledForSubWindows extends AbstractTestCase
     @Override\r
     public void init() {\r
 \r
-        Window mainWindow = new Window() {\r
+        Root.LegacyWindow mainWindow = new Root.LegacyWindow() {\r
             @Override\r
             public void attach() {\r
                 log(this);\r