diff options
author | Maurice Lam <yukl@google.com> | 2018-04-04 19:45:31 -0700 |
---|---|---|
committer | Maurice Lam <yukl@google.com> | 2018-04-06 12:00:01 -0700 |
commit | e4475dcf52c1235a7d9756b454fa81fb4b54726c (patch) | |
tree | ae8b3673061cddfd95d880ef7d65c1c0a50c1610 /library/test | |
parent | 618a4449bb3e9be43586040ea2fb9a6371365ae7 (diff) | |
download | setupwizard-e4475dcf52c1235a7d9756b454fa81fb4b54726c.tar.gz |
Add workaround for touch event propagation
In TextView with links, onTouchEvent always return true regardless of
return value from the movement method because View#onTouchEvent is
hard coded to return true whenever the view is clickable.
In RichTextView, add a layer of abstraction to allow the movement
method to override the return value. TouchableLinkMovementMethod is
an implementation added, which will consume the event only when a
link is being touched.
Test: ./gradlew test
Bug: 77338508
Change-Id: I761579d6f153a41beb979acd88a4090eb3998cf3
Diffstat (limited to 'library/test')
-rw-r--r-- | library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java b/library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java index 2e28b48..f77de68 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java @@ -16,11 +16,14 @@ package com.android.setupwizardlib.view; +import static com.google.common.truth.Truth.assertThat; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; @@ -35,10 +38,12 @@ import android.text.Annotation; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.style.TextAppearanceSpan; +import android.view.MotionEvent; import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; import com.android.setupwizardlib.span.LinkSpan; import com.android.setupwizardlib.span.LinkSpan.OnLinkClickListener; +import com.android.setupwizardlib.view.TouchableMovementMethod.TouchableLinkMovementMethod; import org.junit.Test; import org.junit.runner.RunWith; @@ -62,6 +67,8 @@ public class RichTextViewTest { final CharSequence text = textView.getText(); assertTrue("Text should be spanned", text instanceof Spanned); + assertThat(textView.getMovementMethod()).isInstanceOf(TouchableLinkMovementMethod.class); + Object[] spans = ((Spanned) text).getSpans(0, text.length(), Annotation.class); assertEquals("Annotation should be removed " + Arrays.toString(spans), 0, spans.length); @@ -112,6 +119,44 @@ public class RichTextViewTest { } @Test + public void onTouchEvent_clickOnLinks_shouldReturnTrue() { + Annotation link = new Annotation("link", "foobar"); + SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); + ssb.setSpan(link, 0, 2, 0 /* flags */); + + RichTextView textView = new RichTextView(application); + textView.setText(ssb); + + TouchableLinkMovementMethod mockMovementMethod = mock(TouchableLinkMovementMethod.class); + textView.setMovementMethod(mockMovementMethod); + + MotionEvent motionEvent = + MotionEvent.obtain(123, 22, MotionEvent.ACTION_DOWN, 0, 0, 0); + doReturn(motionEvent).when(mockMovementMethod).getLastTouchEvent(); + doReturn(true).when(mockMovementMethod).isLastTouchEventHandled(); + assertThat(textView.onTouchEvent(motionEvent)).isTrue(); + } + + @Test + public void onTouchEvent_clickOutsideLinks_shouldReturnFalse() { + Annotation link = new Annotation("link", "foobar"); + SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); + ssb.setSpan(link, 0, 2, 0 /* flags */); + + RichTextView textView = new RichTextView(application); + textView.setText(ssb); + + TouchableLinkMovementMethod mockMovementMethod = mock(TouchableLinkMovementMethod.class); + textView.setMovementMethod(mockMovementMethod); + + MotionEvent motionEvent = + MotionEvent.obtain(123, 22, MotionEvent.ACTION_DOWN, 0, 0, 0); + doReturn(motionEvent).when(mockMovementMethod).getLastTouchEvent(); + doReturn(false).when(mockMovementMethod).isLastTouchEventHandled(); + assertThat(textView.onTouchEvent(motionEvent)).isFalse(); + } + + @Test public void testTextStyle() { Annotation link = new Annotation("textAppearance", "foobar"); SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); |