Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make selector field multiline #163

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@ import com.github.tarcv.testingteam.surveyoridea.gui.fixtures.idea
import com.github.tarcv.testingteam.surveyoridea.gui.fixtures.locateElementToolWindow
import com.github.tarcv.testingteam.surveyoridea.hasJavaSupport
import com.intellij.remoterobot.client.IdeaSideException
import com.intellij.remoterobot.fixtures.EditorFixture
import com.intellij.remoterobot.utils.keyboard
import com.intellij.remoterobot.utils.waitFor
import org.apache.commons.text.StringEscapeUtils
import org.junit.jupiter.api.Assumptions
import org.junit.jupiter.api.Test
import java.awt.Point
import java.lang.Thread.sleep
import java.time.Duration
import kotlin.test.assertEquals

class ParsingTest : BaseTestProjectTests() {
class EditingTest : BaseTestProjectTests() {
@Test
fun testDroidSelectorParsing() = with(remoteRobot) {
Assumptions.assumeTrue(hasJavaSupport, "This feature requires an IDE with Java support")
fun testDroidSelectorEditing() {
assertDroidSelectorEditing("""new UiSelector().""", 1)
}

@Test
fun testDroidSelectorMultilineEditing() {
assertDroidSelectorEditing("""new UiSelector()${'\n'}.index(1).""", 2)
}

private fun assertDroidSelectorEditing(selector: String, expectedLineCount: Int) = with(remoteRobot) {
idea {
openFileInTestProject(droidAutomatorSnapshotFile, "editorWithSnapshot")

Expand All @@ -31,8 +38,9 @@ class ParsingTest : BaseTestProjectTests() {
locateElementToolWindow {
// Escaping is required due to simple concatenation in the text#set implementation
editor.apply {
val editorLanguage = callJs<String>(
runInEdt = true, script = """
if (hasJavaSupport) {
val editorLanguage = callJs<String>(
runInEdt = true, script = """
importPackage(com.intellij.openapi.fileEditor.impl.text)
importPackage(com.intellij.openapi.project.ex)
importPackage(com.intellij.psi)
Expand All @@ -42,8 +50,9 @@ class ParsingTest : BaseTestProjectTests() {
.getLanguage()
.toString()
""".trimIndent()
)
assertEquals("Language: JAVA", editorLanguage)
)
assertEquals("Language: JAVA", editorLanguage)
}

sleep(5_000)
click(Point(5, 5))
Expand All @@ -54,16 +63,17 @@ class ParsingTest : BaseTestProjectTests() {

// Escaping is required due to how enterText is implemented
enterText(
StringEscapeUtils.escapeEcmaScript("""new UiSelector().""")
StringEscapeUtils.escapeEcmaScript(selector)
.replace("\\\"", "\"")
)
}
}

val popupItems = waitFor(Duration.ofSeconds(10), functionWithCondition = {
val items: List<String> = try {
editor.callJs(
runInEdt = true, script = """
if (hasJavaSupport) {
val popupItems = waitFor(Duration.ofSeconds(10), functionWithCondition = {
val items: List<String> = try {
editor.callJs(
runInEdt = true, script = """
importPackage(com.intellij.codeInsight.lookup)
const model = LookupManager.getActiveLookup(local.get('editor')).getList().getModel()
const listItems = new ArrayList();
Expand All @@ -72,24 +82,34 @@ class ParsingTest : BaseTestProjectTests() {
}
listItems
""".trimIndent()
)
} catch (e: IdeaSideException) {
emptyList()
)
} catch (e: IdeaSideException) {
emptyList()
}
(items.isNotEmpty() && items.contains("resourceId")) to items
})
assert(popupItems.contains("index")) { "contains 'index'" }
assert(popupItems.contains("resourceId")) { "contains 'resourceId'" }
assert(popupItems.contains("text")) { "contains 'text'" }
[email protected] {
// Check the lookup popup is actually displayed by comparing it with actually found jList
assertEquals(collectItems().size, popupItems.size)
}
(items.isNotEmpty() && items.contains("resourceId")) to items
})
assert(popupItems.contains("index")) { "contains 'index'" }
assert(popupItems.contains("resourceId")) { "contains 'resourceId'" }
assert(popupItems.contains("text")) { "contains 'text'" }
[email protected] {
// Check the lookup popup is actually displayed by comparing it with actually found jList
assertEquals(collectItems().size, popupItems.size)
// At this point it can be assumed locator in the editor is correctly highlighted
// (as it has correct language (Java) and has working auto-completion)
}

// At this point it can be assumed locator in the editor is correctly highlighted
// (as it has correct language (Java) and has working auto-completion)
assertEquals(expectedLineCount, editor.getLineCount())
}

}
}
}
}

private fun EditorFixture.getLineCount(): Int {
return callJs(
runInEdt = true, script = """
const document = local.get('document')
document.getLineCount()
""".trimIndent())
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import java.awt.Point
import java.awt.event.KeyEvent
import java.lang.Thread.sleep

private const val singleLineLocator = """new UiSelector().resourceIdMatches(".+/celsiusText")"""
private const val multiLineLocator = """new UiSelector()${'\n'}.resourceIdMatches(${'\n'}".+/celsiusText")"""

class LocateActionUiTests : BaseTestProjectTests() {
@Test
fun testLocatingFromKeyboard() = verifyLocatingFrom {
fun testLocatingFromKeyboard() = verifyLocatingFrom(singleLineLocator) {
locateElementToolWindow {
editor.apply {
keyboard {
Expand All @@ -31,7 +34,7 @@ class LocateActionUiTests : BaseTestProjectTests() {
}

@Test
fun testLocatingFromMenu() = verifyLocatingFrom {
fun testLocatingFromMenu() = verifyLocatingFrom(singleLineLocator) {
locateElementToolWindow {
selectInMenuBar(
"Edit",
Expand All @@ -42,13 +45,20 @@ class LocateActionUiTests : BaseTestProjectTests() {
}

@Test
fun testLocatingFromToolButton() = verifyLocatingFrom {
locateElementToolWindow {
locateButton.click()
fun testLocatingFromToolButton() = assertLocatingFromToolButton(singleLineLocator)

@Test
fun testLocatingMultilineFromToolButton() = assertLocatingFromToolButton(multiLineLocator)

private fun assertLocatingFromToolButton(locator: String) {
verifyLocatingFrom(locator) {
locateElementToolWindow {
locateButton.click()
}
}
}

private fun verifyLocatingFrom(triggerActionWithBlock: IdeaFrame.() -> Unit) = with(remoteRobot) {
private fun verifyLocatingFrom(locator: String, triggerActionWithBlock: IdeaFrame.() -> Unit) = with(remoteRobot) {
idea {
openFileInTestProject(droidAutomatorSnapshotFile, "editorWithSnapshot")

Expand All @@ -73,9 +83,13 @@ class LocateActionUiTests : BaseTestProjectTests() {

// Escaping is required due to how enterText is implemented
enterText(
StringEscapeUtils.escapeEcmaScript("""new UiSelector().resourceIdMatches(".+/celsiusText")""")
StringEscapeUtils.escapeEcmaScript(locator)
.replace("\\\"", "\"")
)

repeat(5) { // make sure there is no extra automagically added braces
key(KeyEvent.VK_DELETE)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class IdeaFrame(remoteRobot: RemoteRobot, remoteComponent: RemoteComponent) :
}

fun selectInMenuBar(vararg items: String) {
attempt {
attempt(5) {
menuBar.select(*items)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ abstract class LocateToolWindow(protected val project: Project) {
editorField
)

editorField.setOneLineMode(false)
initSelectorField(editorField)

project.getService(LocateToolHoldingService::class.java).registerToolWindow(this)
Expand Down
Loading