Skip to main content
summaryrefslogtreecommitdiffstats
blob: fc60f8a21abdda729e53417377504d6b66322c2e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*******************************************************************************
 *  Copyright (c) 2006, 2011 IBM Corporation and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 * 
 *  Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ua.tests.help.toc;

import java.util.ArrayList;
import java.util.List;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.help.IIndexEntry;
import org.eclipse.help.ITopic;
import org.eclipse.help.IUAElement;
import org.eclipse.help.internal.UAElement;
import org.eclipse.help.internal.webapp.data.EnabledTopicUtils;

public class EnabledTopicTest extends TestCase {
	
	private class ETopic implements ITopic {
		
		private String label;
		private boolean isEnabled;
		private List<ITopic> children = new ArrayList<ITopic>();

		public ETopic(String label, boolean isEnabled) {
			this.label = label; 
			this.isEnabled = isEnabled;
		}

		public ITopic[] getSubtopics() {
			return children.toArray(new ITopic[children.size()]);
		}

		public IUAElement[] getChildren() {

			return getSubtopics();
		}

		public boolean isEnabled(IEvaluationContext context) {
			return isEnabled;
		}

		public String getHref() {
			return "http://www.eclipse.org";
		}

		public String getLabel() {
			return label;
		}	
		
		public void addSubTopic(ITopic subTopic) {
			children.add(subTopic);
		}
	}
	
	private class NoHrefTopic extends ETopic {
		
		public NoHrefTopic(String label) {
			super(label, true);
		}
		
		public String getHref() {
			return null;
		}
		
	}
	
	private class EIndexEntry extends UAElement implements IIndexEntry  {
		
		private String keyword;
		private List<ITopic> topics = new ArrayList<ITopic>();
		private List<IIndexEntry> subEntries = new ArrayList<IIndexEntry>();

		public EIndexEntry(String keyword) {
			super(keyword);
			this.keyword = keyword;
		}

		public String getKeyword() {
			return keyword;
		}

		public void addSubEntry(IIndexEntry entry) {
			subEntries.add(entry);
		}
		
		public void addTopic(ITopic topic) {
			topics.add(topic);
		}

		public IIndexEntry[] getSubentries() {
			return subEntries.toArray(new IIndexEntry[subEntries.size()]);
		}

		public ITopic[] getTopics() {
			return topics.toArray(new ITopic[topics.size()]);
		}

		public synchronized IUAElement[] getChildren() {
			List<IUAElement> all = new ArrayList<IUAElement>();
			all.addAll(subEntries);
			all.addAll(topics);
			return all.toArray(new IUAElement[all.size()]);
		}	
	}
	
	/*
	 * Returns an instance of this Test.
	 */
	public static Test suite() {
		return new TestSuite(EnabledTopicTest.class);
	}
	
	public void testEnabledTopic() {
		assertTrue(EnabledTopicUtils.isEnabled(new ETopic("T1", true)));
		assertFalse(EnabledTopicUtils.isEnabled(new ETopic("T2", false)));
	}

	public void testEnabledTopicsEmptyArray() throws Exception {
         ITopic[] enabled = EnabledTopicUtils.getEnabled(new ITopic[0]);
         assertTrue(enabled.length == 0);
	}
	
	public void testEnabledTopicsAllEnabled() throws Exception {
        ITopic[] topics = new ITopic[2];
        topics[0] = new ETopic("T1", true);
        topics[1] = new ETopic("T2", true);
		ITopic[] enabled = EnabledTopicUtils.getEnabled(topics);
        assertTrue(enabled.length == 2);
        assertTrue(topics[0].getLabel().equals("T1"));
        assertTrue(topics[1].getLabel().equals("T2"));
	}

	public void testEnabledTopicsAllDisabled() throws Exception { 
		ITopic[] topics = new ITopic[2];
	    topics[0] = new ETopic("T1", false);
	    topics[1] = new ETopic("T2", false);
		ITopic[] enabled = EnabledTopicUtils.getEnabled(topics);
	    assertTrue(enabled.length == 0);
	}
	
	public void testEnabledTopicsMix() throws Exception {
		ITopic[] topics = new ITopic[4];
        topics[0] = new ETopic("T1", true);
        topics[1] = new ETopic("T2", false);
        topics[2] = new ETopic("T3", true);
        topics[3] = new ETopic("T4", false);
		ITopic[] enabled = EnabledTopicUtils.getEnabled(topics);
        assertEquals(2, enabled.length);
        assertEquals("T1", enabled[0].getLabel());
        assertEquals("T3", enabled[1].getLabel());
	}

	public void testNoHref() {
		ITopic noHref = new NoHrefTopic("N1");
		assertFalse(EnabledTopicUtils.isEnabled(noHref));
	}

	public void testNoHrefValidChild() {
		ETopic noHref = new NoHrefTopic("N1");
		noHref.addSubTopic(new ETopic("T1", true));
		assertTrue(EnabledTopicUtils.isEnabled(noHref));
	}

	public void testNoHrefInvalidChild() {
		ETopic noHref = new NoHrefTopic("N1");
		noHref.addSubTopic(new ETopic("T1", false));
		assertFalse(EnabledTopicUtils.isEnabled(noHref));
	}
	
	public void testNoHrefMixedChildren() {
		ETopic noHref = new NoHrefTopic("N1");
		noHref.addSubTopic(new ETopic("T1", false));
		noHref.addSubTopic(new ETopic("T2", true));
		assertTrue(EnabledTopicUtils.isEnabled(noHref));
	}

	public void testNoHrefValidGrandchild() {
		ETopic noHref = new NoHrefTopic("N1");
		ETopic subTopic = new NoHrefTopic("N2");
		noHref.addSubTopic(subTopic);
		subTopic.addSubTopic(new ETopic("T2", true));
		assertTrue(EnabledTopicUtils.isEnabled(noHref));
	}
	
	public void testNoHrefInvalidGrandchild() {
		ETopic noHref = new NoHrefTopic("N1");
		ETopic subTopic = new NoHrefTopic("N2");
		noHref.addSubTopic(subTopic);
		subTopic.addSubTopic(new ETopic("T2", false));
		assertFalse(EnabledTopicUtils.isEnabled(noHref));
	}

	public void testEmptyIndexEntry() {
		EIndexEntry entry1 = new EIndexEntry("abc");
		assertFalse(EnabledTopicUtils.isEnabled(entry1));
	}
	
	public void testEnabledIndexEntry() {
		EIndexEntry entry1 = new EIndexEntry("abc");
		entry1.addTopic(new ETopic("T1", true));
		assertTrue(EnabledTopicUtils.isEnabled(entry1));
	}
	
	public void testDisabledIndexEntry() {
		EIndexEntry entry1 = new EIndexEntry("abc");
		entry1.addTopic(new ETopic("T1", false));
		assertFalse(EnabledTopicUtils.isEnabled(entry1));
	}

	public void testMixedIndexEntry() {
		EIndexEntry entry1 = new EIndexEntry("abc");
		entry1.addTopic(new ETopic("T1", true));
		entry1.addTopic(new ETopic("T2", false));
		assertTrue(EnabledTopicUtils.isEnabled(entry1));
	}

	public void testIndexEntryEnabledChild() {
		EIndexEntry entry1 = new EIndexEntry("abc");
		EIndexEntry entry2 = new EIndexEntry("def");
		entry2.addTopic(new ETopic("T1", true));
		entry1.addSubEntry(entry2);
		assertTrue(EnabledTopicUtils.isEnabled(entry1));
	}
	
	public void testIndexEntryEnabledGrandChild() {
		EIndexEntry entry1 = new EIndexEntry("abc");
		EIndexEntry entry2 = new EIndexEntry("def");
		EIndexEntry entry3 = new EIndexEntry("ghi");
		entry1.addSubEntry(entry2);
		entry2.addSubEntry(entry3);
		entry3.addTopic(new ETopic("T1", true));
		assertTrue(EnabledTopicUtils.isEnabled(entry1));
	}

	public void testIndexEntryDisabledChild() {
		EIndexEntry entry1 = new EIndexEntry("abc");
		EIndexEntry entry2 = new EIndexEntry("def");
		entry2.addTopic(new ETopic("T1", false));
		entry1.addSubEntry(entry2);
		assertFalse(EnabledTopicUtils.isEnabled(entry1));
	}
	
	public void testIndexEntryMixedChildren() {
		EIndexEntry entry1 = new EIndexEntry("abc");
		EIndexEntry entry2 = new EIndexEntry("def");
		EIndexEntry entry3 = new EIndexEntry("ghi");
		entry2.addTopic(new ETopic("T1", false));
		entry3.addTopic(new ETopic("T2", true));
		entry1.addSubEntry(entry2);
		entry1.addSubEntry(entry3);
		assertTrue(EnabledTopicUtils.isEnabled(entry1));
	}

	public void testEnabledIndexArrayEmpty() {
		IIndexEntry[] entries = new EIndexEntry[0];
		IIndexEntry[] filtered =EnabledTopicUtils.getEnabled(entries);
		assertEquals(0, filtered.length);
	}

	public void testEnabledIndexArrayDisabled() {
		EIndexEntry entry1 = new EIndexEntry("abc");
		EIndexEntry entry2 = new EIndexEntry("def");
		IIndexEntry[] entries = new EIndexEntry[]{entry1, entry2};
		IIndexEntry[] filtered =EnabledTopicUtils.getEnabled(entries);
		assertEquals(0, filtered.length);
	}

	public void testEnabledIndexArrayEnabled() {
		EIndexEntry entry1 = new EIndexEntry("abc");
		EIndexEntry entry2 = new EIndexEntry("def");
		entry1.addTopic(new ETopic("T1", true));
		entry2.addTopic(new ETopic("T2", true));
		IIndexEntry[] entries = new EIndexEntry[]{entry1, entry2};
		IIndexEntry[] filtered =EnabledTopicUtils.getEnabled(entries);
		assertEquals(2, filtered.length);
		assertEquals(filtered[0].getKeyword(), "abc");
		assertEquals(filtered[1].getKeyword(), "def");
	}
	
	public void testEnabledIndexArrayMixed() {
		EIndexEntry entry1 = new EIndexEntry("abc");
		EIndexEntry entry2 = new EIndexEntry("def");
		EIndexEntry entry3 = new EIndexEntry("ghi");
		EIndexEntry entry4 = new EIndexEntry("jkl");
		entry2.addTopic(new ETopic("T1", true));
		entry4.addTopic(new ETopic("T2", true));
		IIndexEntry[] entries = new EIndexEntry[]{entry1, entry2, entry3, entry4};
		IIndexEntry[] filtered =EnabledTopicUtils.getEnabled(entries);
		assertEquals(2, filtered.length);
		assertEquals(filtered[0].getKeyword(), "def");
		assertEquals(filtered[1].getKeyword(), "jkl");
	}
	
}

Back to the top