1 /*******************************************************************************
2  *  Copyright (c) 2006, 2016 IBM Corporation and others.
3  *
4  *  This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License 2.0
6  *  which accompanies this distribution, and is available at
7  *  https://www.eclipse.org/legal/epl-2.0/
8  *
9  *  SPDX-License-Identifier: EPL-2.0
10  *
11  *  Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.ua.tests.help.toc;
15 
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertFalse;
18 import static org.junit.Assert.assertTrue;
19 
20 import java.util.ArrayList;
21 import java.util.List;
22 
23 import org.eclipse.core.expressions.IEvaluationContext;
24 import org.eclipse.help.IIndexEntry;
25 import org.eclipse.help.ITopic;
26 import org.eclipse.help.IUAElement;
27 import org.eclipse.help.internal.UAElement;
28 import org.eclipse.help.internal.webapp.data.EnabledTopicUtils;
29 import org.junit.Test;
30 
31 public class EnabledTopicTest {
32 
33 	private class ETopic implements ITopic {
34 
35 		private String label;
36 		private boolean isEnabled;
37 		private List<ITopic> children = new ArrayList<>();
38 
ETopic(String label, boolean isEnabled)39 		public ETopic(String label, boolean isEnabled) {
40 			this.label = label;
41 			this.isEnabled = isEnabled;
42 		}
43 
44 		@Override
getSubtopics()45 		public ITopic[] getSubtopics() {
46 			return children.toArray(new ITopic[children.size()]);
47 		}
48 
49 		@Override
getChildren()50 		public IUAElement[] getChildren() {
51 
52 			return getSubtopics();
53 		}
54 
55 		@Override
isEnabled(IEvaluationContext context)56 		public boolean isEnabled(IEvaluationContext context) {
57 			return isEnabled;
58 		}
59 
60 		@Override
getHref()61 		public String getHref() {
62 			return "http://www.eclipse.org";
63 		}
64 
65 		@Override
getLabel()66 		public String getLabel() {
67 			return label;
68 		}
69 
addSubTopic(ITopic subTopic)70 		public void addSubTopic(ITopic subTopic) {
71 			children.add(subTopic);
72 		}
73 	}
74 
75 	private class NoHrefTopic extends ETopic {
76 
NoHrefTopic(String label)77 		public NoHrefTopic(String label) {
78 			super(label, true);
79 		}
80 
81 		@Override
getHref()82 		public String getHref() {
83 			return null;
84 		}
85 
86 	}
87 
88 	private class EIndexEntry extends UAElement implements IIndexEntry  {
89 
90 		private String keyword;
91 		private List<ITopic> topics = new ArrayList<>();
92 		private List<IIndexEntry> subEntries = new ArrayList<>();
93 
EIndexEntry(String keyword)94 		public EIndexEntry(String keyword) {
95 			super(keyword);
96 			this.keyword = keyword;
97 		}
98 
99 		@Override
getKeyword()100 		public String getKeyword() {
101 			return keyword;
102 		}
103 
addSubEntry(IIndexEntry entry)104 		public void addSubEntry(IIndexEntry entry) {
105 			subEntries.add(entry);
106 		}
107 
addTopic(ITopic topic)108 		public void addTopic(ITopic topic) {
109 			topics.add(topic);
110 		}
111 
112 		@Override
getSubentries()113 		public IIndexEntry[] getSubentries() {
114 			return subEntries.toArray(new IIndexEntry[subEntries.size()]);
115 		}
116 
117 		@Override
getTopics()118 		public ITopic[] getTopics() {
119 			return topics.toArray(new ITopic[topics.size()]);
120 		}
121 
122 		@Override
getChildren()123 		public synchronized IUAElement[] getChildren() {
124 			List<IUAElement> all = new ArrayList<>();
125 			all.addAll(subEntries);
126 			all.addAll(topics);
127 			return all.toArray(new IUAElement[all.size()]);
128 		}
129 	}
130 
131 	@Test
testEnabledTopic()132 	public void testEnabledTopic() {
133 		assertTrue(EnabledTopicUtils.isEnabled(new ETopic("T1", true)));
134 		assertFalse(EnabledTopicUtils.isEnabled(new ETopic("T2", false)));
135 	}
136 
137 	@Test
testEnabledTopicsEmptyArray()138 	public void testEnabledTopicsEmptyArray() throws Exception {
139 		ITopic[] enabled = EnabledTopicUtils.getEnabled(new ITopic[0]);
140 		assertTrue(enabled.length == 0);
141 	}
142 
143 	@Test
testEnabledTopicsAllEnabled()144 	public void testEnabledTopicsAllEnabled() throws Exception {
145 		ITopic[] topics = new ITopic[2];
146 		topics[0] = new ETopic("T1", true);
147 		topics[1] = new ETopic("T2", true);
148 		ITopic[] enabled = EnabledTopicUtils.getEnabled(topics);
149 		assertTrue(enabled.length == 2);
150 		assertTrue(topics[0].getLabel().equals("T1"));
151 		assertTrue(topics[1].getLabel().equals("T2"));
152 	}
153 
154 	@Test
testEnabledTopicsAllDisabled()155 	public void testEnabledTopicsAllDisabled() throws Exception {
156 		ITopic[] topics = new ITopic[2];
157 		topics[0] = new ETopic("T1", false);
158 		topics[1] = new ETopic("T2", false);
159 		ITopic[] enabled = EnabledTopicUtils.getEnabled(topics);
160 		assertTrue(enabled.length == 0);
161 	}
162 
163 	@Test
testEnabledTopicsMix()164 	public void testEnabledTopicsMix() throws Exception {
165 		ITopic[] topics = new ITopic[4];
166 		topics[0] = new ETopic("T1", true);
167 		topics[1] = new ETopic("T2", false);
168 		topics[2] = new ETopic("T3", true);
169 		topics[3] = new ETopic("T4", false);
170 		ITopic[] enabled = EnabledTopicUtils.getEnabled(topics);
171 		assertEquals(2, enabled.length);
172 		assertEquals("T1", enabled[0].getLabel());
173 		assertEquals("T3", enabled[1].getLabel());
174 	}
175 
176 	@Test
testNoHref()177 	public void testNoHref() {
178 		ITopic noHref = new NoHrefTopic("N1");
179 		assertFalse(EnabledTopicUtils.isEnabled(noHref));
180 	}
181 
182 	@Test
testNoHrefValidChild()183 	public void testNoHrefValidChild() {
184 		ETopic noHref = new NoHrefTopic("N1");
185 		noHref.addSubTopic(new ETopic("T1", true));
186 		assertTrue(EnabledTopicUtils.isEnabled(noHref));
187 	}
188 
189 	@Test
testNoHrefInvalidChild()190 	public void testNoHrefInvalidChild() {
191 		ETopic noHref = new NoHrefTopic("N1");
192 		noHref.addSubTopic(new ETopic("T1", false));
193 		assertFalse(EnabledTopicUtils.isEnabled(noHref));
194 	}
195 
196 	@Test
testNoHrefMixedChildren()197 	public void testNoHrefMixedChildren() {
198 		ETopic noHref = new NoHrefTopic("N1");
199 		noHref.addSubTopic(new ETopic("T1", false));
200 		noHref.addSubTopic(new ETopic("T2", true));
201 		assertTrue(EnabledTopicUtils.isEnabled(noHref));
202 	}
203 
204 	@Test
testNoHrefValidGrandchild()205 	public void testNoHrefValidGrandchild() {
206 		ETopic noHref = new NoHrefTopic("N1");
207 		ETopic subTopic = new NoHrefTopic("N2");
208 		noHref.addSubTopic(subTopic);
209 		subTopic.addSubTopic(new ETopic("T2", true));
210 		assertTrue(EnabledTopicUtils.isEnabled(noHref));
211 	}
212 
213 	@Test
testNoHrefInvalidGrandchild()214 	public void testNoHrefInvalidGrandchild() {
215 		ETopic noHref = new NoHrefTopic("N1");
216 		ETopic subTopic = new NoHrefTopic("N2");
217 		noHref.addSubTopic(subTopic);
218 		subTopic.addSubTopic(new ETopic("T2", false));
219 		assertFalse(EnabledTopicUtils.isEnabled(noHref));
220 	}
221 
222 	@Test
testEmptyIndexEntry()223 	public void testEmptyIndexEntry() {
224 		EIndexEntry entry1 = new EIndexEntry("abc");
225 		assertFalse(EnabledTopicUtils.isEnabled(entry1));
226 	}
227 
228 	@Test
testEnabledIndexEntry()229 	public void testEnabledIndexEntry() {
230 		EIndexEntry entry1 = new EIndexEntry("abc");
231 		entry1.addTopic(new ETopic("T1", true));
232 		assertTrue(EnabledTopicUtils.isEnabled(entry1));
233 	}
234 
235 	@Test
testDisabledIndexEntry()236 	public void testDisabledIndexEntry() {
237 		EIndexEntry entry1 = new EIndexEntry("abc");
238 		entry1.addTopic(new ETopic("T1", false));
239 		assertFalse(EnabledTopicUtils.isEnabled(entry1));
240 	}
241 
242 	@Test
testMixedIndexEntry()243 	public void testMixedIndexEntry() {
244 		EIndexEntry entry1 = new EIndexEntry("abc");
245 		entry1.addTopic(new ETopic("T1", true));
246 		entry1.addTopic(new ETopic("T2", false));
247 		assertTrue(EnabledTopicUtils.isEnabled(entry1));
248 	}
249 
250 	@Test
testIndexEntryEnabledChild()251 	public void testIndexEntryEnabledChild() {
252 		EIndexEntry entry1 = new EIndexEntry("abc");
253 		EIndexEntry entry2 = new EIndexEntry("def");
254 		entry2.addTopic(new ETopic("T1", true));
255 		entry1.addSubEntry(entry2);
256 		assertTrue(EnabledTopicUtils.isEnabled(entry1));
257 	}
258 
259 	@Test
testIndexEntryEnabledGrandChild()260 	public void testIndexEntryEnabledGrandChild() {
261 		EIndexEntry entry1 = new EIndexEntry("abc");
262 		EIndexEntry entry2 = new EIndexEntry("def");
263 		EIndexEntry entry3 = new EIndexEntry("ghi");
264 		entry1.addSubEntry(entry2);
265 		entry2.addSubEntry(entry3);
266 		entry3.addTopic(new ETopic("T1", true));
267 		assertTrue(EnabledTopicUtils.isEnabled(entry1));
268 	}
269 
270 	@Test
testIndexEntryDisabledChild()271 	public void testIndexEntryDisabledChild() {
272 		EIndexEntry entry1 = new EIndexEntry("abc");
273 		EIndexEntry entry2 = new EIndexEntry("def");
274 		entry2.addTopic(new ETopic("T1", false));
275 		entry1.addSubEntry(entry2);
276 		assertFalse(EnabledTopicUtils.isEnabled(entry1));
277 	}
278 
279 	@Test
testIndexEntryMixedChildren()280 	public void testIndexEntryMixedChildren() {
281 		EIndexEntry entry1 = new EIndexEntry("abc");
282 		EIndexEntry entry2 = new EIndexEntry("def");
283 		EIndexEntry entry3 = new EIndexEntry("ghi");
284 		entry2.addTopic(new ETopic("T1", false));
285 		entry3.addTopic(new ETopic("T2", true));
286 		entry1.addSubEntry(entry2);
287 		entry1.addSubEntry(entry3);
288 		assertTrue(EnabledTopicUtils.isEnabled(entry1));
289 	}
290 
291 	@Test
testEnabledIndexArrayEmpty()292 	public void testEnabledIndexArrayEmpty() {
293 		IIndexEntry[] entries = new EIndexEntry[0];
294 		IIndexEntry[] filtered =EnabledTopicUtils.getEnabled(entries);
295 		assertEquals(0, filtered.length);
296 	}
297 
298 	@Test
testEnabledIndexArrayDisabled()299 	public void testEnabledIndexArrayDisabled() {
300 		EIndexEntry entry1 = new EIndexEntry("abc");
301 		EIndexEntry entry2 = new EIndexEntry("def");
302 		IIndexEntry[] entries = new EIndexEntry[]{entry1, entry2};
303 		IIndexEntry[] filtered =EnabledTopicUtils.getEnabled(entries);
304 		assertEquals(0, filtered.length);
305 	}
306 
307 	@Test
testEnabledIndexArrayEnabled()308 	public void testEnabledIndexArrayEnabled() {
309 		EIndexEntry entry1 = new EIndexEntry("abc");
310 		EIndexEntry entry2 = new EIndexEntry("def");
311 		entry1.addTopic(new ETopic("T1", true));
312 		entry2.addTopic(new ETopic("T2", true));
313 		IIndexEntry[] entries = new EIndexEntry[]{entry1, entry2};
314 		IIndexEntry[] filtered =EnabledTopicUtils.getEnabled(entries);
315 		assertEquals(2, filtered.length);
316 		assertEquals(filtered[0].getKeyword(), "abc");
317 		assertEquals(filtered[1].getKeyword(), "def");
318 	}
319 
320 	@Test
testEnabledIndexArrayMixed()321 	public void testEnabledIndexArrayMixed() {
322 		EIndexEntry entry1 = new EIndexEntry("abc");
323 		EIndexEntry entry2 = new EIndexEntry("def");
324 		EIndexEntry entry3 = new EIndexEntry("ghi");
325 		EIndexEntry entry4 = new EIndexEntry("jkl");
326 		entry2.addTopic(new ETopic("T1", true));
327 		entry4.addTopic(new ETopic("T2", true));
328 		IIndexEntry[] entries = new EIndexEntry[]{entry1, entry2, entry3, entry4};
329 		IIndexEntry[] filtered =EnabledTopicUtils.getEnabled(entries);
330 		assertEquals(2, filtered.length);
331 		assertEquals(filtered[0].getKeyword(), "def");
332 		assertEquals(filtered[1].getKeyword(), "jkl");
333 	}
334 
335 }
336