1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-21 21:52:10 +02:00

Bug 572593: reenable disabled meson tests

Upgrade the tests to JUnit5 so they can be marked as flaky and slow.
Also removed a bunch of unused code.

This partially reverts commit 8220215a2e.

Reason for revert: These tests were "temporarily" disabled a while ago
as part of the JIRO migration in Bug 545624.

Change-Id: I111cf4d9d9f5e07e49074ca2355a3d1f52fb1070
This commit is contained in:
Jonah Graham 2021-04-05 09:28:47 -04:00
parent 48180e437c
commit a028e22442
12 changed files with 72 additions and 820 deletions

View file

@ -8,7 +8,6 @@ Bundle-RequiredExecutionEnvironment: JavaSE-11
Require-Bundle: org.eclipse.cdt.meson.core;bundle-version="1.0.0",
org.eclipse.cdt.meson.ui;bundle-version="1.0.0",
org.eclipse.swtbot.junit4_x;bundle-version="2.6.0",
org.junit,
org.eclipse.cdt.core,
org.eclipse.swtbot.eclipse.finder,
org.eclipse.core.resources,
@ -21,6 +20,8 @@ Require-Bundle: org.eclipse.cdt.meson.core;bundle-version="1.0.0",
org.eclipse.ui.console;bundle-version="3.8.0",
org.eclipse.ui.views;bundle-version="3.9.100",
org.eclipse.ui.views.properties.tabbed;bundle-version="3.8.100"
Import-Package: org.assertj.core.api;version="1.7.1"
Import-Package: org.assertj.core.api;version="1.7.1",
org.junit.jupiter.api;version="5.7.1",
org.junit.jupiter.api.extension;version="5.7.1"
Automatic-Module-Name: org.eclipse.cdt.meson.ui.tests
Bundle-Localization: plugin

View file

@ -1,23 +0,0 @@
/*******************************************************************************
* Copyright (c) 2017, 2018 QNX Software Systems and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - modified for use in Meson testing
*******************************************************************************/
package org.eclipse.cdt.internal.meson.ui.tests;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({ NewMesonProjectTest.class, NewMesonConfigureTest.class, NewManualNinjaTest.class })
public class AutomatedIntegrationSuite {
}

View file

@ -13,18 +13,19 @@
*******************************************************************************/
package org.eclipse.cdt.internal.meson.ui.tests;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.IBinary;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.meson.ui.tests.utils.CloseWelcomePageRule;
import org.eclipse.cdt.internal.meson.ui.tests.utils.CloseWelcomePag;
import org.eclipse.cdt.meson.core.MesonNature;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
@ -39,26 +40,24 @@ import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTableItem;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.FixMethodOrder;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer.MethodName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.ExtendWith;
@SuppressWarnings("nls")
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@Ignore
@TestMethodOrder(MethodName.class)
@ExtendWith(CloseWelcomePag.class)
@Tag("flakyTest"/*BaseTestCase5.FLAKY_TEST_TAG*/)
@Tag("slowTest"/*BaseTestCase5.SLOW_TEST_TAG*/)
public class NewManualNinjaTest {
private static SWTWorkbenchBot bot;
@ClassRule
public static CloseWelcomePageRule closeWelcomePage = new CloseWelcomePageRule(
CloseWelcomePageRule.CDT_PERSPECTIVE_ID);
@BeforeClass
@BeforeAll
public static void beforeClass() {
SWTBotPreferences.TIMEOUT = 50000;
SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
@ -66,12 +65,13 @@ public class NewManualNinjaTest {
bot = new SWTWorkbenchBot();
}
@Before
@BeforeEach
public void before() {
bot.resetWorkbench();
}
@Test(timeout = 120000)
@Test
@Timeout(value = 2, unit = TimeUnit.MINUTES)
public void addNewMesonProject() throws Exception {
// open C++ perspective
if (!"C/C++".equals(bot.activePerspective().getLabel())) {

View file

@ -14,15 +14,17 @@
package org.eclipse.cdt.internal.meson.ui.tests;
import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withRegex;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.concurrent.TimeUnit;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.IBinary;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.meson.ui.tests.utils.CloseWelcomePageRule;
import org.eclipse.cdt.internal.meson.ui.tests.utils.CloseWelcomePag;
import org.eclipse.cdt.meson.core.MesonNature;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
@ -36,26 +38,24 @@ import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTableItem;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.FixMethodOrder;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer.MethodName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.ExtendWith;
@SuppressWarnings("nls")
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@Ignore
@TestMethodOrder(MethodName.class)
@ExtendWith(CloseWelcomePag.class)
@Tag("flakyTest"/*BaseTestCase5.FLAKY_TEST_TAG*/)
@Tag("slowTest"/*BaseTestCase5.SLOW_TEST_TAG*/)
public class NewMesonConfigureTest {
private static SWTWorkbenchBot bot;
@ClassRule
public static CloseWelcomePageRule closeWelcomePage = new CloseWelcomePageRule(
CloseWelcomePageRule.CDT_PERSPECTIVE_ID);
@BeforeClass
@BeforeAll
public static void beforeClass() {
SWTBotPreferences.TIMEOUT = 50000;
SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
@ -63,12 +63,13 @@ public class NewMesonConfigureTest {
bot = new SWTWorkbenchBot();
}
@Before
@BeforeEach
public void before() {
bot.resetWorkbench();
}
@Test(timeout = 120000)
@Test
@Timeout(value = 2, unit = TimeUnit.MINUTES)
public void addNewMesonProject() throws Exception {
// open C++ perspective
if (!"C/C++".equals(bot.activePerspective().getLabel())) {

View file

@ -14,15 +14,17 @@
package org.eclipse.cdt.internal.meson.ui.tests;
import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withRegex;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.concurrent.TimeUnit;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.IBinary;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.meson.ui.tests.utils.CloseWelcomePageRule;
import org.eclipse.cdt.internal.meson.ui.tests.utils.CloseWelcomePag;
import org.eclipse.cdt.meson.core.MesonNature;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
@ -36,26 +38,24 @@ import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTableItem;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.FixMethodOrder;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer.MethodName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.ExtendWith;
@SuppressWarnings("nls")
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@Ignore
@TestMethodOrder(MethodName.class)
@ExtendWith(CloseWelcomePag.class)
@Tag("flakyTest"/*BaseTestCase5.FLAKY_TEST_TAG*/)
@Tag("slowTest"/*BaseTestCase5.SLOW_TEST_TAG*/)
public class NewMesonProjectTest {
private static SWTWorkbenchBot bot;
@ClassRule
public static CloseWelcomePageRule closeWelcomePage = new CloseWelcomePageRule(
CloseWelcomePageRule.CDT_PERSPECTIVE_ID);
@BeforeClass
@BeforeAll
public static void beforeClass() {
SWTBotPreferences.TIMEOUT = 50000;
SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
@ -63,12 +63,13 @@ public class NewMesonProjectTest {
bot = new SWTWorkbenchBot();
}
@Before
@BeforeEach
public void before() {
bot.resetWorkbench();
}
@Test(timeout = 120000)
@Test
@Timeout(value = 2, unit = TimeUnit.MINUTES)
public void addNewMesonProject() throws Exception {
// open C++ perspective
if (!"C/C++".equals(bot.activePerspective().getLabel())) {

View file

@ -1,59 +0,0 @@
/*******************************************************************************
* Copyright (c) 2015 Red Hat.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat - Initial Contribution
*******************************************************************************/
package org.eclipse.cdt.internal.meson.ui.tests.utils;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.ui.PlatformUI;
import org.junit.rules.ExternalResource;
/**
* Closes the wizard(s) after each test, if the "Cancel" button is available
*/
public class CloseShellRule extends ExternalResource {
private final String buttonLabel;
public CloseShellRule(final String buttonLabel) {
this.buttonLabel = buttonLabel;
}
@Override
protected void after() {
final SWTWorkbenchBot bot = new SWTWorkbenchBot();
try {
while (isInDialog(bot) && getButton(bot, this.buttonLabel) != null) {
getButton(bot, this.buttonLabel).click();
}
} catch (WidgetNotFoundException e) {
// ignoring
}
}
private static boolean isInDialog(final SWTWorkbenchBot bot) {
final SWTBotShell activeShell = bot.activeShell();
final String text = SWTUtils
.syncExec(() -> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getText());
final String shellText = activeShell.getText();
return text != null && !text.equals(shellText);
}
private static SWTBotButton getButton(final SWTWorkbenchBot bot, final String buttonLabel) {
return bot.button(buttonLabel);
}
}

View file

@ -19,35 +19,25 @@ import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.junit.rules.ExternalResource;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
/**
* Closes the Welcome page and optionally opens a given perspective
*/
public class CloseWelcomePageRule extends ExternalResource {
public static final String DOCKER_PERSPECTIVE_ID = "org.eclipse.linuxtools.docker.ui.perspective";
public class CloseWelcomePag implements BeforeEachCallback {
public static final String CDT_PERSPECTIVE_ID = "org.eclipse.cdt.ui.CPerspective";
public static final String JAVA_PERSPECTIVE_ID = "org.eclipse.jdt.ui.JavaPerspective";
/** the Id of the perspective to open. */
private final String defaultPerspectiveId;
/**
* Custom constructor with the id of the perspective to open once the
* welcome page was closed.
*
* @param perspectiveId
* the id of the perspective to open.
*/
public CloseWelcomePageRule(final String perspectiveId) {
this.defaultPerspectiveId = perspectiveId;
public CloseWelcomePag() {
this.defaultPerspectiveId = CloseWelcomePag.CDT_PERSPECTIVE_ID;
}
@Override
protected void before() {
public void beforeEach(ExtensionContext context) throws Exception {
Display.getDefault().syncExec(() -> {
final IWorkbench workbench = PlatformUI.getWorkbench();
if (workbench.getIntroManager().getIntro() != null) {

View file

@ -1,39 +0,0 @@
/*******************************************************************************
* Copyright (c) 2016 Red Hat.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat - Initial Contribution
*******************************************************************************/
package org.eclipse.cdt.internal.meson.ui.tests.utils;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.ui.console.IConsoleConstants;
import org.junit.rules.ExternalResource;
/**
* An {@link ExternalResource} to close the Console view.
*/
public class ConsoleViewRule extends ExternalResource {
@Override
protected void before() {
Display.getDefault().syncExec(() -> {
final SWTBotView consoleView = SWTUtils.getSWTBotView(new SWTWorkbenchBot(),
IConsoleConstants.ID_CONSOLE_VIEW);
if (consoleView != null) {
consoleView.close();
}
});
}
}

View file

@ -1,52 +0,0 @@
/*******************************************************************************
* Copyright (c) 2016 Red Hat.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat - Initial Contribution
*******************************************************************************/
package org.eclipse.cdt.internal.meson.ui.tests.utils;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.junit.Rule;
import org.junit.rules.ExternalResource;
/**
* A JUnit {@link Rule} to open the Project Explorer view.
*/
public class ProjectExplorerViewRule extends ExternalResource {
private SWTBotView projectExplorerBotView;
public static final String PROJECT_EXPLORER_VIEW_ID = "org.eclipse.ui.navigator.ProjectExplorer";
@Override
protected void before() {
final SWTWorkbenchBot bot = new SWTWorkbenchBot();
SWTUtils.syncExec(() -> {
try {
return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.showView(PROJECT_EXPLORER_VIEW_ID);
} catch (PartInitException e) {
e.printStackTrace();
return null;
}
});
this.projectExplorerBotView = bot.viewById(PROJECT_EXPLORER_VIEW_ID);
this.projectExplorerBotView.setFocus();
}
public SWTBotView getProjectExplorerBotView() {
return this.projectExplorerBotView;
}
}

View file

@ -1,68 +0,0 @@
/*******************************************************************************
* Copyright (c) 2016 Red Hat.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat - Initial Contribution
*******************************************************************************/
package org.eclipse.cdt.internal.meson.ui.tests.utils;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.PlatformUI;
import org.junit.Assert;
import org.junit.rules.ExternalResource;
/**
*
*/
public class SWTBotViewRule extends ExternalResource {
private final SWTWorkbenchBot bot = new SWTWorkbenchBot();
private final String viewId;
private SWTBotView botView = null;
private IViewPart view = null;
public SWTBotViewRule(final String viewId) {
this.viewId = viewId;
}
@Override
protected void before() {
SWTUtils.asyncExec(() -> {
try {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(this.viewId);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Failed to open view with id '" + this.viewId + "': " + e.getMessage());
}
});
this.botView = this.bot.viewById(this.viewId);
this.botView.show();
this.view = this.botView.getViewReference().getView(true);
}
public SWTBotView bot() {
return this.botView;
}
@SuppressWarnings("unchecked")
public <T> T view() {
return (T) view;
}
public void close() {
this.botView.close();
}
}

View file

@ -13,47 +13,11 @@
*******************************************************************************/
package org.eclipse.cdt.internal.meson.ui.tests.utils;
import static org.assertj.core.api.Assertions.fail;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.finders.ContextMenuHelper;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.Result;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTableItem;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarButton;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.progress.UIJob;
import org.junit.Assert;
import org.junit.ComparisonFailure;
/**
* Utility class for SWT
@ -73,439 +37,4 @@ public class SWTUtils {
Display.getDefault().syncExec(() -> result.add(supplier.get()));
return result.poll();
}
/**
* Executes <strong>synchronously</strong> the given {@link Runnable} in the
* default Display
*
* @param runnable
* the {@link Runnable} to execute
*/
public static void syncExec(final Runnable runnable) {
Display.getDefault().syncExec(runnable);
}
/**
* Executes <strong>synchronously</strong> the given {@link Runnable} in the
* default Display. The given {@link Runnable} is ran into a rapping
* {@link Runnable} that will catch the {@link ComparisonFailure} that may
* be raised during an assertion.
*
* @param runnable
* the {@link Runnable} to execute
* @throws ComparisonFailure
* if an assertion failed.
* @throws SWTException
* if an {@link SWTException} occurred
*/
public static void syncAssert(final Runnable runnable) throws SWTException, ComparisonFailure {
final Queue<ComparisonFailure> failure = new ArrayBlockingQueue<>(1);
final Queue<SWTException> swtException = new ArrayBlockingQueue<>(1);
Display.getDefault().syncExec(() -> {
try {
runnable.run();
} catch (ComparisonFailure e1) {
failure.add(e1);
} catch (SWTException e2) {
swtException.add(e2);
}
});
if (!failure.isEmpty()) {
throw failure.poll();
}
if (!swtException.isEmpty()) {
throw swtException.poll();
}
}
/**
* Executes the given {@link Runnable} <strong>asynchronously</strong> in
* the default {@link Display} and waits until all jobs are done before
* completing.
*
* @param runnable
* @throws InterruptedException
*/
public static void asyncExec(final Runnable runnable) {
asyncExec(runnable, true);
}
/**
* Executes the given {@link Runnable} <strong>asynchronously</strong> in
* the default {@link Display} and waits until all jobs are done before
* completing.
*
* @param runnable
* the {@link Runnable} to execute
* @param waitForJobsToComplete
* boolean flag to indicate if the method should wait for all
* jobs to complete before finishing
* @throws InterruptedException
*/
public static void asyncExec(final Runnable runnable, final boolean waitForJobsToComplete) {
final Queue<ComparisonFailure> failure = new ArrayBlockingQueue<>(1);
final Queue<SWTException> swtException = new ArrayBlockingQueue<>(1);
Display.getDefault().asyncExec(() -> {
try {
runnable.run();
} catch (ComparisonFailure e1) {
failure.add(e1);
} catch (SWTException e2) {
swtException.add(e2);
}
});
if (waitForJobsToComplete) {
waitForJobsToComplete();
}
if (!failure.isEmpty()) {
throw failure.poll();
}
if (!swtException.isEmpty()) {
throw swtException.poll();
}
}
/**
* Waits for all {@link Job} to complete.
*
* @throws InterruptedException
*/
public static void waitForJobsToComplete() {
wait(1, TimeUnit.SECONDS);
while (!Job.getJobManager().isIdle()) {
wait(1, TimeUnit.SECONDS);
}
}
/**
* @param viewBot
* the {@link SWTBotView} containing the {@link Tree} to traverse
* @param paths
* the node path in the {@link SWTBotTree} associated with the
* given {@link SWTBotView}
* @return the first {@link SWTBotTreeItem} matching the given node names
*/
public static SWTBotTreeItem getTreeItem(final SWTBotView viewBot, final String... paths) {
final SWTBotTree tree = viewBot.bot().tree();
return getTreeItem(tree.getAllItems(), paths);
}
/**
*
* @param parentTreeItem
* the parent tree item from which to start
* @param paths
* the relative path to the item to return
* @return the {@link SWTBotTreeItem} that matches the given path from the
* given parent tree item
*/
public static SWTBotTreeItem getTreeItem(final SWTBotTreeItem parentTreeItem, final String... paths) {
if (paths.length == 1) {
return getTreeItem(parentTreeItem, paths[0]);
}
final String[] remainingPaths = new String[paths.length - 1];
System.arraycopy(paths, 1, remainingPaths, 0, paths.length - 1);
return getTreeItem(getTreeItem(parentTreeItem, paths[0]), remainingPaths);
}
/**
* Returns the first child node in the given parent tree item whose text
* matches (ie, begins with) the given path argument.
*
* @param parentTree
* the parent tree item
* @param path
* the text of the node that should match
* @return the first matching node or <code>null</code> if none could be
* found
*/
public static SWTBotTreeItem getTreeItem(final SWTBotTree parentTree, final String path) {
for (SWTBotTreeItem child : parentTree.getAllItems()) {
if (child.getText().startsWith(path)) {
return child;
}
}
return null;
}
/**
* Returns the first child node in the given parent tree item whose text
* matches (ie, begins with) the given path argument.
*
* @param parentTreeItem
* the parent tree item
* @param path
* the text of the node that should match
* @return the first matching node or <code>null</code> if none could be
* found
*/
public static SWTBotTreeItem getTreeItem(final SWTBotTreeItem parentTreeItem, final String path) {
for (SWTBotTreeItem child : parentTreeItem.getItems()) {
if (child.getText().startsWith(path)) {
return child;
}
}
return null;
}
private static SWTBotTreeItem getTreeItem(final SWTBotTreeItem[] treeItems, final String[] paths) {
final SWTBotTreeItem swtBotTreeItem = Stream.of(treeItems).filter(item -> item.getText().startsWith(paths[0]))
.findFirst().orElseThrow(() -> new RuntimeException("Only available items: "
+ Stream.of(treeItems).map(item -> item.getText()).collect(Collectors.joining(", "))));
if (paths.length > 1) {
syncExec(() -> swtBotTreeItem.expand());
final String[] remainingPath = new String[paths.length - 1];
System.arraycopy(paths, 1, remainingPath, 0, remainingPath.length);
return getTreeItem(swtBotTreeItem.getItems(), remainingPath);
}
return swtBotTreeItem;
}
public static SWTBotTableItem getListItem(final SWTBotTable table, final String name) {
return Stream.iterate(0, i -> i + 1).limit(table.rowCount()).map(rowNumber -> table.getTableItem(rowNumber))
.filter(rowItem -> {
return Stream.iterate(0, j -> j + 1).limit(table.columnCount())
.map(colNum -> rowItem.getText(colNum)).anyMatch(colValue -> colValue.contains(name));
}).findFirst().orElse(null);
}
/**
* Waits for the given duration
*
* @param duration
* the duration
* @param unit
* the duration unit
*/
public static void wait(final int duration, final TimeUnit unit) {
try {
Thread.sleep(unit.toMillis(duration));
} catch (InterruptedException e) {
fail("Failed to wait for a " + unit.toMillis(duration) + "ms", e);
}
}
/**
* Selects <strong> all child items</strong> in the given
* <code>parentTreeItem</code> whose labels match the given
* <code>items</code>.
*
* @param parentTreeItem
* the parent tree item
* @param matchItems
* the items to select
* @return
*/
public static SWTBotTreeItem select(final SWTBotTreeItem parentTreeItem, final String... matchItems) {
final List<String> fullyQualifiedItems = Stream.of(parentTreeItem.getItems()).filter(
treeItem -> Stream.of(matchItems).anyMatch(matchItem -> treeItem.getText().startsWith(matchItem)))
.map(item -> item.getText()).collect(Collectors.toList());
return parentTreeItem.select(fullyQualifiedItems.toArray(new String[0]));
}
/**
* Selects <strong> all child items</strong> in the given
* <code>parentTreeItem</code> whose labels match the given
* <code>items</code>.
*
* @param parentTree
* the parent tree
* @param matchItems
* the items to select
* @return
*/
public static SWTBotTree select(final SWTBotTree parentTree, final String... matchItems) {
final List<String> fullyQualifiedItems = Stream.of(parentTree.getAllItems()).filter(
treeItem -> Stream.of(matchItems).anyMatch(matchItem -> treeItem.getText().startsWith(matchItem)))
.map(item -> item.getText()).collect(Collectors.toList());
return parentTree.select(fullyQualifiedItems.toArray(new String[0]));
}
/**
* Selects the given <code>treeItem</code> whose labels match the given
* <code>items</code>.
*
* @param treeItem
* the parent tree item
* @param matchItems
* the items to select
*/
public static void select(SWTBotTreeItem treeItem) {
treeItem.select();
}
/**
* @param tree
* the root {@link SWTBotTree}
* @param path
* the path for the menu
* @return the child {@link SWTBotMenu} named with the first item in the
* given <code>path</code> from the given {@link SWTBotTree}
*/
public static SWTBotMenu getContextMenu(final SWTBotTree tree, String... path) {
final SWTBotMenu contextMenu = tree.contextMenu(path[0]);
if (contextMenu == null) {
Assert.fail("Failed to find context menu '" + path[0] + "'.");
}
if (path.length == 1) {
return contextMenu;
}
final String[] remainingPath = new String[path.length - 1];
System.arraycopy(path, 1, remainingPath, 0, remainingPath.length);
return getSubMenu(contextMenu, remainingPath);
}
/**
* Hides the menu for the given <code>tree</code>
*
* @param tree
* the tree whose {@link Menu} should be hidden
*/
public static void hideMenu(final SWTBotTree tree) {
try {
final Menu menu = UIThreadRunnable.syncExec((Result<Menu>) () -> tree.widget.getMenu());
UIThreadRunnable.syncExec(new VoidResult() {
@Override
public void run() {
hide(menu);
}
private void hide(final Menu menu) {
menu.notifyListeners(SWT.Hide, new Event());
if (menu.getParentMenu() != null) {
hide(menu.getParentMenu());
}
}
});
} catch (WidgetNotFoundException e) {
// ignore if widget is not found, that's probably because there's no
// tree in the
// Docker Explorer view for the test that just ran.
}
}
/**
* @param menu
* the parent menu
* @param path
* the path for the menu
* @return the child {@link SWTBotMenu} named with the first item in the
* given <code>path</code> from the given {@link SWTBotMenu}
*/
public static SWTBotMenu getSubMenu(final SWTBotMenu menu, String... path) {
final SWTBotMenu subMenu = menu.menu(path[0]);
if (subMenu == null) {
Assert.fail("Failed to find submenu '" + path[0] + "'.");
}
if (path.length == 1) {
return subMenu;
}
final String[] remainingPath = new String[path.length - 1];
System.arraycopy(path, 1, remainingPath, 0, remainingPath.length);
return getSubMenu(subMenu, remainingPath);
}
public static SWTBotTreeItem expand(final SWTBotTree tree, final String... paths) {
final SWTBotTreeItem rootItem = getTreeItem(tree, paths[0]);
expandTreeItem(rootItem);
if (paths.length > 1) {
final String[] remainingPath = new String[paths.length - 1];
System.arraycopy(paths, 1, remainingPath, 0, remainingPath.length);
return expand(rootItem, remainingPath);
}
return rootItem;
}
public static SWTBotTreeItem expand(final SWTBotTreeItem treeItem, final String... paths) {
final SWTBotTreeItem childItem = getTreeItem(treeItem, paths[0]);
expandTreeItem(childItem);
if (paths.length > 1) {
final String[] remainingPath = new String[paths.length - 1];
System.arraycopy(paths, 1, remainingPath, 0, remainingPath.length);
return expand(childItem, remainingPath);
}
return getTreeItem(treeItem, paths[0]);
}
private static SWTBotTreeItem expandTreeItem(final SWTBotTreeItem treeItem) {
final UIJob expandJob = new UIJob("expanding tree") {
@Override
public IStatus runInUIThread(IProgressMonitor monitor) {
treeItem.expand();
return Status.OK_STATUS;
}
};
expandJob.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
final int maxAttempts = 30;
int currentAttempt = 0;
while (currentAttempt < maxAttempts && treeItem.getItems().length == 1
&& treeItem.getItems()[0].getText().isEmpty()) {
SWTUtils.wait(1, TimeUnit.SECONDS);
currentAttempt++;
}
}
});
expandJob.schedule();
SWTUtils.wait(1, TimeUnit.SECONDS);
return treeItem;
}
public static SWTBotView getSWTBotView(final SWTWorkbenchBot bot, final String viewId) {
return bot.views().stream().filter(v -> v.getViewReference().getId().equals(viewId)).findFirst().orElse(null);
}
@SuppressWarnings("unchecked")
public static <T> T getView(final SWTWorkbenchBot bot, final String viewId) {
return (T) getView(bot, viewId, false);
}
@SuppressWarnings("unchecked")
public static <T> T getView(final SWTWorkbenchBot bot, final String viewId, final boolean restore) {
final SWTBotView viewBot = bot.viewById(viewId);
viewBot.setFocus();
return (T) viewBot.getReference().getView(restore);
}
/**
* @return <code>true</code> if the Console view is visible in the active
* page, <code>false</code> otherwise.
* @throws InterruptedException
*/
public static boolean isConsoleViewVisible(final SWTWorkbenchBot bot) {
return bot.views().stream()
.anyMatch(v -> v.getViewReference().getId().equals(IConsoleConstants.ID_CONSOLE_VIEW));
}
public static SWTBotToolbarButton getConsoleToolbarButtonWithTooltipText(final SWTWorkbenchBot bot,
final String tooltipText) {
return bot.viewById(IConsoleConstants.ID_CONSOLE_VIEW).getToolbarButtons().stream()
.filter(button -> button.getToolTipText().equals(tooltipText)).findFirst().get();
}
public static void closeView(final SWTWorkbenchBot bot, final String viewId) {
bot.views().stream().filter(v -> v.getReference().getId().equals(viewId)).forEach(v -> v.close());
}
/**
* Creates a new {@link SWTBotMenu} from the context. This avoids some
* unexpected "Widget is disposed" errors.
*
* @param bot
* the bot
* @param menuName
* the name of the menu to find
* @return the context menu
* @see <a href=
* "https://www.eclipse.org/forums/index.php?t=msg&th=11863&start=0&">Eclipse
* forum</a>
*/
public static SWTBotMenu getContextMenu(final AbstractSWTBot<? extends Control> bot, final String menuName) {
return new SWTBotMenu(ContextMenuHelper.contextMenu(bot, menuName));
}
}

View file

@ -1,29 +0,0 @@
/*******************************************************************************
* Copyright (c) 2015 Red Hat.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat - Initial Contribution
*******************************************************************************/
package org.eclipse.cdt.internal.meson.ui.tests.utils;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
/**
*
*/
public class TestLoggerRule extends TestWatcher {
@Override
protected void starting(final Description description) {
System.out.println("Starting " + description.getClassName() + "." + description.getMethodName());
}
}