blob: aeaa0aeee1145fc4f89d10539ed77faac0c1a0ec [file] [log] [blame]
Simon Hunt629b99e2015-07-27 17:38:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Simon Hunt629b99e2015-07-27 17:38:33 -07003 *
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.
Simon Hunt629b99e2015-07-27 17:38:33 -070015 */
16
17package org.onosproject.ui.topo;
18
Simon Hunt00a27ff2015-07-28 18:53:40 -070019import org.junit.BeforeClass;
Simon Hunt629b99e2015-07-27 17:38:33 -070020import org.junit.Test;
21import org.onosproject.ui.topo.PropertyPanel.Prop;
22
Simon Huntcd150272015-11-04 14:09:36 -080023import java.text.NumberFormat;
Simon Hunt00a27ff2015-07-28 18:53:40 -070024import java.util.HashMap;
Simon Hunt629b99e2015-07-27 17:38:33 -070025import java.util.Iterator;
Simon Huntcd150272015-11-04 14:09:36 -080026import java.util.Locale;
Simon Hunt00a27ff2015-07-28 18:53:40 -070027import java.util.Map;
Simon Hunt629b99e2015-07-27 17:38:33 -070028
Simon Huntcd150272015-11-04 14:09:36 -080029import static org.junit.Assert.assertEquals;
30import static org.junit.Assert.assertFalse;
31import static org.junit.Assert.assertNull;
32import static org.junit.Assert.fail;
Simon Hunt629b99e2015-07-27 17:38:33 -070033
34/**
35 * Unit tests for {@link PropertyPanel}.
36 */
37public class PropertyPanelTest {
38
Simon Huntcd150272015-11-04 14:09:36 -080039 // Modified property panel subclass to use ENGLISH locale formatter so
40 // we know formatted numbers will use comma for the thousand separator.
41 private static final class EnglishPropertyPanel extends PropertyPanel {
42 private static final NumberFormat ENGLISH_FORMATTER =
43 NumberFormat.getInstance(Locale.ENGLISH);
44
Simon Hunta58d8942017-08-11 12:51:14 -070045 public EnglishPropertyPanel(String title, String glyphId) {
46 super(title, glyphId);
Simon Huntcd150272015-11-04 14:09:36 -080047 }
48
49 @Override
50 protected NumberFormat formatter() {
51 return ENGLISH_FORMATTER;
52 }
53 }
54
Simon Hunt629b99e2015-07-27 17:38:33 -070055 private static final String TITLE_ORIG = "Original Title";
Simon Hunta58d8942017-08-11 12:51:14 -070056 private static final String GLYPH_ORIG = "Original glyph ID";
Simon Hunt629b99e2015-07-27 17:38:33 -070057 private static final String TITLE_NEW = "New Title";
Simon Hunta58d8942017-08-11 12:51:14 -070058 private static final String GLYPH_NEW = "New glyph ID";
Simon Hunt00a27ff2015-07-28 18:53:40 -070059 private static final String SOME_IDENTIFICATION = "It's Me!";
Simon Hunt629b99e2015-07-27 17:38:33 -070060
Simon Hunt00a27ff2015-07-28 18:53:40 -070061 private static final String KEY_A = "A";
62 private static final String KEY_B = "B";
63 private static final String KEY_C = "C";
Simon Hunt3a0598f2015-08-04 19:59:04 -070064 private static final String SEP = "-";
Simon Hunt00a27ff2015-07-28 18:53:40 -070065 private static final String KEY_Z = "Z";
ilhem fajjarib0a06842015-11-02 18:59:02 +010066
Simon Hunt879ce452017-08-10 23:32:00 -070067 private static final String LABEL_A = "labA";
68 private static final String LABEL_B = "labB";
69 private static final String LABEL_C = "labC";
70 private static final String LABEL_Z = "labZ";
71
Simon Hunt00a27ff2015-07-28 18:53:40 -070072 private static final String VALUE_A = "Hay";
73 private static final String VALUE_B = "Bee";
74 private static final String VALUE_C = "Sea";
75 private static final String VALUE_Z = "Zed";
76
77 private static final Map<String, Prop> PROP_MAP = new HashMap<>();
78
79 private static class FooClass {
80 private final String s;
81 FooClass(String s) {
82 this.s = s;
83 }
84
85 @Override
86 public String toString() {
87 return ">" + s + "<";
88 }
89 }
Simon Hunt629b99e2015-07-27 17:38:33 -070090
91 private PropertyPanel pp;
92
93
Simon Hunt00a27ff2015-07-28 18:53:40 -070094
95 @BeforeClass
96 public static void setUpClass() {
Simon Hunt879ce452017-08-10 23:32:00 -070097 PROP_MAP.put(KEY_A, new Prop(KEY_A, LABEL_A, VALUE_A));
98 PROP_MAP.put(KEY_B, new Prop(KEY_B, LABEL_B, VALUE_B));
99 PROP_MAP.put(KEY_C, new Prop(KEY_C, LABEL_C, VALUE_C));
100 PROP_MAP.put(KEY_Z, new Prop(KEY_Z, LABEL_Z, VALUE_Z));
Simon Hunt3a0598f2015-08-04 19:59:04 -0700101 PROP_MAP.put(SEP, new PropertyPanel.Separator());
Simon Hunt00a27ff2015-07-28 18:53:40 -0700102 }
103
Simon Hunt629b99e2015-07-27 17:38:33 -0700104 @Test
105 public void basic() {
Simon Hunta58d8942017-08-11 12:51:14 -0700106 pp = new EnglishPropertyPanel(TITLE_ORIG, GLYPH_ORIG);
Simon Hunt629b99e2015-07-27 17:38:33 -0700107 assertEquals("wrong title", TITLE_ORIG, pp.title());
Simon Hunta58d8942017-08-11 12:51:14 -0700108 assertEquals("wrong glyph", GLYPH_ORIG, pp.glyphId());
Simon Hunt00a27ff2015-07-28 18:53:40 -0700109 assertNull("id?", pp.id());
Simon Hunt629b99e2015-07-27 17:38:33 -0700110 assertEquals("unexpected props", 0, pp.properties().size());
Simon Hunt3a0598f2015-08-04 19:59:04 -0700111 assertEquals("unexpected buttons", 0, pp.buttons().size());
Simon Hunt629b99e2015-07-27 17:38:33 -0700112 }
113
114 @Test
115 public void changeTitle() {
116 basic();
117 pp.title(TITLE_NEW);
118 assertEquals("wrong title", TITLE_NEW, pp.title());
119 }
120
121 @Test
Simon Hunta58d8942017-08-11 12:51:14 -0700122 public void changeGlyph() {
Simon Hunt629b99e2015-07-27 17:38:33 -0700123 basic();
Simon Hunta58d8942017-08-11 12:51:14 -0700124 pp.glyphId(GLYPH_NEW);
125 assertEquals("wrong glyph", GLYPH_NEW, pp.glyphId());
Simon Hunt629b99e2015-07-27 17:38:33 -0700126 }
127
Simon Hunt00a27ff2015-07-28 18:53:40 -0700128 @Test
129 public void setId() {
130 basic();
131 pp.id(SOME_IDENTIFICATION);
132 assertEquals("wrong id", SOME_IDENTIFICATION, pp.id());
133 }
134
135 private void validateProps(String... keys) {
Simon Hunt629b99e2015-07-27 17:38:33 -0700136 Iterator<Prop> iter = pp.properties().iterator();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700137 for (String k: keys) {
138 Prop exp = PROP_MAP.get(k);
139 Prop act = iter.next();
140 assertEquals("Bad prop sequence", exp, act);
Simon Hunt629b99e2015-07-27 17:38:33 -0700141 }
142 }
143
Simon Hunt00a27ff2015-07-28 18:53:40 -0700144 private void validateProp(String key, String expValue) {
145 Iterator<Prop> iter = pp.properties().iterator();
146 Prop prop = null;
147 while (iter.hasNext()) {
148 Prop p = iter.next();
149 if (p.key().equals(key)) {
150 prop = p;
151 break;
152 }
153 }
154 if (prop == null) {
155 fail("no prop found with key: " + key);
156 }
157 assertEquals("Wrong prop value", expValue, prop.value());
158 }
159
Simon Hunt629b99e2015-07-27 17:38:33 -0700160 @Test
161 public void props() {
162 basic();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700163 pp.addProp(KEY_A, VALUE_A)
ilhem fajjarib0a06842015-11-02 18:59:02 +0100164 .addProp(KEY_B, VALUE_B)
165 .addProp(KEY_C, VALUE_C);
Simon Hunt629b99e2015-07-27 17:38:33 -0700166 assertEquals("bad props", 3, pp.properties().size());
Simon Hunt00a27ff2015-07-28 18:53:40 -0700167 validateProps(KEY_A, KEY_B, KEY_C);
Simon Hunt629b99e2015-07-27 17:38:33 -0700168 }
169
Simon Hunt879ce452017-08-10 23:32:00 -0700170
171 @Test
172 public void localizedProp() {
173 basic();
174 pp.addProp(KEY_A, LABEL_A, VALUE_A);
175 Prop p = pp.properties().get(0);
176 assertEquals("wrong key", KEY_A, p.key());
177 assertEquals("wrong label", LABEL_A, p.label());
178 assertEquals("wrong value", VALUE_A, p.value());
179 }
180
181 @Test
182 public void nonLocalizedProp() {
183 basic();
184 pp.addProp(KEY_A, VALUE_A);
185 Prop p = pp.properties().get(0);
186 assertEquals("wrong key", KEY_A, p.key());
187 assertEquals("wrong label", KEY_A, p.label());
188 assertEquals("wrong value", VALUE_A, p.value());
189 }
190
Simon Hunt629b99e2015-07-27 17:38:33 -0700191 @Test
Simon Hunt3a0598f2015-08-04 19:59:04 -0700192 public void separator() {
193 props();
194 pp.addSeparator()
ilhem fajjarib0a06842015-11-02 18:59:02 +0100195 .addProp(KEY_Z, VALUE_Z);
Simon Hunt3a0598f2015-08-04 19:59:04 -0700196
197 assertEquals("bad props", 5, pp.properties().size());
198 validateProps(KEY_A, KEY_B, KEY_C, SEP, KEY_Z);
199 }
200
201 @Test
Simon Hunt629b99e2015-07-27 17:38:33 -0700202 public void removeAllProps() {
203 props();
204 assertEquals("wrong props", 3, pp.properties().size());
205 pp.removeAllProps();
206 assertEquals("unexpected props", 0, pp.properties().size());
207 }
208
209 @Test
210 public void adjustProps() {
211 props();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700212 pp.removeProps(KEY_B, KEY_A);
213 pp.addProp(KEY_Z, VALUE_Z);
214 validateProps(KEY_C, KEY_Z);
Simon Hunt629b99e2015-07-27 17:38:33 -0700215 }
Simon Hunt00a27ff2015-07-28 18:53:40 -0700216
217 @Test
218 public void intValues() {
219 basic();
220 pp.addProp(KEY_A, 200)
ilhem fajjarib0a06842015-11-02 18:59:02 +0100221 .addProp(KEY_B, 2000)
222 .addProp(KEY_C, 1234567);
Simon Hunt00a27ff2015-07-28 18:53:40 -0700223
224 validateProp(KEY_A, "200");
225 validateProp(KEY_B, "2,000");
226 validateProp(KEY_C, "1,234,567");
227 }
228
229 @Test
230 public void longValues() {
231 basic();
232 pp.addProp(KEY_A, 200L)
ilhem fajjarib0a06842015-11-02 18:59:02 +0100233 .addProp(KEY_B, 2000L)
234 .addProp(KEY_C, 1234567L)
235 .addProp(KEY_Z, Long.MAX_VALUE);
Simon Hunt00a27ff2015-07-28 18:53:40 -0700236
237 validateProp(KEY_A, "200");
238 validateProp(KEY_B, "2,000");
239 validateProp(KEY_C, "1,234,567");
240 validateProp(KEY_Z, "9,223,372,036,854,775,807");
241 }
242
243 @Test
244 public void objectValue() {
245 basic();
246 pp.addProp(KEY_A, new FooClass("a"))
ilhem fajjarib0a06842015-11-02 18:59:02 +0100247 .addProp(KEY_B, new FooClass("bxyyzy"), "[xz]");
Simon Hunt00a27ff2015-07-28 18:53:40 -0700248
249 validateProp(KEY_A, ">a<");
250 validateProp(KEY_B, ">byyy<");
251 }
252
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700253 private static final ButtonId BD_A = new ButtonId(KEY_A);
254 private static final ButtonId BD_B = new ButtonId(KEY_B);
255 private static final ButtonId BD_C = new ButtonId(KEY_C);
256 private static final ButtonId BD_Z = new ButtonId(KEY_Z);
Simon Hunt3a0598f2015-08-04 19:59:04 -0700257
258 private void verifyButtons(String... keys) {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700259 Iterator<ButtonId> iter = pp.buttons().iterator();
Simon Hunt3a0598f2015-08-04 19:59:04 -0700260 for (String k: keys) {
261 assertEquals("wrong button", k, iter.next().id());
262 }
263 assertFalse("too many buttons", iter.hasNext());
264 }
265
266 @Test
267 public void buttons() {
268 basic();
269 pp.addButton(BD_A)
270 .addButton(BD_B);
271 assertEquals("wrong buttons", 2, pp.buttons().size());
272 verifyButtons(KEY_A, KEY_B);
273
274 pp.removeButtons(BD_B)
275 .addButton(BD_C)
276 .addButton(BD_Z);
277 assertEquals("wrong buttons", 3, pp.buttons().size());
278 verifyButtons(KEY_A, KEY_C, KEY_Z);
279
280 pp.removeAllButtons()
281 .addButton(BD_B);
282 assertEquals("wrong buttons", 1, pp.buttons().size());
283 verifyButtons(KEY_B);
284 }
Simon Hunt629b99e2015-07-27 17:38:33 -0700285}