From f3d2a2804d9ecc382e99ad20ccad3bf638404d4c Mon Sep 17 00:00:00 2001 From: Sergey Karashevich Date: Fri, 28 Apr 2017 15:47:33 +0300 Subject: [PATCH] [gui-test] improve getBoundedLabel() method --- .../testGuiFramework/generators/Generators.kt | 91 ++++++++++++------- 1 file changed, 57 insertions(+), 34 deletions(-) diff --git a/platform/testGuiFramework/src/com/intellij/testGuiFramework/generators/Generators.kt b/platform/testGuiFramework/src/com/intellij/testGuiFramework/generators/Generators.kt index 1561b20388ff..9a9cbc6dbf8f 100644 --- a/platform/testGuiFramework/src/com/intellij/testGuiFramework/generators/Generators.kt +++ b/platform/testGuiFramework/src/com/intellij/testGuiFramework/generators/Generators.kt @@ -24,8 +24,10 @@ import com.intellij.openapi.actionSystem.impl.ActionMenuItem import com.intellij.openapi.editor.impl.EditorComponentImpl import com.intellij.openapi.ui.DialogWrapper import com.intellij.openapi.ui.JBPopupMenu +import com.intellij.openapi.ui.LabeledComponent import com.intellij.openapi.ui.Messages import com.intellij.openapi.util.Ref +import com.intellij.openapi.util.text.StringUtil import com.intellij.openapi.wm.impl.IdeFrameImpl import com.intellij.openapi.wm.impl.ToolWindowImpl import com.intellij.openapi.wm.impl.ToolWindowManagerImpl @@ -38,10 +40,10 @@ import com.intellij.testGuiFramework.fixtures.SettingsTreeFixture import com.intellij.testGuiFramework.framework.GuiTestUtil import com.intellij.testGuiFramework.generators.Utils.clicks import com.intellij.testGuiFramework.generators.Utils.convertSimpleTreeItemToPath -import com.intellij.testGuiFramework.generators.Utils.getBoundedLabelForComboBox +import com.intellij.testGuiFramework.generators.Utils.getBoundedLabel import com.intellij.testGuiFramework.generators.Utils.getCellText import com.intellij.testGuiFramework.generators.Utils.getJTreePath -import com.intellij.testGuiFramework.generators.Utils.getLabel +import com.intellij.testGuiFramework.generators.Utils.getJTreePathItemsString import com.intellij.ui.CheckboxTree import com.intellij.ui.components.JBCheckBox import com.intellij.ui.components.JBList @@ -99,7 +101,7 @@ class ActionLinkGenerator : ComponentCodeGenerator { class JTextFieldGenerator : ComponentCodeGenerator { override fun accept(cmp: Component) = cmp is JTextField - override fun generate(cmp: JTextField, me: MouseEvent, cp: Point) = "textfield(\"${getLabel(cmp)?.text.orEmpty()}\").${clicks( + override fun generate(cmp: JTextField, me: MouseEvent, cp: Point) = "textfield(\"${getBoundedLabel(3, cmp).text.orEmpty()}\").${clicks( me)}" } @@ -153,7 +155,7 @@ class JCheckBoxGenerator : ComponentCodeGenerator { class JComboBoxGenerator : ComponentCodeGenerator> { override fun accept(cmp: Component) = cmp is JComboBox<*> - override fun generate(cmp: JComboBox<*>, me: MouseEvent, cp: Point) = "combobox(\"${getBoundedLabelForComboBox(cmp).text}\")" + override fun generate(cmp: JComboBox<*>, me: MouseEvent, cp: Point) = "combobox(\"${getBoundedLabel(3, cmp).text}\")" } class BasicArrowButtonDelegatedGenerator : ComponentCodeGenerator { @@ -188,10 +190,10 @@ class ProjectViewTreeGenerator : ComponentCodeGenerator { override fun accept(cmp: Component) = cmp is ProjectViewTree private fun JTree.getPath(cp: Point) = this.getClosestPathForLocation(cp.x, cp.y) override fun generate(cmp: ProjectViewTree, me: MouseEvent, cp: Point): String { - val path = getJTreePath(cmp, cmp.getPath(cp)) - if (me.isRightButton()) return "path(\"$path\").rightClick()" - if (me.clickCount == 2) return "path(\"$path\").doubleClick()" - return "path(\"$path\").click()" + val path = getJTreePathItemsString(cmp, cmp.getPath(cp)) + if (me.isRightButton()) return "path($path).rightClick()" + if (me.clickCount == 2) return "path($path).doubleClick()" + return "path($path).click()" } } @@ -573,46 +575,67 @@ object Utils { return (0..path.pathCount - 1).map { path.getPathComponent(it).toString() }.filter(String::isNotEmpty).joinToString("/") } - fun getBoundedLabelForComboBox(cb: JComboBox<*>): JLabel { - val robot = BasicRobot.robotWithCurrentAwtHierarchyWithoutScreenLock() + /** + * @hierarchyLevel: started from 1 to see bounded label for a component itself + */ + fun getBoundedLabel(hierarchyLevel: Int, component: Component): JLabel { - val findBoundedLabel: (Component) -> JLabel? = { component -> - try { - robot.finder().find(component.parent as Container, object : GenericTypeMatcher(JLabel::class.java) { - override fun isMatching(label: JLabel): Boolean { - return label.labelFor != null && label.labelFor == component - } - }) - } - catch(e: ComponentLookupException) { - null + var currentComponent = component + if (hierarchyLevel < 1) throw Exception("Hierarchy level (actual is $hierarchyLevel) should starts from 1 to see bounded label for a component itself") + + for (i in 1..hierarchyLevel) { + val boundedLabel = findBoundedLabel(currentComponent) + if (boundedLabel != null) return boundedLabel + else { + if (currentComponent.parent == null) break + currentComponent = currentComponent.parent } } - val bounded1 = findBoundedLabel(cb) - if (bounded1 !== null) return bounded1 - - val bounded2 = findBoundedLabel(cb.parent) - if (bounded2 !== null) return bounded2 - - val bounded3 = findBoundedLabel(cb.parent!!.parent) - if (bounded3 !== null) return bounded3 - - throw ComponentLookupException("Unable to find bounded label in 2 levels from JComboBox") + throw ComponentLookupException("Unable to find bounded label in ${hierarchyLevel - 1} level(s) from ${component.toString()}") } + private fun findBoundedLabel(component: Component): JLabel? { + val robot = BasicRobot.robotWithCurrentAwtHierarchyWithoutScreenLock() + var resultLabel: JLabel? = null + + if (component is LabeledComponent<*>) return component.label + try { + resultLabel = robot.finder().find(component.parent as Container, object : GenericTypeMatcher(JLabel::class.java) { + override fun isMatching(label: JLabel): Boolean { + return label.labelFor != null && label.labelFor == component + } + }) + } + catch(e: ComponentLookupException) { + resultLabel = null + } + return resultLabel + } + fun getJTreePath(cmp: JTree, path: TreePath): String { + val stringBuilder = getJTreePathArray(cmp, path) + return StringUtil.join(stringBuilder, "/") + } + + fun getJTreePathItemsString(cmp: JTree, path: TreePath): String { + return getJTreePathArray(cmp, path) + .map { StringUtil.wrapWithDoubleQuote(it) } + .reduceRight({ s, s1 -> "$s, $s1" }) + } + + private fun getJTreePathArray(cmp: JTree, path: TreePath): ArrayList { var treePath = path - val result = StringBuilder() + val result = ArrayList() val bcr = org.fest.swing.driver.BasicJTreeCellReader() while (treePath.pathCount != 1 || (cmp.isRootVisible && treePath.pathCount == 1)) { - val valueAt = bcr.valueAt(cmp, treePath.lastPathComponent) - result.insert(0, "$valueAt${if (!result.isEmpty()) "/" else ""}") + val valueAt = bcr.valueAt(cmp, treePath.lastPathComponent) ?: "null" + result.add(0, valueAt) if (treePath.pathCount == 1) break else treePath = treePath.parentPath } - return result.toString() + return result } }