Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 1 | /* |
| 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 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"; |
| 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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 84 | |
| 85 | private PropertyPanel pp; |
| 86 | |
| 87 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 88 | |
| 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 Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 95 | PROP_MAP.put(SEP, new PropertyPanel.Separator()); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 98 | @Test |
| 99 | public void basic() { |
Simon Hunt | cd15027 | 2015-11-04 14:09:36 -0800 | [diff] [blame^] | 100 | pp = new EnglishPropertyPanel(TITLE_ORIG, TYPE_ORIG); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 101 | assertEquals("wrong title", TITLE_ORIG, pp.title()); |
| 102 | assertEquals("wrong type", TYPE_ORIG, pp.typeId()); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 103 | assertNull("id?", pp.id()); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 104 | assertEquals("unexpected props", 0, pp.properties().size()); |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 105 | assertEquals("unexpected buttons", 0, pp.buttons().size()); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 106 | } |
| 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 Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 122 | @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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 130 | Iterator<Prop> iter = pp.properties().iterator(); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 131 | for (String k: keys) { |
| 132 | Prop exp = PROP_MAP.get(k); |
| 133 | Prop act = iter.next(); |
| 134 | assertEquals("Bad prop sequence", exp, act); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 138 | 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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 154 | @Test |
| 155 | public void props() { |
| 156 | basic(); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 157 | pp.addProp(KEY_A, VALUE_A) |
| 158 | .addProp(KEY_B, VALUE_B) |
| 159 | .addProp(KEY_C, VALUE_C); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 160 | assertEquals("bad props", 3, pp.properties().size()); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 161 | validateProps(KEY_A, KEY_B, KEY_C); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Test |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 165 | 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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 175 | 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 Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 185 | pp.removeProps(KEY_B, KEY_A); |
| 186 | pp.addProp(KEY_Z, VALUE_Z); |
| 187 | validateProps(KEY_C, KEY_Z); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 188 | } |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 189 | |
| 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 Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 226 | 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 Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 230 | |
| 231 | private void verifyButtons(String... keys) { |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 232 | Iterator<ButtonId> iter = pp.buttons().iterator(); |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 233 | 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 Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 258 | } |