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

Commit ff7dd896 authored by Wenbo Jie's avatar Wenbo Jie
Browse files

[DocsUI] Click search chip should reset scroll view

When we have too many search chips which can't be display
in one screen (e.g. on compact size screen), clicking the
chip should reset the horizontal scroll view so the checked
chip can be seen.

Check the attached bug for the before/after comparison.

Bug: 403433043
Test: m DocumentsUIGoogle && manual inspection
Test: atest DocumentsUIGoogleTests:com.android.documentsui.queries.SearchChipViewManagerTest#testChipChecked_resetScroll
Flag: com.android.documentsui.flags.use_material3
Change-Id: Ib0bcd7295953940215c967e021f8ca96e465d6f1
parent e1604abe
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -518,7 +518,7 @@ public class SearchChipViewManager {
            }

            // Let the first checked chip can be shown.
            View parent = (View) mChipGroup.getParent();
            View parent = (View) mChipGroup.getParent().getParent();
            if (parent instanceof HorizontalScrollView) {
                final int scrollToX = isRtl ? parent.getWidth() : 0;
                ((HorizontalScrollView) parent).smoothScrollTo(scrollToX, 0);
+35 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.documentsui.queries;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.spy;

@@ -35,6 +36,9 @@ import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.provider.DocumentsContract;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import androidx.test.ext.junit.runners.AndroidJUnit4;
@@ -235,6 +239,37 @@ public final class SearchChipViewManagerTest {
        assertThat(View.VISIBLE).isEqualTo(mirror.getVisibility());
    }

    @Test
    public void testChipChecked_resetScroll() {
        // Mock chip group's parent chain according to search_chip_row.xml.
        FrameLayout parent = spy(new FrameLayout(mContext));
        HorizontalScrollView grandparent = spy(new HorizontalScrollView(mContext));
        parent.addView(mChipGroup);
        grandparent.addView(parent);
        // Verify that getParent().getParent() returns the HorizontalScrollView mock.
        ViewParent result = mChipGroup.getParent().getParent();
        assertEquals(grandparent, result);

        mSearchChipViewManager.initChipSets(
                new String[] {"image/*", "audio/*", "video/*", "text/*"});
        mSearchChipViewManager.updateChips(new String[] {"*/*"});

        // Manually set HorizontalScrollView's scrollX to something larger than 0.
        grandparent.scrollTo(100, 0);
        assertTrue(grandparent.getScaleX() > 0);

        assertEquals(6, mChipGroup.getChildCount());
        Chip lastChip = (Chip) mChipGroup.getChildAt(5);

        // chip.setChecked will trigger reorder animation, which needs to be run inside
        // the looper thread.
        InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
            // Check last chip will move it to the first child and reset scroll view.
            lastChip.setChecked(true);
            assertEquals(0, grandparent.getScrollX());
        });
    }

    private static Set<SearchChipData> getFakeSearchChipDataList() {
        final Set<SearchChipData> chipDataList = new HashSet<>();
        chipDataList.add(new SearchChipData(CHIP_TYPE, 0 /* titleRes */, TEST_MIME_TYPES));