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. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | package org.onosproject.ui.topo; |
| 19 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 20 | import org.junit.BeforeClass; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 21 | import org.junit.Test; |
| 22 | import org.onosproject.ui.topo.PropertyPanel.Prop; |
| 23 | |
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 | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 26 | import java.util.Map; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 27 | |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame^] | 28 | import static org.junit.Assert.*; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Unit tests for {@link PropertyPanel}. |
| 32 | */ |
| 33 | public class PropertyPanelTest { |
| 34 | |
| 35 | private static final String TITLE_ORIG = "Original Title"; |
| 36 | private static final String TYPE_ORIG = "Original type ID"; |
| 37 | private static final String TITLE_NEW = "New Title"; |
| 38 | private static final String TYPE_NEW = "New type"; |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 39 | private static final String SOME_IDENTIFICATION = "It's Me!"; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 40 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 41 | private static final String KEY_A = "A"; |
| 42 | private static final String KEY_B = "B"; |
| 43 | private static final String KEY_C = "C"; |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame^] | 44 | private static final String SEP = "-"; |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 45 | private static final String KEY_Z = "Z"; |
| 46 | private static final String VALUE_A = "Hay"; |
| 47 | private static final String VALUE_B = "Bee"; |
| 48 | private static final String VALUE_C = "Sea"; |
| 49 | private static final String VALUE_Z = "Zed"; |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame^] | 50 | private static final String GID_A = "gid-A"; |
| 51 | private static final String GID_B = "gid-B"; |
| 52 | private static final String GID_C = "gid-C"; |
| 53 | private static final String GID_Z = "gid-Z"; |
| 54 | private static final String TT_A = "toolTip-A"; |
| 55 | private static final String TT_B = "toolTip-B"; |
| 56 | private static final String TT_C = "toolTip-C"; |
| 57 | private static final String TT_Z = "toolTip-Z"; |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 58 | |
| 59 | private static final Map<String, Prop> PROP_MAP = new HashMap<>(); |
| 60 | |
| 61 | private static class FooClass { |
| 62 | private final String s; |
| 63 | FooClass(String s) { |
| 64 | this.s = s; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public String toString() { |
| 69 | return ">" + s + "<"; |
| 70 | } |
| 71 | } |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 72 | |
| 73 | private PropertyPanel pp; |
| 74 | |
| 75 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 76 | |
| 77 | @BeforeClass |
| 78 | public static void setUpClass() { |
| 79 | PROP_MAP.put(KEY_A, new Prop(KEY_A, VALUE_A)); |
| 80 | PROP_MAP.put(KEY_B, new Prop(KEY_B, VALUE_B)); |
| 81 | PROP_MAP.put(KEY_C, new Prop(KEY_C, VALUE_C)); |
| 82 | PROP_MAP.put(KEY_Z, new Prop(KEY_Z, VALUE_Z)); |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame^] | 83 | PROP_MAP.put(SEP, new PropertyPanel.Separator()); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 86 | @Test |
| 87 | public void basic() { |
| 88 | pp = new PropertyPanel(TITLE_ORIG, TYPE_ORIG); |
| 89 | assertEquals("wrong title", TITLE_ORIG, pp.title()); |
| 90 | assertEquals("wrong type", TYPE_ORIG, pp.typeId()); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 91 | assertNull("id?", pp.id()); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 92 | assertEquals("unexpected props", 0, pp.properties().size()); |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame^] | 93 | assertEquals("unexpected buttons", 0, pp.buttons().size()); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void changeTitle() { |
| 98 | basic(); |
| 99 | pp.title(TITLE_NEW); |
| 100 | assertEquals("wrong title", TITLE_NEW, pp.title()); |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void changeType() { |
| 105 | basic(); |
| 106 | pp.typeId(TYPE_NEW); |
| 107 | assertEquals("wrong type", TYPE_NEW, pp.typeId()); |
| 108 | } |
| 109 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 110 | @Test |
| 111 | public void setId() { |
| 112 | basic(); |
| 113 | pp.id(SOME_IDENTIFICATION); |
| 114 | assertEquals("wrong id", SOME_IDENTIFICATION, pp.id()); |
| 115 | } |
| 116 | |
| 117 | private void validateProps(String... keys) { |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 118 | Iterator<Prop> iter = pp.properties().iterator(); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 119 | for (String k: keys) { |
| 120 | Prop exp = PROP_MAP.get(k); |
| 121 | Prop act = iter.next(); |
| 122 | assertEquals("Bad prop sequence", exp, act); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 126 | private void validateProp(String key, String expValue) { |
| 127 | Iterator<Prop> iter = pp.properties().iterator(); |
| 128 | Prop prop = null; |
| 129 | while (iter.hasNext()) { |
| 130 | Prop p = iter.next(); |
| 131 | if (p.key().equals(key)) { |
| 132 | prop = p; |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | if (prop == null) { |
| 137 | fail("no prop found with key: " + key); |
| 138 | } |
| 139 | assertEquals("Wrong prop value", expValue, prop.value()); |
| 140 | } |
| 141 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 142 | @Test |
| 143 | public void props() { |
| 144 | basic(); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 145 | pp.addProp(KEY_A, VALUE_A) |
| 146 | .addProp(KEY_B, VALUE_B) |
| 147 | .addProp(KEY_C, VALUE_C); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 148 | assertEquals("bad props", 3, pp.properties().size()); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 149 | validateProps(KEY_A, KEY_B, KEY_C); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Test |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame^] | 153 | public void separator() { |
| 154 | props(); |
| 155 | pp.addSeparator() |
| 156 | .addProp(KEY_Z, VALUE_Z); |
| 157 | |
| 158 | assertEquals("bad props", 5, pp.properties().size()); |
| 159 | validateProps(KEY_A, KEY_B, KEY_C, SEP, KEY_Z); |
| 160 | } |
| 161 | |
| 162 | @Test |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 163 | public void removeAllProps() { |
| 164 | props(); |
| 165 | assertEquals("wrong props", 3, pp.properties().size()); |
| 166 | pp.removeAllProps(); |
| 167 | assertEquals("unexpected props", 0, pp.properties().size()); |
| 168 | } |
| 169 | |
| 170 | @Test |
| 171 | public void adjustProps() { |
| 172 | props(); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 173 | pp.removeProps(KEY_B, KEY_A); |
| 174 | pp.addProp(KEY_Z, VALUE_Z); |
| 175 | validateProps(KEY_C, KEY_Z); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 176 | } |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 177 | |
| 178 | @Test |
| 179 | public void intValues() { |
| 180 | basic(); |
| 181 | pp.addProp(KEY_A, 200) |
| 182 | .addProp(KEY_B, 2000) |
| 183 | .addProp(KEY_C, 1234567); |
| 184 | |
| 185 | validateProp(KEY_A, "200"); |
| 186 | validateProp(KEY_B, "2,000"); |
| 187 | validateProp(KEY_C, "1,234,567"); |
| 188 | } |
| 189 | |
| 190 | @Test |
| 191 | public void longValues() { |
| 192 | basic(); |
| 193 | pp.addProp(KEY_A, 200L) |
| 194 | .addProp(KEY_B, 2000L) |
| 195 | .addProp(KEY_C, 1234567L) |
| 196 | .addProp(KEY_Z, Long.MAX_VALUE); |
| 197 | |
| 198 | validateProp(KEY_A, "200"); |
| 199 | validateProp(KEY_B, "2,000"); |
| 200 | validateProp(KEY_C, "1,234,567"); |
| 201 | validateProp(KEY_Z, "9,223,372,036,854,775,807"); |
| 202 | } |
| 203 | |
| 204 | @Test |
| 205 | public void objectValue() { |
| 206 | basic(); |
| 207 | pp.addProp(KEY_A, new FooClass("a")) |
| 208 | .addProp(KEY_B, new FooClass("bxyyzy"), "[xz]"); |
| 209 | |
| 210 | validateProp(KEY_A, ">a<"); |
| 211 | validateProp(KEY_B, ">byyy<"); |
| 212 | } |
| 213 | |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame^] | 214 | private static final ButtonDescriptor BD_A = |
| 215 | new ButtonDescriptor(KEY_A, GID_A, TT_A); |
| 216 | private static final ButtonDescriptor BD_B = |
| 217 | new ButtonDescriptor(KEY_B, GID_B, TT_B); |
| 218 | private static final ButtonDescriptor BD_C = |
| 219 | new ButtonDescriptor(KEY_C, GID_C, TT_C); |
| 220 | private static final ButtonDescriptor BD_Z = |
| 221 | new ButtonDescriptor(KEY_Z, GID_Z, TT_Z); |
| 222 | |
| 223 | private void verifyButtons(String... keys) { |
| 224 | Iterator<ButtonDescriptor> iter = pp.buttons().iterator(); |
| 225 | for (String k: keys) { |
| 226 | assertEquals("wrong button", k, iter.next().id()); |
| 227 | } |
| 228 | assertFalse("too many buttons", iter.hasNext()); |
| 229 | } |
| 230 | |
| 231 | @Test |
| 232 | public void buttons() { |
| 233 | basic(); |
| 234 | pp.addButton(BD_A) |
| 235 | .addButton(BD_B); |
| 236 | assertEquals("wrong buttons", 2, pp.buttons().size()); |
| 237 | verifyButtons(KEY_A, KEY_B); |
| 238 | |
| 239 | pp.removeButtons(BD_B) |
| 240 | .addButton(BD_C) |
| 241 | .addButton(BD_Z); |
| 242 | assertEquals("wrong buttons", 3, pp.buttons().size()); |
| 243 | verifyButtons(KEY_A, KEY_C, KEY_Z); |
| 244 | |
| 245 | pp.removeAllButtons() |
| 246 | .addButton(BD_B); |
| 247 | assertEquals("wrong buttons", 1, pp.buttons().size()); |
| 248 | verifyButtons(KEY_B); |
| 249 | } |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 250 | } |