001    package org.springframework.richclient.command.support;
002    
003    import javax.swing.AbstractButton;
004    
005    import org.springframework.binding.value.support.EqualsValueChangeDetector;
006    import org.springframework.richclient.application.ApplicationServicesLocator;
007    import org.springframework.richclient.application.config.ApplicationObjectConfigurer;
008    import org.springframework.richclient.application.support.DefaultApplicationServices;
009    import org.springframework.richclient.command.AbstractCommand;
010    import org.springframework.richclient.command.config.CommandFaceDescriptor;
011    import org.springframework.richclient.test.SpringRichTestCase;
012    
013    
014    public class ButtonTests extends SpringRichTestCase
015    {
016        
017        /**
018         * May be implemented in subclasses that need to register services with the global
019         * application services instance.
020         */
021        protected void registerAdditionalServices( DefaultApplicationServices applicationServices ) {
022            applicationServices.setValueChangeDetector(new EqualsValueChangeDetector());
023        }
024    
025        public void testButtonEnabling()
026        {
027            TestCommand testCommand = new TestCommand();
028            AbstractButton button = testCommand.createButton();
029            AbstractButton button2 = testCommand.createButton();
030            enableTests(testCommand, new Object[]{button, button2});
031        }
032        
033        public void enableTests(AbstractCommand command, Object[] buttons)
034        {
035            enableTest(command, buttons, false);
036            enableTest(command, buttons, true);
037        }
038        
039        public void enableTest(AbstractCommand command, Object[] buttons, boolean enable)
040        {
041            command.setEnabled(enable);
042            assertEquals(command.isEnabled(), enable);
043            for (int i = 0; i < buttons.length; ++i)
044            {
045                assertEquals(enable, ((AbstractButton)buttons[i]).isEnabled());    
046            }        
047        }
048        
049        public void testButtonVisible()
050        {
051            TestCommand testCommand = new TestCommand();
052            AbstractButton button = testCommand.createButton();
053            AbstractButton button2 = testCommand.createButton();
054            visibleTests(testCommand, new Object[]{button, button2});
055        }
056        
057        public void visibleTests(AbstractCommand command, Object[] buttons)
058        {
059            enableTest(command, buttons, false);
060            enableTest(command, buttons, true);
061        }
062        
063        public void visibleTest(AbstractCommand command, Object[] buttons, boolean visible)
064        {
065            command.setVisible(visible);
066            assertEquals(command.isVisible(), visible);
067            for (int i = 0; i < buttons.length; ++i)
068            {
069                assertEquals(visible, ((AbstractButton)buttons[i]).isVisible());    
070            }        
071        }
072    
073        // testing different sequences because error was in sequence of hashmap:
074        public void testMultipleFaceDescriptorsEnabling()
075        {
076            multipleFaceDescriptorSequence("face1", "face2", "face3");
077            multipleFaceDescriptorSequence("face1", "face3", "face2");
078    
079            multipleFaceDescriptorSequence("face2", "face1", "face3");
080            multipleFaceDescriptorSequence("face2", "face3", "face1");
081    
082            multipleFaceDescriptorSequence("face3", "face2", "face1");
083            multipleFaceDescriptorSequence("face3", "face1", "face2");
084        }
085    
086        private void multipleFaceDescriptorSequence(String face1, String face2, String face3)
087        {
088            TestCommand testCommand = new TestCommand();
089            registerCommandFaceDescriptor(face1, testCommand);
090            registerCommandFaceDescriptor(face2, testCommand);
091            registerCommandFaceDescriptor(face3, testCommand);
092    
093            AbstractButton face1button = testCommand.createButton(face1);
094            enableTests(testCommand, new Object[]{face1button});
095            
096            AbstractButton face2button = testCommand.createButton(face2);
097            enableTests(testCommand, new Object[]{face1button, face2button});
098            
099            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    }