Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [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 | package org.onosproject.ui; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import org.junit.Test; |
| 20 | |
| 21 | import java.io.IOException; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 22 | import java.util.List; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 23 | |
| 24 | import static com.google.common.io.ByteStreams.toByteArray; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 25 | import static org.junit.Assert.*; |
Thomas Vachuska | 8b91f4f | 2015-04-23 17:55:36 -0700 | [diff] [blame] | 26 | import static org.onosproject.ui.UiView.Category.OTHER; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Tests the default user interface extension descriptor. |
| 30 | */ |
| 31 | public class UiExtensionTest { |
| 32 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 33 | private static final String FOO_ID = "foo"; |
| 34 | private static final String FOO_LABEL = "Foo View"; |
| 35 | private static final String BAR_ID = "bar"; |
| 36 | private static final String BAR_LABEL = "Bar View"; |
| 37 | private static final String CUSTOM = "custom"; |
| 38 | |
| 39 | |
| 40 | private static final UiView FOO_VIEW = new UiView(OTHER, FOO_ID, FOO_LABEL); |
| 41 | private static final UiView BAR_VIEW = new UiView(OTHER, BAR_ID, BAR_LABEL); |
| 42 | private static final UiView HIDDEN_VIEW = new UiViewHidden(FOO_ID); |
| 43 | |
| 44 | private static final UiMessageHandlerFactory MH_FACTORY = () -> null; |
| 45 | private static final UiTopoOverlayFactory TO_FACTORY = () -> null; |
| 46 | |
| 47 | private final ClassLoader cl = getClass().getClassLoader(); |
| 48 | |
| 49 | private List<UiView> viewList; |
| 50 | private UiExtension ext; |
| 51 | private String css; |
| 52 | private String js; |
| 53 | private UiView view; |
| 54 | |
| 55 | |
| 56 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 57 | @Test |
| 58 | public void basics() throws IOException { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 59 | viewList = ImmutableList.of(FOO_VIEW); |
| 60 | ext = new UiExtension.Builder(cl, viewList).build(); |
| 61 | |
| 62 | css = new String(toByteArray(ext.css())); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 63 | assertTrue("incorrect css stream", css.contains("foo-css")); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 64 | js = new String(toByteArray(ext.js())); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 65 | assertTrue("incorrect js stream", js.contains("foo-js")); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 66 | |
| 67 | assertEquals("expected 1 view", 1, ext.views().size()); |
| 68 | view = ext.views().get(0); |
| 69 | assertEquals("wrong view category", OTHER, view.category()); |
| 70 | assertEquals("wrong view id", FOO_ID, view.id()); |
| 71 | assertEquals("wrong view label", FOO_LABEL, view.label()); |
| 72 | |
| 73 | assertNull("unexpected message handler factory", ext.messageHandlerFactory()); |
| 74 | assertNull("unexpected topo overlay factory", ext.topoOverlayFactory()); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void withPath() throws IOException { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 79 | viewList = ImmutableList.of(FOO_VIEW); |
| 80 | ext = new UiExtension.Builder(cl, viewList) |
| 81 | .resourcePath(CUSTOM) |
| 82 | .build(); |
| 83 | |
| 84 | css = new String(toByteArray(ext.css())); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 85 | assertTrue("incorrect css stream", css.contains("custom-css")); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 86 | js = new String(toByteArray(ext.js())); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 87 | assertTrue("incorrect js stream", js.contains("custom-js")); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 88 | |
| 89 | assertEquals("expected 1 view", 1, ext.views().size()); |
| 90 | view = ext.views().get(0); |
| 91 | assertEquals("wrong view category", OTHER, view.category()); |
| 92 | assertEquals("wrong view id", FOO_ID, view.id()); |
| 93 | assertEquals("wrong view label", FOO_LABEL, view.label()); |
| 94 | |
| 95 | assertNull("unexpected message handler factory", ext.messageHandlerFactory()); |
| 96 | assertNull("unexpected topo overlay factory", ext.topoOverlayFactory()); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void messageHandlerFactory() { |
| 101 | viewList = ImmutableList.of(FOO_VIEW); |
| 102 | ext = new UiExtension.Builder(cl, viewList) |
| 103 | .messageHandlerFactory(MH_FACTORY) |
| 104 | .build(); |
| 105 | |
| 106 | assertEquals("wrong message handler factory", MH_FACTORY, |
| 107 | ext.messageHandlerFactory()); |
| 108 | assertNull("unexpected topo overlay factory", ext.topoOverlayFactory()); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void topoOverlayFactory() { |
| 113 | viewList = ImmutableList.of(HIDDEN_VIEW); |
| 114 | ext = new UiExtension.Builder(cl, viewList) |
| 115 | .topoOverlayFactory(TO_FACTORY) |
| 116 | .build(); |
| 117 | |
| 118 | assertNull("unexpected message handler factory", ext.messageHandlerFactory()); |
| 119 | assertEquals("wrong topo overlay factory", TO_FACTORY, |
| 120 | ext.topoOverlayFactory()); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void twoViews() { |
| 125 | viewList = ImmutableList.of(FOO_VIEW, BAR_VIEW); |
| 126 | ext = new UiExtension.Builder(cl, viewList).build(); |
| 127 | |
| 128 | assertEquals("expected 2 views", 2, ext.views().size()); |
| 129 | |
| 130 | view = ext.views().get(0); |
| 131 | assertEquals("wrong view category", OTHER, view.category()); |
| 132 | assertEquals("wrong view id", FOO_ID, view.id()); |
| 133 | assertEquals("wrong view label", FOO_LABEL, view.label()); |
| 134 | |
| 135 | view = ext.views().get(1); |
| 136 | assertEquals("wrong view category", OTHER, view.category()); |
| 137 | assertEquals("wrong view id", BAR_ID, view.id()); |
| 138 | assertEquals("wrong view label", BAR_LABEL, view.label()); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 139 | } |
| 140 | } |