Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4e25daefcf41dca594241121cf442104ddcf8562 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*****************************************************************************
 * Copyright (c) 2014 CEA LIST.
 *
 *    
 * 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:
 *  Benoit Maggi (CEA LIST) benoit.maggi@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.views.modelexplorer.tests;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.facet.util.core.internal.exported.FileUtils;
import org.eclipse.emf.transaction.RunnableWithResult;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.core.editor.CoreMultiDiagramEditor;
import org.eclipse.papyrus.views.modelexplorer.ModelExplorerPage;
import org.eclipse.papyrus.views.modelexplorer.ModelExplorerPageBookView;
import org.eclipse.papyrus.views.modelexplorer.ModelExplorerView;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.internal.handlers.HandlerProxy;
import org.eclipse.ui.part.IPage;
import org.junit.Assert;
import org.junit.Before;
import org.osgi.framework.Bundle;

/**
 * Just duplicate the mecanism to handle scenario with 2 handlers
 */
public abstract class AbstractDualHandlerTest extends AbstractHandlerTest{


	/** The second tested command. */
	protected Command secondTestedCommand;
	
	/** The second command id. */
	protected final String secondCommandId;	
	
	/**
	 * Instantiates a new abstract dual handler test.
	 *
	 * @param commandId the command id
	 * @param secondCommandId the second command id
	 * @param bundle the bundle
	 */
	public AbstractDualHandlerTest(String commandId, String secondCommandId, Bundle bundle) {
		super(commandId, bundle);
		this.secondCommandId = secondCommandId;
	}

	
	/**
	 * Gets the second active handler.
	 *
	 * @return the second active handler
	 */
	protected IHandler getSecondActiveHandler() {
		IHandler currentHandler = secondTestedCommand.getHandler();
		if(currentHandler instanceof HandlerProxy) {
			currentHandler = ((HandlerProxy)currentHandler).getHandler();
		}
		return currentHandler;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.papyrus.views.modelexplorer.tests.AbstractHandlerTest#initTests()
	 */
	@Before
	@Override
	public void initTests() throws CoreException, IOException {
		// we clean the workspace and create a new project to test the handlers
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		for(IProject project : workspace.getRoot().getProjects()) {
			project.delete(true, new NullProgressMonitor());
		}
		IProject testProject = workspace.getRoot().getProject(AbstractHandlerTest.PROJECT_NAME);
		testProject.create(new NullProgressMonitor());
		testProject.open(new NullProgressMonitor());

		// we copy the file of the tested model in the new project
		FileUtils.copyFileFromBundle("/resources/" + AbstractHandlerTest.FILE_NAME + AbstractHandlerTest.EXTENSION_UML, //$NON-NLS-1$
			testProject, '/' + AbstractHandlerTest.FILE_NAME + AbstractHandlerTest.EXTENSION_UML, bundle);
		FileUtils.copyFileFromBundle("/resources/" + AbstractHandlerTest.FILE_NAME + AbstractHandlerTest.EXTENSION_NOTATION, //$NON-NLS-1$
			testProject, '/' + AbstractHandlerTest.FILE_NAME + AbstractHandlerTest.EXTENSION_NOTATION, bundle);
		FileUtils.copyFileFromBundle("/resources/" + AbstractHandlerTest.FILE_NAME + AbstractHandlerTest.EXTENSION_DI, //$NON-NLS-1$
			testProject, '/' + AbstractHandlerTest.FILE_NAME + AbstractHandlerTest.EXTENSION_DI, bundle);
		IFile file = testProject.getFile(AbstractHandlerTest.FILE_NAME + AbstractHandlerTest.EXTENSION_DI);

		// we open the editor
		editor = (CoreMultiDiagramEditor)openEditor(file);

		RunnableWithResult runnable;

		Display.getDefault().syncExec(runnable = new RunnableWithResult.Impl() {

			public void run() {
				IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();

				// we look for the modelexplorer
				IViewPart modelexplorer;
				try {
					modelexplorer = activeWorkbenchWindow.getActivePage().showView(AbstractHandlerTest.viewId);
				} catch (PartInitException ex) {
					ex.printStackTrace(System.out);
					setStatus(new Status(IStatus.ERROR, bundle.getSymbolicName(), ex.getMessage()));
					return;
				}
				ModelExplorerPageBookView view = (ModelExplorerPageBookView)modelexplorer;
				IPage currentPage = view.getCurrentPage();
				ModelExplorerPage page = (ModelExplorerPage)currentPage;
				IViewPart viewer = page.getViewer();
				modelExplorerView = (ModelExplorerView)viewer;
				modelExplorerView.setFocus();

				// we look for the common viewer
				commonViewer = modelExplorerView.getCommonViewer();

				// we look for the selection service
				selectionService = activeWorkbenchWindow.getSelectionService();

				// we look for the testedCommand
				ICommandService commandService = (ICommandService)activeWorkbenchWindow.getService(ICommandService.class);
				if(commandId != null) {
					testedCommand = commandService.getCommand(commandId);
				}
				if(commandId != null) {
					secondTestedCommand = commandService.getCommand(secondCommandId);
				}
				
				commonViewer.expandToLevel(2);

				// store the root of the model
				Object[] visibleElement = commonViewer.getVisibleExpandedElements();
				if(visibleElement[0] instanceof IAdaptable) {
					modelRoot = (EObject)((IAdaptable)visibleElement[0]).getAdapter(EObject.class);
				}

				setStatus(Status.OK_STATUS);
			}
		});

		Assert.assertEquals(runnable.getStatus().getMessage(), IStatus.OK, runnable.getStatus().getSeverity());

		while(modelRoot.eContainer() != null) {
			modelRoot = modelRoot.eContainer();
		}
	}
	
	/**
	 * This method selects many elements in the modelexplorer and test that the new
	 * selection is the wanted selection using assertion
	 * 
	 * @param elementToSelect
	 *        the list element to select
	 */
	protected void selectListElementInTheModelexplorer(List<EObject> listElementToSelect) {
		final List<EObject> selectedElement = new ArrayList<EObject>();
		selectedElement.addAll(listElementToSelect);
		Display.getDefault().syncExec(new Runnable() {

			public void run() {
				modelExplorerView.revealSemanticElement(selectedElement);
			}
		});

		IStructuredSelection currentSelection = (IStructuredSelection)selectionService.getSelection();
		Assert.assertEquals("Only one element should be selected", listElementToSelect.size(), currentSelection.size()); //$NON-NLS-1$
	}
	
	
	
}

Back to the top