summaryrefslogtreecommitdiff
path: root/platform/smRunner/src/com/intellij/execution/testframework/sm/runner/GeneralTestEventsProcessor.java
blob: 23145d52702f500d1d31826600e622843079e4bb (plain)
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
/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.execution.testframework.sm.runner;

import com.intellij.execution.testframework.sm.runner.events.*;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Key;
import com.intellij.testIntegration.TestLocationProvider;
import com.intellij.util.Processor;
import com.intellij.util.containers.TransferToEDTQueue;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @author: Roman Chernyatchik
 * <p/>
 * Processes events of test runner in general text-based form
 * <p/>
 * NB: Test name should be unique for all suites. E.g. it can consist of suite name
 * and name of test method
 */
public abstract class GeneralTestEventsProcessor implements Disposable {
  private TransferToEDTQueue<Runnable> myTransferToEDTQueue =
    new TransferToEDTQueue<Runnable>("SM queue", new Processor<Runnable>() {
      @Override
      public boolean process(Runnable runnable) {
        runnable.run();
        return true;
      }
    }, getDisposedCondition(), 300);

  public abstract void onStartTesting();

  public abstract void onTestsCountInSuite(final int count);

  public abstract void onTestStarted(@NotNull TestStartedEvent testStartedEvent);

  public abstract void onTestFinished(@NotNull TestFinishedEvent testFinishedEvent);

  public abstract void onTestFailure(@NotNull TestFailedEvent testFailedEvent);

  public abstract void onTestIgnored(@NotNull TestIgnoredEvent testIgnoredEvent);

  public abstract void onTestOutput(@NotNull TestOutputEvent testOutputEvent);

  public abstract void onSuiteStarted(@NotNull TestSuiteStartedEvent suiteStartedEvent);

  public abstract void onSuiteFinished(@NotNull TestSuiteFinishedEvent suiteFinishedEvent);

  public abstract void onUncapturedOutput(@NotNull final String text,
                                          final Key outputType);

  public abstract void onError(@NotNull final String localizedMessage,
                               @Nullable final String stackTrace,
                               final boolean isCritical);

  // Custom progress statistics

  /**
   * @param categoryName If isn't empty then progress statistics will use only custom start/failed events.
   *                     If name is null statistics will be switched to normal mode
   * @param testCount    - 0 will be considered as unknown tests number
   */
  public abstract void onCustomProgressTestsCategory(@Nullable final String categoryName,
                                                     final int testCount);

  public abstract void onCustomProgressTestStarted();

  public abstract void onCustomProgressTestFailed();

  public abstract void onTestsReporterAttached();

  public abstract void setLocator(@NotNull TestLocationProvider locator);

  public abstract void addEventsListener(@NotNull SMTRunnerEventsListener viewer);

  public abstract void onFinishTesting();

  public abstract void setPrinterProvider(@NotNull TestProxyPrinterProvider printerProvider);

  @Override
  public void dispose() {
    if (!ApplicationManager.getApplication().isUnitTestMode()) {
      UIUtil.invokeAndWaitIfNeeded(new Runnable() {
        @Override
        public void run() {
          myTransferToEDTQueue.drain();
        }
      });
    }
  }

  public Condition getDisposedCondition() {
    return Condition.FALSE;
  }

  /**
   * Adds runnable to Event Dispatch Queue
   * if we aren't in UnitTest of Headless environment mode
   *
   * @param runnable Runnable
   */
  public void addToInvokeLater(final Runnable runnable) {
    final Application application = ApplicationManager.getApplication();
    final boolean unitTestMode = application.isUnitTestMode();
    if (unitTestMode) {
      UIUtil.invokeLaterIfNeeded(runnable);
    } else if (application.isHeadlessEnvironment() || SwingUtilities.isEventDispatchThread()) {
      runnable.run();
    }
    else {
      myTransferToEDTQueue.offer(runnable);
    }
  }
}