1   package org.springframework.richclient.command.support;
2   
3   import javax.swing.AbstractButton;
4   
5   import org.springframework.binding.value.support.EqualsValueChangeDetector;
6   import org.springframework.richclient.application.ApplicationServicesLocator;
7   import org.springframework.richclient.application.config.ApplicationObjectConfigurer;
8   import org.springframework.richclient.application.support.DefaultApplicationServices;
9   import org.springframework.richclient.command.AbstractCommand;
10  import org.springframework.richclient.command.config.CommandFaceDescriptor;
11  import org.springframework.richclient.test.SpringRichTestCase;
12  
13  
14  public class ButtonTests extends SpringRichTestCase
15  {
16      
17      /**
18       * May be implemented in subclasses that need to register services with the global
19       * application services instance.
20       */
21      protected void registerAdditionalServices( DefaultApplicationServices applicationServices ) {
22          applicationServices.setValueChangeDetector(new EqualsValueChangeDetector());
23      }
24  
25      public void testButtonEnabling()
26      {
27          TestCommand testCommand = new TestCommand();
28          AbstractButton button = testCommand.createButton();
29          AbstractButton button2 = testCommand.createButton();
30          enableTests(testCommand, new Object[]{button, button2});
31      }
32      
33      public void enableTests(AbstractCommand command, Object[] buttons)
34      {
35          enableTest(command, buttons, false);
36          enableTest(command, buttons, true);
37      }
38      
39      public void enableTest(AbstractCommand command, Object[] buttons, boolean enable)
40      {
41          command.setEnabled(enable);
42          assertEquals(command.isEnabled(), enable);
43          for (int i = 0; i < buttons.length; ++i)
44          {
45              assertEquals(enable, ((AbstractButton)buttons[i]).isEnabled());    
46          }        
47      }
48      
49      public void testButtonVisible()
50      {
51          TestCommand testCommand = new TestCommand();
52          AbstractButton button = testCommand.createButton();
53          AbstractButton button2 = testCommand.createButton();
54          visibleTests(testCommand, new Object[]{button, button2});
55      }
56      
57      public void visibleTests(AbstractCommand command, Object[] buttons)
58      {
59          enableTest(command, buttons, false);
60          enableTest(command, buttons, true);
61      }
62      
63      public void visibleTest(AbstractCommand command, Object[] buttons, boolean visible)
64      {
65          command.setVisible(visible);
66          assertEquals(command.isVisible(), visible);
67          for (int i = 0; i < buttons.length; ++i)
68          {
69              assertEquals(visible, ((AbstractButton)buttons[i]).isVisible());    
70          }        
71      }
72  
73      // testing different sequences because error was in sequence of hashmap:
74      public void testMultipleFaceDescriptorsEnabling()
75      {
76          multipleFaceDescriptorSequence("face1", "face2", "face3");
77          multipleFaceDescriptorSequence("face1", "face3", "face2");
78  
79          multipleFaceDescriptorSequence("face2", "face1", "face3");
80          multipleFaceDescriptorSequence("face2", "face3", "face1");
81  
82          multipleFaceDescriptorSequence("face3", "face2", "face1");
83          multipleFaceDescriptorSequence("face3", "face1", "face2");
84      }
85  
86      private void multipleFaceDescriptorSequence(String face1, String face2, String face3)
87      {
88          TestCommand testCommand = new TestCommand();
89          registerCommandFaceDescriptor(face1, testCommand);
90          registerCommandFaceDescriptor(face2, testCommand);
91          registerCommandFaceDescriptor(face3, testCommand);
92  
93          AbstractButton face1button = testCommand.createButton(face1);
94          enableTests(testCommand, new Object[]{face1button});
95          
96          AbstractButton face2button = testCommand.createButton(face2);
97          enableTests(testCommand, new Object[]{face1button, face2button});
98          
99          AbstractButton face3button = testCommand.createButton(face3);
100         enableTests(testCommand, new Object[]{face1button, face2button, face3button});
101     }
102     
103     private void registerCommandFaceDescriptor(String faceId, AbstractCommand command)
104     {
105         CommandFaceDescriptor face = new CommandFaceDescriptor();
106         ApplicationObjectConfigurer configurer = (ApplicationObjectConfigurer)ApplicationServicesLocator.services().getService(ApplicationObjectConfigurer.class);
107         configurer.configure(face, faceId);
108         command.setFaceDescriptor(faceId, face);        
109     }
110     
111     /**
112      * Tests if a command subclass can use an additional condition when enable is enabled or not
113      * @throws Exception
114      */
115     public void testAdditionalConditionToEnable() throws Exception {
116         AdditionalStateTestCommand testCommand = new AdditionalStateTestCommand();
117         AbstractButton button = testCommand.createButton();
118         assertEquals(testCommand.isEnabled(), button.isEnabled());
119         testCommand.setMyenabledState(false);
120         assertEquals(false, testCommand.isEnabled());
121         assertEquals(testCommand.isEnabled(), button.isEnabled());
122         testCommand.setMyenabledState(true);
123         assertEquals(true, testCommand.isEnabled());
124         assertEquals(testCommand.isEnabled(), button.isEnabled());
125     }
126 
127     /**
128      * Tests if a command subclass can use an additional condition when visible is enabled or not
129      * @throws Exception
130      */
131     public void testAdditionalConditionToVisible() throws Exception {
132         AdditionalStateTestCommand testCommand = new AdditionalStateTestCommand();
133         AbstractButton button = testCommand.createButton();
134         assertEquals(testCommand.isVisible(), button.isVisible());
135         testCommand.setMyvisibleState(false);
136         assertEquals(false, testCommand.isVisible());
137         assertEquals(testCommand.isVisible(), button.isVisible());
138         testCommand.setMyvisibleState(true);
139         assertEquals(true, testCommand.isVisible());
140         assertEquals(testCommand.isVisible(), button.isVisible());
141     }
142 }