1 package com.vaadin.tests.server.component.textfield;
3 import java.util.Collections;
5 import junit.framework.TestCase;
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;
14 public class TextFieldWithPropertyFormatterTest extends TestCase {
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;
28 protected void setUp() throws Exception {
31 field = new TextField() {
33 public void markAsDirty() {
39 formatter = new PropertyFormatter<String>() {
42 public String parse(String formattedValue) throws Exception {
43 assertEquals(INPUT_VALUE, formattedValue);
48 public String format(String value) {
49 return FORMATTED_VALUE;
53 property = new ObjectProperty<String>(ORIGINAL_VALUE);
55 formatter.setPropertyDataSource(property);
56 field.setPropertyDataSource(formatter);
58 listener = new Property.ValueChangeListener() {
61 public void valueChange(ValueChangeEvent event) {
63 assertEquals(1, listenerCalled);
64 assertEquals(FORMATTED_VALUE, event.getProperty().getValue());
68 field.addListener(listener);
73 public void testWithServerApi() {
76 field.setValue(INPUT_VALUE);
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());
90 private void checkInitialState() {
91 assertEquals(ORIGINAL_VALUE, property.getValue());
92 assertEquals(FORMATTED_VALUE, formatter.getValue());
93 assertEquals(FORMATTED_VALUE, field.getValue());
96 public void testWithSimulatedClientSideChange() {
99 field.changeVariables(null,
100 Collections.singletonMap("text", (Object) INPUT_VALUE));