Skip to main content
summaryrefslogtreecommitdiffstats
blob: ce42ccdc0d3ab78d9466dc82731f457e68604d0f (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/**********************************************************************
 * Copyright (c) 2004, 2016 Intel 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: 
 *     Intel Corporation - Initial API and implementation
 **********************************************************************/
package org.eclipse.cdt.ui.tests.chelp;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import junit.framework.Assert;

import org.eclipse.cdt.internal.ui.CHelpProviderManager;
import org.eclipse.cdt.internal.ui.text.CHelpBookDescriptor;
import org.eclipse.cdt.internal.ui.text.CHelpSettings;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.ICHelpBook;
import org.eclipse.cdt.ui.ICHelpResourceDescriptor;
import org.eclipse.cdt.ui.IFunctionSummary;
import org.eclipse.cdt.ui.IRequiredInclude;
import org.eclipse.cdt.ui.text.ICHelpInvocationContext;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.help.IHelpResource;

/**
 * 
 */
public class CHelpProviderTester{
	private static final String KEY_PROVIDER_ID = "providerID";
	private static final String KEY_REQUESTED_NAME = "requestedName";
	private static final String KEY_BOOK_TITLE = "bookTitle";
	private static final String KEY_BOOK_TYPE = "bookType";
	
	private Properties fProperties;
	private static CHelpProviderTester fDefaultInstance = null;
	
	private CHelpProviderTester(){
	}
	
	public static CHelpProviderTester getDefault(){
		if(fDefaultInstance == null)
			fDefaultInstance = new CHelpProviderTester();
		return fDefaultInstance;
	}
	
	private class CHelpBook implements ICHelpBook{
		private int fCHelpType;
		private String fTitle;
		private List<IFunctionSummary> fFunctions = new ArrayList<>();

		public CHelpBook(String providerID, int type) {
			fCHelpType = type;
			fTitle = generateBookTitle(providerID, type);
			if (fCHelpType == HELP_TYPE_C) {
				fFunctions.add(new FunctionSummary(this, "setvbuf", providerID));
				fFunctions.add(new FunctionSummary(this, "wait", providerID));
			}
		}
			
		@Override
		public String getTitle(){
			return fTitle;
		}
			
		@Override
		public int getCHelpType(){
			return fCHelpType;
		}

		public List<IFunctionSummary> getMatchingFunctions(String prefix) {
			List<IFunctionSummary> result = new ArrayList<>();
			for (IFunctionSummary function : fFunctions) {
				if (function.getName().startsWith(prefix)) {
					result.add(function);
				}
			}
			return result;
		}
	}
	
	private class CHelpResourceDescriptor implements ICHelpResourceDescriptor{
		ICHelpBook fBook;
		String fString;
		String fLabel;
		String fHref;
		IHelpResource fResources[];
		
		public CHelpResourceDescriptor(ICHelpBook helpBook, String string, String providerID){
			fBook = helpBook;
			fString = string;
			fHref = string + helpBook.getTitle() + ".html";
			fLabel = generateHelpString(helpBook, string, providerID);
			fResources = new IHelpResource[1];
			fResources[0] = new IHelpResource(){
				@Override
				public String getHref(){
					return fHref;
				}
				
				@Override
				public String getLabel(){
					return fLabel;
				}				
			};
		}
	
		@Override
		public ICHelpBook getCHelpBook(){
			return fBook;
		}
		
		@Override
		public IHelpResource[] getHelpResources(){
			return fResources;
		}
	}

	private class FunctionSummary implements IFunctionSummary {

        private String fName = "Name";
        private String fReturnType = "ReturnType";
        private String fPrototype = "Prototype";
        private String fSummary = "Summary";
        private String fSynopsis = "Synopsis";
        private IRequiredInclude[] incs = new IRequiredInclude[] { new RequiredInclude("dummy.h")};
        private class RequiredInclude implements IRequiredInclude {
        	private String include;
        	
        	public RequiredInclude (String file) {
        		include = file;
        	}
        	
        	@Override
			public String getIncludeName() {
        		return include;
        	}
        	
        	@Override
			public boolean isStandard() {
        		return true;
        	}
        }
        
        public FunctionSummary(ICHelpBook helpBook, String string, String providerID){
        	fName = string;
        	fSummary = generateHelpString(helpBook, string, providerID);
        }

        public class FunctionPrototypeSummary implements IFunctionPrototypeSummary {
            @Override
			public String getName()             { return fName; }
            @Override
			public String getReturnType()       { return fReturnType; }
            @Override
			public String getArguments()        { return fPrototype; }
            @Override
			public String getPrototypeString(boolean namefirst) {
                if (true == namefirst) {
                    return fName + " (" + fPrototype + ") " + fReturnType;
                }
                else {
                    return fReturnType + " " + fName + " (" + fPrototype + ")";
                }
            }
        }

        @Override
		public String getName()                         { return fName; }
        @Override
		public String getNamespace()                    { return "dummy namespace"; }
        @Override
		public String getDescription()                  { return fSummary; }
        @Override
		public IFunctionPrototypeSummary getPrototype() { return new FunctionPrototypeSummary(); }
        
        @Override
		public IRequiredInclude[] getIncludes() {
        	return incs; 
        }
        
    }

	private static String generateHelpString(ICHelpBook helpBook, String name, String providerID){
		Properties props = new Properties();
		props.setProperty(KEY_PROVIDER_ID, providerID);
		props.setProperty(KEY_REQUESTED_NAME, name);
		props.setProperty(KEY_BOOK_TITLE, helpBook.getTitle());
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		try{
			props.store(outputStream,null);
		}
		catch(Exception e){
		}
		return outputStream.toString();
	}
	
	private static String generateBookTitle(String providerID, int bookType){
		Properties props = new Properties();
		props.setProperty(KEY_PROVIDER_ID, providerID);
		props.setProperty(KEY_BOOK_TYPE, String.valueOf(bookType));
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		try{
			props.store(outputStream,null);
		}
		catch(Exception e){
		}
		return outputStream.toString();
	}

	private CHelpProviderTester(String string) throws IOException{
		fProperties = new Properties();
		ByteArrayInputStream stream = new ByteArrayInputStream(string.getBytes());
		
		try{
			fProperties.load(stream);
		}catch(IOException e){
			//TODO: handle
			throw e;
		}
	}
	
	private String getValueByKey(String key){
		String val = fProperties.getProperty(key);
		if(val == null)
			val = ""; //$NON-NLS-1$
		return val;
	}
	
	private String getHelpProviderID(){
		return getValueByKey(KEY_PROVIDER_ID);
	}

	private String getRequestedName(){
		return getValueByKey(KEY_REQUESTED_NAME);
	}

	private String getBookTitle(){
		return getValueByKey(KEY_BOOK_TITLE);
	}
	
	public boolean onlyTestInfoProvidersAvailable(){
		IConfigurationElement configElements[] = Platform.getExtensionRegistry().getConfigurationElementsFor(CUIPlugin.PLUGIN_ID, CHelpSettings.CONTRIBUTION_EXTENSION);
		int numExts = 0;
		for(int i = 0; i < configElements.length; i++){
			String id = configElements[i].getAttribute("id");
			if(!id.startsWith(CHelpTest.TEST_EXTENSION_ID_PREFIX))
				return false;
		}
		return true;
	}

	public ICHelpResourceDescriptor[] generateHelpResources(ICHelpBook[] helpBooks, String name, String providerID){
		ICHelpResourceDescriptor des[] = new ICHelpResourceDescriptor[helpBooks.length];
		for(int i = 0; i < helpBooks.length; i++){
			des[i] = new CHelpResourceDescriptor(helpBooks[i],name,providerID);
		}
		return des;
	}
	
	public IFunctionSummary generateFunctionInfo(ICHelpBook[] helpBooks, String name, String providerID){
		if(helpBooks.length == 0)
			return null;
		return new FunctionSummary(helpBooks[0],name,providerID);
	}

	public IFunctionSummary[] generateMatchingFunctions(ICHelpBook[] helpBooks, String prefix, String providerID) {
		ArrayList<IFunctionSummary> lst = new ArrayList<IFunctionSummary>();
		for (ICHelpBook helpBook : helpBooks) {
			if (helpBook instanceof CHelpBook) {
				lst.addAll(((CHelpBook) helpBook).getMatchingFunctions(prefix));
			}
		}
		return lst.toArray(new IFunctionSummary[lst.size()]);
	}
	
	public ICHelpBook[] generateCHelpBooks(final String providerID){
		ICHelpBook books[] = new ICHelpBook[3];
		books[0] = new CHelpBook(providerID,ICHelpBook.HELP_TYPE_C);
		books[1] = new CHelpBook(providerID,ICHelpBook.HELP_TYPE_CPP);
		books[2] = new CHelpBook(providerID,ICHelpBook.HELP_TYPE_ASM);
		return books;
	}
	
	private void checkResponse(CHelpProviderTester data[], ICHelpInvocationContext context, String name, boolean allBooksResponded){
		CHelpBookDescriptor bookDes[] = CHelpProviderManager.getDefault().getCHelpBookDescriptors(context);
		for(int i = 0; i < data.length; i++){
			CHelpProviderTester tester = data[i];
			Assert.assertTrue("the name passed to CHelpProvider (" + tester.getRequestedName() + ") differs prom tha name passed to manager (" + name + ")",name.equals(tester.getRequestedName()));
			String bookTitle = tester.getBookTitle();
			int j = 0;
			for(; j < bookDes.length; j++){
				if(bookTitle.equals(bookDes[j].getCHelpBook().getTitle())){
					Assert.assertTrue("provider was requested for help in disabled book",bookDes[j].isEnabled());
					break;
				}
			}
			Assert.assertFalse("provider was requested for help in non-existent book",j == bookDes.length);
		}
		
		if(allBooksResponded){
			for(int i = 0; i < bookDes.length; i++){
				if(bookDes[i].isEnabled()){
					String bookTitle = bookDes[i].getCHelpBook().getTitle();
					int j = 0;
					for(; j < data.length; j++){
						if(bookTitle.equals(data[j].getBookTitle()))
							break;
					}
					Assert.assertFalse("provider was not requested for help in enabled book",j == bookDes.length);
				}
			}
		}
	}

	public void checkHelpResources(ICHelpResourceDescriptor helpDescriptors[], ICHelpInvocationContext context, String name){
		if(helpDescriptors == null || helpDescriptors.length == 0)
			return;
		List dataList = new ArrayList(helpDescriptors.length);
		for(int i = 0; i < helpDescriptors.length; i++){
			try{
				dataList.add(new CHelpProviderTester(helpDescriptors[i].getHelpResources()[0].getLabel()));
			}catch(IOException e){
				Assert.fail("checkHelpResources failed to instantiate CHelpProviderTester, IOException occured: " + e.getMessage());
			}
		}
		if(dataList.size() > 0)
			checkResponse((CHelpProviderTester[])dataList.toArray(new CHelpProviderTester[dataList.size()]), context, name, true);
	}

	public void checkMatchingFunctions(IFunctionSummary summaries[], ICHelpInvocationContext context, String name){
		if(summaries == null || summaries.length == 0)
			return;
		List dataList = new ArrayList(summaries.length);
		for(int i = 0; i < summaries.length; i++){
			try{
				dataList.add(new CHelpProviderTester(summaries[i].getDescription()));
			}catch(IOException e){
				Assert.fail("checkMatchingFunctions failed to instantiate CHelpProviderTester, IOException occured: " + e.getMessage());
			}
		}
		if(dataList.size() > 0)
			checkResponse((CHelpProviderTester[])dataList.toArray(new CHelpProviderTester[dataList.size()]), context, name, true);
	}
	
	public void checkFunctionInfo(IFunctionSummary summary, ICHelpInvocationContext context, String name){
		if(summary == null)
			return;
		CHelpProviderTester data[] = new CHelpProviderTester[1];
		try{
			data[0] = new CHelpProviderTester(summary.getDescription());
			checkResponse(data, context, name, false);
		}catch(IOException e){
			Assert.fail("checkFunctionInfo failed to instantiate CHelpProviderTester, IOException occured: " + e.getMessage());
		}
		
	}
}

Back to the top