blob: 157cdc74187bd9b7350c2c71315e03424e9d56a6 [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.
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";
66 private static final String VALUE_A = "Hay";
67 private static final String VALUE_B = "Bee";
68 private static final String VALUE_C = "Sea";
69 private static final String VALUE_Z = "Zed";
70
71 private static final Map<String, Prop> PROP_MAP = new HashMap<>();
72
73 private static class FooClass {
74 private final String s;
75 FooClass(String s) {
76 this.s = s;
77 }
78
79 @Override
80 public String toString() {
81 return ">" + s + "<";
82 }
83 }
Simon Hunt629b99e2015-07-27 17:38:33 -070084
85 private PropertyPanel pp;
86
87
Simon Hunt00a27ff2015-07-28 18:53:40 -070088
89 @BeforeClass
90 public static void setUpClass() {
91 PROP_MAP.put(KEY_A, new Prop(KEY_A, VALUE_A));
92 PROP_MAP.put(KEY_B, new Prop(KEY_B, VALUE_B));
93 PROP_MAP.put(KEY_C, new Prop(KEY_C, VALUE_C));
94 PROP_MAP.put(KEY_Z, new Prop(KEY_Z, VALUE_Z));
Simon Hunt3a0598f2015-08-04 19:59:04 -070095 PROP_MAP.put(SEP, new PropertyPanel.Separator());
Simon Hunt00a27ff2015-07-28 18:53:40 -070096 }
97
Simon Hunt629b99e2015-07-27 17:38:33 -070098 @Test
99 public void basic() {
Simon Huntcd150272015-11-04 14:09:36 -0800100 pp = new EnglishPropertyPanel(TITLE_ORIG, TYPE_ORIG);
Simon Hunt629b99e2015-07-27 17:38:33 -0700101 assertEquals("wrong title", TITLE_ORIG, pp.title());
102 assertEquals("wrong type", TYPE_ORIG, pp.typeId());
Simon Hunt00a27ff2015-07-28 18:53:40 -0700103 assertNull("id?", pp.id());
Simon Hunt629b99e2015-07-27 17:38:33 -0700104 assertEquals("unexpected props", 0, pp.properties().size());
Simon Hunt3a0598f2015-08-04 19:59:04 -0700105 assertEquals("unexpected buttons", 0, pp.buttons().size());
Simon Hunt629b99e2015-07-27 17:38:33 -0700106 }
107
108 @Test
109 public void changeTitle() {
110 basic();
111 pp.title(TITLE_NEW);
112 assertEquals("wrong title", TITLE_NEW, pp.title());
113 }
114
115 @Test
116 public void changeType() {
117 basic();
118 pp.typeId(TYPE_NEW);
119 assertEquals("wrong type", TYPE_NEW, pp.typeId());
120 }
121
Simon Hunt00a27ff2015-07-28 18:53:40 -0700122 @Test
123 public void setId() {
124 basic();
125 pp.id(SOME_IDENTIFICATION);
126 assertEquals("wrong id", SOME_IDENTIFICATION, pp.id());
127 }
128
129 private void validateProps(String... keys) {
Simon Hunt629b99e2015-07-27 17:38:33 -0700130 Iterator<Prop> iter = pp.properties().iterator();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700131 for (String k: keys) {
132 Prop exp = PROP_MAP.get(k);
133 Prop act = iter.next();
134 assertEquals("Bad prop sequence", exp, act);
Simon Hunt629b99e2015-07-27 17:38:33 -0700135 }
136 }
137
Simon Hunt00a27ff2015-07-28 18:53:40 -0700138 private void validateProp(String key, String expValue) {
139 Iterator<Prop> iter = pp.properties().iterator();
140 Prop prop = null;
141 while (iter.hasNext()) {
142 Prop p = iter.next();
143 if (p.key().equals(key)) {
144 prop = p;
145 break;
146 }
147 }
148 if (prop == null) {
149 fail("no prop found with key: " + key);
150 }
151 assertEquals("Wrong prop value", expValue, prop.value());
152 }
153
Simon Hunt629b99e2015-07-27 17:38:33 -0700154 @Test
155 public void props() {
156 basic();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700157 pp.addProp(KEY_A, VALUE_A)
158 .addProp(KEY_B, VALUE_B)
159 .addProp(KEY_C, VALUE_C);
Simon Hunt629b99e2015-07-27 17:38:33 -0700160 assertEquals("bad props", 3, pp.properties().size());
Simon Hunt00a27ff2015-07-28 18:53:40 -0700161 validateProps(KEY_A, KEY_B, KEY_C);
Simon Hunt629b99e2015-07-27 17:38:33 -0700162 }
163
164 @Test
Simon Hunt3a0598f2015-08-04 19:59:04 -0700165 public void separator() {
166 props();
167 pp.addSeparator()
168 .addProp(KEY_Z, VALUE_Z);
169
170 assertEquals("bad props", 5, pp.properties().size());
171 validateProps(KEY_A, KEY_B, KEY_C, SEP, KEY_Z);
172 }
173
174 @Test
Simon Hunt629b99e2015-07-27 17:38:33 -0700175 public void removeAllProps() {
176 props();
177 assertEquals("wrong props", 3, pp.properties().size());
178 pp.removeAllProps();
179 assertEquals("unexpected props", 0, pp.properties().size());
180 }
181
182 @Test
183 public void adjustProps() {
184 props();
Simon Hunt00a27ff2015-07-28 18:53:40 -0700185 pp.removeProps(KEY_B, KEY_A);
186 pp.addProp(KEY_Z, VALUE_Z);
187 validateProps(KEY_C, KEY_Z);
Simon Hunt629b99e2015-07-27 17:38:33 -0700188 }
Simon Hunt00a27ff2015-07-28 18:53:40 -0700189
190 @Test
191 public void intValues() {
192 basic();
193 pp.addProp(KEY_A, 200)
194 .addProp(KEY_B, 2000)
195 .addProp(KEY_C, 1234567);
196
197 validateProp(KEY_A, "200");
198 validateProp(KEY_B, "2,000");
199 validateProp(KEY_C, "1,234,567");
200 }
201
202 @Test
203 public void longValues() {
204 basic();
205 pp.addProp(KEY_A, 200L)
206 .addProp(KEY_B, 2000L)
207 .addProp(KEY_C, 1234567L)
208 .addProp(KEY_Z, Long.MAX_VALUE);
209
210 validateProp(KEY_A, "200");
211 validateProp(KEY_B, "2,000");
212 validateProp(KEY_C, "1,234,567");
213 validateProp(KEY_Z, "9,223,372,036,854,775,807");
214 }
215
216 @Test
217 public void objectValue() {
218 basic();
219 pp.addProp(KEY_A, new FooClass("a"))
220 .addProp(KEY_B, new FooClass("bxyyzy"), "[xz]");
221
222 validateProp(KEY_A, ">a<");
223 validateProp(KEY_B, ">byyy<");
224 }
225
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700226 private static final ButtonId BD_A = new ButtonId(KEY_A);
227 private static final ButtonId BD_B = new ButtonId(KEY_B);
228 private static final ButtonId BD_C = new ButtonId(KEY_C);
229 private static final ButtonId BD_Z = new ButtonId(KEY_Z);
Simon Hunt3a0598f2015-08-04 19:59:04 -0700230
231 private void verifyButtons(String... keys) {
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700232 Iterator<ButtonId> iter = pp.buttons().iterator();
Simon Hunt3a0598f2015-08-04 19:59:04 -0700233 for (String k: keys) {
234 assertEquals("wrong button", k, iter.next().id());
235 }
236 assertFalse("too many buttons", iter.hasNext());
237 }
238
239 @Test
240 public void buttons() {
241 basic();
242 pp.addButton(BD_A)
243 .addButton(BD_B);
244 assertEquals("wrong buttons", 2, pp.buttons().size());
245 verifyButtons(KEY_A, KEY_B);
246
247 pp.removeButtons(BD_B)
248 .addButton(BD_C)
249 .addButton(BD_Z);
250 assertEquals("wrong buttons", 3, pp.buttons().size());
251 verifyButtons(KEY_A, KEY_C, KEY_Z);
252
253 pp.removeAllButtons()
254 .addButton(BD_B);
255 assertEquals("wrong buttons", 1, pp.buttons().size());
256 verifyButtons(KEY_B);
257 }
Simon Hunt629b99e2015-07-27 17:38:33 -0700258}