Skip to main content
summaryrefslogtreecommitdiffstats
blob: ec26997e2e4e8bab282d9b6a77acd480c22e7f90 (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*******************************************************************************
 * Copyright (c) 2007, 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.other;

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

import org.eclipse.help.ITopic;
import org.eclipse.help.internal.Topic;
import org.eclipse.help.internal.base.BaseHelpSystem;
import org.eclipse.help.internal.base.HelpEvaluationContext;
import org.eclipse.ua.tests.help.util.DocumentCreator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class TopicTest extends TestCase {

	private static final String ECLIPSE_HREF = "http://www.eclipse.org";
	private static final String ECLIPSE = "eclipse";
	private static final String BUGZILLA = "bugzilla";
	private static final String BUGZILLA_HREF = "https://bugs.eclipse.org/bugs/";
	private static final String INVALID_INSTALLED = "<with variable=\"platform\">" +
	"<test property=\"org.eclipse.core.runtime.isBundleInstalled\" args=\"org.eclipse.ui.invalid\"/></with>";
	private static final String CS_INSTALLED = "<with variable=\"platform\">" +
	    "<test property=\"org.eclipse.core.runtime.isBundleInstalled\" args=\"org.eclipse.ui.cheatsheets\"/></with>";
	private static final String ENABLEMENT_CHEATSHEETS = "<enablement>" + CS_INSTALLED + "</enablement>";
	private static final String ENABLEMENT_INVALID = "<enablement>" +  INVALID_INSTALLED  + "</enablement>";
	private static final String FILTER_IN = "<filter name = \"plugin\" value = \"org.eclipse.ua.tests\"/>";
	private static final String FILTER_OUT = "<filter name = \"plugin\" value = \"org.eclipse.ua.invalid\"/>";
	private static final String NEGATED_FILTER_IN = "<filter name = \"plugin\" value = \"!org.eclipse.ua.tests\"/>";
	private static final String NEGATED_FILTER_OUT = "<filter name = \"plugin\" value = \"!org.eclipse.ua.invalid\"/>";
	private static final String TOPIC_END = "</topic>";
	private static final String TOPIC_HEAD_ECLIPSE = "<topic href=\"http://www.eclipse.org\" label=\"eclipse\">";
	private final String TOPIC_ECLIPSE = "<topic href=\"http://www.eclipse.org\" label=\"eclipse\"/>";
	private final String TOPIC_BUGZILLA = "<topic href=\"https://bugs.eclipse.org/bugs/\" label=\"bugzilla\"/>";
	
	private final String TOPIC_WITH_ENABLEMENT = TOPIC_HEAD_ECLIPSE + ENABLEMENT_CHEATSHEETS + TOPIC_END;
	private final String TOPIC_NOT_ENABLED = TOPIC_HEAD_ECLIPSE + ENABLEMENT_INVALID + TOPIC_END;
	private final String TOPIC_FILTER_IN = TOPIC_HEAD_ECLIPSE + FILTER_IN + TOPIC_END;
	private final String TOPIC_FILTER_OUT = TOPIC_HEAD_ECLIPSE + FILTER_OUT + TOPIC_END;
	private final String TOPIC_FILTER_MIXED = TOPIC_HEAD_ECLIPSE + FILTER_IN + FILTER_OUT + TOPIC_END;
	private final String TOPIC_OLD_FILTER = "<topic filter=\"plugin=org.eclipse.ua.tests\" href=\"www.eclipse.org\"" 
	    + " label=\"Transformations and transformation configurations\"/>";
	private final String TOPIC_OLD_FILTER_DISABLED = "<topic filter=\"plugin=org.eclipse.ua.invalid\" href=\"www.eclipse.org\"" 
	    + " label=\"Transformations and transformation configurations\"/>";
	private final String TOPIC_OLD_FILTER_IN__NEGATED = "<topic filter=\"plugin!=org.eclipse.ua.tests\" href=\"www.eclipse.org\"" 
	    + " label=\"Transformations and transformation configurations\"/>";
	private final String TOPIC_OLD_FILTER_OUT_NEGATED = "<topic filter=\"plugin!=org.eclipse.ua.invalid\" href=\"www.eclipse.org\"" 
	    + " label=\"Transformations and transformation configurations\"/>";
	private final String TOPIC_WITH_CHILD = TOPIC_HEAD_ECLIPSE + TOPIC_BUGZILLA + TOPIC_END;
	
	public static Test suite() {
		return new TestSuite(TopicTest.class);
	}
	
	@Override
	protected void setUp() throws Exception {
		// Required for isEnabled() to work correctly
		BaseHelpSystem.setMode(BaseHelpSystem.MODE_WORKBENCH);
	}

	private Topic createTopic(final String topicSource) {
		Topic topic;
		Document doc;
		try {
		    doc = DocumentCreator.createDocument(topicSource);
		} catch (Exception e) {
			fail("Caught Exception");
			doc = null;
		}
		topic = new Topic((Element) doc.getFirstChild());
		return topic;
	}

	public void testSimpleTopic() {
		Topic topic;
		topic = createTopic(TOPIC_ECLIPSE);
		assertEquals(ECLIPSE, topic.getLabel());
		assertEquals(ECLIPSE_HREF, topic.getHref());
	}

	public void testCopySimpleTopic() {
		Topic topic1;
		topic1 = createTopic(TOPIC_ECLIPSE);
		Topic topic2 = new Topic(topic1);
		assertEquals(ECLIPSE, topic1.getLabel());
		assertEquals(ECLIPSE_HREF, topic1.getHref());
		assertEquals(ECLIPSE, topic2.getLabel());
		assertEquals(ECLIPSE_HREF, topic2.getHref());
	}
	
	public void testCopyTopicWithChild() {
		Topic topic1;
		topic1 = createTopic(TOPIC_WITH_CHILD);
		Topic topic2 = new Topic(topic1);

		assertEquals(1, topic1.getSubtopics().length);
		Topic child1 = (Topic)topic1.getSubtopics()[0];
		assertEquals(BUGZILLA, child1.getLabel());
		assertEquals(BUGZILLA_HREF, child1.getHref());
		
		assertEquals(1, topic2.getSubtopics().length);
		Topic child2 = (Topic)topic2.getSubtopics()[0];
		assertEquals(BUGZILLA, child2.getLabel());
		assertEquals(BUGZILLA_HREF, child2.getHref());
		assertEquals(1, topic2.getSubtopics().length);
	}
	
	/*
	 * Disabled, see Bug 210024 [Help] Topic element problems constructing from an ITopic
	public void testCopyTopicWithChildRemoveChild() {
		Topic topic1;
		topic1 = createTopic(TOPIC_WITH_CHILD);
		Topic topic2 = new Topic(topic1);
		assertEquals(1, topic1.getSubtopics().length);
		Topic child1 = (Topic)topic1.getSubtopics()[0];
		assertEquals(1, topic2.getSubtopics().length);
		topic1.removeChild(child1);
		assertEquals(0, topic1.getSubtopics().length);
		assertEquals(1, topic2.getSubtopics().length);
	}
	*/
	
	/*
	 * Test the assumption that when a topic is created from another topic not only
	 * the topic but all the children are recursively copied
	 */
	/*
	 * Disabled, see Bug 210024 [Help] Topic element problems constructing from an ITopic
	public void testCopyTopicWithChildCheckParents() {
		Topic topic1;
		topic1 = createTopic(TOPIC_WITH_CHILD);
		Topic topic2 = new Topic(topic1);
		assertEquals(ECLIPSE, topic1.getLabel());
		assertEquals(ECLIPSE_HREF, topic1.getHref());
		assertEquals(1, topic1.getSubtopics().length);
		Topic child1 = (Topic)topic1.getSubtopics()[0];
		assertTrue(child1.getParentElement() == topic1);
		assertEquals(ECLIPSE, topic2.getLabel());
		assertEquals(ECLIPSE_HREF, topic2.getHref());
		assertEquals(1, topic2.getSubtopics().length);
		Topic child2 = (Topic)topic1.getSubtopics()[0];
		assertTrue(child2.getParentElement() == topic2);
	}
	*/

	public void testEnabledTopic() {
		Topic topic;
		topic = createTopic(TOPIC_WITH_ENABLEMENT);
		assertTrue(topic.isEnabled(HelpEvaluationContext.getContext()));
	}

	public void testDisabledTopic() {
		Topic topic;
		topic = createTopic(TOPIC_NOT_ENABLED);
		assertFalse(topic.isEnabled(HelpEvaluationContext.getContext()));
	}
	
	public void testCopyDisabledTopic() {
		Topic topic1;
		topic1 = createTopic(TOPIC_NOT_ENABLED);
		Topic topic2 = new Topic(topic1);
		Topic topic3 = new Topic(topic2);
		assertFalse(topic1.isEnabled(HelpEvaluationContext.getContext()));
		assertFalse(topic2.isEnabled(HelpEvaluationContext.getContext()));
		assertFalse(topic3.isEnabled(HelpEvaluationContext.getContext()));
	}
	
	public void testCompoundEnablement() {
		Topic topic;
		topic = createTopic(TOPIC_HEAD_ECLIPSE + "<enablement>"
				+ CS_INSTALLED 
				+ INVALID_INSTALLED 
				+ "</enablement>" + TOPIC_END);
		assertFalse(topic.isEnabled(HelpEvaluationContext.getContext()));
		topic = createTopic(TOPIC_HEAD_ECLIPSE + "<enablement><and>" 
				+ INVALID_INSTALLED 
				+ CS_INSTALLED 
				+ "</and></enablement>" + TOPIC_END);
		assertFalse(topic.isEnabled(HelpEvaluationContext.getContext()));
	}

	public void testOldStyleEnablement() {
		Topic topic;
		topic = createTopic(TOPIC_OLD_FILTER);
		assertTrue(topic.isEnabled(HelpEvaluationContext.getContext()));
	}

	public void testOldStyleDisabled() {
		Topic topic;
		topic = createTopic(TOPIC_OLD_FILTER_DISABLED);
		assertFalse(topic.isEnabled(HelpEvaluationContext.getContext()));
	}
	
	public void testOldStyleNegated() {
		Topic topic;
		topic = createTopic(TOPIC_OLD_FILTER_IN__NEGATED);
		assertFalse(topic.isEnabled(HelpEvaluationContext.getContext()));
		topic = createTopic(TOPIC_OLD_FILTER_OUT_NEGATED);
		assertTrue(topic.isEnabled(HelpEvaluationContext.getContext()));
	}
	
	public void testCopyOldStyleDisabled() {
		Topic topic1;
		topic1 = createTopic(TOPIC_OLD_FILTER_DISABLED);
		Topic topic2 = new Topic(topic1);
		Topic topic3 = new Topic(topic2);
		assertFalse(topic1.isEnabled(HelpEvaluationContext.getContext()));
		assertFalse(topic2.isEnabled(HelpEvaluationContext.getContext()));
		assertFalse(topic3.isEnabled(HelpEvaluationContext.getContext()));
	}

	public void testFilterIn() {
		Topic topic;
		topic = createTopic(TOPIC_FILTER_IN);
		assertTrue(topic.isEnabled(HelpEvaluationContext.getContext()));
	}

	public void testFilterOut() {
		Topic topic;
		topic = createTopic(TOPIC_FILTER_OUT);
		assertFalse(topic.isEnabled(HelpEvaluationContext.getContext()));
	}
	
	public void testFilterMixed() {
		Topic topic;
		topic = createTopic(TOPIC_FILTER_MIXED);
		assertFalse(topic.isEnabled(HelpEvaluationContext.getContext()));
	}
	
	public void testNegatedFilters() {
		Topic topic;
		topic = createTopic(TOPIC_HEAD_ECLIPSE + NEGATED_FILTER_IN + TOPIC_END);
		assertFalse(topic.isEnabled(HelpEvaluationContext.getContext()));
		topic = createTopic(TOPIC_HEAD_ECLIPSE + NEGATED_FILTER_OUT + TOPIC_END);
		assertTrue(topic.isEnabled(HelpEvaluationContext.getContext()));
	}

	public void testCopyFilterOut() {
		Topic topic1;
		topic1 = createTopic(TOPIC_FILTER_OUT);
		Topic topic2 = new Topic(topic1);
		Topic topic3 = new Topic(topic2);
		assertFalse(topic1.isEnabled(HelpEvaluationContext.getContext()));
		assertFalse(topic2.isEnabled(HelpEvaluationContext.getContext()));
		assertFalse(topic3.isEnabled(HelpEvaluationContext.getContext()));
	}

	public void testUserTopic() {
		UserTopic u1 = new UserTopic(ECLIPSE, ECLIPSE_HREF, false);
		Topic t1 = new Topic(u1);
		assertEquals(ECLIPSE, t1.getLabel());
		assertEquals(ECLIPSE_HREF, t1.getHref());
		assertFalse(t1.isEnabled(HelpEvaluationContext.getContext()));
	}

	public void testCopyFilteredUserTopic() {
		UserTopic u1 = new UserTopic(ECLIPSE, ECLIPSE_HREF, false);
		Topic t1 = new Topic(u1);
		Topic t2 = new Topic(t1);
		assertEquals(ECLIPSE, t1.getLabel());
		assertEquals(ECLIPSE_HREF, t1.getHref());
		assertFalse(t1.isEnabled(HelpEvaluationContext.getContext()));
		assertEquals(ECLIPSE, t2.getLabel());
		assertEquals(ECLIPSE_HREF, t2.getHref());
		assertFalse(t2.isEnabled(HelpEvaluationContext.getContext()));
	}
	

	public void testUserTopicWithFilteredChildren() {
		UserTopic u1 = new UserTopic(ECLIPSE, ECLIPSE_HREF, true);
		UserTopic u2 = new UserTopic(BUGZILLA, BUGZILLA_HREF, false);
		u1.addTopic(u2);
		Topic t1 = new Topic(u1);
		assertEquals(ECLIPSE, t1.getLabel());
		assertEquals(ECLIPSE_HREF, t1.getHref());
		assertTrue(t1.isEnabled(HelpEvaluationContext.getContext()));
		assertEquals(1, t1.getChildren().length);
		ITopic t2 = t1.getSubtopics()[0];
		assertEquals(BUGZILLA, t2.getLabel());
		assertEquals(BUGZILLA_HREF, t2.getHref());
		assertFalse(t2.isEnabled(HelpEvaluationContext.getContext()));
	}
	
	
	public void testCopyUserTopicWithChildren() {
		UserTopic u1 = new UserTopic(ECLIPSE, ECLIPSE_HREF, true);
		UserTopic u2 = new UserTopic(BUGZILLA, BUGZILLA_HREF, true);
		u1.addTopic(u2);
		Topic t1 = new Topic(u1);
		Topic t2 = new Topic(t1);

		assertEquals(ECLIPSE, t1.getLabel());
		assertEquals(ECLIPSE_HREF, t1.getHref());
		assertTrue(t1.isEnabled(HelpEvaluationContext.getContext()));
		assertEquals(1, t1.getChildren().length);
		ITopic t1s = t1.getSubtopics()[0];
		assertEquals(BUGZILLA, t1s.getLabel());
		assertEquals(BUGZILLA_HREF, t1s.getHref());
		
		assertEquals(ECLIPSE, t2.getLabel());
		assertEquals(ECLIPSE_HREF, t2.getHref());
		assertTrue(t2.isEnabled(HelpEvaluationContext.getContext()));
		assertEquals(1, t2.getChildren().length);
		ITopic t2s = t2.getSubtopics()[0];
		assertEquals(BUGZILLA, t2s.getLabel());
		assertEquals(BUGZILLA_HREF, t2s.getHref());
	}
		
}

Back to the top