Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9381519ca2f09616868976f9d4dd4a3dfa86b187 (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
package org.eclipse.linuxtools.gcov.test;

import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withText;
import static org.eclipse.swtbot.swt.finder.waits.Conditions.waitForShell;

import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.TreeSet;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.linuxtools.dataviewers.actions.STExportToCSVAction;
import org.eclipse.linuxtools.dataviewers.annotatedsourceeditor.actions.AbstractOpenSourceFileAction;
import org.eclipse.linuxtools.gcov.Activator;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEclipseEditor;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotRadio;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.hamcrest.Matcher;

public abstract class GcovTest {

	public static void createProject(SWTWorkbenchBot bot, String projectName, String projectType) {
		SWTBotMenu fileMenu = bot.menu("File");
		SWTBotMenu newMenu = fileMenu.menu("New");
		SWTBotMenu projectMenu = newMenu.menu(projectType);
		projectMenu.click();
		
		SWTBotShell shell = bot.shell(projectType);
		shell.activate();
		
		bot.tree().expandNode("Makefile project").select("Empty Project");
		bot.textWithLabel("Project name:").setText(projectName);
		bot.table().getTableItem("Linux GCC").click();
		
		bot.button("Next >").click();
		bot.button("Finish").click();
	}
	
	public static void populateProject(SWTWorkbenchBot bot, String projectName) throws Exception {
		SWTBot viewBot = bot.viewByTitle("Project Explorer").bot();
		viewBot.activeShell().activate();
		SWTBotTree treeBot = viewBot.tree();
		treeBot.setFocus();
		treeBot = treeBot.select(projectName);
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
		InputStream is = FileLocator.openStream(Activator.getDefault().getBundle(), new Path("resource/" + projectName + "/content"), false);
		LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is));
		String filename;
		while (null != (filename = lnr.readLine())) {
			final ProgressMonitor pm = new ProgressMonitor();
			final IFile ifile = project.getFile(filename);
			InputStream fis = FileLocator.openStream(Activator.getDefault().getBundle(), new Path("resource/" + projectName + "/" + filename), false);
			ifile.create(fis, true, pm);
			bot.waitUntil(new DefaultCondition() {

				@Override
				public boolean test() throws Exception {
					return pm.isDone();
				}

				@Override
				public String getFailureMessage() {
					return ifile + " not yet created after 3000ms";
				}
			}, 3000);
		}
		lnr.close();
	}

	public static void compileProject(SWTWorkbenchBot bot, String projectName) {
		SWTBot viewBot = bot.viewByTitle("Project Explorer").bot();
		viewBot.activeShell().activate();
		SWTBotTree treeBot = viewBot.tree();
		treeBot.setFocus();
		treeBot = treeBot.select(projectName);
		SWTBotMenu menu = bot.menu("Build Project");
		menu.click();
		bot.waitUntil(new JobsRunning(ResourcesPlugin.FAMILY_MANUAL_BUILD), 30000);
	}

	private static TreeSet<String> getGcovFiles(SWTWorkbenchBot bot, String projectName) throws Exception {
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
		TreeSet<String> ret = new TreeSet<String>();
		for (IResource r : project.members()) {
			if (r.getType() == IResource.FILE && r.exists()) {
				if (r.getName().endsWith(".gcda") || r.getName().endsWith(".gcno")) {
					ret.add(r.getFullPath().toOSString());
				}
			}
		}
		return ret;
	}
	
	private static void testGcovSummary(SWTWorkbenchBot bot, String projectName, String filename, String binName) throws Exception {
		IPath filePath = new Path(filename);
		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(filePath);
		String binPath = file.getProject().getFile(binName).getLocation().toOSString();
		
		SWTBot viewBot = bot.viewByTitle("Project Explorer").bot();
		SWTBotShell wbShell = viewBot.shells()[0];
		wbShell.activate();
		SWTBotTree treeBot = viewBot.tree();
		treeBot.setFocus();
		treeBot.expandNode(projectName).select(file.getName());
		treeBot.contextMenu("Open").click();
		
		Matcher<Shell> withText = withText("Gcov - Open coverage results...");
		waitForShell(withText);
		SWTBotShell shell = bot.shell("Gcov - Open coverage results...");
		shell.activate();
		bot.textInGroup("Binary File", 0).setText(binPath);
		bot.button("OK").click();
		
		wbShell.activate();
		
		SWTBotView botView = bot.viewByTitle("gcov");
		botView.toolbarButton("Sort coverage per function").click();
		dumpCSV(bot, botView, projectName, "function");
		botView.toolbarButton("Sort coverage per file").click();
		dumpCSV(bot, botView, projectName, "file");
		botView.toolbarButton("Sort coverage per folder").click();
		dumpCSV(bot, botView, projectName, "folder");
		botView.close();
	}
	

	private static void testGcovFileDetails(SWTWorkbenchBot bot, String projectName, String filename, String binName) throws Exception {
		IPath filePath = new Path(filename);
		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(filePath);
		String binPath = file.getProject().getFile(binName).getLocation().toOSString();
		
		SWTBot viewBot = bot.viewByTitle("Project Explorer").bot();
		SWTBotShell wbShell = viewBot.shells()[0];
		wbShell.activate();
		SWTBotTree treeBot = viewBot.tree();
		treeBot.setFocus();
		treeBot.expandNode(projectName).select(file.getName());
		treeBot.contextMenu("Open").click();
		
		Matcher<Shell> withText = withText("Gcov - Open coverage results...");
		waitForShell(withText);

		SWTBotShell shell = bot.shell("Gcov - Open coverage results...");
		shell.activate();
		bot.textInGroup("Binary File", 0).setText(binPath);
		SWTBotRadio button = bot.radioInGroup("Coverage result", 0);
		button.click();
		bot.button("OK").click();

		wbShell.activate();
		
		SWTBotEditor editor = bot.editorById(AbstractOpenSourceFileAction.EDITOR_ID);
		SWTBotEclipseEditor edt = editor.toTextEditor(); /* just to verify that the correct file was found */
		edt.close();
	}
	
	private static void dumpCSV(SWTWorkbenchBot bot, SWTBotView botView, String projectName, String type) {
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
		botView.toolbarButton("Export to CSV").click();
		SWTBotShell shell = bot.shell("Export to CSV");
		shell.activate();
		String s = project.getLocation() + "/" + type + "-dump.csv";
		new File(s).delete();
		bot.text().setText(s);
		bot.button("OK").click();
		bot.waitUntil(new JobsRunning(STExportToCSVAction.EXPORT_TO_CSV_JOB_FAMILY), 3000);
		String ref = STJunitUtils.getAbsolutePath(Activator.PLUGIN_ID, "resource/" + projectName + "/" + type + ".csv");
		STJunitUtils.compareIgnoreEOL(project.getLocation() + "/" + type + "-dump.csv", ref, false);
	}

	public static void openGcovFileDetails(SWTWorkbenchBot bot, String projectName) throws Exception {
		openGcovFileDetails(bot, projectName, "a.out");
	}

	public static void openGcovSummary(SWTWorkbenchBot bot, String projectName) throws Exception {
		openGcovSummary(bot, projectName, "a.out");
	}
	
	public static void openGcovSummary(SWTWorkbenchBot bot, String projectName, String binName) throws Exception {
		TreeSet<String> ts = getGcovFiles(bot, projectName);
		for (String string : ts) {
			testGcovSummary(bot, projectName, string, binName);
		}
	}

	public static void openGcovFileDetails(SWTWorkbenchBot bot,
			String projectName, String binName) throws Exception {
		TreeSet<String> ts = getGcovFiles(bot, projectName);
		for (String string : ts) {
			testGcovFileDetails(bot, projectName, string, binName);
		}
	}
	
	
	
	
	
}

Back to the top