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.form.binding.swing;
17  
18  import javax.swing.JToggleButton;
19  
20  import org.springframework.binding.support.TestBean;
21  import org.springframework.binding.form.FieldMetadata;
22  import org.springframework.binding.form.FormModel;
23  import org.springframework.binding.form.support.DefaultFormModel;
24  
25  /**
26   * @author Mathias Broekelmann
27   * 
28   */
29  public class ToggleButtonBindingTests extends BindingAbstractTests {
30  
31      private JToggleButton tb;
32  
33      private ToggleButtonBinding tbb;
34  
35      protected String setUpBinding() {
36          tbb = new ToggleButtonBinding(new JToggleButton(), fm, "booleanProperty");
37          tb = (JToggleButton) tbb.getControl();
38          return "booleanProperty";
39      }
40  
41      public void testInitialValue() {
42          TestBean bean = new TestBean();
43          bean.setBooleanProperty(true);
44          FormModel fm = new DefaultFormModel(bean);
45          ToggleButtonBinding binding = new ToggleButtonBinding(new JToggleButton(), fm, "booleanProperty");
46          JToggleButton control = (JToggleButton) binding.getControl();
47          assertEquals(true, control.isSelected());
48      }
49  
50      public void testComponentTracksEnabledChanges() {
51          assertEquals(true, tb.isEnabled());
52          fm.setEnabled(false);
53          assertEquals(false, tb.isEnabled());
54          fm.setEnabled(true);
55          assertEquals(true, tb.isEnabled());
56      }
57  
58      public void testComponentTracksReadOnlyChanges() {
59          FieldMetadata state = fm.getFieldMetadata(property);
60          assertEquals(true, tb.isEnabled());
61          state.setReadOnly(true);
62          assertEquals(false, tb.isEnabled());
63          state.setReadOnly(false);
64          assertEquals(true, tb.isEnabled());
65      }
66  
67      public void testComponentUpdatesValueModel() {
68          tb.setSelected(true);
69          assertEquals(Boolean.TRUE, vm.getValue());
70          tb.setSelected(false);
71          assertEquals(Boolean.FALSE, vm.getValue());
72          tb.setSelected(true);
73          assertEquals(Boolean.TRUE, vm.getValue());
74      }
75  
76      public void testValueModelUpdatesComponent() {
77          vm.setValue(Boolean.TRUE);
78          assertTrue(tb.isSelected());
79          vm.setValue(Boolean.FALSE);
80          assertFalse(tb.isSelected());
81          vm.setValue(Boolean.TRUE);
82          assertTrue(tb.isSelected());
83      }
84  
85  }