Skip to main content
summaryrefslogtreecommitdiffstats
blob: 10f00acbff9d14b6219a5759b44def29c5d84136 (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
/*******************************************************************************
 * Copyright (c) 2006, 2016 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.search;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.help.internal.base.BaseHelpSystem;
import org.eclipse.help.internal.search.ISearchQuery;
import org.eclipse.help.internal.search.SearchHit;
import org.eclipse.help.internal.search.SearchQuery;
import org.eclipse.help.internal.search.SearchResults;
import org.junit.Test;

public class SearchCheatsheet {

	public SearchHit[] findHits(String searchWord) {
		ISearchQuery query = new SearchQuery(searchWord, false, new ArrayList<String>(), Platform.getNL());
		SearchResults collector = new SearchResults(null, 10, Platform.getNL());
		BaseHelpSystem.getSearchManager().search(query, collector, new NullProgressMonitor());
		return collector.getSearchHits();
	}

	@Test
	public void testCheatSheetTitleSearch() {
		SearchHit[] hits = findHits("CSTitle_AhB4U8");
		checkForCheatSheetMatch(hits);
	}

	@Test
	public void testCheatSheetIntroSearch() {
		SearchHit[] hits = findHits("CSIntro_AhB4U8");
		checkForCheatSheetMatch(hits);
	}

	@Test
	public void testCheatSheetItemSearch() {
		SearchHit[] hits = findHits("CSItem_AhB4U8");
		checkForCheatSheetMatch(hits);
		hits = findHits("CSItemDesc_AhB4U8");
		checkForCheatSheetMatch(hits);
		hits = findHits("CSItemCompletion_AhB4U8");
		checkForCheatSheetMatch(hits);
	}

	@Test
	public void testCheatSheetSubitemSearch() {
		SearchHit[] hits = findHits("SubItem1_AhB4U8");
		checkForCheatSheetMatch(hits);
		hits = findHits("SubItem2_AhB4U8");
		checkForCheatSheetMatch(hits);
		hits = findHits("ConditionalSubItem_AhB4U8");
		checkForCheatSheetMatch(hits);
	}

	@Test
	public void testCompositeTitleSearch() {
		SearchHit[] hits = findHits("CompositeName_AhB4U8");
		checkForCompositeMatch(hits);
	}

	@Test
	public void testCompositeTaskSearch() {
		SearchHit[] hits = findHits("TaskName_AhB4U8");
		checkForCompositeMatch(hits);
		hits = findHits("TaskIntro_AhB4U8");
		checkForCompositeMatch(hits);
		hits = findHits("TaskCompletion_AhB4U8");
		checkForCompositeMatch(hits);
		// Matches in task ids should not be hits
		hits = findHits("TaskId_AhB4U8");
		assertEquals(0, hits.length);
	}

	@Test
	public void testCompositeTaskGroupSearch() {
		SearchHit[] hits = findHits("TaskGroup_AhB4U8");
		checkForCompositeMatch(hits);
		hits = findHits("TaskGroupIntro_AhB4U8");
		checkForCompositeMatch(hits);
		hits = findHits("TaskGroupCompletion_AhB4U8");
		checkForCompositeMatch(hits);
	}

	/*
	 * Chech that there was one match, the
	 */
	private void checkForCheatSheetMatch(SearchHit[] hits) {
		assertEquals(1, hits.length);
		assertEquals("/org.eclipse.ua.tests/data/cheatsheet/search/CSSearchTest.xml",
				ignoreQuery(hits[0].getHref()));
		assertTrue(hits[0].getDescription().startsWith("CSIntro_AhB4U8 This cheat sheet is used to test search."));
		assertEquals("org.eclipse.ui.cheatsheets/org.eclipse.ua.tests.cheatsheet.searchTest", hits[0].getId());
		}

	private void checkForCompositeMatch(SearchHit[] hits) {
		assertEquals(1, hits.length);
		assertEquals("/org.eclipse.ua.tests/data/cheatsheet/search/CompositeSearchTest.xml",
				ignoreQuery(hits[0].getHref()));
		assertTrue(hits[0].getDescription().startsWith("Intro text TaskGroupIntro_AhB4U8"));
		assertEquals("org.eclipse.ui.cheatsheets/org.eclipse.ua.tests.composite.searchTest",
				hits[0].getId());
	}

	private String ignoreQuery(String href) {
		int index = href.indexOf('?');
		if (index != -1) {
			return href.substring(0, index);
		}
		return href;
	}

}

Back to the top