1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.tests.util.observer;
18
19 import static org.junit.Assert.assertEquals;
20
21 import org.junit.Before;
22 import org.junit.Test;
23
24 import ch.elca.el4j.util.observer.ValueObserver;
25 import ch.elca.el4j.util.observer.impl.Computable;
26 import ch.elca.el4j.util.observer.impl.LiveValue;
27 import ch.elca.el4j.util.observer.impl.SettableObservableValue;
28
29
30
31
32
33
34
35
36 public class ObserverTest implements ValueObserver<Boolean> {
37
38 SettableObservableValue<Boolean> m_a, m_b, m_c;
39
40
41 LiveValue<Boolean> m_e, m_f, m_g;
42
43
44 Computable<Boolean> m_comp;
45
46
47 int m_notified;
48
49
50
51
52 @Before
53 public void setUp() {
54 m_a = new SettableObservableValue<Boolean>(false);
55 m_b = new SettableObservableValue<Boolean>(false);
56 m_c = new SettableObservableValue<Boolean>(false);
57
58
59
60
61
62
63 m_e = new LiveValue<Boolean>() {
64 @Override
65 protected Boolean is() {
66 return m_b.get() != m_c.get();
67 }
68 };
69 m_f = new LiveValue<Boolean>() {
70 @Override
71 protected Boolean is() {
72 return m_a.get() ? m_g.get() : m_b.get();
73 }
74 };
75 m_comp = new Computable<Boolean>() {
76 public Boolean is() {
77 return (m_c.get() ? m_f.get() : m_e.get());
78 }
79 };
80 m_g = new LiveValue<Boolean>(m_comp);
81 m_notified = 0;
82 }
83
84
85 @Test
86 public void testSubscribe() {
87 assertEquals(0, m_notified);
88 m_g.subscribe(this);
89 assertEquals(1, m_notified);
90 }
91
92
93 @Test
94 public void testSilentSubscribe() {
95 m_g.subscribeSilently(this);
96 assertEquals(m_notified, 0);
97 }
98
99
100 @Test
101 public void testInputChange() {
102 m_g.subscribe(this);
103 check();
104 m_a.set(true);
105 check();
106 m_b.set(true);
107 check();
108 m_a.set(false);
109 check();
110 m_b.set(false);
111 check();
112 m_c.set(true);
113 check();
114 }
115
116
117 private void check() {
118 check(m_g.get());
119 }
120
121
122
123
124
125 private void check(Boolean newRef) {
126 assertEquals(m_notified % 2 == 0, (boolean) newRef);
127 assertEquals(m_comp.is(), newRef);
128 }
129
130
131
132
133 public void changed(Boolean newRef) {
134 m_notified++;
135 check(newRef);
136 }
137 }