Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 29f2ea1e3d2ced0d024aae7c66df90be4a60dbba (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
/*******************************************************************************
 * Copyright (c) 2009, 2010 QNX Software Systems
 * 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:
 *     QNX Software Systems (Alena Laskavaia)  - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.internal.checkers.ui.quickfix;

import java.io.IOException;

import org.eclipse.cdt.codan.core.PreferenceConstants;
import org.eclipse.cdt.codan.core.test.CheckerTestCase;
import org.eclipse.cdt.codan.core.test.TestUtils;
import org.eclipse.cdt.codan.internal.ui.CodanUIActivator;
import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * Abstract base class for Quck Fix tests.
 */
@SuppressWarnings("restriction")
public abstract class QuickFixTestCase extends CheckerTestCase {
	AbstractCodanCMarkerResolution quickFix;
	Display display;

	/**
	 * Dispatch ui events for at least msec - milliseconds
	 *
	 * @param msec -
	 *        milliseconds delay
	 * @param display -
	 *        display that dispatches events
	 */
	public void dispatch(int msec) {
		long cur = System.currentTimeMillis();
		long pass = 0;
		while (pass < msec) {
			if (!display.readAndDispatch())
				display.sleep();
			pass = System.currentTimeMillis() - cur;
		}
	}

	public static void closeWelcome() {
		try {
			IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
			IWorkbenchPage activePage = window.getActivePage();
			IWorkbenchPart activePart = activePage.getActivePart();
			if (activePart.getTitle().equals("Welcome")) { //$NON-NLS-1$
				//activePage.close();
				activePart.dispose();
			}
		} catch (Exception e) {
			// ignore
		}
	}

	@Override
	public void setUp() throws Exception {
		super.setUp();
		quickFix = createQuickFix();
		display = PlatformUI.getWorkbench().getDisplay();
		closeWelcome();
		IPreferenceStore store = CodanUIActivator.getDefault().getPreferenceStore(cproject.getProject());
		// turn off editor reconciler
		store.setValue(PreferenceConstants.P_RUN_IN_EDITOR, false);
	}

	@Override
	public void tearDown() throws Exception {
		Display.getDefault().syncExec(new Runnable() {
			@Override
			public void run() {
				IWorkbenchPage[] pages = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPages();
				for (IWorkbenchPage page : pages) {
					page.closeAllEditors(false);
					dispatch(0);
				}
			}
		});

		super.tearDown();
	}

	/**
	 * @return
	 */
	protected abstract AbstractCodanCMarkerResolution createQuickFix();

	/**
	 * @param code
	 * @param string
	 * @return
	 */
	protected ISelection textSelection(String code, String string) {
		return new TextSelection(code.indexOf(string), string.length());
	}

	public String runQuickFixOneFile() throws IOException, CoreException {
		// need to load before running codan because otherwise marker is lost when doing quick fix 8[]
		runCodan();
		doRunQuickFix();
		String result = TestUtils.loadFile(currentIFile.getContents());
		return result;
	}

	public void doRunQuickFix() {
		Display.getDefault().syncExec(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < markers.length; i++) {
					IMarker marker = markers[i];
					quickFix.run(marker);
					dispatch(0);
				}
				PlatformUI.getWorkbench().saveAllEditors(false);
			}
		});

	}

	/**
	 * @param result
	 * @param expected
	 */
	public void assertContainedIn(String expected, String result) {
		assertTrue("Text <" + expected + "> not found in <" + result + ">", result.contains(expected)); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
	}

	/**
	 * Changes the quick fix to be used
	 * @param quickFix
	 */
	public void setQuickFix(AbstractCodanCMarkerResolution quickFix) {
		this.quickFix = quickFix;
	}
}

Back to the top