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