Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5f2f820c authored by Richard Ledley's avatar Richard Ledley
Browse files

Remove selection on nonselectable text (which would be there if a user tapped...

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
parent 9d3986bd
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -1299,6 +1299,16 @@ public class Editor {
            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 @@ public class Editor {

            // Don't leave us in the middle of a batch edit. Same as in onFocusChanged
            ensureEndedBatchEdit();

            ensureNoSelectionIfNonSelectable();
        }
    }

+3 −0
Original line number Diff line number Diff line
@@ -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"
+59 −8
Original line number Diff line number Diff line
@@ -66,6 +66,7 @@ import android.support.test.espresso.action.EspressoKey;
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 @@ public class TextViewActivityTest {

    @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();
    }

    @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 void runToolbarAppearsAfterLinkClickedTest(int id) throws Throwable {
    private TextLinks.TextLink addLinkifiedTextToTextView(int id) throws Throwable {
        TextView textView = mActivity.findViewById(id);
        useSystemDefaultTextClassifier();
        TextClassificationManager textClassificationManager =
@@ -338,11 +393,7 @@ public class TextViewActivityTest {
        // 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