| /***************************************************************************** |
| * Copyright (c) 2021 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 |
| * http://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation |
| * |
| *****************************************************************************/ |
| |
| package org.eclipse.papyrus.model2doc.integration.ieee.requirements.sysml16.odt.architecture.tests; |
| |
| import java.util.Collections; |
| |
| import org.eclipse.core.commands.Command; |
| import org.eclipse.core.commands.ExecutionEvent; |
| import org.eclipse.core.commands.ExecutionException; |
| import org.eclipse.core.commands.NotEnabledException; |
| import org.eclipse.core.commands.NotHandledException; |
| import org.eclipse.core.commands.common.NotDefinedException; |
| import org.eclipse.jface.viewers.StructuredSelection; |
| import org.eclipse.osgi.util.NLS; |
| import org.eclipse.papyrus.junit.utils.rules.PapyrusEditorFixture; |
| import org.eclipse.papyrus.junit.utils.rules.PluginResource; |
| import org.eclipse.papyrus.model2doc.integration.ieee.requirements.sysml16.odt.architecture.utils.IEEEConstants; |
| import org.eclipse.papyrus.views.modelexplorer.ModelExplorerView; |
| import org.eclipse.swt.widgets.Display; |
| import org.eclipse.ui.IWorkbench; |
| import org.eclipse.ui.IWorkbenchWindow; |
| import org.eclipse.ui.PlatformUI; |
| import org.eclipse.ui.commands.ICommandService; |
| import org.eclipse.uml2.uml.Package; |
| import org.junit.Assert; |
| import org.junit.Rule; |
| import org.junit.Test; |
| |
| /** |
| * This test checks the status of the Eclipse command used to create the required IEEE Package Structure in the model |
| */ |
| @SuppressWarnings("nls") |
| @PluginResource("resources/IEEEPackagesCreationCommand/IEEEPackagesCreationCommand.di") |
| public class IEEEPackagesCreationCommandTest { |
| |
| private static final String TESTED_COMMAND = IEEEConstants.CREATE_IEEE_STRUCTURE_COMMAND_ID; |
| |
| @Rule |
| public final PapyrusEditorFixture fixture = new PapyrusEditorFixture(); |
| |
| /** |
| * This method checks the status of the command used to create the required {@link Package} |
| * |
| * @throws ExecutionException |
| * @throws NotDefinedException |
| * @throws NotEnabledException |
| * @throws NotHandledException |
| */ |
| @Test |
| public void checkStatusOfCommand() throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException { |
| final Package pack = fixture.getModel(); |
| final ModelExplorerView view = fixture.getModelExplorerView(); |
| view.revealSemanticElement(Collections.singletonList(pack)); |
| flushEventLoop(); |
| |
| final ICommandService service = getEclipseCommandService(); |
| Assert.assertNotNull("The Eclipse ICommandService can't be found", service); |
| final Command cmd = service.getCommand(TESTED_COMMAND); |
| Assert.assertNotNull(NLS.bind("The Eclipse command {0} can't be found", TESTED_COMMAND), service); |
| Assert.assertTrue(cmd.isEnabled()); |
| Assert.assertTrue(cmd.isHandled()); |
| |
| cmd.executeWithChecks(new ExecutionEvent()); |
| view.selectReveal(new StructuredSelection(pack)); |
| |
| Assert.assertTrue("The command must not be eanbled after it first execution", !cmd.isEnabled()); |
| } |
| |
| /** |
| * |
| * @return |
| * the {@link ICommandService} from Eclipse |
| */ |
| private ICommandService getEclipseCommandService() { |
| final IWorkbench workbench = PlatformUI.getWorkbench(); |
| if (workbench != null && workbench.getActiveWorkbenchWindow() != null) { |
| IWorkbenchWindow activeWorkbenchWindows = workbench.getActiveWorkbenchWindow(); |
| if (activeWorkbenchWindows != null) { |
| final ICommandService service = activeWorkbenchWindows.getService(ICommandService.class); |
| return service; |
| } |
| } |
| return null; |
| |
| } |
| |
| /** |
| * Processes all events waiting in the Display's event loop and then returns. |
| */ |
| private static final void flushEventLoop() { |
| final Display display = Display.getDefault(); |
| display.syncExec(new Runnable() { |
| |
| @Override |
| public void run() { |
| try { |
| while (display.readAndDispatch()) { |
| // nothing |
| } |
| } catch (Exception ex) { |
| Activator.log.error("Exceptions occured during the flueshEventLoop method", ex); |
| } |
| } |
| }); |
| } |
| |
| |
| |
| } |