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