use app invokeAndWait in tests by default
authorpeter <peter@jetbrains.com>
Tue, 3 May 2016 21:02:48 +0000 (23:02 +0200)
committerpeter <peter@jetbrains.com>
Tue, 3 May 2016 21:02:48 +0000 (23:02 +0200)
java/java-tests/testSrc/com/intellij/navigation/ChooseByNameTest.groovy
java/testFramework/src/com/intellij/testFramework/fixtures/impl/JavaCodeInsightTestFixtureImpl.java
platform/platform-tests/testSrc/com/intellij/application/TransactionTest.groovy
platform/testFramework/src/com/intellij/testFramework/EdtTestUtil.kt

index b311dc954b2068badf97a6cde1ee3a4ba5f66c31..fe9acaa3b7fe342ea341b49c126cddc00ea7212e 100644 (file)
@@ -31,6 +31,8 @@ import com.intellij.util.concurrency.Semaphore
 import org.jetbrains.annotations.NotNull
 import org.jetbrains.plugins.groovy.lang.psi.GroovyFile
 
+import javax.swing.SwingUtilities
+
 /**
  * @author peter
  */
@@ -323,7 +325,7 @@ class Intf {
     List<Object> elements = ['empty']
     def semaphore = new Semaphore()
     semaphore.down()
-    edt {
+    SwingUtilities.invokeLater {
       popup.scheduleCalcElements(text, checkboxState, ModalityState.NON_MODAL, { set ->
         elements = set as List
         semaphore.up()
index d348a5e450b810738d6ea2f00310cb51f693f08b..165b1c41d7695bb190ba551f508d6088690e0e87 100644 (file)
@@ -23,10 +23,10 @@ import com.intellij.psi.*;
 import com.intellij.psi.impl.JavaPsiFacadeEx;
 import com.intellij.psi.impl.PsiModificationTrackerImpl;
 import com.intellij.psi.search.ProjectScope;
+import com.intellij.testFramework.EdtTestUtil;
 import com.intellij.testFramework.fixtures.IdeaProjectTestFixture;
 import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture;
 import com.intellij.testFramework.fixtures.TempDirTestFixture;
-import com.intellij.util.ui.UIUtil;
 import org.jetbrains.annotations.NonNls;
 import org.jetbrains.annotations.NotNull;
 import org.junit.Assert;
@@ -92,7 +92,7 @@ public class JavaCodeInsightTestFixtureImpl extends CodeInsightTestFixtureImpl i
   @Override
   public void tearDown() throws Exception {
     try {
-      UIUtil.invokeLaterIfNeeded(new Runnable() {
+      EdtTestUtil.runInEdtAndWait(new Runnable() {
         @Override
         public void run() {
           ((PsiModificationTrackerImpl)getPsiManager().getModificationTracker()).incCounter();// drop all caches
index 747bb66e3e616fee991979e027fa3606e269d17f..bad43284d3c7c1eaac57713c3be1906911d2ee4a 100644 (file)
@@ -8,6 +8,7 @@ import com.intellij.openapi.util.registry.Registry
 import com.intellij.testFramework.LightPlatformTestCase
 import com.intellij.testFramework.LoggedErrorProcessor
 import com.intellij.util.ui.UIUtil
+import org.jetbrains.annotations.NotNull
 
 import javax.swing.*
 /**
@@ -24,6 +25,12 @@ class TransactionTest extends LightPlatformTestCase {
     return ApplicationManager.getApplication()
   }
 
+  @Override
+  protected void invokeTestRunnable(@NotNull Runnable runnable) throws Exception {
+    SwingUtilities.invokeLater(runnable)
+    UIUtil.dispatchAllInvocationEvents()
+  }
+
   @Override
   protected void setUp() throws Exception {
     assert LaterInvocator.currentModalityState == ModalityState.NON_MODAL
index 81c4c3379074611de80ad2b07bc05daa97885c90..5cea358ef6cf471912b4d658294596042c001d38 100644 (file)
@@ -15,6 +15,8 @@
  */
 package com.intellij.testFramework
 
+import com.intellij.openapi.application.ApplicationManager
+import com.intellij.openapi.application.ModalityState
 import com.intellij.util.ThrowableRunnable
 import org.jetbrains.annotations.TestOnly
 import java.lang.reflect.InvocationTargetException
@@ -32,12 +34,14 @@ class EdtTestUtil {
   }
 }
 
-
-// Test only because in production you must use Application.invokeAndWait(Runnable, ModalityState).
-// The problem is - Application logs errors, but not throws. But in tests must be thrown.
-// In any case name "runInEdtAndWait" is better than "invokeAndWait".
 @TestOnly
 fun runInEdtAndWait(runnable: () -> Unit) {
+  val application = ApplicationManager.getApplication()
+  if (application != null) {
+    application.invokeAndWait(runnable, ModalityState.defaultModalityState())
+    return
+  }
+
   if (SwingUtilities.isEventDispatchThread()) {
     runnable()
   }