001    /*
002     * Copyright 2002-2006 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.form.binding.swing;
017    
018    import javax.swing.JToggleButton;
019    
020    import org.springframework.binding.support.TestBean;
021    import org.springframework.binding.form.FieldMetadata;
022    import org.springframework.binding.form.FormModel;
023    import org.springframework.binding.form.support.DefaultFormModel;
024    
025    /**
026     * @author Mathias Broekelmann
027     * 
028     */
029    public class ToggleButtonBindingTests extends BindingAbstractTests {
030    
031        private JToggleButton tb;
032    
033        private ToggleButtonBinding tbb;
034    
035        protected String setUpBinding() {
036            tbb = new ToggleButtonBinding(new JToggleButton(), fm, "booleanProperty");
037            tb = (JToggleButton) tbb.getControl();
038            return "booleanProperty";
039        }
040    
041        public void testInitialValue() {
042            TestBean bean = new TestBean();
043            bean.setBooleanProperty(true);
044            FormModel fm = new DefaultFormModel(bean);
045            ToggleButtonBinding binding = new ToggleButtonBinding(new JToggleButton(), fm, "booleanProperty");
046            JToggleButton control = (JToggleButton) binding.getControl();
047            assertEquals(true, control.isSelected());
048        }
049    
050        public void testComponentTracksEnabledChanges() {
051            assertEquals(true, tb.isEnabled());
052            fm.setEnabled(false);
053            assertEquals(false, tb.isEnabled());
054            fm.setEnabled(true);
055            assertEquals(true, tb.isEnabled());
056        }
057    
058        public void testComponentTracksReadOnlyChanges() {
059            FieldMetadata state = fm.getFieldMetadata(property);
060            assertEquals(true, tb.isEnabled());
061            state.setReadOnly(true);
062            assertEquals(false, tb.isEnabled());
063            state.setReadOnly(false);
064            assertEquals(true, tb.isEnabled());
065        }
066    
067        public void testComponentUpdatesValueModel() {
068            tb.setSelected(true);
069            assertEquals(Boolean.TRUE, vm.getValue());
070            tb.setSelected(false);
071            assertEquals(Boolean.FALSE, vm.getValue());
072            tb.setSelected(true);
073            assertEquals(Boolean.TRUE, vm.getValue());
074        }
075    
076        public void testValueModelUpdatesComponent() {
077            vm.setValue(Boolean.TRUE);
078            assertTrue(tb.isSelected());
079            vm.setValue(Boolean.FALSE);
080            assertFalse(tb.isSelected());
081            vm.setValue(Boolean.TRUE);
082            assertTrue(tb.isSelected());
083        }
084    
085    }