Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5ef068e73fdb288c7b255724bd3ba371a75b2693 (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
/*****************************************************************************
 * Copyright (c) 2012, 2016 CEA LIST, 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:
 *   CEA LIST - Initial API and implementation
 *   Christian W. Damus - bug 485220
 *   
 *****************************************************************************/
package org.eclipse.papyrus.infra.services.edit.tests;

import static org.junit.Assert.fail;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.transaction.RunnableWithResult;
import org.eclipse.gmf.runtime.emf.type.core.ClientContextManager;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IClientContext;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.MetamodelType;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditServiceProvider;
import org.eclipse.papyrus.infra.services.edit.tests.edit.helper.EPackageEditHelper;
import org.eclipse.papyrus.junit.framework.classification.tests.AbstractPapyrusTest;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

/**
 * Abstract Papyrus initialization class (required to get Service activation).
 */
public abstract class AbstractTestElementEditService extends AbstractPapyrusTest {

	private static Bundle testBundle;

	protected static final String PAPYRUS_CONTEXT_ID = "org.eclipse.papyrus.infra.services.edit.TypeContext"; //$NON-NLS-1$

	protected IElementType eClassType;

	protected IElementType ePackgType;

	protected IElementEditServiceProvider service;

	protected static IEditorPart editor;

	@BeforeClass
	public static void openPapyrusWithAnEmptyProject() throws Exception {
		// Prepare new project for tests
		IProject testProject = ResourcesPlugin.getWorkspace().getRoot().getProject("TestProject");
		if (!testProject.exists()) {
			testProject.create(new NullProgressMonitor());
		}
		if (!testProject.isOpen()) {
			testProject.open(new NullProgressMonitor());
		}

		// Copy EmptyModel from bundle to the test project
		final IFile emptyModel_di = testProject.getFile("EmptyModel.di");
		IFile emptyModel_no = testProject.getFile("EmptyModel.notation");
		IFile emptyModel_uml = testProject.getFile("EmptyModel.uml");

		emptyModel_di.create(getTestBundle().getResource("/model/EmptyModel.di").openStream(), true, new NullProgressMonitor());
		emptyModel_no.create(getTestBundle().getResource("/model/EmptyModel.notation").openStream(), true, new NullProgressMonitor());
		emptyModel_uml.create(getTestBundle().getResource("/model/EmptyModel.uml").openStream(), true, new NullProgressMonitor());

		// Open the EmptyModel.di file with Papyrus (assumed to be the default editor for "di" files here).
		Display.getDefault().syncExec(new Runnable() {

			public void run() {
				IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(emptyModel_di.getName());
				try {
					editor = page.openEditor(new FileEditorInput(emptyModel_di), desc.getId());
				} catch (Exception ex) {
					ex.printStackTrace(System.out);
				}
			}
		});

		Assert.assertNotNull(editor);
	}

	protected static Bundle getTestBundle() {
		if (testBundle == null) {
			testBundle = FrameworkUtil.getBundle(AbstractTestElementEditService.class);
		}
		return testBundle;
	}

	@Before
	public void setUp() {
		// To prepare this test two element types are added to Papyrus Client Context
		ElementTypeRegistry.getInstance().register(new MetamodelType("ECLASS_TEST_ID", null, "ECLASS", EcorePackage.eINSTANCE.getEClass(), null)); //$NON-NLS-1$
		ElementTypeRegistry.getInstance().register(new MetamodelType("EPACKG_TEST_ID", null, "EPACKG", EcorePackage.eINSTANCE.getEPackage(), new EPackageEditHelper())); //$NON-NLS-1$

		eClassType = ElementTypeRegistry.getInstance().getType("ECLASS_TEST_ID");
		ePackgType = ElementTypeRegistry.getInstance().getType("EPACKG_TEST_ID");

		IClientContext context = ClientContextManager.getInstance().getClientContext(PAPYRUS_CONTEXT_ID);
		context.bindId("ECLASS_TEST_ID"); //$NON-NLS-1$
		context.bindId("EPACKG_TEST_ID"); //$NON-NLS-1$

		service = ElementEditServiceUtils.getEditServiceProvider();
	}

	@AfterClass
	public static void closePapyrusAndCleanProject() {
		RunnableWithResult<Boolean> closeEditorsRunnable;
		Display.getDefault().syncExec(closeEditorsRunnable = new RunnableWithResult.Impl<Boolean>() {

			public void run() {
				setResult(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeAllEditors(false));
			}
		});
		editor = null;

		// Close all editors
		Assert.assertTrue("Could not close editors correctly.", closeEditorsRunnable.getResult());

		// Delete test project
		IProject testProject = ResourcesPlugin.getWorkspace().getRoot().getProject("TestProject");
		try {
			testProject.delete(true, new NullProgressMonitor());
		} catch (CoreException e) {
			fail("Could delete test project correctly (" + e.getMessage() + ").");
		}
	}
}

Back to the top