Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3962a44edc6c34ffbb64698360c2eb57ca3a6c14 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * Copyright (c) 2004 - 2011 Eike Stepper (Berlin, Germany) 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:
 *    Martin Fluegge - initial API and implementation
 */
package org.eclipse.emf.cdo.dawn.tests.ui.util;

import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.instanceOf;

import org.eclipse.emf.cdo.dawn.examples.acore.AClass;
import org.eclipse.emf.cdo.dawn.examples.acore.diagram.edit.parts.AClassEditPart;
import org.eclipse.emf.cdo.dawn.gmf.synchronize.DawnConflictHelper;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;

import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
import org.eclipse.gmf.runtime.diagram.core.providers.IViewProvider;
import org.eclipse.gmf.runtime.diagram.core.services.ViewService;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.Bounds;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.RelativeBendpoints;
import org.eclipse.gmf.runtime.notation.datatype.RelativeBendpoint;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.eclipse.gef.finder.widgets.SWTBotGefConnectionEditPart;
import org.eclipse.swtbot.eclipse.gef.finder.widgets.SWTBotGefEditPart;
import org.eclipse.swtbot.eclipse.gef.finder.widgets.SWTBotGefEditor;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.matchers.AbstractMatcher;
import org.eclipse.swtbot.swt.finder.results.WidgetResult;
import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;

import org.hamcrest.Description;
import org.hamcrest.Matcher;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Martin Fluegge
 */
public class DawnSWTBotUtil
{
  private static final String LABEL_OK = "OK";

  private static final String LABEL_OTHERS = "Other...";

  private static final String LABEL_WINDOW = "Window";

  private static final String LABEL_SHOW_VIEW = "Show View";

  public static void initTest(SWTWorkbenchBot bot)
  {
    closeWelcomePage(bot);
  }

  public static void closeWelcomePage(SWTWorkbenchBot bot)
  {
    try
    {
      bot.viewByTitle("Welcome").close();
    }
    catch (WidgetNotFoundException ex)
    {
      // We can ignore this because it it thrown when the widget cannot be found which can be the case if another test
      // already closed the welcome screen.
    }
  }

  public static void setConnectorType(SWTWorkbenchBot bot, String serverName, String serverPort, String repository,
      String protocol)
  {
    bot.menu(LABEL_WINDOW).menu("Preferences").click();
    SWTBotShell shell = bot.shell("Preferences");
    shell.activate();

    bot.tree().select("Dawn Remote Preferences");

    SWTBotText serverNameLabel = bot.textWithLabel("server name:");
    SWTBotText serverPortLabel = bot.textWithLabel("server port:");
    SWTBotText repositoryLabel = bot.textWithLabel("repository:");
    SWTBotText fileNameLabel = bot.textWithLabel("protocol:");

    serverNameLabel.setText(serverName);
    serverPortLabel.setText(serverPort);
    repositoryLabel.setText(repository);
    fileNameLabel.setText(protocol);
    bot.button(LABEL_OK).click();
  }

  public static List<SWTBotGefEditPart> getAllEditParts(SWTBotGefEditor editor)
  {
    List<SWTBotGefEditPart> editParts = editor.editParts(new AbstractMatcher<EditPart>()
    {
      @Override
      protected boolean doMatch(Object item)
      {
        return item instanceof AClassEditPart;
      }

      public void describeTo(Description description)
      {
      }
    });
    return editParts;
  }

  public static List<SWTBotGefEditPart> getConnectionEditParts(SWTBotGefEditor editor, Class<? extends EditPart> clazz)
  {
    List<SWTBotGefEditPart> editParts = getAllEditParts(editor);
    return getConnectionEditParts(editor, clazz, editParts);
  }

  public static List<SWTBotGefEditPart> getConnectionEditParts(SWTBotGefEditor editor, Class<? extends EditPart> clazz,
      List<SWTBotGefEditPart> editParts)
  {

    List<SWTBotGefEditPart> ret = new ArrayList<SWTBotGefEditPart>();
    for (SWTBotGefEditPart editPart : editParts)
    {

      for (SWTBotGefConnectionEditPart sourceConnection : editPart.sourceConnections())
      {
        if (clazz.isInstance(sourceConnection.part()))
        {
          ret.add(sourceConnection);
        }
      }
    }
    return ret;
  }

  static Object monitor = new Object();

  /**
   * returns all ConnectionEditparts from teh given editor
   */
  public static List<SWTBotGefEditPart> getAllConnections(final SWTBotGefEditor editor)
  {
    AbstractMatcher<EditPart> matcher = new AbstractMatcher<EditPart>()
    {
      @Override
      protected boolean doMatch(Object item)
      {
        return true;
      }

      public void describeTo(Description description)
      {
      }
    };
    List<SWTBotGefEditPart> aClassEditParts = editor.editParts(matcher);

    List<SWTBotGefEditPart> ret = new ArrayList<SWTBotGefEditPart>();

    for (SWTBotGefEditPart editPart : aClassEditParts)
    {
      ret.addAll(editPart.sourceConnections());
    }
    return ret;
  }

  public static Node createNewNodeRemote(Diagram diagram, AClass newAClass, String type)
  {
    Node newNode = ViewService.createNode(diagram, newAClass, type, PreferencesHint.USE_DEFAULTS);
    return newNode;
  }

  public static Edge createEdgeRemote(Node source, Node target, String type, IElementType elementType,
      List<RelativeBendpoint> bendpoints, IViewProvider viewProvider)
  {
    Edge edge = viewProvider.createEdge(elementType, source.getDiagram(), type, ViewUtil.APPEND, true,
        PreferencesHint.USE_DEFAULTS);

    edge.setTarget(target);
    edge.setSource(source);

    RelativeBendpoints exitingBendpoints = (RelativeBendpoints)edge.getBendpoints();
    @SuppressWarnings("unchecked")
    List<RelativeBendpoint> points = exitingBendpoints.getPoints();
    List<RelativeBendpoint> newBendPoints = new ArrayList<RelativeBendpoint>(points);
    newBendPoints.addAll(1, bendpoints);
    exitingBendpoints.setPoints(newBendPoints);

    return edge;
  }

  public static void addBendPoint(Edge edge, int sourceX, int sourceY, int targetX, int targetY)
  {
    List<RelativeBendpoint> newBendPoints = new ArrayList<RelativeBendpoint>();
    RelativeBendpoint relativeBendpoint = new RelativeBendpoint(sourceX, sourceY, targetX, targetY);
    newBendPoints.add(relativeBendpoint);
    DawnSWTBotUtil.addBendpoints(edge, newBendPoints);
  }

  public static void addBendpoints(final Edge edge, final List<RelativeBendpoint> bendpoints)
  {
    ResourceSet resourceSet = edge.eResource().getResourceSet();

    if (resourceSet instanceof IEditingDomainProvider)
    {
      EditingDomain editingDomain = ((IEditingDomainProvider)resourceSet).getEditingDomain();

      editingDomain.getCommandStack().execute(new RecordingCommand((TransactionalEditingDomain)editingDomain)
      {
        @Override
        protected void doExecute()
        {
          DawnSWTBotUtil.addBendPointsInternal(edge, bendpoints);
        }
      });
    }
    else
    {
      addBendPointsInternal(edge, bendpoints);
    }
  }

  private static void addBendPointsInternal(final Edge edge, final List<RelativeBendpoint> bendpoints)
  {
    RelativeBendpoints exitingBendpoints = (RelativeBendpoints)edge.getBendpoints();
    @SuppressWarnings("unchecked")
    List<RelativeBendpoint> points = exitingBendpoints.getPoints();
    List<RelativeBendpoint> newBendPoints = new ArrayList<RelativeBendpoint>(points);
    newBendPoints.addAll(1, bendpoints);
    exitingBendpoints.setPoints(newBendPoints);
  }

  public static Object showsConflict(EditPart editPart)
  {
    return DawnConflictHelper.isConflicted((EObject)editPart.getModel());
  }

  public static void moveNodeRemotely(Node nodeA, int x, int y)
  {
    Bounds bounds = (Bounds)nodeA.getLayoutConstraint();
    bounds.setX(x);
    bounds.setY(y);
  }

  public static void resizeNodeRemotely(Node nodeA, int height, int width)
  {
    Bounds bounds = (Bounds)nodeA.getLayoutConstraint();
    bounds.setHeight(height);
    bounds.setWidth(width);
  }

  public static boolean checkNodePosition(Node node, int x, int y)
  {
    boolean ret = true;
    Bounds bounds = (Bounds)node.getLayoutConstraint();
    ret = bounds.getX() == x && ret;
    ret = bounds.getY() == y && ret;

    return ret;
  }

  public static boolean checkNodeSize(Node node, int height, int width)
  {
    boolean ret = true;
    Bounds bounds = (Bounds)node.getLayoutConstraint();
    ret = bounds.getHeight() == height && ret;
    ret = bounds.getWidth() == width && ret;

    return ret;
  }

  public static boolean checkNodePosistionAndSize(Node node, int x, int y, int height, int width)
  {
    boolean ret = true;
    ret = checkNodePosition(node, x, y) && ret;
    ret = checkNodeSize(node, height, width) && ret;

    return ret;
  }

  public static SWTBotView openView(SWTWorkbenchBot bot, String categoryName, String viewName)
  {
    bot.menu(LABEL_WINDOW).menu(LABEL_SHOW_VIEW).menu(LABEL_OTHERS).click();

    SWTBotShell shell = bot.shell(LABEL_SHOW_VIEW);
    shell.activate();
    bot.tree().expandNode(categoryName).select(viewName);
    bot.button(LABEL_OK).click();

    return bot.activeView();
  }

  @SuppressWarnings("unchecked")
  public static SWTBotMenu findContextMenu(final AbstractSWTBot<?> bot, final String... texts)
  {
    final Matcher<?>[] matchers = new Matcher<?>[texts.length];
    for (int i = 0; i < texts.length; i++)
    {
      matchers[i] = allOf(instanceOf(MenuItem.class), withMnemonic(texts[i]));
    }

    final MenuItem menuItem = UIThreadRunnable.syncExec(new WidgetResult<MenuItem>()
    {
      public MenuItem run()
      {
        MenuItem menuItem = null;
        Control control = (Control)bot.widget;
        Menu menu = control.getMenu();
        for (int i = 0; i < matchers.length; i++)
        {
          menuItem = show(menu, matchers[i]);
          if (menuItem != null)
          {
            menu = menuItem.getMenu();
          }
        }

        return menuItem;
      }
    });
    if (menuItem == null)
    {
      throw new WidgetNotFoundException("Could not find menu: " + Arrays.asList(texts));
    }

    return new SWTBotMenu(menuItem);
  }

  private static MenuItem show(final Menu menu, final Matcher<?> matcher)
  {
    if (menu != null)
    {
      menu.notifyListeners(SWT.Show, new Event());
      MenuItem[] items = menu.getItems();
      for (final MenuItem menuItem : items)
      {
        if (matcher.matches(menuItem))
        {
          return menuItem;
        }
      }
      menu.notifyListeners(SWT.Hide, new Event());
    }
    return null;
  }

  public static void setAutomaticBuild(SWTWorkbenchBot bot, boolean enabled)
  {
    SWTBotMenu menu = bot.menu("Project").menu("Build Automatically");
    // if(menu.isEnabled()&&enabled)
    {
      menu.click();
    }
  }

  // public static Edge createEdgeRemote(Node source, Node target, String type)
  // {
  // Edge newEdge = ViewService.createEdge(source, target, type, PreferencesHint.USE_DEFAULTS);
  //
  // return newEdge;
  // }
  //
  // public static Edge createEdgeRemote(Node source, Node target, EObject model, String type)
  // {
  // Edge newEdge = ViewService.createEdge(source, target, model, type, PreferencesHint.USE_DEFAULTS);
  // return newEdge;
  // }
  //
  // public static Edge createEdgeRemote(Node source, Node target, String type, List<RelativeBendpoint> bendPoints)
  // {
  // Edge edge = createEdgeRemote(source, target, type);
  // RelativeBendpoints exitingBendpoints = (RelativeBendpoints)edge.getBendpoints();
  //
  // @SuppressWarnings("unchecked")
  // List<RelativeBendpoint> points = exitingBendpoints.getPoints();
  // List<RelativeBendpoint> newBendPoints = new ArrayList<RelativeBendpoint>(points);
  // newBendPoints.addAll(1, bendPoints);
  // exitingBendpoints.setPoints(newBendPoints);
  //
  // return edge;
  // }
}

Back to the top