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
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
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
assertUnattached(main);\r
assertUnattached(sub);\r
\r
- testApp.setMainWindow(main);\r
+ main.setApplication(testApp);\r
assertAttached(main);\r
assertUnattached(sub);\r
\r
\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
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
* 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
* \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