Skip to content

Commit

Permalink
Fix broken 'click span' Android tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartinesp committed Feb 29, 2024
1 parent 06d9fcb commit aaaa69e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import android.widget.TextView
import androidx.core.text.buildSpannedString
import androidx.core.text.inSpans
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.withText
Expand All @@ -17,12 +16,14 @@ import io.element.android.wysiwyg.test.R
import io.element.android.wysiwyg.test.utils.FakeLinkClickedListener
import io.element.android.wysiwyg.test.utils.TestActivity
import io.element.android.wysiwyg.test.utils.TextViewActions
import io.element.android.wysiwyg.test.utils.clickXY
import io.element.android.wysiwyg.view.spans.CustomMentionSpan
import io.element.android.wysiwyg.view.spans.LinkSpan
import io.element.android.wysiwyg.view.spans.PillSpan
import org.junit.Rule
import org.junit.Test


internal class EditorStyledTextViewTest {

@get:Rule
Expand Down Expand Up @@ -71,7 +72,7 @@ internal class EditorStyledTextViewTest {
.perform(TextViewActions.setText(urlSpanText, TextView.BufferType.SPANNABLE))
.perform(TextViewActions.setOnLinkClickedListener(fakeLinkClickedListener))
.check(matches(withText(HELLO_WORLD)))
.perform(ViewActions.click())
.perform(clickXY(0f, 0f))

fakeLinkClickedListener.assertLinkClicked(url = URL)
}
Expand All @@ -87,7 +88,7 @@ internal class EditorStyledTextViewTest {
.perform(TextViewActions.setText(linkSpanText, TextView.BufferType.SPANNABLE))
.perform(TextViewActions.setOnLinkClickedListener(fakeLinkClickedListener))
.check(matches(withText(HELLO_WORLD)))
.perform(ViewActions.click())
.perform(clickXY(0f, 0f))

fakeLinkClickedListener.assertLinkClicked(url = URL)
}
Expand All @@ -103,7 +104,7 @@ internal class EditorStyledTextViewTest {
.perform(TextViewActions.setText(pillSpanText, TextView.BufferType.SPANNABLE))
.perform(TextViewActions.setOnLinkClickedListener(fakeLinkClickedListener))
.check(matches(withText(HELLO_WORLD)))
.perform(ViewActions.click())
.perform(clickXY(0f, 0f))

fakeLinkClickedListener.assertLinkClicked(url = URL)
}
Expand All @@ -119,7 +120,7 @@ internal class EditorStyledTextViewTest {
.perform(TextViewActions.setText(mentionSpanText, TextView.BufferType.SPANNABLE))
.perform(TextViewActions.setOnLinkClickedListener(fakeLinkClickedListener))
.check(matches(withText(HELLO_WORLD)))
.perform(ViewActions.click())
.perform(clickXY(0f, 0f))

fakeLinkClickedListener.assertLinkClicked(url = URL)
}
Expand All @@ -130,15 +131,16 @@ internal class EditorStyledTextViewTest {
.perform(TextViewActions.setHtml(MENTION_HTML))
.perform(TextViewActions.setOnLinkClickedListener(fakeLinkClickedListener))
.check(matches(withText(MENTION_TEXT)))
.perform(ViewActions.click())
.perform(clickXY(0f, 0f))

fakeLinkClickedListener.assertLinkClicked(MENTION_URI)
}
}

object DummyReplacementSpan : ReplacementSpan() {
override fun getSize(paint: Paint, text: CharSequence?, start: Int, end: Int, fm: Paint.FontMetricsInt?): Int = 1
override fun getSize(paint: Paint, text: CharSequence?, start: Int, end: Int, fm: Paint.FontMetricsInt?): Int = 100

override fun draw(canvas: Canvas, text: CharSequence?, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) = Unit

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.element.android.wysiwyg.test.utils

import android.view.InputDevice
import android.view.MotionEvent
import androidx.test.espresso.action.GeneralClickAction
import androidx.test.espresso.action.Press
import androidx.test.espresso.action.Tap

fun clickXY(x: Float, y: Float): GeneralClickAction {
return GeneralClickAction(
Tap.SINGLE,
{ view ->
val screenPos = IntArray(2)
view.getLocationOnScreen(screenPos)
val screenX = screenPos[0] + x
val screenY = screenPos[1] + y
floatArrayOf(screenX, screenY)
},
Press.FINGER,
InputDevice.SOURCE_MOUSE,
MotionEvent.BUTTON_PRIMARY,
)
}

0 comments on commit aaaa69e

Please sign in to comment.