001    /*
002     * Copyright 2002-2008 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.application.support;
017    
018    import javax.swing.JComponent;
019    import javax.swing.JLabel;
020    
021    import junit.framework.TestCase;
022    
023    import org.springframework.richclient.application.ApplicationPage;
024    
025    /**
026     * Abstract base testcase for {@link ApplicationPage} implementations.
027     * 
028     * @author Peter De Bruycker
029     */
030    public abstract class AbstractApplicationPageTestCase extends TestCase {
031    
032        private AbstractApplicationPage applicationPage;
033        private TestView testView1;
034        private TestView testView2;
035    
036        @Override
037        protected void setUp() throws Exception {
038            setUpViews();
039    
040            applicationPage = (AbstractApplicationPage) createApplicationPage();
041            assertNotNull("createApplicationPage returns null", applicationPage);
042    
043            SimpleViewDescriptorRegistry viewDescriptorRegistry = new SimpleViewDescriptorRegistry();
044            viewDescriptorRegistry.addViewDescriptor(new SimpleViewDescriptor("testView1", testView1));
045            viewDescriptorRegistry.addViewDescriptor(new SimpleViewDescriptor("testView2", testView2));
046    
047            applicationPage.setViewDescriptorRegistry(viewDescriptorRegistry);
048    
049            applicationPage.setPageComponentPaneFactory(new SimplePageComponentPaneFactory());
050    
051            applicationPage.setDescriptor(new EmptyPageDescriptor());
052    
053            // trigger control creation
054            JComponent control = applicationPage.getControl();
055            assertNotNull("getControl cannot return null", control);
056        }
057    
058        private void setUpViews() {
059            testView1 = new TestView("this is test view 1");
060            testView2 = new TestView("this is test view 2");
061        }
062    
063        protected abstract ApplicationPage createApplicationPage();
064    
065        public void testShowViewAndClose() {
066            assertNull(applicationPage.getView("testView1"));
067    
068            applicationPage.showView("testView1");
069    
070            TestView view = (TestView) applicationPage.getView("testView1");
071    
072            assertNotNull(view);
073            assertEquals("testView1", view.getId());
074    
075            applicationPage.close(view);
076            assertNull(applicationPage.getView("testView1"));
077        }
078    
079        public void testShowViewWithInput() {
080            Object input = "the input";
081    
082            applicationPage.showView("testView1", input);
083    
084            TestView view = applicationPage.getView("testView1");
085            assertNotNull(view);
086    
087            assertTrue(view.isSetInputCalled());
088            assertEquals(input, view.getInput());
089        }
090        
091        public void testShowView() {
092            assertSame(testView1, applicationPage.showView("testView1"));
093            assertSame(testView1, applicationPage.getActiveComponent());
094            
095            assertSame(testView2, applicationPage.showView("testView2"));
096            assertSame(testView2, applicationPage.getActiveComponent());
097        }
098        
099        public void testShowViewWithoutInput() {
100            applicationPage.showView("testView1");
101    
102            TestView view = applicationPage.getView("testView1");
103            assertNotNull(view);
104    
105            assertFalse(view.isSetInputCalled());
106        }
107    
108        public void testGetView() {
109            assertNull(applicationPage.getView("testView1"));
110    
111            applicationPage.showView("testView1");
112    
113            TestView view = applicationPage.getView("testView1");
114    
115            assertNotNull(view);
116            assertEquals("testView1", view.getId());
117    
118            applicationPage.close(view);
119            assertNull(applicationPage.getView("testView1"));
120        }
121    
122        private static class TestView extends AbstractView {
123    
124            private String label;
125            private Object input;
126            private boolean setInputCalled;
127    
128            public TestView(String label) {
129                this.label = label;
130            }
131    
132            @Override
133            protected JComponent createControl() {
134                return new JLabel(label);
135            }
136    
137            @Override
138            public void setInput(Object input) {
139                this.input = input;
140                setInputCalled = true;
141            }
142    
143            public Object getInput() {
144                return input;
145            }
146            
147            public boolean isSetInputCalled() {
148                return setInputCalled;
149            }
150    
151        }
152    }