tmf: add test to check if views are populated in a given perspective
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui.swtbot.tests / src / org / eclipse / linuxtools / tmf / ui / swtbot / tests / SWTBotUtil.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.swtbot.tests;
14
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.fail;
17
18 import java.util.List;
19
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.runtime.NullProgressMonitor;
22 import org.eclipse.core.runtime.jobs.Job;
23 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry;
24 import org.eclipse.linuxtools.tmf.ui.swtbot.tests.conditions.ConditionHelpers;
25 import org.eclipse.linuxtools.tmf.ui.views.TracingPerspectiveFactory;
26 import org.eclipse.swt.widgets.Display;
27 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
28 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
29 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
30 import org.eclipse.swtbot.swt.finder.results.VoidResult;
31 import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.WorkbenchException;
34
35 /**
36 * SWTBot Helper functions
37 *
38 * @author Matthew Khouzam
39 */
40 public abstract class SWTBotUtil {
41 private static final String TRACING_PERSPECTIVE_ID = TracingPerspectiveFactory.ID;
42
43 /**
44 * Waits for all Eclipse jobs to finish
45 */
46 public static void waitForJobs() {
47 while (!Job.getJobManager().isIdle()) {
48 delay(100);
49 }
50 }
51
52 /**
53 * Sleeps current thread for a given time.
54 *
55 * @param waitTimeMillis
56 * time in milliseconds to wait
57 */
58 public static void delay(final long waitTimeMillis) {
59 try {
60 Thread.sleep(waitTimeMillis);
61 } catch (final InterruptedException e) {
62 // Ignored
63 }
64 }
65
66 /**
67 * Create a tracing project
68 *
69 * @param projectName
70 * the name of the tracing project
71 */
72 public static void createProject(final String projectName) {
73 /*
74 * Make a new test
75 */
76 UIThreadRunnable.syncExec(new VoidResult() {
77 @Override
78 public void run() {
79 IProject project = TmfProjectRegistry.createProject(projectName, null, new NullProgressMonitor());
80 assertNotNull(project);
81 }
82 });
83
84 SWTBotUtil.waitForJobs();
85 }
86
87 /**
88 * Focus on the main window
89 *
90 * @param shellBots
91 * swtbotshells for all the shells
92 */
93 public static void focusMainWindow(SWTBotShell[] shellBots) {
94 for (SWTBotShell shellBot : shellBots) {
95 if (shellBot.getText().toLowerCase().contains("eclipse")) {
96 shellBot.activate();
97 }
98 }
99 }
100
101 /**
102 * Close a view with a title
103 *
104 * @param title
105 * the title, like "welcome"
106 * @param bot
107 * the workbench bot
108 */
109 public static void closeView(String title, SWTWorkbenchBot bot) {
110 final List<SWTBotView> openViews = bot.views();
111 for (SWTBotView view : openViews) {
112 if (view.getTitle().equalsIgnoreCase(title)) {
113 view.close();
114 bot.waitUntil(ConditionHelpers.ViewIsClosed(view));
115 }
116 }
117 }
118
119 /**
120 * Switch to the tracing perspective
121 */
122 public static void switchToTracingPerspective() {
123 switchToPerspective(TRACING_PERSPECTIVE_ID);
124 }
125
126 /**
127 * Switch to a given perspective
128 *
129 * @param id
130 * the perspective id (like
131 * "org.eclipse.linuxtools.tmf.ui.perspective"
132 */
133 public static void switchToPerspective(final String id) {
134 UIThreadRunnable.syncExec(new VoidResult() {
135 @Override
136 public void run() {
137 try {
138 PlatformUI.getWorkbench().showPerspective(id, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
139 } catch (WorkbenchException e) {
140 fail(e.getMessage());
141 }
142 }
143 });
144 }
145
146 /**
147 * If the test is running in the UI thread then fail
148 */
149 public static void failIfUIThread() {
150 if (Display.getCurrent() != null && Display.getCurrent().getThread() == Thread.currentThread()) {
151 fail("SWTBot test needs to run in a non-UI thread. Make sure that \"Run in UI thread\" is unchecked in your launch configuration or"
152 + " that useUIThread is set to false in the pom.xml");
153 }
154
155 }
156 }
This page took 0.038059 seconds and 6 git commands to generate.