]> source.dussan.org Git - vaadin-framework.git/blob
8f2bec455b507c1a1d5eb1fcc361257ab355dd41
[vaadin-framework.git] /
1 package com.vaadin.tests.server.component.textfield;
2
3 import java.util.Collections;
4
5 import junit.framework.TestCase;
6
7 import com.vaadin.data.Property;
8 import com.vaadin.data.Property.ValueChangeEvent;
9 import com.vaadin.data.Property.ValueChangeListener;
10 import com.vaadin.data.util.ObjectProperty;
11 import com.vaadin.data.util.PropertyFormatter;
12 import com.vaadin.ui.TextField;
13
14 public class TextFieldWithPropertyFormatterTest extends TestCase {
15
16     private static final String INPUT_VALUE = "foo";
17     private static final String PARSED_VALUE = "BAR";
18     private static final String FORMATTED_VALUE = "FOOBAR";
19     private static final String ORIGINAL_VALUE = "Original";
20     private TextField field;
21     private PropertyFormatter<String> formatter;
22     private ObjectProperty<String> property;
23     private ValueChangeListener listener;
24     private int listenerCalled;
25     private int repainted;
26
27     @Override
28     protected void setUp() throws Exception {
29         super.setUp();
30
31         field = new TextField() {
32             @Override
33             public void markAsDirty() {
34                 repainted++;
35                 super.markAsDirty();
36             }
37         };
38
39         formatter = new PropertyFormatter<String>() {
40
41             @Override
42             public String parse(String formattedValue) throws Exception {
43                 assertEquals(INPUT_VALUE, formattedValue);
44                 return PARSED_VALUE;
45             }
46
47             @Override
48             public String format(String value) {
49                 return FORMATTED_VALUE;
50             }
51         };
52
53         property = new ObjectProperty<String>(ORIGINAL_VALUE);
54
55         formatter.setPropertyDataSource(property);
56         field.setPropertyDataSource(formatter);
57
58         listener = new Property.ValueChangeListener() {
59
60             @Override
61             public void valueChange(ValueChangeEvent event) {
62                 listenerCalled++;
63                 assertEquals(1, listenerCalled);
64                 assertEquals(FORMATTED_VALUE, event.getProperty().getValue());
65             }
66         };
67
68         field.addListener(listener);
69         listenerCalled = 0;
70         repainted = 0;
71     }
72
73     public void testWithServerApi() {
74         checkInitialState();
75
76         field.setValue(INPUT_VALUE);
77
78         checkEndState();
79
80     }
81
82     private void checkEndState() {
83         assertEquals(1, listenerCalled);
84         assertTrue(repainted >= 1);
85         assertEquals(FORMATTED_VALUE, field.getValue());
86         assertEquals(FORMATTED_VALUE, formatter.getValue());
87         assertEquals(PARSED_VALUE, property.getValue());
88     }
89
90     private void checkInitialState() {
91         assertEquals(ORIGINAL_VALUE, property.getValue());
92         assertEquals(FORMATTED_VALUE, formatter.getValue());
93         assertEquals(FORMATTED_VALUE, field.getValue());
94     }
95
96     public void testWithSimulatedClientSideChange() {
97         checkInitialState();
98
99         field.changeVariables(null,
100                 Collections.singletonMap("text", (Object) INPUT_VALUE));
101
102         checkEndState();
103
104     }
105
106 }