1   /*
2    * Copyright 2002-2004 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.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 AsYouTypeTextComponentAdapter}
25   * 
26   * @author Oliver Hutchison
27   */
28  public class AsYouTypeTextComponentAdapterTests 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          
42          // Just creatnig this object is all that's needed
43          new AsYouTypeTextComponentAdapter(comp, valueModel);
44      }
45  
46      public void testComponentChangeUpdatesValueModel() {
47          comp.setText("newValue");
48          assertEquals("newValue", valueModel.getValue());
49          assertEquals(1, valueListener.eventCount());
50      }
51  
52      public void testValueModelChangeUpdatesComponent() {
53          valueModel.setValue("newValue");
54          assertEquals("newValue", comp.getText());
55          assertEquals(1, valueListener.eventCount());
56      }
57  
58      public void testTypingUpdatesValueModel() {
59          comp.typeText("a");
60          assertEquals("a", valueModel.getValue());
61          assertEquals(1, valueListener.eventCount());
62  
63          valueListener.reset();
64          comp.typeText("bc");
65          assertEquals("abc", valueModel.getValue());
66          assertEquals(2, valueListener.eventCount());
67  
68          valueListener.reset();
69          comp.setCaretPosition(1);
70          comp.typeText("d");
71          assertEquals("adbc", valueModel.getValue());
72          assertEquals(1, valueListener.eventCount());
73  
74          valueListener.reset();
75          comp.setCaretPosition(1);
76          comp.typeBackSpace();
77          assertEquals("dbc", valueModel.getValue());
78          assertEquals(1, valueListener.eventCount());
79      }
80  }