1   /*
2    * Copyright 2002-2006 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.command;
17  
18  import junit.framework.TestCase;
19  
20  import java.beans.PropertyChangeEvent;
21  import java.beans.PropertyChangeListener;
22  
23  /**
24   * @author Mathias Broekelmann
25   * 
26   */
27  public abstract class AbstractCommandTests extends TestCase {
28  
29      private TestListener testListener;
30  
31      protected void setUp() throws Exception {
32          testListener = new TestListener();
33      }
34  
35      public void testEnabledState() throws Exception {
36          TestAbstractCommand command = new TestAbstractCommand();
37          command.addEnabledListener(testListener);
38          // test initial value
39          command.secondaryEnabledState = false;
40          assertFalse(command.isEnabled());
41          command.setSecondaryEnabledState(false);
42          assertEquals(0, testListener.eventCount);
43          assertFalse(command.isEnabled());
44  
45          // test state change
46          testListener.eventCount = 0;
47          command.setSecondaryEnabledState(true);
48          assertEquals(1, testListener.eventCount);
49          assertTrue(command.isEnabled());
50  
51          // test initial value
52          command = new TestAbstractCommand();
53          command.addEnabledListener(testListener);
54          testListener.eventCount = 0;
55          command.secondaryEnabledState = true;
56          assertTrue(command.isEnabled());
57          command.setSecondaryEnabledState(false);
58          assertEquals(1, testListener.eventCount);
59          assertFalse(command.isEnabled());
60      }
61  
62      public void testVisibleState() throws Exception {
63          TestAbstractCommand command = new TestAbstractCommand();
64          command.addPropertyChangeListener("visible", testListener);
65          // test initial value
66          command.secondaryVisibleState = false;
67          command.setSecondaryVisibleState(false);
68          assertEquals(0, testListener.eventCount);
69          assertFalse(command.isVisible());
70  
71          // test state change
72          testListener.eventCount = 0;
73          command.setSecondaryVisibleState(true);
74          assertEquals(1, testListener.eventCount);
75          assertTrue(command.isVisible());
76  
77          // test initial value
78          command = new TestAbstractCommand();
79          command.addPropertyChangeListener("visible", testListener);
80          testListener.eventCount = 0;
81          command.secondaryVisibleState = true;
82          assertTrue(command.isVisible());
83          command.setSecondaryVisibleState(false);
84          assertEquals(1, testListener.eventCount);
85          assertFalse(command.isVisible());
86      }
87  
88      protected class TestListener implements PropertyChangeListener {
89  
90          int eventCount = 0;
91  
92          public void propertyChange(PropertyChangeEvent evt) {
93              eventCount++;
94          }
95  
96      }
97  
98      protected class TestAbstractCommand extends AbstractCommand {
99  
100         boolean secondaryEnabledState;
101 
102         boolean secondaryVisibleState;
103 
104         public boolean isEnabled() {
105             return super.isEnabled() && secondaryEnabledState;
106         }
107 
108         public boolean isVisible() {
109             return super.isVisible() && secondaryVisibleState;
110         }
111 
112         public void setSecondaryEnabledState(boolean secondaryEnabledState) {
113             if (secondaryEnabledState != this.secondaryEnabledState) {
114                 this.secondaryEnabledState = secondaryEnabledState;
115                 updatedEnabledState();
116             }
117         }
118 
119         public void setSecondaryVisibleState(boolean secondaryVisibleState) {
120             if (secondaryVisibleState != this.secondaryVisibleState) {
121                 this.secondaryVisibleState = secondaryVisibleState;
122                 updatedVisibleState();
123             }
124         }
125 
126         public void execute() {
127         }
128     }
129 }