diff options
Diffstat (limited to 'tests/junit/framework/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/HideViewRule.java')
-rw-r--r-- | tests/junit/framework/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/HideViewRule.java | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/junit/framework/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/HideViewRule.java b/tests/junit/framework/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/HideViewRule.java new file mode 100644 index 00000000000..66830a5159b --- /dev/null +++ b/tests/junit/framework/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/HideViewRule.java @@ -0,0 +1,72 @@ +/***************************************************************************** + * 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 v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * 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; + } +} |