Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d0b0ad4c6758a3d1a696d892d63e3a372cac84e3 (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
/*****************************************************************************
 * Copyright (c) 2014 CEA LIST and others.
 * 
 * All rights reserved. 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:
 *   CEA LIST - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.junit.utils.rules;

import static org.junit.Assert.fail;

import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;


/**
 * A rule that hides a view for the duration of a test.
 */
public class HideViewRule extends TestWatcher {

	private final String viewID;

	private IWorkbenchPage page;

	public HideViewRule(String viewID) {
		super();

		this.viewID = viewID;
	}

	@Override
	protected void starting(Description description) {
		IWorkbench bench = PlatformUI.getWorkbench();
		IWorkbenchWindow window = bench.getActiveWorkbenchWindow();
		if(window == null) {
			window = bench.getWorkbenchWindows()[0];
		}

		IWorkbenchPage page = window.getActivePage();
		IViewPart viewPart = page.findView(viewID);

		if(viewPart != null) {
			this.page = page;
			page.hideView(viewPart);
		}
	}

	@Override
	protected void finished(Description description) {
		if(page != null) {
			try {
				page.showView(viewID);
			} catch (PartInitException e) {
				fail(String.format("Failed to restore view %s: %s", viewID, e.getLocalizedMessage()));
			}
		}

		page = null;
	}
}

Back to the top