mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-06 03:21:12 +07:00
Java: change test to use UiInterceptor (IJ-CR-138368)
to enable testing of ConvertToInstanceMethodHandler and UI GitOrigin-RevId: 0e12079c867c9c6a5f4abee985b42b9ef74b9914
This commit is contained in:
committed by
intellij-monorepo-bot
parent
941612674c
commit
04ee5be1f6
@@ -1,6 +1,7 @@
|
||||
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.refactoring.convertToInstanceMethod;
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiParameter;
|
||||
@@ -10,6 +11,7 @@ import com.intellij.refactoring.move.moveInstanceMethod.MoveInstanceMethodDialog
|
||||
import com.intellij.ui.DoubleClickListener;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
@@ -27,10 +29,12 @@ public class ConvertToInstanceMethodDialog extends MoveInstanceMethodDialogBase
|
||||
protected void doAction() {
|
||||
final Object targetVariable = myList.getSelectedValue();
|
||||
LOG.assertTrue(targetVariable != null);
|
||||
final ConvertToInstanceMethodProcessor processor = new ConvertToInstanceMethodProcessor(myMethod.getProject(),
|
||||
myMethod, targetVariable instanceof PsiParameter ? (PsiParameter)targetVariable : null,
|
||||
myVisibilityPanel.getVisibility());
|
||||
if (!verifyTargetClass(processor.getTargetClass())) return;
|
||||
final ConvertToInstanceMethodProcessor processor =
|
||||
new ConvertToInstanceMethodProcessor(myMethod.getProject(),
|
||||
myMethod,
|
||||
targetVariable instanceof PsiParameter ? (PsiParameter)targetVariable : null,
|
||||
myVisibilityPanel.getVisibility());
|
||||
if (!ApplicationManager.getApplication().isUnitTestMode() && !verifyTargetClass(processor.getTargetClass())) return;
|
||||
invokeRefactoring(processor);
|
||||
}
|
||||
|
||||
@@ -64,4 +68,9 @@ public class ConvertToInstanceMethodDialog extends MoveInstanceMethodDialogBase
|
||||
}.installOn(variableChooser);
|
||||
return variableChooser;
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
public void setVisibility(String visibility) {
|
||||
myVisibilityPanel.setVisibility(visibility);
|
||||
}
|
||||
}
|
||||
@@ -14,26 +14,14 @@ public class ConvertToInstance8MethodTest extends ConvertToInstanceMethodTest {
|
||||
public void testConflictingMembers() { doTest(0); }
|
||||
public void testNoConflictingMembers() { doTest(0); }
|
||||
public void testNoConflictingMembers2() { doTest(0); }
|
||||
|
||||
public void testThisInsteadOfNoQualifier() {
|
||||
doTest(0);
|
||||
}
|
||||
|
||||
public void testMethodReferenceAcceptableBySecondSearch() {
|
||||
doTest(0);
|
||||
}
|
||||
|
||||
public void testConvertToInstanceMethodOfTheSameClass() {
|
||||
doTest(-1);
|
||||
}
|
||||
|
||||
public void testStaticMethodOfInterfaceWithNonAccessibleInheritor() {
|
||||
doTest(0);
|
||||
}
|
||||
public void testThisInsteadOfNoQualifier() { doTest(0); }
|
||||
public void testMethodReferenceAcceptableBySecondSearch() { doTest(0); }
|
||||
public void testConvertToInstanceMethodOfTheSameClass() { doTest(0); }
|
||||
public void testStaticMethodOfInterfaceWithNonAccessibleInheritor() { doTest(0); }
|
||||
|
||||
public void testConvertToInstanceMethodOfTheSameClassWithTypeParams() {
|
||||
try {
|
||||
doTest(-1);
|
||||
doTest(0);
|
||||
}
|
||||
catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
|
||||
assertEquals(StringUtil.trimEnd(StringUtil.repeat("Impossible to infer class type arguments. When proceed, raw Bar would be created\n", 3), "\n"), e.getMessage());
|
||||
@@ -41,12 +29,11 @@ public class ConvertToInstance8MethodTest extends ConvertToInstanceMethodTest {
|
||||
}
|
||||
|
||||
public void testMethodReferenceToLambda() {
|
||||
BaseRefactoringProcessor.ConflictsInTestsException.withIgnoredConflicts(() -> doTest(1));
|
||||
BaseRefactoringProcessor.ConflictsInTestsException.withIgnoredConflicts(() -> doTest(0));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LanguageLevel getLanguageLevel() {
|
||||
return LanguageLevel.JDK_1_8;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.java.refactoring.convertToInstanceMethod;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.codeInsight.TargetElementUtil;
|
||||
import com.intellij.java.refactoring.LightRefactoringTestCase;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiModifier;
|
||||
import com.intellij.refactoring.BaseRefactoringProcessor;
|
||||
import com.intellij.refactoring.convertToInstanceMethod.ConvertToInstanceMethodProcessor;
|
||||
import com.intellij.refactoring.convertToInstanceMethod.ConvertToInstanceMethodDialog;
|
||||
import com.intellij.refactoring.convertToInstanceMethod.ConvertToInstanceMethodHandler;
|
||||
import com.intellij.ui.UiInterceptors;
|
||||
import com.intellij.util.VisibilityUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class ConvertToInstanceMethodTest extends LightRefactoringTestCase {
|
||||
@NotNull
|
||||
@@ -21,24 +23,14 @@ public class ConvertToInstanceMethodTest extends LightRefactoringTestCase {
|
||||
}
|
||||
|
||||
public void testSimple() { doTest(0); }
|
||||
|
||||
public void testInterface() { doTest(1); }
|
||||
|
||||
public void testInterfacePrivate() { doTest(1); }
|
||||
|
||||
public void testInterface() { doTest(0); }
|
||||
public void testInterfacePrivate() { doTest(0); }
|
||||
public void testInterface2() { doTest(0); }
|
||||
|
||||
public void testInterface3() { doTest(0); }
|
||||
|
||||
public void testTypeParameter() { doTest(0); }
|
||||
|
||||
public void testInterfaceTypeParameter() { doTest(0); }
|
||||
|
||||
public void testJavadocParameter() { doTest(0); }
|
||||
|
||||
public void testConflictingParameterName() {
|
||||
doTest(0);
|
||||
}
|
||||
public void testConflictingParameterName() { doTest(0); }
|
||||
|
||||
public void testVisibilityConflict() {
|
||||
try {
|
||||
@@ -50,23 +42,18 @@ public class ConvertToInstanceMethodTest extends LightRefactoringTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
protected void doTest(final int targetParameter) {
|
||||
protected void doTest(int targetParameter) {
|
||||
doTest(targetParameter, VisibilityUtil.ESCALATE_VISIBILITY);
|
||||
}
|
||||
|
||||
private void doTest(final int targetParameter, final String visibility) {
|
||||
private void doTest(int targetParameter, String visibility) {
|
||||
final String filePath = getBasePath() + getTestName(false) + ".java";
|
||||
configureByFile(filePath);
|
||||
final PsiElement targetElement = TargetElementUtil.findTargetElement(getEditor(), TargetElementUtil.ELEMENT_NAME_ACCEPTED);
|
||||
assertTrue("<caret> is not on method name", targetElement instanceof PsiMethod);
|
||||
PsiMethod method = (PsiMethod) targetElement;
|
||||
new ConvertToInstanceMethodProcessor(getProject(),
|
||||
method, targetParameter < 0 ? null : method.getParameterList().getParameters()[targetParameter],
|
||||
visibility).run();
|
||||
UiInterceptors.register(new ConvertToInstanceMethodDialogUiInterceptor(targetParameter, visibility));
|
||||
new ConvertToInstanceMethodHandler().invoke(getProject(), getEditor(), getFile(), getCurrentEditorDataContext());
|
||||
checkResultByFile(filePath + ".after");
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected String getBasePath() {
|
||||
return "/refactoring/convertToInstanceMethod/";
|
||||
}
|
||||
@@ -76,4 +63,27 @@ public class ConvertToInstanceMethodTest extends LightRefactoringTestCase {
|
||||
return LanguageLevel.JDK_1_6;
|
||||
}
|
||||
|
||||
private static class ConvertToInstanceMethodDialogUiInterceptor extends UiInterceptors.UiInterceptor<ConvertToInstanceMethodDialog> {
|
||||
private final int myTargetParameter;
|
||||
private final String myVisibility;
|
||||
|
||||
ConvertToInstanceMethodDialogUiInterceptor(int targetParameter, @Nullable String visibility) {
|
||||
super(ConvertToInstanceMethodDialog.class);
|
||||
myTargetParameter = targetParameter;
|
||||
myVisibility = visibility;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doIntercept(@NotNull ConvertToInstanceMethodDialog dialog) {
|
||||
@SuppressWarnings("unchecked") JList<Object> list = (JList<Object>)dialog.getPreferredFocusedComponent();
|
||||
ListModel<Object> model = list.getModel();
|
||||
int size = model.getSize();
|
||||
if (myTargetParameter < 0 || myTargetParameter >= size) {
|
||||
fail("targetParameter out of bounds: " + myTargetParameter);
|
||||
}
|
||||
list.setSelectedIndex(myTargetParameter);
|
||||
dialog.setVisibility(myVisibility == null ? VisibilityUtil.ESCALATE_VISIBILITY : myVisibility);
|
||||
dialog.performOKAction();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user