blob: dfcfddf44a0e202c491b876d30dc53f346b146f0 [file] [log] [blame]
Simon Hunt629b99e2015-07-27 17:38:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
45 public EnglishPropertyPanel(String title, String typeId) {
46 super(title, typeId);
47 }
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";
56 private static final String TYPE_ORIG = "Original type ID";
57 private static final String TITLE_NEW = "New Title";
58 private static final String TYPE_NEW = "New type";
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 Hunt00a27ff2015-07-28 18:53:40 -070067 private static final String VALUE_A = "Hay";
68 private static final String VALUE_B = "Bee";
69 private static final String VALUE_C = "Sea";
70 private static final String VALUE_Z = "Zed";
71
72 private static final Map<String, Prop> PROP_MAP = new HashMap<>();
73
74 private static class FooClass {
75 private final String s;
76 FooClass(String s) {
77 this.s = s;
78 }
79
80 @Override
81 public String toString() {
82 return ">" + s + "<";
83 }
84 }
Simon Hunt629b99e2015-07-27 17:38:33 -070085
86 private PropertyPanel pp;
87
88
Simon Hunt00a27ff2015-07-28 18:53:40 -070089
90 @BeforeClass
91 public static void setUpClass() {
92 PROP_MAP.put(KEY_A, new Prop(KEY_A, VALUE_A));
93 PROP_MAP.put(KEY_B, new Prop(KEY_B, VALUE_B));
94 PROP_MAP.put(KEY_C, new Prop(KEY_C, VALUE_C));
95 PROP_MAP.put(KEY_Z, new Prop(KEY_Z, VALUE_Z));
Simon Hunt3a0598f2015-08-04 19:59:04 -070096 PROP_MAP.put(SEP, new PropertyPanel.Separator());
Simon Hunt00a27ff2015-07-28 18:53:40 -070097 }
98
Simon Hunt629b99e2015-07-27 17:38:33 -070099 @Test
100 public void basic() {
Simon Huntcd150272015-11-04 14:09:36 -0800101 pp = new EnglishPropertyPanel(TITLE_ORIG, TYPE_ORIG);
Simon Hunt629b99e2015-07-27 17:38:33 -0700102 assertEquals("wrong title", TITLE_ORIG, pp.title());
103 assertEquals("wrong type", TYPE_ORIG, pp.typeId());
Simon Hunt00a27ff2015-07-28 18:53:40 -0700104 assertNull("id?", pp.id());
Simon Hunt629b99e2015-07-27 17:38:33 -0700105 assertEquals("unexpected props", 0, pp.properties().size());
Simon Hunt3a0598f2015-08-04 19:59:04 -0700106 assertEquals("unexpected buttons", 0, pp.buttons().size());
Simon Hunt629b99e2015-07-27 17:38:33 -0700107 }
108
109 @Test
110 public void changeTitle() {
111 basic();
112 pp.title(TITLE_NEW);
113 assertEquals("wrong title", TITLE_NEW, pp.title());
114 }
115
116 @Test
117 public void changeType() {
118 basic();
119 pp.typeId(TYPE_NEW);
120 assertEquals("wrong type", TYPE_NEW, pp.typeId());
121 }
122
Simon Hunt00a27ff2015-07-28 18:53:40 -0700123 @Test
124 public void setId() {
125 basic();
126 pp.id(SOME_IDENTIFICATION);
127 assertEquals("wrong id", SOME_IDENTIFICATION, pp.id());
128 }
129
130 private void validateProps(String... keys) {
Simon Hunt629b99e2015-07-27 17:38:33 -0700131 Iterator<Prop> iter = pp.properties().iterator();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700132 for (String k: keys) {
133 Prop exp = PROP_MAP.get(k);
134 Prop act = iter.next();
135 assertEquals("Bad prop sequence", exp, act);
Simon Hunt629b99e2015-07-27 17:38:33 -0700136 }
137 }
138
Simon Hunt00a27ff2015-07-28 18:53:40 -0700139 private void validateProp(String key, String expValue) {
140 Iterator<Prop> iter = pp.properties().iterator();
141 Prop prop = null;
142 while (iter.hasNext()) {
143 Prop p = iter.next();
144 if (p.key().equals(key)) {
145 prop = p;
146 break;
147 }
148 }
149 if (prop == null) {
150 fail("no prop found with key: " + key);
151 }
152 assertEquals("Wrong prop value", expValue, prop.value());
153 }
154
Simon Hunt629b99e2015-07-27 17:38:33 -0700155 @Test
156 public void props() {
157 basic();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700158 pp.addProp(KEY_A, VALUE_A)
ilhem fajjarib0a06842015-11-02 18:59:02 +0100159 .addProp(KEY_B, VALUE_B)
160 .addProp(KEY_C, VALUE_C);
Simon Hunt629b99e2015-07-27 17:38:33 -0700161 assertEquals("bad props", 3, pp.properties().size());
Simon Hunt00a27ff2015-07-28 18:53:40 -0700162 validateProps(KEY_A, KEY_B, KEY_C);
Simon Hunt629b99e2015-07-27 17:38:33 -0700163 }
164
165 @Test
Simon Hunt3a0598f2015-08-04 19:59:04 -0700166 public void separator() {
167 props();
168 pp.addSeparator()
ilhem fajjarib0a06842015-11-02 18:59:02 +0100169 .addProp(KEY_Z, VALUE_Z);
Simon Hunt3a0598f2015-08-04 19:59:04 -0700170
171 assertEquals("bad props", 5, pp.properties().size());
172 validateProps(KEY_A, KEY_B, KEY_C, SEP, KEY_Z);
173 }
174
175 @Test
Simon Hunt629b99e2015-07-27 17:38:33 -0700176 public void removeAllProps() {
177 props();
178 assertEquals("wrong props", 3, pp.properties().size());
179 pp.removeAllProps();
180 assertEquals("unexpected props", 0, pp.properties().size());
181 }
182
183 @Test
184 public void adjustProps() {
185 props();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700186 pp.removeProps(KEY_B, KEY_A);
187 pp.addProp(KEY_Z, VALUE_Z);
188 validateProps(KEY_C, KEY_Z);
Simon Hunt629b99e2015-07-27 17:38:33 -0700189 }
Simon Hunt00a27ff2015-07-28 18:53:40 -0700190
191 @Test
192 public void intValues() {
193 basic();
194 pp.addProp(KEY_A, 200)
ilhem fajjarib0a06842015-11-02 18:59:02 +0100195 .addProp(KEY_B, 2000)
196 .addProp(KEY_C, 1234567);
Simon Hunt00a27ff2015-07-28 18:53:40 -0700197
198 validateProp(KEY_A, "200");
199 validateProp(KEY_B, "2,000");
200 validateProp(KEY_C, "1,234,567");
201 }
202
203 @Test
204 public void longValues() {
205 basic();
206 pp.addProp(KEY_A, 200L)
ilhem fajjarib0a06842015-11-02 18:59:02 +0100207 .addProp(KEY_B, 2000L)
208 .addProp(KEY_C, 1234567L)
209 .addProp(KEY_Z, Long.MAX_VALUE);
Simon Hunt00a27ff2015-07-28 18:53:40 -0700210
211 validateProp(KEY_A, "200");
212 validateProp(KEY_B, "2,000");
213 validateProp(KEY_C, "1,234,567");
214 validateProp(KEY_Z, "9,223,372,036,854,775,807");
215 }
216
217 @Test
218 public void objectValue() {
219 basic();
220 pp.addProp(KEY_A, new FooClass("a"))
ilhem fajjarib0a06842015-11-02 18:59:02 +0100221 .addProp(KEY_B, new FooClass("bxyyzy"), "[xz]");
Simon Hunt00a27ff2015-07-28 18:53:40 -0700222
223 validateProp(KEY_A, ">a<");
224 validateProp(KEY_B, ">byyy<");
225 }
226
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700227 private static final ButtonId BD_A = new ButtonId(KEY_A);
228 private static final ButtonId BD_B = new ButtonId(KEY_B);
229 private static final ButtonId BD_C = new ButtonId(KEY_C);
230 private static final ButtonId BD_Z = new ButtonId(KEY_Z);
Simon Hunt3a0598f2015-08-04 19:59:04 -0700231
232 private void verifyButtons(String... keys) {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700233 Iterator<ButtonId> iter = pp.buttons().iterator();
Simon Hunt3a0598f2015-08-04 19:59:04 -0700234 for (String k: keys) {
235 assertEquals("wrong button", k, iter.next().id());
236 }
237 assertFalse("too many buttons", iter.hasNext());
238 }
239
240 @Test
241 public void buttons() {
242 basic();
243 pp.addButton(BD_A)
244 .addButton(BD_B);
245 assertEquals("wrong buttons", 2, pp.buttons().size());
246 verifyButtons(KEY_A, KEY_B);
247
248 pp.removeButtons(BD_B)
249 .addButton(BD_C)
250 .addButton(BD_Z);
251 assertEquals("wrong buttons", 3, pp.buttons().size());
252 verifyButtons(KEY_A, KEY_C, KEY_Z);
253
254 pp.removeAllButtons()
255 .addButton(BD_B);
256 assertEquals("wrong buttons", 1, pp.buttons().size());
257 verifyButtons(KEY_B);
258 }
Simon Hunt629b99e2015-07-27 17:38:33 -0700259}