1   /*
2    * Copyright 2002-2004 the original author or authors.
3    * 
4    * Licensed under the Apache LicenseVersion 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 writingsoftware
11   * distributed under the License is distributed on an "AS IS" BASISWITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KINDeither express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.binding.value.swing;
17  
18  import org.springframework.binding.support.TestPropertyChangeListener;
19  import org.springframework.binding.value.ValueModel;
20  import org.springframework.binding.value.support.ValueHolder;
21  import org.springframework.richclient.test.SpringRichTestCase;
22  
23  /**
24   * Test cases for {@link FocusLostTextComponentAdapter}
25   * 
26   * @author Oliver Hutchison
27   */
28  public class FocusLostTextComponentAdapterTests extends SpringRichTestCase {
29  
30      private ValueModel valueModel;
31  
32      private TestPropertyChangeListener valueListener;
33  
34      private TestableJTextComponent comp;
35  
36      public void doSetUp() throws Exception {
37          valueModel = new ValueHolder("");
38          valueListener = new TestPropertyChangeListener(ValueModel.VALUE_PROPERTY);
39          valueModel.addValueChangeListener(valueListener);
40          comp = new TestableJTextComponent();
41          new FocusLostTextComponentAdapter(comp, valueModel);
42      }
43  
44      public void testComponentChangeDoesNotUpdateValueModel() {
45          comp.setText("newValue");
46          assertTrue(!valueModel.getValue().equals("newValue"));
47          assertEquals(0, valueListener.eventCount());
48      }
49  
50      public void testValueModelChangeUpdatesComponent() {
51          valueModel.setValue("newValue");
52          assertEquals("newValue", comp.getText());
53          assertEquals(1, valueListener.eventCount());
54      }
55  
56      public void testFocusChangeUpdatesValueModel() {
57          comp.typeText("a");
58          assertEquals("", valueModel.getValue());
59          assertEquals(0, valueListener.eventCount());
60  
61          comp.gainFocus();
62          comp.typeText("b");
63          assertEquals("", valueModel.getValue());
64          assertEquals(0, valueListener.eventCount());
65  
66          comp.loseFocus();
67          assertEquals("ab", valueModel.getValue());
68          assertEquals(1, valueListener.eventCount());
69      }
70  }