Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package org.onosproject.ui.topo; |
| 18 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 19 | import org.junit.BeforeClass; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 20 | import org.junit.Test; |
| 21 | import org.onosproject.ui.topo.PropertyPanel.Prop; |
| 22 | |
Simon Hunt | cd15027 | 2015-11-04 14:09:36 -0800 | [diff] [blame] | 23 | import java.text.NumberFormat; |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 24 | import java.util.HashMap; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 25 | import java.util.Iterator; |
Simon Hunt | cd15027 | 2015-11-04 14:09:36 -0800 | [diff] [blame] | 26 | import java.util.Locale; |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 27 | import java.util.Map; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 28 | |
Simon Hunt | cd15027 | 2015-11-04 14:09:36 -0800 | [diff] [blame] | 29 | import static org.junit.Assert.assertEquals; |
| 30 | import static org.junit.Assert.assertFalse; |
| 31 | import static org.junit.Assert.assertNull; |
| 32 | import static org.junit.Assert.fail; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Unit tests for {@link PropertyPanel}. |
| 36 | */ |
| 37 | public class PropertyPanelTest { |
| 38 | |
Simon Hunt | cd15027 | 2015-11-04 14:09:36 -0800 | [diff] [blame] | 39 | // 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 Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 45 | public EnglishPropertyPanel(String title, String glyphId) { |
| 46 | super(title, glyphId); |
Simon Hunt | cd15027 | 2015-11-04 14:09:36 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Override |
| 50 | protected NumberFormat formatter() { |
| 51 | return ENGLISH_FORMATTER; |
| 52 | } |
| 53 | } |
| 54 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 55 | private static final String TITLE_ORIG = "Original Title"; |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 56 | private static final String GLYPH_ORIG = "Original glyph ID"; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 57 | private static final String TITLE_NEW = "New Title"; |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 58 | private static final String GLYPH_NEW = "New glyph ID"; |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 59 | private static final String SOME_IDENTIFICATION = "It's Me!"; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 60 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 61 | private static final String KEY_A = "A"; |
| 62 | private static final String KEY_B = "B"; |
| 63 | private static final String KEY_C = "C"; |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 64 | private static final String SEP = "-"; |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 65 | private static final String KEY_Z = "Z"; |
ilhem fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 66 | |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 67 | 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 Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 72 | 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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 90 | |
| 91 | private PropertyPanel pp; |
| 92 | |
| 93 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 94 | |
| 95 | @BeforeClass |
| 96 | public static void setUpClass() { |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 97 | 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 Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 101 | PROP_MAP.put(SEP, new PropertyPanel.Separator()); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 104 | @Test |
| 105 | public void basic() { |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 106 | pp = new EnglishPropertyPanel(TITLE_ORIG, GLYPH_ORIG); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 107 | assertEquals("wrong title", TITLE_ORIG, pp.title()); |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 108 | assertEquals("wrong glyph", GLYPH_ORIG, pp.glyphId()); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 109 | assertNull("id?", pp.id()); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 110 | assertEquals("unexpected props", 0, pp.properties().size()); |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 111 | assertEquals("unexpected buttons", 0, pp.buttons().size()); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 112 | } |
| 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 Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 122 | public void changeGlyph() { |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 123 | basic(); |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 124 | pp.glyphId(GLYPH_NEW); |
| 125 | assertEquals("wrong glyph", GLYPH_NEW, pp.glyphId()); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 128 | @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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 136 | Iterator<Prop> iter = pp.properties().iterator(); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 137 | for (String k: keys) { |
| 138 | Prop exp = PROP_MAP.get(k); |
| 139 | Prop act = iter.next(); |
| 140 | assertEquals("Bad prop sequence", exp, act); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 144 | 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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 160 | @Test |
| 161 | public void props() { |
| 162 | basic(); |
Ray Milkey | 312221c | 2019-02-19 11:31:29 -0800 | [diff] [blame] | 163 | pp.addProp(KEY_A, KEY_A, VALUE_A) |
| 164 | .addProp(KEY_B, KEY_B, VALUE_B) |
| 165 | .addProp(KEY_C, KEY_C, VALUE_C); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 166 | assertEquals("bad props", 3, pp.properties().size()); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 167 | validateProps(KEY_A, KEY_B, KEY_C); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 170 | |
| 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(); |
Ray Milkey | 312221c | 2019-02-19 11:31:29 -0800 | [diff] [blame] | 184 | pp.addProp(KEY_A, KEY_A, VALUE_A); |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 191 | @Test |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 192 | public void separator() { |
| 193 | props(); |
| 194 | pp.addSeparator() |
Ray Milkey | 312221c | 2019-02-19 11:31:29 -0800 | [diff] [blame] | 195 | .addProp(KEY_Z, KEY_Z, VALUE_Z); |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 196 | |
| 197 | assertEquals("bad props", 5, pp.properties().size()); |
| 198 | validateProps(KEY_A, KEY_B, KEY_C, SEP, KEY_Z); |
| 199 | } |
| 200 | |
| 201 | @Test |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 202 | 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 Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 212 | pp.removeProps(KEY_B, KEY_A); |
Ray Milkey | 312221c | 2019-02-19 11:31:29 -0800 | [diff] [blame] | 213 | pp.addProp(KEY_Z, KEY_Z, VALUE_Z); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 214 | validateProps(KEY_C, KEY_Z); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 215 | } |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 216 | |
| 217 | @Test |
| 218 | public void intValues() { |
| 219 | basic(); |
Ray Milkey | 312221c | 2019-02-19 11:31:29 -0800 | [diff] [blame] | 220 | pp.addProp(KEY_A, KEY_A, 200) |
| 221 | .addProp(KEY_B, KEY_B, 2000) |
| 222 | .addProp(KEY_C, KEY_C, 1234567); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 223 | |
| 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(); |
Ray Milkey | 312221c | 2019-02-19 11:31:29 -0800 | [diff] [blame] | 232 | pp.addProp(KEY_A, KEY_A, 200L) |
| 233 | .addProp(KEY_B, KEY_B, 2000L) |
| 234 | .addProp(KEY_C, KEY_C, 1234567L) |
| 235 | .addProp(KEY_Z, KEY_Z, Long.MAX_VALUE); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 236 | |
| 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(); |
Ray Milkey | 312221c | 2019-02-19 11:31:29 -0800 | [diff] [blame] | 246 | pp.addProp(KEY_A, KEY_A, new FooClass("a")) |
| 247 | .addProp(KEY_B, KEY_B, new FooClass("bxyyzy"), "[xz]"); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 248 | |
| 249 | validateProp(KEY_A, ">a<"); |
| 250 | validateProp(KEY_B, ">byyy<"); |
| 251 | } |
| 252 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 253 | 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 Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 257 | |
| 258 | private void verifyButtons(String... keys) { |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 259 | Iterator<ButtonId> iter = pp.buttons().iterator(); |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 260 | 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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 285 | } |