001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache LicenseVersion 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 writingsoftware
011     * distributed under the License is distributed on an "AS IS" BASISWITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KINDeither express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.binding.value.swing;
017    
018    import org.springframework.binding.support.TestPropertyChangeListener;
019    import org.springframework.binding.value.ValueModel;
020    import org.springframework.binding.value.support.ValueHolder;
021    import org.springframework.richclient.test.SpringRichTestCase;
022    
023    /**
024     * Test cases for {@link FocusLostTextComponentAdapter}
025     * 
026     * @author Oliver Hutchison
027     */
028    public class FocusLostTextComponentAdapterTests extends SpringRichTestCase {
029    
030        private ValueModel valueModel;
031    
032        private TestPropertyChangeListener valueListener;
033    
034        private TestableJTextComponent comp;
035    
036        public void doSetUp() throws Exception {
037            valueModel = new ValueHolder("");
038            valueListener = new TestPropertyChangeListener(ValueModel.VALUE_PROPERTY);
039            valueModel.addValueChangeListener(valueListener);
040            comp = new TestableJTextComponent();
041            new FocusLostTextComponentAdapter(comp, valueModel);
042        }
043    
044        public void testComponentChangeDoesNotUpdateValueModel() {
045            comp.setText("newValue");
046            assertTrue(!valueModel.getValue().equals("newValue"));
047            assertEquals(0, valueListener.eventCount());
048        }
049    
050        public void testValueModelChangeUpdatesComponent() {
051            valueModel.setValue("newValue");
052            assertEquals("newValue", comp.getText());
053            assertEquals(1, valueListener.eventCount());
054        }
055    
056        public void testFocusChangeUpdatesValueModel() {
057            comp.typeText("a");
058            assertEquals("", valueModel.getValue());
059            assertEquals(0, valueListener.eventCount());
060    
061            comp.gainFocus();
062            comp.typeText("b");
063            assertEquals("", valueModel.getValue());
064            assertEquals(0, valueListener.eventCount());
065    
066            comp.loseFocus();
067            assertEquals("ab", valueModel.getValue());
068            assertEquals(1, valueListener.eventCount());
069        }
070    }