blob: e97c0375a85956c9100a161c4a55ab5e27085ebf [file] [log] [blame]
Simon Hunt629b99e2015-07-27 17:38:33 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.onosproject.ui.topo;
19
Simon Hunt00a27ff2015-07-28 18:53:40 -070020import org.junit.BeforeClass;
Simon Hunt629b99e2015-07-27 17:38:33 -070021import org.junit.Test;
22import org.onosproject.ui.topo.PropertyPanel.Prop;
23
Simon Hunt00a27ff2015-07-28 18:53:40 -070024import java.util.HashMap;
Simon Hunt629b99e2015-07-27 17:38:33 -070025import java.util.Iterator;
Simon Hunt00a27ff2015-07-28 18:53:40 -070026import java.util.Map;
Simon Hunt629b99e2015-07-27 17:38:33 -070027
Simon Hunt3a0598f2015-08-04 19:59:04 -070028import static org.junit.Assert.*;
Simon Hunt629b99e2015-07-27 17:38:33 -070029
30/**
31 * Unit tests for {@link PropertyPanel}.
32 */
33public class PropertyPanelTest {
34
35 private static final String TITLE_ORIG = "Original Title";
36 private static final String TYPE_ORIG = "Original type ID";
37 private static final String TITLE_NEW = "New Title";
38 private static final String TYPE_NEW = "New type";
Simon Hunt00a27ff2015-07-28 18:53:40 -070039 private static final String SOME_IDENTIFICATION = "It's Me!";
Simon Hunt629b99e2015-07-27 17:38:33 -070040
Simon Hunt00a27ff2015-07-28 18:53:40 -070041 private static final String KEY_A = "A";
42 private static final String KEY_B = "B";
43 private static final String KEY_C = "C";
Simon Hunt3a0598f2015-08-04 19:59:04 -070044 private static final String SEP = "-";
Simon Hunt00a27ff2015-07-28 18:53:40 -070045 private static final String KEY_Z = "Z";
46 private static final String VALUE_A = "Hay";
47 private static final String VALUE_B = "Bee";
48 private static final String VALUE_C = "Sea";
49 private static final String VALUE_Z = "Zed";
Simon Hunt3a0598f2015-08-04 19:59:04 -070050 private static final String GID_A = "gid-A";
51 private static final String GID_B = "gid-B";
52 private static final String GID_C = "gid-C";
53 private static final String GID_Z = "gid-Z";
54 private static final String TT_A = "toolTip-A";
55 private static final String TT_B = "toolTip-B";
56 private static final String TT_C = "toolTip-C";
57 private static final String TT_Z = "toolTip-Z";
Simon Hunt00a27ff2015-07-28 18:53:40 -070058
59 private static final Map<String, Prop> PROP_MAP = new HashMap<>();
60
61 private static class FooClass {
62 private final String s;
63 FooClass(String s) {
64 this.s = s;
65 }
66
67 @Override
68 public String toString() {
69 return ">" + s + "<";
70 }
71 }
Simon Hunt629b99e2015-07-27 17:38:33 -070072
73 private PropertyPanel pp;
74
75
Simon Hunt00a27ff2015-07-28 18:53:40 -070076
77 @BeforeClass
78 public static void setUpClass() {
79 PROP_MAP.put(KEY_A, new Prop(KEY_A, VALUE_A));
80 PROP_MAP.put(KEY_B, new Prop(KEY_B, VALUE_B));
81 PROP_MAP.put(KEY_C, new Prop(KEY_C, VALUE_C));
82 PROP_MAP.put(KEY_Z, new Prop(KEY_Z, VALUE_Z));
Simon Hunt3a0598f2015-08-04 19:59:04 -070083 PROP_MAP.put(SEP, new PropertyPanel.Separator());
Simon Hunt00a27ff2015-07-28 18:53:40 -070084 }
85
Simon Hunt629b99e2015-07-27 17:38:33 -070086 @Test
87 public void basic() {
88 pp = new PropertyPanel(TITLE_ORIG, TYPE_ORIG);
89 assertEquals("wrong title", TITLE_ORIG, pp.title());
90 assertEquals("wrong type", TYPE_ORIG, pp.typeId());
Simon Hunt00a27ff2015-07-28 18:53:40 -070091 assertNull("id?", pp.id());
Simon Hunt629b99e2015-07-27 17:38:33 -070092 assertEquals("unexpected props", 0, pp.properties().size());
Simon Hunt3a0598f2015-08-04 19:59:04 -070093 assertEquals("unexpected buttons", 0, pp.buttons().size());
Simon Hunt629b99e2015-07-27 17:38:33 -070094 }
95
96 @Test
97 public void changeTitle() {
98 basic();
99 pp.title(TITLE_NEW);
100 assertEquals("wrong title", TITLE_NEW, pp.title());
101 }
102
103 @Test
104 public void changeType() {
105 basic();
106 pp.typeId(TYPE_NEW);
107 assertEquals("wrong type", TYPE_NEW, pp.typeId());
108 }
109
Simon Hunt00a27ff2015-07-28 18:53:40 -0700110 @Test
111 public void setId() {
112 basic();
113 pp.id(SOME_IDENTIFICATION);
114 assertEquals("wrong id", SOME_IDENTIFICATION, pp.id());
115 }
116
117 private void validateProps(String... keys) {
Simon Hunt629b99e2015-07-27 17:38:33 -0700118 Iterator<Prop> iter = pp.properties().iterator();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700119 for (String k: keys) {
120 Prop exp = PROP_MAP.get(k);
121 Prop act = iter.next();
122 assertEquals("Bad prop sequence", exp, act);
Simon Hunt629b99e2015-07-27 17:38:33 -0700123 }
124 }
125
Simon Hunt00a27ff2015-07-28 18:53:40 -0700126 private void validateProp(String key, String expValue) {
127 Iterator<Prop> iter = pp.properties().iterator();
128 Prop prop = null;
129 while (iter.hasNext()) {
130 Prop p = iter.next();
131 if (p.key().equals(key)) {
132 prop = p;
133 break;
134 }
135 }
136 if (prop == null) {
137 fail("no prop found with key: " + key);
138 }
139 assertEquals("Wrong prop value", expValue, prop.value());
140 }
141
Simon Hunt629b99e2015-07-27 17:38:33 -0700142 @Test
143 public void props() {
144 basic();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700145 pp.addProp(KEY_A, VALUE_A)
146 .addProp(KEY_B, VALUE_B)
147 .addProp(KEY_C, VALUE_C);
Simon Hunt629b99e2015-07-27 17:38:33 -0700148 assertEquals("bad props", 3, pp.properties().size());
Simon Hunt00a27ff2015-07-28 18:53:40 -0700149 validateProps(KEY_A, KEY_B, KEY_C);
Simon Hunt629b99e2015-07-27 17:38:33 -0700150 }
151
152 @Test
Simon Hunt3a0598f2015-08-04 19:59:04 -0700153 public void separator() {
154 props();
155 pp.addSeparator()
156 .addProp(KEY_Z, VALUE_Z);
157
158 assertEquals("bad props", 5, pp.properties().size());
159 validateProps(KEY_A, KEY_B, KEY_C, SEP, KEY_Z);
160 }
161
162 @Test
Simon Hunt629b99e2015-07-27 17:38:33 -0700163 public void removeAllProps() {
164 props();
165 assertEquals("wrong props", 3, pp.properties().size());
166 pp.removeAllProps();
167 assertEquals("unexpected props", 0, pp.properties().size());
168 }
169
170 @Test
171 public void adjustProps() {
172 props();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700173 pp.removeProps(KEY_B, KEY_A);
174 pp.addProp(KEY_Z, VALUE_Z);
175 validateProps(KEY_C, KEY_Z);
Simon Hunt629b99e2015-07-27 17:38:33 -0700176 }
Simon Hunt00a27ff2015-07-28 18:53:40 -0700177
178 @Test
179 public void intValues() {
180 basic();
181 pp.addProp(KEY_A, 200)
182 .addProp(KEY_B, 2000)
183 .addProp(KEY_C, 1234567);
184
185 validateProp(KEY_A, "200");
186 validateProp(KEY_B, "2,000");
187 validateProp(KEY_C, "1,234,567");
188 }
189
190 @Test
191 public void longValues() {
192 basic();
193 pp.addProp(KEY_A, 200L)
194 .addProp(KEY_B, 2000L)
195 .addProp(KEY_C, 1234567L)
196 .addProp(KEY_Z, Long.MAX_VALUE);
197
198 validateProp(KEY_A, "200");
199 validateProp(KEY_B, "2,000");
200 validateProp(KEY_C, "1,234,567");
201 validateProp(KEY_Z, "9,223,372,036,854,775,807");
202 }
203
204 @Test
205 public void objectValue() {
206 basic();
207 pp.addProp(KEY_A, new FooClass("a"))
208 .addProp(KEY_B, new FooClass("bxyyzy"), "[xz]");
209
210 validateProp(KEY_A, ">a<");
211 validateProp(KEY_B, ">byyy<");
212 }
213
Simon Hunt3a0598f2015-08-04 19:59:04 -0700214 private static final ButtonDescriptor BD_A =
215 new ButtonDescriptor(KEY_A, GID_A, TT_A);
216 private static final ButtonDescriptor BD_B =
217 new ButtonDescriptor(KEY_B, GID_B, TT_B);
218 private static final ButtonDescriptor BD_C =
219 new ButtonDescriptor(KEY_C, GID_C, TT_C);
220 private static final ButtonDescriptor BD_Z =
221 new ButtonDescriptor(KEY_Z, GID_Z, TT_Z);
222
223 private void verifyButtons(String... keys) {
224 Iterator<ButtonDescriptor> iter = pp.buttons().iterator();
225 for (String k: keys) {
226 assertEquals("wrong button", k, iter.next().id());
227 }
228 assertFalse("too many buttons", iter.hasNext());
229 }
230
231 @Test
232 public void buttons() {
233 basic();
234 pp.addButton(BD_A)
235 .addButton(BD_B);
236 assertEquals("wrong buttons", 2, pp.buttons().size());
237 verifyButtons(KEY_A, KEY_B);
238
239 pp.removeButtons(BD_B)
240 .addButton(BD_C)
241 .addButton(BD_Z);
242 assertEquals("wrong buttons", 3, pp.buttons().size());
243 verifyButtons(KEY_A, KEY_C, KEY_Z);
244
245 pp.removeAllButtons()
246 .addButton(BD_B);
247 assertEquals("wrong buttons", 1, pp.buttons().size());
248 verifyButtons(KEY_B);
249 }
Simon Hunt629b99e2015-07-27 17:38:33 -0700250}