[527676] Add test for double-click tool dynamic availability

When defining a double click tool on a VSM, it is now immediately
available on diagram in the same workspace using this VSM.

The actual fix was made in 5f0b59fa8b6d88e0c862bd676c1ae19c378768f6 as
a side-effect (probably unintended) of work for bug #527109, which
modifies DoubleClickEditPolicyProvider.java. This simply adds the
corresponding non-regression test.

Bug: 527676
Change-Id: I2bca354aed67affb51db4351d50d25db8db836a1
Signed-off-by: pguilet <pierre.guilet@obeo.fr>
diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/graphical/edit/policies/DoubleClickEditPolicyProvider.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/graphical/edit/policies/DoubleClickEditPolicyProvider.java
index 4fd9771..bff3fbd 100644
--- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/graphical/edit/policies/DoubleClickEditPolicyProvider.java
+++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/graphical/edit/policies/DoubleClickEditPolicyProvider.java
@@ -44,11 +44,6 @@
         this.listeners = new ArrayList<IProviderChangeListener>();
     }
 
-    /**
-     * {@inheritDoc}
-     * 
-     * @see org.eclipse.gmf.runtime.diagram.ui.services.editpolicy.IEditPolicyProvider#createEditPolicies(org.eclipse.gef.EditPart)
-     */
     @Override
     public void createEditPolicies(EditPart editPart) {
         if (editPart instanceof IAbstractDiagramNodeEditPart || editPart instanceof IDiagramEdgeEditPart || editPart instanceof IDiagramNameEditPart) {
@@ -56,21 +51,11 @@
         }
     }
 
-    /**
-     * {@inheritDoc}
-     * 
-     * @see org.eclipse.gmf.runtime.common.core.service.IProvider#addProviderChangeListener(org.eclipse.gmf.runtime.common.core.service.IProviderChangeListener)
-     */
     @Override
     public void addProviderChangeListener(IProviderChangeListener listener) {
         this.listeners.add(listener);
     }
 
-    /**
-     * {@inheritDoc}
-     * 
-     * @see org.eclipse.gmf.runtime.common.core.service.IProvider#provides(org.eclipse.gmf.runtime.common.core.service.IOperation)
-     */
     @Override
     public boolean provides(IOperation operation) {
         if (operation instanceof CreateEditPoliciesOperation) {
@@ -87,11 +72,6 @@
         return false;
     }
 
-    /**
-     * {@inheritDoc}
-     * 
-     * @see org.eclipse.gmf.runtime.common.core.service.IProvider#removeProviderChangeListener(org.eclipse.gmf.runtime.common.core.service.IProviderChangeListener)
-     */
     @Override
     public void removeProviderChangeListener(IProviderChangeListener listener) {
         this.listeners.remove(listener);
diff --git a/plugins/org.eclipse.sirius.tests.swtbot/src/org/eclipse/sirius/tests/swtbot/DoubleClickToolNavigationOperationTest.java b/plugins/org.eclipse.sirius.tests.swtbot/src/org/eclipse/sirius/tests/swtbot/DoubleClickToolNavigationOperationTest.java
index a7e7564..0264e86 100644
--- a/plugins/org.eclipse.sirius.tests.swtbot/src/org/eclipse/sirius/tests/swtbot/DoubleClickToolNavigationOperationTest.java
+++ b/plugins/org.eclipse.sirius.tests.swtbot/src/org/eclipse/sirius/tests/swtbot/DoubleClickToolNavigationOperationTest.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010, 2014 THALES GLOBAL SERVICES.
+ * Copyright (c) 2010, 2017 THALES GLOBAL SERVICES.
  * 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
@@ -12,8 +12,19 @@
 
 import static org.hamcrest.MatcherAssert.assertThat;
 
+import org.eclipse.emf.edit.command.AbstractOverrideableCommand;
+import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
 import org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderedShapeEditPart;
 import org.eclipse.sirius.diagram.DDiagram;
+import org.eclipse.sirius.diagram.business.internal.metamodel.description.spec.LayerSpec;
+import org.eclipse.sirius.diagram.business.internal.metamodel.description.tool.spec.ToolSectionSpec;
+import org.eclipse.sirius.diagram.description.DiagramElementMapping;
+import org.eclipse.sirius.diagram.description.tool.DoubleClickDescription;
+import org.eclipse.sirius.diagram.description.tool.ElementDoubleClickVariable;
+import org.eclipse.sirius.diagram.description.tool.ToolFactory;
+import org.eclipse.sirius.editor.tools.internal.presentation.CustomSiriusEditor;
+import org.eclipse.sirius.tests.support.api.ICondition;
+import org.eclipse.sirius.tests.support.api.TestsUtil;
 import org.eclipse.sirius.tests.swtbot.support.api.AbstractSiriusSwtBotGefTestCase;
 import org.eclipse.sirius.tests.swtbot.support.api.business.UIDiagramRepresentation;
 import org.eclipse.sirius.tests.swtbot.support.api.business.UILocalSession;
@@ -21,9 +32,17 @@
 import org.eclipse.sirius.tests.swtbot.support.api.editor.SWTBotSiriusDiagramEditor;
 import org.eclipse.sirius.tests.swtbot.support.api.editor.SWTBotSiriusHelper;
 import org.eclipse.sirius.ui.business.api.preferences.SiriusUIPreferencesKeys;
+import org.eclipse.sirius.viewpoint.description.tool.ChangeContext;
+import org.eclipse.sirius.viewpoint.description.tool.InitialOperation;
+import org.eclipse.sirius.viewpoint.description.tool.SetValue;
 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
+import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
+import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
+import org.eclipse.swtbot.eclipse.gef.finder.widgets.SWTBotGefEditPart;
+import org.eclipse.swtbot.swt.finder.SWTBot;
 import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
 import org.eclipse.swtbot.swt.finder.widgets.TimeoutException;
+import org.eclipse.ui.PlatformUI;
 import org.hamcrest.Matchers;
 
 /**
@@ -58,6 +77,7 @@
         /**
          * {@inheritDoc}
          */
+        @Override
         public boolean test() throws Exception {
             int newNumberOfEditors = bot.editors().size();
             return initialNumberOfEditors < newNumberOfEditors;
@@ -66,11 +86,37 @@
         /**
          * {@inheritDoc}
          */
+        @Override
         public String getFailureMessage() {
             return null;
         }
     }
 
+    /**
+     * Condition testing that the label of the class edit part has been upodated after the double click.
+     * 
+     * @author <a href=mailto:pierre.guilet@obeo.fr>Pierre Guilet</a>
+     *
+     */
+    private final class DoubleClickChangeCondition implements ICondition {
+
+        private SWTBotSiriusDiagramEditor diagramEditor;
+
+        public DoubleClickChangeCondition(SWTBotSiriusDiagramEditor diagramEditor) {
+            this.diagramEditor = diagramEditor;
+        }
+
+        @Override
+        public boolean test() throws Exception {
+            SWTBotGefEditPart editPart = diagramEditor.getEditPart("test");
+            return editPart != null;
+        }
+
+        @Override
+        public String getFailureMessage() {
+            return "Double click tool has not been taken in consideration. No change has been applied after double click.";
+        }
+    }
     private static final String REPRESENTATION_INSTANCE_NAME_R1_Root = "TC1054 representation 1 root";
 
     private static final String REPRESENTATION_INSTANCE_NAME_R1_SP2 = "TC1054 representation 1 sp2";
@@ -103,6 +149,12 @@
      */
     protected UIDiagramRepresentation diagram;
 
+    private AdapterFactoryEditingDomain editingDomain;
+
+    private LayerSpec layer;
+
+    private ToolSectionSpec toolDescription;
+
     /**
      * {@inheritDoc}
      */
@@ -118,7 +170,6 @@
     protected void onSetUpAfterOpeningDesignerPerspective() throws Exception {
         sessionAirdResource = new UIResource(designerProject, FILE_DIR, SESSION_FILE);
         localSession = designerPerspective.openSessionFromFile(sessionAirdResource);
-
         editor = (SWTBotSiriusDiagramEditor) openRepresentation(localSession.getOpenedSession(), REPRESENTATION_NAME, REPRESENTATION_INSTANCE_NAME_R1_Root, DDiagram.class);
     }
 
@@ -215,6 +266,75 @@
     }
 
     /**
+     * Tests that a double click tool created in a workspace VSM is immediately available in a representation using this
+     * VSM in the same workspace.
+     */
+    public void testDoubleClickToolApplicationInWorkspaceContext() {
+        SWTBotView projectExplorer = bot.viewByTitle("Model Explorer");
+        projectExplorer.setFocus();
+        SWTBot projectExplorerBot = projectExplorer.bot();
+        projectExplorerBot.tree().expandNode(getProjectName()).expandNode(VSM_FILE).doubleClick();
+        CustomSiriusEditor customSiriusEditor = (CustomSiriusEditor) PlatformUI.getWorkbench().getWorkbenchWindows()[0].getActivePage().getActiveEditor();
+        editingDomain = (AdapterFactoryEditingDomain) customSiriusEditor.getEditingDomain();
+        layer = (LayerSpec) editingDomain.getResourceSet().getResources().get(0).getContents().get(0).eContents().get(0).eContents().get(0).eContents().get(0);
+        toolDescription = (ToolSectionSpec) layer.eContents().get(1);
+
+        editingDomain.getCommandStack().execute(new AbstractOverrideableCommand(editingDomain) {
+
+            @Override
+            public void doUndo() {
+            }
+
+            @Override
+            public void doRedo() {
+            }
+
+            @Override
+            public boolean doCanExecute() {
+                return true;
+            }
+
+            @Override
+            public void doExecute() {
+                DoubleClickDescription newDoubleClickDescription = ToolFactory.eINSTANCE.createDoubleClickDescription();
+                newDoubleClickDescription.setName("test");
+                ElementDoubleClickVariable newElementDoubleClickVariable = ToolFactory.eINSTANCE.createElementDoubleClickVariable();
+                newElementDoubleClickVariable.setName("element");
+                newDoubleClickDescription.setElement(newElementDoubleClickVariable);
+                newElementDoubleClickVariable = ToolFactory.eINSTANCE.createElementDoubleClickVariable();
+                newElementDoubleClickVariable.setName("elementView");
+                newDoubleClickDescription.setElementView(newElementDoubleClickVariable);
+
+                InitialOperation initialOperation = newDoubleClickDescription.getInitialOperation();
+                ChangeContext newChangeContext = org.eclipse.sirius.viewpoint.description.tool.ToolFactory.eINSTANCE.createChangeContext();
+                newChangeContext.setBrowseExpression("var:element");
+                initialOperation.setFirstModelOperations(newChangeContext);
+
+                SetValue newSetValue = org.eclipse.sirius.viewpoint.description.tool.ToolFactory.eINSTANCE.createSetValue();
+                newSetValue.setFeatureName("name");
+                newSetValue.setValueExpression("aql:'test'");
+                newChangeContext.getSubModelOperations().add(newSetValue);
+
+                DiagramElementMapping mapping = (DiagramElementMapping) layer.eContents().get(0).eContents().get(0);
+                newDoubleClickDescription.getMappings().add(mapping);
+
+                toolDescription.getOwnedTools().add(newDoubleClickDescription);
+
+            }
+        });
+
+        SWTBotEditor customSiriusEditorBot = bot.editorByTitle(VSM_FILE);
+        customSiriusEditorBot.save();
+
+        editor.show();
+        editor.doubleClick("Sous-package1");
+
+
+        ICondition doubleClickChangeCondition = new DoubleClickChangeCondition(editor);
+        TestsUtil.waitUntil(doubleClickChangeCondition);
+    }
+
+    /**
      * {@inheritDoc}
      */
     @Override
@@ -222,6 +342,10 @@
         sessionAirdResource = null;
         localSession.save();
         localSession = null;
+        sessionAirdResource = null;
+        editingDomain = null;
+        toolDescription = null;
+        layer = null;
         super.tearDown();
     }
 }