Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | |
| 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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 55 | 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 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 | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 67 | 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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 85 | |
| 86 | private PropertyPanel pp; |
| 87 | |
| 88 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 89 | |
| 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 Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 96 | PROP_MAP.put(SEP, new PropertyPanel.Separator()); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 99 | @Test |
| 100 | public void basic() { |
Simon Hunt | cd15027 | 2015-11-04 14:09:36 -0800 | [diff] [blame] | 101 | pp = new EnglishPropertyPanel(TITLE_ORIG, TYPE_ORIG); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 102 | assertEquals("wrong title", TITLE_ORIG, pp.title()); |
| 103 | assertEquals("wrong type", TYPE_ORIG, pp.typeId()); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 104 | assertNull("id?", pp.id()); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 105 | assertEquals("unexpected props", 0, pp.properties().size()); |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 106 | assertEquals("unexpected buttons", 0, pp.buttons().size()); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 107 | } |
| 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 Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 123 | @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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 131 | Iterator<Prop> iter = pp.properties().iterator(); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 132 | for (String k: keys) { |
| 133 | Prop exp = PROP_MAP.get(k); |
| 134 | Prop act = iter.next(); |
| 135 | assertEquals("Bad prop sequence", exp, act); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 139 | 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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 155 | @Test |
| 156 | public void props() { |
| 157 | basic(); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 158 | pp.addProp(KEY_A, VALUE_A) |
ilhem fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 159 | .addProp(KEY_B, VALUE_B) |
| 160 | .addProp(KEY_C, VALUE_C); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 161 | assertEquals("bad props", 3, pp.properties().size()); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 162 | validateProps(KEY_A, KEY_B, KEY_C); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Test |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 166 | public void separator() { |
| 167 | props(); |
| 168 | pp.addSeparator() |
ilhem fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 169 | .addProp(KEY_Z, VALUE_Z); |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 170 | |
| 171 | assertEquals("bad props", 5, pp.properties().size()); |
| 172 | validateProps(KEY_A, KEY_B, KEY_C, SEP, KEY_Z); |
| 173 | } |
| 174 | |
| 175 | @Test |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 176 | 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 Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 186 | pp.removeProps(KEY_B, KEY_A); |
| 187 | pp.addProp(KEY_Z, VALUE_Z); |
| 188 | validateProps(KEY_C, KEY_Z); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 189 | } |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 190 | |
| 191 | @Test |
| 192 | public void intValues() { |
| 193 | basic(); |
| 194 | pp.addProp(KEY_A, 200) |
ilhem fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 195 | .addProp(KEY_B, 2000) |
| 196 | .addProp(KEY_C, 1234567); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 197 | |
| 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 fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 207 | .addProp(KEY_B, 2000L) |
| 208 | .addProp(KEY_C, 1234567L) |
| 209 | .addProp(KEY_Z, Long.MAX_VALUE); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 210 | |
| 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 fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 221 | .addProp(KEY_B, new FooClass("bxyyzy"), "[xz]"); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 222 | |
| 223 | validateProp(KEY_A, ">a<"); |
| 224 | validateProp(KEY_B, ">byyy<"); |
| 225 | } |
| 226 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 227 | 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 Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 231 | |
| 232 | private void verifyButtons(String... keys) { |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 233 | Iterator<ButtonId> iter = pp.buttons().iterator(); |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 234 | 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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 259 | } |