Skip to main content
summaryrefslogtreecommitdiffstats
blob: 99e8893a244ed2d688cd00dd8a33aa103b78bded (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
/*
 * Copyright (c) 2004 - 2012 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.tests.ui;

import org.eclipse.emf.cdo.tests.AbstractCDOTest;

import org.eclipse.net4j.util.concurrent.TimeoutRuntimeException;
import org.eclipse.net4j.util.om.OMPlatform;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.keyboard.Keyboard;
import org.eclipse.swtbot.swt.finder.keyboard.KeyboardFactory;
import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author Martin Fluegge
 */
public abstract class AbstractCDOUITest<T extends SWTWorkbenchBot> extends AbstractCDOTest
{
  private T bot;

  @Override
  public void setUp() throws Exception
  {
    super.setUp();
    SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
    SWTBotPreferences.SCREENSHOTS_DIR = OMPlatform.INSTANCE.getProperty("java.io.tmpdir") + "/cdotests";
    createBot();
  }

  @SuppressWarnings("unchecked")
  protected void createBot()
  {
    setBot((T)new SWTWorkbenchBot());
  }

  protected T getBot()
  {
    return bot;
  }

  protected void setBot(T bot)
  {
    this.bot = bot;
  }

  @Override
  public void tearDown() throws Exception
  {
    super.tearDown();
    closeAllEditors();
  }

  protected void closeAllEditors()
  {
    LatchedRunnable runnable = new LatchedRunnable()
    {
      @Override
      protected void runWithLatch()
      {
        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeAllEditors(false);
      }
    };

    Display.getDefault().asyncExec(runnable);
    runnable.await();
  }

  @Deprecated
  protected void closeAllEditorsSync()
  {
    UIThreadRunnable.syncExec(new VoidResult()
    {
      public void run()
      {
        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeAllEditors(false);
      }
    });
  }

  protected void resetWorkbench()
  {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    UIThreadRunnable.asyncExec(new VoidResult()
    {
      public void run()
      {
        try
        {
          IWorkbench workbench = PlatformUI.getWorkbench();
          IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
          IWorkbenchPage page = workbenchWindow.getActivePage();
          Shell activeShell = Display.getCurrent().getActiveShell();

          if (activeShell != workbenchWindow.getShell() && activeShell != null)
          {
            activeShell.close();
          }

          page.closeAllEditors(false);
          page.resetPerspective();

          String defaultPerspectiveId = workbench.getPerspectiveRegistry().getDefaultPerspective();
          workbench.showPerspective(defaultPerspectiveId, workbenchWindow);

          page.resetPerspective();
        }
        catch (WorkbenchException e)
        {
          throw new RuntimeException(e);
        }
        finally
        {
          countDownLatch.countDown();
        }
      }
    });

    try
    {
      countDownLatch.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
    }
    catch (InterruptedException ex)
    {
      throw new RuntimeException(ex);
    }
  }

  /**
   * Walks through the tree and selects the first element which matches the name.
   */
  protected SWTBotTreeItem selectFolder(SWTBotTreeItem[] items, String name, boolean exactMatch)
  {
    SWTBotTreeItem ret = null;
    for (SWTBotTreeItem item : items)
    {
      if (exactMatch)
      {
        if (item.getText().equals(name))
        {
          item.select();
          return item;
        }
      }
      else
      {
        if (item.getText().contains(name))
        {
          item.select();
          return item;
        }
      }

      item.expand();
      ret = selectFolder(item.getItems(), name, exactMatch);
    }
    return ret;
  }

  protected void typeTextToFocusedWidget(String text, SWTBot bot, boolean hitCR)
  {
    Keyboard keyboard = KeyboardFactory.getSWTKeyboard();
    bot.getFocusedWidget();
    keyboard.typeText(text, 50);

    if (hitCR)
    {
      keyboard.pressShortcut(Keystrokes.CR);
    }
  }

  /**
   * @author Eike Stepper
   */
  public static abstract class LatchedRunnable implements Runnable
  {
    private CountDownLatch latch = new CountDownLatch(1);

    private Throwable result;

    public LatchedRunnable()
    {
    }

    public void run()
    {
      try
      {
        runWithLatch();
      }
      catch (Throwable t)
      {
        result = t;
      }
      finally
      {
        latch.countDown();
      }
    }

    protected abstract void runWithLatch();

    public void await(long timeout)
    {
      try
      {
        if (!latch.await(timeout, TimeUnit.MILLISECONDS))
        {
          throw new TimeoutRuntimeException("Timeout after " + timeout + " milliseconds");
        }

        if (result instanceof RuntimeException)
        {
          throw (RuntimeException)result;
        }

        if (result instanceof Error)
        {
          throw (Error)result;
        }
      }
      catch (InterruptedException ex)
      {
        throw new RuntimeException(ex);
      }
    }

    public void await()
    {
      await(DEFAULT_TIMEOUT);
    }

    /**
     * @author Eike Stepper
     */
    public static class Delegating extends LatchedRunnable
    {
      private Runnable delegate;

      public Delegating(Runnable delegate)
      {
        this.delegate = delegate;
      }

      @Override
      protected void runWithLatch()
      {
        delegate.run();
      }
    }
  }
}

Back to the top