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

Commit e711c0f2 authored by Behnam Heydarshahi's avatar Behnam Heydarshahi Committed by Automerger Merge Worker
Browse files

Merge "PanelSlicesAdapter: load slice label safely" into udc-qpr-dev am: c46e8546 am: a38edf1a

parents f54ad5e0 a38edf1a
Loading
Loading
Loading
Loading
+6 −8
Original line number Diff line number Diff line
@@ -61,15 +61,12 @@ public class PanelSlicesAdapter
    private final List<LiveData<Slice>> mSliceLiveData;
    private final int mMetricsCategory;
    private final PanelFragment mPanelFragment;
    private final String mSliceClickActionLabel;

    public PanelSlicesAdapter(
            PanelFragment fragment, Map<Uri, LiveData<Slice>> sliceLiveData, int metricsCategory) {
        mPanelFragment = fragment;
        mSliceLiveData = new ArrayList<>(sliceLiveData.values());
        mMetricsCategory = metricsCategory;
        mSliceClickActionLabel = mPanelFragment.getContext().getString(
                R.string.accessibility_action_label_panel_slice);
    }

    @NonNull
@@ -77,7 +74,7 @@ public class PanelSlicesAdapter
    public SliceRowViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        final Context context = viewGroup.getContext();
        final LayoutInflater inflater = LayoutInflater.from(context);
        View view;
        final View view;
        if (viewType == PanelContent.VIEW_TYPE_SLIDER) {
            view = inflater.inflate(R.layout.panel_slice_slider_row, viewGroup, false);
        } else {
@@ -189,7 +186,6 @@ public class PanelSlicesAdapter
                    return;
                }
                sliceView.setTag(ROW_VIEW_TAG, new Object());

                sliceView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                    @Override
                    public void onLayoutChange(View v, int left, int top, int right, int bottom,
@@ -208,15 +204,17 @@ public class PanelSlicesAdapter
         * Update the action label for TalkBack to be more specific
         * @param view the RowView within the Slice
         */
        private void setActionLabel(View view) {
        @VisibleForTesting void setActionLabel(View view) {
            view.setAccessibilityDelegate(new View.AccessibilityDelegate() {
                @Override
                public void onInitializeAccessibilityNodeInfo(View host,
                        AccessibilityNodeInfo info) {
                    super.onInitializeAccessibilityNodeInfo(host, info);

                    AccessibilityNodeInfo.AccessibilityAction customClick =
                            new AccessibilityNodeInfo.AccessibilityAction(
                                    ACTION_CLICK, mSliceClickActionLabel);
                            new AccessibilityNodeInfo.AccessibilityAction(ACTION_CLICK, host
                                    .getResources()
                                    .getString(R.string.accessibility_action_label_panel_slice));
                    info.addAction(customClick);
                }
            });
+61 −2
Original line number Diff line number Diff line
@@ -33,9 +33,13 @@ import static org.mockito.Mockito.when;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.lifecycle.LiveData;
import androidx.slice.Slice;
@@ -44,6 +48,7 @@ import com.android.settings.R;
import com.android.settings.panel.PanelSlicesAdapter.SliceRowViewHolder;
import com.android.settings.testutils.FakeFeatureFactory;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -94,7 +99,6 @@ public class PanelSlicesAdapterTest {
                                .get()
                                .getSupportFragmentManager()
                                .findFragmentById(R.id.main_content));

    }

    private void addTestLiveData(Uri uri) {
@@ -106,6 +110,61 @@ public class PanelSlicesAdapterTest {
        mData.put(uri, liveData);
    }

    /**
     * Edge case where fragment context is not available.
     */
    @Test
    public void withPanelFragmentContextNull_createAdapter_noExceptionThrown() {
        when(mPanelFragment.getContext()).thenReturn(null);

        final PanelSlicesAdapter adapter = spy(new PanelSlicesAdapter(mPanelFragment, mData, 0));

        Assert.assertNotNull(adapter);
    }

    /**
     * ViewHolder should load and set the action label correctly.
     */
    @Test
    public void setActionLabel_loadsActionLabel() {
        addTestLiveData(VOLUME_NOTIFICATION_URI);
        final PanelSlicesAdapter adapter = new PanelSlicesAdapter(mPanelFragment, mData, 0);
        final ViewGroup view = new FrameLayout(mContext);
        final SliceRowViewHolder viewHolder = adapter.onCreateViewHolder(view, VIEW_TYPE_SLIDER);

        // now let's see if setActionLabel can load and set the label correctly.
        LinearLayout llRow = new LinearLayout(mContext);
        viewHolder.setActionLabel(llRow);

        boolean isLabelSet = isActionLabelSet(llRow);
        Assert.assertTrue("Action label was not set correctly.", isLabelSet);
    }

    /**
     * @param rowView the view with id row_view
     * @return whether the accessibility action label is set
     */
    private boolean isActionLabelSet(View rowView) {
        View.AccessibilityDelegate delegate = rowView.getAccessibilityDelegate();
        if (delegate == null) {
            return false;
        }
        AccessibilityNodeInfo node = new AccessibilityNodeInfo(rowView);
        delegate.onInitializeAccessibilityNodeInfo(rowView, node);

        boolean foundLabel = false;
        final String expectedLabel =
                mContext.getString(R.string.accessibility_action_label_panel_slice);
        for (AccessibilityNodeInfo.AccessibilityAction action : node.getActionList()) {
            if (action.equals(AccessibilityNodeInfo.AccessibilityAction.ACTION_CLICK)
                    && TextUtils.equals(action.getLabel(), expectedLabel)) {
                foundLabel = true;
                break;
            }
        }
        return foundLabel;
    }

    @Test
    public void sizeOfAdapter_shouldNotExceedMaxNum() {
        for (int i = 0; i < MAX_NUM_OF_SLICES + 2; i++) {
@@ -141,7 +200,7 @@ public class PanelSlicesAdapterTest {
    }

    @Test
    public void onCreateViewHolder_viewTypeSlider_verifyActionLabelSet() {
    public void onBindViewHolder_viewTypeSlider_verifyActionLabelSet() {
        addTestLiveData(VOLUME_NOTIFICATION_URI);

        final PanelSlicesAdapter adapter =