1   /*
2    * Copyright 2002-2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.richclient.application.support;
17  
18  import javax.swing.JComponent;
19  import javax.swing.JLabel;
20  
21  import junit.framework.TestCase;
22  
23  import org.springframework.richclient.application.ApplicationPage;
24  
25  /**
26   * Abstract base testcase for {@link ApplicationPage} implementations.
27   * 
28   * @author Peter De Bruycker
29   */
30  public abstract class AbstractApplicationPageTestCase extends TestCase {
31  
32      private AbstractApplicationPage applicationPage;
33      private TestView testView1;
34      private TestView testView2;
35  
36      @Override
37      protected void setUp() throws Exception {
38          setUpViews();
39  
40          applicationPage = (AbstractApplicationPage) createApplicationPage();
41          assertNotNull("createApplicationPage returns null", applicationPage);
42  
43          SimpleViewDescriptorRegistry viewDescriptorRegistry = new SimpleViewDescriptorRegistry();
44          viewDescriptorRegistry.addViewDescriptor(new SimpleViewDescriptor("testView1", testView1));
45          viewDescriptorRegistry.addViewDescriptor(new SimpleViewDescriptor("testView2", testView2));
46  
47          applicationPage.setViewDescriptorRegistry(viewDescriptorRegistry);
48  
49          applicationPage.setPageComponentPaneFactory(new SimplePageComponentPaneFactory());
50  
51          applicationPage.setDescriptor(new EmptyPageDescriptor());
52  
53          // trigger control creation
54          JComponent control = applicationPage.getControl();
55          assertNotNull("getControl cannot return null", control);
56      }
57  
58      private void setUpViews() {
59          testView1 = new TestView("this is test view 1");
60          testView2 = new TestView("this is test view 2");
61      }
62  
63      protected abstract ApplicationPage createApplicationPage();
64  
65      public void testShowViewAndClose() {
66          assertNull(applicationPage.getView("testView1"));
67  
68          applicationPage.showView("testView1");
69  
70          TestView view = (TestView) applicationPage.getView("testView1");
71  
72          assertNotNull(view);
73          assertEquals("testView1", view.getId());
74  
75          applicationPage.close(view);
76          assertNull(applicationPage.getView("testView1"));
77      }
78  
79      public void testShowViewWithInput() {
80          Object input = "the input";
81  
82          applicationPage.showView("testView1", input);
83  
84          TestView view = applicationPage.getView("testView1");
85          assertNotNull(view);
86  
87          assertTrue(view.isSetInputCalled());
88          assertEquals(input, view.getInput());
89      }
90      
91      public void testShowView() {
92          assertSame(testView1, applicationPage.showView("testView1"));
93          assertSame(testView1, applicationPage.getActiveComponent());
94          
95          assertSame(testView2, applicationPage.showView("testView2"));
96          assertSame(testView2, applicationPage.getActiveComponent());
97      }
98      
99      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 }