Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 60036cf148d01b5ba00a7a02a2a73c15a8fbff38 (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
/*****************************************************************************
 * Copyright (c) 2015 Christian W. Damus and others.
 * 
 * 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.infra.editor.welcome.tests;

import static org.junit.Assert.fail;

import java.util.Optional;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.eclipse.papyrus.infra.core.sasheditor.editor.IComponentPage;
import org.eclipse.papyrus.infra.core.sasheditor.editor.IEditorPage;
import org.eclipse.papyrus.infra.core.sasheditor.editor.IPage;
import org.eclipse.papyrus.infra.core.sasheditor.editor.IPageVisitor;
import org.eclipse.papyrus.infra.core.sasheditor.editor.ISashWindowsContainer;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.editor.welcome.IWelcomePageService;
import org.eclipse.papyrus.infra.editor.welcome.Welcome;
import org.eclipse.papyrus.infra.editor.welcome.internal.WelcomePage;
import org.eclipse.papyrus.infra.tools.util.PlatformHelper;
import org.eclipse.papyrus.junit.framework.classification.tests.AbstractPapyrusTest;
import org.eclipse.papyrus.junit.utils.DisplayUtils;
import org.eclipse.papyrus.junit.utils.rules.PapyrusEditorFixture;
import org.eclipse.papyrus.views.properties.runtime.ConfigurationManager;
import org.eclipse.swt.widgets.Display;
import org.hamcrest.Matcher;
import org.junit.BeforeClass;
import org.junit.Rule;

/**
 * Abstract test class for common behaviours of welcome-page tests.
 */
public abstract class AbstractWelcomePageTest extends AbstractPapyrusTest {

	@Rule
	public final PapyrusEditorFixture editor = new PapyrusEditorFixture();

	protected AbstractWelcomePageTest() {
		super();
	}

	@BeforeClass
	public static void loadConfigurationManager() throws InterruptedException, ExecutionException {
		// Preëmptively load the configuration manager to avoid deadlocks in
		// diagram bundle loading and preferences initialization
		ExecutorService exec = Executors.newSingleThreadExecutor();
		CompletionService<ConfigurationManager> completion = new ExecutorCompletionService<ConfigurationManager>(exec);

		Future<ConfigurationManager> waitFor = completion.submit(() -> ConfigurationManager.getInstance());

		try {
			// Wait for these bundles to finish activating
			Display.getDefault().syncExec(() -> {
				while (completion.poll() != waitFor) {
					// Process the sync runnables that the diagram bundles are waiting to do during activation
					DisplayUtils.flushEventLoop();
				}
			});

			// Just to be sure that it's done
			waitFor.get();
		} finally {
			exec.shutdown();
		}
	}

	protected IWelcomePageService getService() {
		try {
			return editor.getServiceRegistry().getService(IWelcomePageService.class);
		} catch (ServiceException e) {
			fail("Cannot get welcome-page service: " + e.getMessage());
			return null; // Unreachable
		}
	}

	protected Optional<IWelcomePageService> getServiceOpt() {
		try {
			return Optional.of(editor.getServiceRegistry().getService(IWelcomePageService.class));
		} catch (ServiceException e) {
			fail("Cannot get welcome-page service: " + e.getMessage());
			return Optional.empty(); // Unreachable
		}
	}

	protected WelcomePage getWelcomePage() {
		WelcomePage[] result = { null };

		ISashWindowsContainer sash = PlatformHelper.getAdapter(editor.getEditor(), ISashWindowsContainer.class);
		sash.visit(new IPageVisitor() {

			@Override
			public void accept(IEditorPage page) {
				// Pass
			}

			@Override
			public void accept(IComponentPage page) {
				WelcomePage welcome = PlatformHelper.getAdapter(page, WelcomePage.class);
				if (welcome != null) {
					result[0] = welcome;
				}
			}
		});

		return result[0];
	}

	protected boolean isActivePage(WelcomePage page) {
		IPage activePage = getActivePage();
		return (activePage != null) && (PlatformHelper.getAdapter(activePage, WelcomePage.class) == page);
	}

	protected IPage getActivePage() {
		ISashWindowsContainer sash = PlatformHelper.getAdapter(editor.getEditor(), ISashWindowsContainer.class);
		return sash.getActiveSashWindowsPage();
	}

	protected Optional<Welcome> getWelcomeOpt() {
		return getServiceOpt().map(IWelcomePageService::getWelcome);
	}

	protected Welcome getWelcome() {
		return getWelcomeOpt().orElse(null);
	}

	protected Welcome requireWelcome() {
		return getWelcomeOpt().orElseThrow(AssertionError::new);
	}

	protected Matcher<Object> isPageVisible() {
		ISashWindowsContainer sash = PlatformHelper.getAdapter(editor.getEditor(), ISashWindowsContainer.class);
		return WelcomeMatchers.isPageVisible(sash);
	}
}

Back to the top