blob: ef3e416f96f8d4dacac3cff75b9668de99420ca8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/*
* Copyright 2000-2016 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.data.util;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.v7.ui.LegacyTextField;
/**
* Test verifying that TransactionalPropertyWrapper removes it's listener from
* wrapped Property
*
* @since 7.1.15
* @author Vaadin Ltd
*/
public class TransactionalPropertyWrapperTest {
@SuppressWarnings("serial")
public class TestingProperty<T extends Object>
extends ObjectProperty<Object> {
private List<ValueChangeListener> listeners = new ArrayList<ValueChangeListener>();
public TestingProperty(Object value) {
super(value);
}
@Override
public void addValueChangeListener(ValueChangeListener listener) {
super.addValueChangeListener(listener);
listeners.add(listener);
}
@Override
public void removeValueChangeListener(ValueChangeListener listener) {
super.removeValueChangeListener(listener);
if (listeners.contains(listener)) {
listeners.remove(listener);
}
}
public boolean hasListeners() {
return !listeners.isEmpty();
}
}
private final LegacyTextField nameField = new LegacyTextField("Name");
private final LegacyTextField ageField = new LegacyTextField("Age");
private final LegacyTextField unboundField = new LegacyTextField(
"No FieldGroup");
private final TestingProperty<String> unboundProp = new TestingProperty<String>(
"Hello World");
private final PropertysetItem item = new PropertysetItem();
@Test
public void fieldGroupBindAndUnbind() {
item.addItemProperty("name",
new TestingProperty<String>("Just some text"));
item.addItemProperty("age", new TestingProperty<String>("42"));
final FieldGroup binder = new FieldGroup(item);
binder.setBuffered(false);
for (int i = 0; i < 2; ++i) {
binder.bind(nameField, "name");
binder.bind(ageField, "age");
unboundField.setPropertyDataSource(unboundProp);
assertTrue("No listeners in Properties", fieldsHaveListeners(true));
binder.unbind(nameField);
binder.unbind(ageField);
unboundField.setPropertyDataSource(null);
assertTrue("Listeners in Properties after unbinding",
fieldsHaveListeners(false));
}
}
/**
* Check that all listeners have same hasListeners() response
*
* @param expected
* expected response
* @return true if all are the same as expected. false if not
*/
private boolean fieldsHaveListeners(boolean expected) {
for (Object id : item.getItemPropertyIds()) {
TestingProperty<?> itemProperty = (TestingProperty<?>) item
.getItemProperty(id);
if (itemProperty.hasListeners() != expected) {
return false;
}
}
return unboundProp.hasListeners() == expected;
}
}
|