Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f6b6c7e1ebbe1bb2ccce2482e89e9ef26a70c56f (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
/*******************************************************************************
 * Copyright (c) 2015, 2018 Red Hat.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which 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.linuxtools.internal.docker.ui.testutils.swt;

import org.eclipse.linuxtools.docker.core.DockerConnectionManager;
import org.eclipse.linuxtools.internal.docker.core.DefaultDockerConnectionSettingsFinder;
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();
			}

			DockerConnectionManager.getInstance()
					.setConnectionSettingsFinder(new DefaultDockerConnectionSettingsFinder());
		} 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);
	}
}

Back to the top