diff --git a/platform/testGuiFramework/src/com/intellij/testGuiFramework/fixtures/newProjectWizard/NewProjectWizardFixture.java b/platform/testGuiFramework/src/com/intellij/testGuiFramework/fixtures/newProjectWizard/NewProjectWizardFixture.java index c1936e0bef9e..1553cf2455d0 100644 --- a/platform/testGuiFramework/src/com/intellij/testGuiFramework/fixtures/newProjectWizard/NewProjectWizardFixture.java +++ b/platform/testGuiFramework/src/com/intellij/testGuiFramework/fixtures/newProjectWizard/NewProjectWizardFixture.java @@ -16,8 +16,6 @@ package com.intellij.testGuiFramework.fixtures.newProjectWizard; import com.intellij.ide.IdeBundle; -import com.intellij.openapi.application.ApplicationBundle; -import com.intellij.openapi.project.ProjectBundle; import com.intellij.openapi.roots.ui.configuration.JdkComboBox; import com.intellij.testGuiFramework.fixtures.FrameworksTreeFixture; import com.intellij.testGuiFramework.fixtures.SelectSdkDialogFixture; @@ -38,7 +36,6 @@ import java.awt.*; import java.io.File; import static com.intellij.testGuiFramework.framework.GuiTestUtil.LONG_TIMEOUT; -import static com.intellij.testGuiFramework.framework.GuiTestUtil.getSystemJdk; import static org.fest.assertions.Assertions.assertThat; import static org.fest.swing.edt.GuiActionRunner.execute; import static org.fest.swing.finder.WindowFinder.findDialog; @@ -135,19 +132,19 @@ public class NewProjectWizardFixture extends AbstractWizardFixture { - ProjectJdkTable.getInstance().addJdk(newJdk); - }); - } - - ApplicationManager.getApplication().runWriteAction(() -> { - SdkModificator modificator = newJdk.getSdkModificator(); - JavaSdkImpl.attachJdkAnnotations(modificator); - modificator.commitChanges(); - }); - } - else { - throw new IllegalStateException("The resolved path '" + path.getPath() + "' was not found"); - } - } - } } @Nullable @@ -665,17 +581,27 @@ GuiTestUtil { @Nullable public static JLabel findBoundedLabel(@NotNull Container container, @NotNull JTextField textField, @NotNull Robot robot) { - Collection labels = robot.finder().findAll(container, new GenericTypeMatcher(JLabel.class) { - @Override - protected boolean isMatching(@NotNull JLabel label) { - return (label.getLabelFor() != null && label.getLabelFor().equals(textField)); + //in Case of parent component is TextFieldWithBrowseButton + if(textField.getParent() instanceof TextFieldWithBrowseButton) { + return robot.finder().find(container, new GenericTypeMatcher(JLabel.class) { + @Override + protected boolean isMatching(@NotNull JLabel label) { + return (label.getLabelFor() != null && label.getLabelFor().equals(textField.getParent())); + } + }); + } else { + Collection labels = robot.finder().findAll(container, new GenericTypeMatcher(JLabel.class) { + @Override + protected boolean isMatching(@NotNull JLabel label) { + return (label.getLabelFor() != null && label.getLabelFor().equals(textField)); + } + }); + if (labels != null && !labels.isEmpty()) { + return labels.iterator().next(); + } + else { + return null; } - }); - if (labels != null && !labels.isEmpty()) { - return labels.iterator().next(); - } - else { - return null; } } diff --git a/platform/testGuiFramework/src/com/intellij/testGuiFramework/idea/IdeaGuiTestUtil.kt b/platform/testGuiFramework/src/com/intellij/testGuiFramework/idea/IdeaGuiTestUtil.kt new file mode 100644 index 000000000000..86c5569ea15f --- /dev/null +++ b/platform/testGuiFramework/src/com/intellij/testGuiFramework/idea/IdeaGuiTestUtil.kt @@ -0,0 +1,112 @@ +/* + * Copyright 2000-2017 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.testGuiFramework.idea + +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.diagnostic.Logger +import com.intellij.openapi.projectRoots.JavaSdk +import com.intellij.openapi.projectRoots.JdkUtil +import com.intellij.openapi.projectRoots.JdkUtil.checkForJdk +import com.intellij.openapi.projectRoots.ProjectJdkTable +import com.intellij.openapi.projectRoots.Sdk +import com.intellij.openapi.projectRoots.impl.JavaSdkImpl +import com.intellij.openapi.util.io.FileUtil.pathsEqual +import com.intellij.openapi.util.text.StringUtil +import com.intellij.testGuiFramework.framework.GuiTestUtil.* +import junit.framework.Assert.fail +import org.fest.swing.edt.GuiActionRunner.execute +import org.fest.swing.edt.GuiTask +import java.io.File + +/** + * @author Sergey Karashevich + */ +object IdeaGuiTestUtil{ + + val LOG = Logger.getInstance("#com.intellij.testGuiFramework.idea.IdeaGuiTestUtil") + + fun setUpSdks() { + + var jdkHome: String? = getSystemPropertyOrEnvironmentVariable(JDK_HOME_FOR_TESTS) + if (StringUtil.isEmpty(jdkHome) || !JdkUtil.checkForJdk(jdkHome!!)) { + //than use bundled JDK + jdkHome = getBundledJdLocation() + } + val jdkPath = File(jdkHome) + + execute(object : GuiTask() { + @Throws(Throwable::class) + override fun executeInEDT() { + ApplicationManager.getApplication().runWriteAction { + LOG.info(String.format("Setting JDK: '%1\$s'", jdkPath.path)) + setJdkPath(jdkPath) + } + } + }) + } + + fun setJdkPath(path: File) { + if (JavaSdk.checkForJdk(path)) { + ApplicationManager.getApplication().assertWriteAccessAllowed() + var chosenJdk: Sdk? = null + + for (jdk in ProjectJdkTable.getInstance().getSdksOfType(JavaSdk.getInstance())) { + if (pathsEqual(jdk.homePath, path.path)) { + chosenJdk = jdk + break + } + } + + if (chosenJdk == null) { + if (path.isDirectory) { + + val javaSdk = JavaSdk.getInstance() ?: return + + //in case of running different from IntelliJ or Android Studio IDE (PyCharm for example) + + val jdk_name = "JDK" + val newJdk = javaSdk.createJdk(jdk_name, path.toString(), false) + val foundJdk = ProjectJdkTable.getInstance().findJdk(newJdk.name, newJdk.sdkType.name) + if (foundJdk == null) { + ApplicationManager.getApplication().runWriteAction { ProjectJdkTable.getInstance().addJdk(newJdk) } + } + + ApplicationManager.getApplication().runWriteAction { + val modificator = newJdk.sdkModificator + JavaSdkImpl.attachJdkAnnotations(modificator) + modificator.commitChanges() + } + } + else { + throw IllegalStateException("The resolved path '" + path.path + "' was not found") + } + } + } + } + + fun getSystemJdk(): String { + var jdkHome: String? = getSystemPropertyOrEnvironmentVariable(JDK_HOME_FOR_TESTS) + if (StringUtil.isEmpty(jdkHome) || !checkForJdk(jdkHome!!)) { + //than use bundled JDK + jdkHome = getBundledJdLocation() + } + if (StringUtil.isEmpty(jdkHome) || !checkForJdk(jdkHome!!)) { + fail("Please specify the path to a valid JDK using system property " + JDK_HOME_FOR_TESTS) + } + return jdkHome!! + } + +} \ No newline at end of file diff --git a/platform/testGuiFramework/src/com/intellij/testGuiFramework/impl/GuiTestCase.kt b/platform/testGuiFramework/src/com/intellij/testGuiFramework/impl/GuiTestCase.kt index 26d8e8c9c804..df186fc53e9b 100644 --- a/platform/testGuiFramework/src/com/intellij/testGuiFramework/impl/GuiTestCase.kt +++ b/platform/testGuiFramework/src/com/intellij/testGuiFramework/impl/GuiTestCase.kt @@ -16,6 +16,7 @@ package com.intellij.testGuiFramework.impl import com.intellij.ide.GeneralSettings +import com.intellij.openapi.ui.TextFieldWithBrowseButton import com.intellij.testGuiFramework.cellReader.ExtendedJListCellReader import com.intellij.testGuiFramework.cellReader.SettingsTreeCellReader import com.intellij.testGuiFramework.fixtures.* @@ -23,7 +24,9 @@ import com.intellij.testGuiFramework.fixtures.newProjectWizard.NewProjectWizardF import com.intellij.testGuiFramework.framework.GuiTestBase import com.intellij.testGuiFramework.framework.GuiTestUtil import com.intellij.testGuiFramework.framework.GuiTestUtil.waitUntilFound +import com.intellij.ui.components.labels.LinkLabel import com.intellij.util.net.HttpConfigurable +import org.fest.swing.core.ComponentMatcher import org.fest.swing.core.GenericTypeMatcher import org.fest.swing.core.SmartWaitRobot import org.fest.swing.exception.LocationUnavailableException @@ -46,7 +49,6 @@ open class GuiTestCase : GuiTestBase() { init { GeneralSettings.getInstance().isShowTipsOnStartup = false GuiTestUtil.setUpDefaultProjectCreationLocationPath() - GuiTestUtil.setUpSdks() val ideSettings = HttpConfigurable.getInstance() ideSettings.USE_HTTP_PROXY = false ideSettings.PROXY_HOST = "" @@ -131,7 +133,7 @@ open class GuiTestCase : GuiTestBase() { else throw UnsupportedOperationException( "Sorry, unable to find RadioButton component by label \"${textLabel}\" with ${target().toString()} as a Container") - fun ComponentFixture.textfield(textLabel: String): JTextComponentFixture = if (target() is Container) textfield( + fun ComponentFixture.textfield(textLabel: String?): JTextComponentFixture = if (target() is Container) textfield( target() as Container, textLabel) else throw UnsupportedOperationException( "Sorry, unable to find JTextComponent (JTextField) component by label \"${textLabel}\" with ${target().toString()} as a Container") @@ -146,12 +148,16 @@ open class GuiTestCase : GuiTestBase() { else throw UnsupportedOperationException( "Sorry, unable to find Popup component with ${target().toString()} as a Container") + fun ComponentFixture.linkLabel(itemName: String) = if (target() is Container) linkLabel( + target() as Container, itemName) + else throw UnsupportedOperationException( + "Sorry, unable to find LinkLabel component with ${target().toString()} as a Container") + //*********COMMON FUNCTIONS WITHOUT CONTEXT fun typeText(text: String) = GuiTestUtil.typeText(text, myRobot, 10) fun shortcut(keyStroke: String) = GuiTestUtil.invokeActionViaShortcut(myRobot, keyStroke) - fun ideFrame() = findIdeFrame()!! fun welcomeFrame() = findWelcomeFrame() fun dialog(title: String? = null): JDialogFixture { @@ -185,7 +191,7 @@ open class GuiTestCase : GuiTestBase() { private fun button(container: Container, name: String): JButtonFixture { val jButton = GuiTestUtil.waitUntilFound(myRobot, container, object : GenericTypeMatcher(JButton::class.java) { - override fun isMatching(jButton: JButton): Boolean = (jButton.text == name) + override fun isMatching(jButton: JButton): Boolean = (jButton.isShowing && jButton.text == name) }) return JButtonFixture(myRobot, jButton) @@ -194,7 +200,7 @@ open class GuiTestCase : GuiTestBase() { private fun combobox(container: Container, labelText: String): JComboBoxFixture { //wait until label has appeared GuiTestUtil.waitUntilFound(myRobot, container, object : GenericTypeMatcher(JLabel::class.java) { - override fun isMatching(jLabel: JLabel): Boolean = (jLabel.text == labelText) + override fun isMatching(jLabel: JLabel): Boolean = (jLabel.isShowing && jLabel.text == labelText) }) return GuiTestUtil.findComboBox(myRobot, container, labelText) @@ -203,7 +209,7 @@ open class GuiTestCase : GuiTestBase() { private fun checkbox(container: Container, labelText: String): CheckBoxFixture { //wait until label has appeared GuiTestUtil.waitUntilFound(myRobot, container, object : GenericTypeMatcher(JCheckBox::class.java) { - override fun isMatching(checkBox: JCheckBox): Boolean = (checkBox.text == labelText) + override fun isMatching(checkBox: JCheckBox): Boolean = (checkBox.isShowing && checkBox.text == labelText) }) return CheckBoxFixture.findByText(labelText, container, myRobot, false) @@ -215,11 +221,27 @@ open class GuiTestCase : GuiTestBase() { private fun radioButton(container: Container, labelText: String) = GuiTestUtil.findRadioButton(myRobot, container, labelText) - private fun textfield(container: Container, labelText: String): JTextComponentFixture { - GuiTestUtil.waitUntilFound(myRobot, container, object : GenericTypeMatcher(JLabel::class.java) { - override fun isMatching(jLabel: JLabel): Boolean = (jLabel.text == labelText) + private fun textfield(container: Container, labelText: String?): JTextComponentFixture { + //if 'textfield()' goes without label + if (labelText == null) { + val jTextField = myRobot.finder().find(ComponentMatcher { component -> component!!.isShowing && component is JTextField }) as JTextField + return JTextComponentFixture(myRobot, jTextField) + } + + val jLabel = GuiTestUtil.waitUntilFound(myRobot, container, object : GenericTypeMatcher(JLabel::class.java) { + override fun isMatching(jLabel: JLabel): Boolean = (jLabel.isShowing && jLabel.text == labelText) }) - return JTextComponentFixture(myRobot, myRobot.finder().findByLabel(labelText, JTextComponent::class.java)) + if (jLabel.labelFor != null && jLabel.labelFor is TextFieldWithBrowseButton) + return JTextComponentFixture(myRobot, (jLabel.labelFor as TextFieldWithBrowseButton).textField) + else + return JTextComponentFixture(myRobot, myRobot.finder().findByLabel(labelText, JTextComponent::class.java)) + } + + private fun linkLabel(container: Container, labelText: String): ComponentFixture, LinkLabel<*>> { + val myLinkLabel = waitUntilFound(myRobot, container, object: GenericTypeMatcher>(LinkLabel::class.java) { + override fun isMatching(someLinkLabel: LinkLabel<*>) = (someLinkLabel.isShowing && (someLinkLabel.text == labelText)) + }) + return ComponentFixture, LinkLabel<*>>(ComponentFixture::class.java, myRobot, myLinkLabel) } private fun popupClick(container: Container, itemName: String) { @@ -252,4 +274,6 @@ open class GuiTestCase : GuiTestBase() { return JTreeFixture(myRobot, myTree) } + //*********SOME EXTENSION FUNCTIONS FOR FIXTURES + }