From 8ac8508d89d1c42ed508a0b9b110dcb27256d5c3 Mon Sep 17 00:00:00 2001 From: peter Date: Wed, 5 Sep 2012 15:04:38 +0200 Subject: [PATCH] extract CompletionAutoPopupTester --- .../CompletionAutoPopupTestCase.groovy | 94 ++---------- .../fixtures/CompletionAutoPopupTester.groovy | 140 ++++++++++++++++++ platform/testFramework/testFramework.iml | 2 +- 3 files changed, 151 insertions(+), 85 deletions(-) create mode 100644 platform/testFramework/src/com/intellij/testFramework/fixtures/CompletionAutoPopupTester.groovy diff --git a/java/testFramework/src/com/intellij/codeInsight/completion/CompletionAutoPopupTestCase.groovy b/java/testFramework/src/com/intellij/codeInsight/completion/CompletionAutoPopupTestCase.groovy index 83f2474442f1..503f221bc6a7 100644 --- a/java/testFramework/src/com/intellij/codeInsight/completion/CompletionAutoPopupTestCase.groovy +++ b/java/testFramework/src/com/intellij/codeInsight/completion/CompletionAutoPopupTestCase.groovy @@ -15,26 +15,19 @@ */ package com.intellij.codeInsight.completion -import com.intellij.codeInsight.AutoPopupController -import com.intellij.codeInsight.completion.impl.CompletionServiceImpl -import com.intellij.codeInsight.editorActions.CompletionAutoPopupHandler import com.intellij.codeInsight.lookup.LookupManager import com.intellij.codeInsight.lookup.impl.LookupImpl -import com.intellij.openapi.application.ApplicationManager -import com.intellij.openapi.editor.ex.DocumentEx -import com.intellij.psi.PsiDocumentManager +import com.intellij.testFramework.fixtures.CompletionAutoPopupTester import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase -import com.intellij.util.ui.UIUtil -import java.util.concurrent.atomic.AtomicBoolean -import java.util.concurrent.atomic.AtomicInteger - /** * @author peter */ abstract class CompletionAutoPopupTestCase extends LightCodeInsightFixtureTestCase { + private CompletionAutoPopupTester myTester + @Override protected void setUp() { edt { superSetUp() } - CompletionAutoPopupHandler.ourTestingAutopopup = true + myTester = new CompletionAutoPopupTester(myFixture) } void superSetUp() { super.setUp() @@ -44,8 +37,6 @@ abstract class CompletionAutoPopupTestCase extends LightCodeInsightFixtureTestCa } @Override protected void tearDown() { - CompletionAutoPopupHandler.ourTestingAutopopup = false - ((DocumentEx) myFixture.editor.document).setModificationStamp(0) // to not let autopopup handler sneak in edt { superTearDown() } } @@ -54,92 +45,27 @@ abstract class CompletionAutoPopupTestCase extends LightCodeInsightFixtureTestCa } void type(String s) { - for (i in 0..= 20000) { - fail("too long waiting for documents to be committed. executed: $executed; r: $r; run: $run; ") - printThreadDump() - } - UIUtil.pump(); - } + myTester.joinCommit(c1) } protected void joinAutopopup() { - joinAlarm(); - joinCommit() // physical document commit - joinCommit() // file copy commit in background + myTester.joinAutopopup() } protected def joinAlarm() { - AutoPopupController.getInstance(getProject()).executePendingRequests() + myTester.joinAlarm() } @Override protected void runTest() { - runTestBare() + myTester.runWithAutoPopupEnabled { runTestBare() } } @Override protected boolean runInDispatchThread() { diff --git a/platform/testFramework/src/com/intellij/testFramework/fixtures/CompletionAutoPopupTester.groovy b/platform/testFramework/src/com/intellij/testFramework/fixtures/CompletionAutoPopupTester.groovy new file mode 100644 index 000000000000..59f1725045cd --- /dev/null +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/CompletionAutoPopupTester.groovy @@ -0,0 +1,140 @@ +/* + * Copyright 2000-2012 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.testFramework.fixtures +import com.intellij.codeInsight.AutoPopupController +import com.intellij.codeInsight.completion.CompletionPhase +import com.intellij.codeInsight.completion.impl.CompletionServiceImpl +import com.intellij.codeInsight.editorActions.CompletionAutoPopupHandler +import com.intellij.codeInsight.lookup.LookupManager +import com.intellij.codeInsight.lookup.impl.LookupImpl +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.editor.ex.DocumentEx +import com.intellij.psi.PsiDocumentManager +import com.intellij.testFramework.UsefulTestCase +import com.intellij.util.ui.UIUtil +import junit.framework.Assert + +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicInteger +/** + * @author peter + */ +class CompletionAutoPopupTester { + private final CodeInsightTestFixture myFixture + + CompletionAutoPopupTester(CodeInsightTestFixture fixture) { + myFixture = fixture + } + + void runWithAutoPopupEnabled(Runnable r) { + assert !ApplicationManager.application.isDispatchThread() + CompletionAutoPopupHandler.ourTestingAutopopup = true + try { + r.run() + } + finally { + CompletionAutoPopupHandler.ourTestingAutopopup = false + ((DocumentEx) myFixture.editor.document).setModificationStamp(0) // to not let autopopup handler sneak in + } + } + + void joinCompletion() { + for (i in 0.1000) { + if (i==999) { + UsefulTestCase.printThreadDump() + Assert.fail("Could not wait for committed doc") + } + CompletionPhase phase = CompletionServiceImpl.getCompletionPhase() + if (phase != com.intellij.codeInsight.completion.CompletionPhase.NoCompletion) break; + Thread.sleep(10) + } + + for (j in 1..4000) { + LookupImpl l = null + UsefulTestCase.edt { + l = lookup + } + if (!l || !l.calculating) { + UsefulTestCase.edt {} // for invokeLater in CompletionProgressIndicator.stop() + return + } + Thread.sleep(10) + } + UsefulTestCase.printThreadDump() + UsefulTestCase.fail("Too long completion") + } + + final static AtomicInteger cnt = new AtomicInteger() + def joinCommit(Closure c1={}) { + final AtomicBoolean committed = new AtomicBoolean() + final AtomicBoolean run = new AtomicBoolean() + boolean executed=true; + def closureSeq = cnt.getAndIncrement() + Runnable r = new Runnable() { + @Override + public void run() { + run.set(true) + ApplicationManager.application.invokeLater { + c1(); + committed.set(true) + }; + } + + @Override + public String toString() { + return "Closure "+closureSeq; + } + }; + UsefulTestCase.edt { + executed = PsiDocumentManager.getInstance(myFixture.project).performWhenAllCommitted(r); + } + assert !ApplicationManager.getApplication().isWriteAccessAllowed() + assert !ApplicationManager.getApplication().isReadAccessAllowed() + assert !ApplicationManager.getApplication().isDispatchThread() + def start = System.currentTimeMillis() + while (!committed.get()) { + if (System.currentTimeMillis() - start >= 20000) { + UsefulTestCase.fail("too long waiting for documents to be committed. executed: $executed; r: $r; run: $run; ") + UsefulTestCase.printThreadDump() + } + UIUtil.pump(); + } + } + + void joinAutopopup() { + joinAlarm(); + joinCommit() // physical document commit + joinCommit() // file copy commit in background + } + + def joinAlarm() { + AutoPopupController.getInstance(myFixture.getProject()).executePendingRequests() + } + + LookupImpl getLookup() { + (LookupImpl)LookupManager.getInstance(myFixture.project).getActiveLookup() + } + + void typeWithPauses(String s) { + for (i in 0.. - +