Remove selection on nonselectable text (which would be there if a user tapped Linkified text) if a TextView loses focus.
Test: bit FrameworksCoreTests:android.widget.TextViewActivityTest
Bug: 67629726
Change-Id: Ifc3039f0c814c422b2d727f837ca9c88abc2ebe8
diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java
index 7bb0db1..ac2004d 100644
--- a/core/java/android/widget/Editor.java
+++ b/core/java/android/widget/Editor.java
@@ -1299,6 +1299,16 @@
if (mSelectionModifierCursorController != null) {
mSelectionModifierCursorController.resetTouchOffsets();
}
+
+ ensureNoSelectionIfNonSelectable();
+ }
+ }
+
+ private void ensureNoSelectionIfNonSelectable() {
+ // This could be the case if a TextLink has been tapped.
+ if (!mTextView.textCanBeSelected() && mTextView.hasSelection()) {
+ Selection.setSelection((Spannable) mTextView.getText(),
+ mTextView.length(), mTextView.length());
}
}
@@ -1382,6 +1392,8 @@
// Don't leave us in the middle of a batch edit. Same as in onFocusChanged
ensureEndedBatchEdit();
+
+ ensureNoSelectionIfNonSelectable();
}
}
diff --git a/core/tests/coretests/res/layout/activity_text_view.xml b/core/tests/coretests/res/layout/activity_text_view.xml
index dca1656..d5be87d 100644
--- a/core/tests/coretests/res/layout/activity_text_view.xml
+++ b/core/tests/coretests/res/layout/activity_text_view.xml
@@ -24,6 +24,9 @@
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
+ <Space
+ android:layout_width="1dp"
+ android:layout_height="60dp"/>
<TextView
android:id="@+id/nonselectable_textview"
diff --git a/core/tests/coretests/src/android/widget/TextViewActivityTest.java b/core/tests/coretests/src/android/widget/TextViewActivityTest.java
index 69e5670..7f4f9f7 100644
--- a/core/tests/coretests/src/android/widget/TextViewActivityTest.java
+++ b/core/tests/coretests/src/android/widget/TextViewActivityTest.java
@@ -66,6 +66,7 @@
import android.support.test.filters.MediumTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
+import android.support.test.uiautomator.UiDevice;
import android.test.suitebuilder.annotation.Suppress;
import android.text.InputType;
import android.text.Selection;
@@ -311,15 +312,69 @@
@Test
public void testToolbarAppearsAfterLinkClicked() throws Throwable {
- runToolbarAppearsAfterLinkClickedTest(R.id.textview);
+ TextLinks.TextLink textLink = addLinkifiedTextToTextView(R.id.textview);
+ int position = (textLink.getStart() + textLink.getEnd()) / 2;
+ onView(withId(R.id.textview)).perform(clickOnTextAtIndex(position));
+ sleepForFloatingToolbarPopup();
+ assertFloatingToolbarIsDisplayed();
+
}
@Test
public void testToolbarAppearsAfterLinkClickedNonselectable() throws Throwable {
- runToolbarAppearsAfterLinkClickedTest(R.id.nonselectable_textview);
+ TextLinks.TextLink textLink = addLinkifiedTextToTextView(R.id.nonselectable_textview);
+ int position = (textLink.getStart() + textLink.getEnd()) / 2;
+ onView(withId(R.id.nonselectable_textview)).perform(clickOnTextAtIndex(position));
+ sleepForFloatingToolbarPopup();
+ assertFloatingToolbarIsDisplayed();
}
- private void runToolbarAppearsAfterLinkClickedTest(int id) throws Throwable {
+ @Test
+ public void testSelectionRemovedWhenNonselectableTextLosesFocus() throws Throwable {
+ // Add a link to both selectable and nonselectable TextViews:
+ TextLinks.TextLink textLink = addLinkifiedTextToTextView(R.id.textview);
+ int selectablePosition = (textLink.getStart() + textLink.getEnd()) / 2;
+ textLink = addLinkifiedTextToTextView(R.id.nonselectable_textview);
+ int nonselectablePosition = (textLink.getStart() + textLink.getEnd()) / 2;
+ TextView selectableTextView = mActivity.findViewById(R.id.textview);
+ TextView nonselectableTextView = mActivity.findViewById(R.id.nonselectable_textview);
+
+ onView(withId(R.id.nonselectable_textview))
+ .perform(clickOnTextAtIndex(nonselectablePosition));
+ sleepForFloatingToolbarPopup();
+ assertFloatingToolbarIsDisplayed();
+ assertTrue(nonselectableTextView.hasSelection());
+
+ onView(withId(R.id.textview)).perform(clickOnTextAtIndex(selectablePosition));
+ sleepForFloatingToolbarPopup();
+ assertFloatingToolbarIsDisplayed();
+
+ assertTrue(selectableTextView.hasSelection());
+ assertFalse(nonselectableTextView.hasSelection());
+ }
+
+ @Test
+ public void testSelectionRemovedFromNonselectableTextWhenWindowLosesFocus() throws Throwable {
+ TextLinks.TextLink textLink = addLinkifiedTextToTextView(R.id.nonselectable_textview);
+ int nonselectablePosition = (textLink.getStart() + textLink.getEnd()) / 2;
+ TextView nonselectableTextView = mActivity.findViewById(R.id.nonselectable_textview);
+
+ onView(withId(R.id.nonselectable_textview))
+ .perform(clickOnTextAtIndex(nonselectablePosition));
+ sleepForFloatingToolbarPopup();
+ assertFloatingToolbarIsDisplayed();
+ assertTrue(nonselectableTextView.hasSelection());
+
+ UiDevice device = UiDevice.getInstance(mInstrumentation);
+ device.openNotification();
+ Thread.sleep(2000);
+ device.pressBack();
+ Thread.sleep(2000);
+
+ assertFalse(nonselectableTextView.hasSelection());
+ }
+
+ private TextLinks.TextLink addLinkifiedTextToTextView(int id) throws Throwable {
TextView textView = mActivity.findViewById(id);
useSystemDefaultTextClassifier();
TextClassificationManager textClassificationManager =
@@ -338,11 +393,7 @@
// Wait for the UI thread to refresh
Thread.sleep(1000);
- TextLinks.TextLink textLink = links.getLinks().iterator().next();
- int position = (textLink.getStart() + textLink.getEnd()) / 2;
- onView(withId(id)).perform(clickOnTextAtIndex(position));
- sleepForFloatingToolbarPopup();
- assertFloatingToolbarIsDisplayed();
+ return links.getLinks().iterator().next();
}
@Test