leanback/src/androidTest/java/androidx/leanback/widget/GridWidgetTest.java
@Test
    public void simulatesAccessibilityOfItemsBeingPushedOut() throws Throwable {
        Intent intent = new Intent();
        intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID, R.layout.horizontal_grid);
        intent.putExtra(GridActivity.EXTRA_NUM_ITEMS, 100);
        intent.putExtra(GridActivity.EXTRA_STAGGERED, false);
        mOrientation = BaseGridView.HORIZONTAL;
        mNumRows = 3;

        initActivity(intent);

        final HashSet<View> moveAnimationViews = new HashSet();
        mActivity.mImportantForAccessibilityListener =
                new GridActivity.ImportantForAccessibilityListener() {
            RecyclerView.LayoutManager mLM = mGridView.getLayoutManager();
            @Override
            public void onImportantForAccessibilityChanged(View view, int newValue) {
                // simulates talkack, having setImportantForAccessibility to call
                // onInitializeAccessibilityNodeInfoForItem() for the DISAPPEARING items.
                if (moveAnimationViews.contains(view)) {
                    AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain();
                    mLM.onInitializeAccessibilityNodeInfoForItem(
                            null, null, view, info);
                }
            }
        };
        final int lastPos = mGridView.getChildAdapterPosition(
                mGridView.getChildAt(mGridView.getChildCount() - 1));
        final int numItemsToPushOut = mNumRows;
        for (int i = 0; i < numItemsToPushOut; i++) {
            moveAnimationViews.add(
                    mGridView.getChildAt(mGridView.getChildCount() - 1 - i));
        }
        mActivityTestRule.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mGridView.setItemAnimator(new DefaultItemAnimator() {
                    @Override
                    public void onMoveFinished(RecyclerView.ViewHolder item) {
                        moveAnimationViews.remove(item.itemView);
                    }
                });
                mGridView.getLayoutManager().setItemPrefetchEnabled(false);
            }
        });
        mActivityTestRule.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                final int[] newItems = new int[numItemsToPushOut];
                final int newItemValue = mActivity.mItemLengths[0] + 1;
                for (int i = 0; i < newItems.length; i++) {
                    newItems[i] = newItemValue;
                }
                mActivity.addItems(lastPos - numItemsToPushOut + 1, newItems);
            }
        });
        while (moveAnimationViews.size() != 0) {
            Thread.sleep(100);
        }
    }
/**
     * This test simulates talkback by calling setImportanceForAccessibility at end of animation
     */
This test simulates talkback by calling setImportanceForAccessibility at end of animation
This test simulates talkback by calling setImportanceForAccessibility at end of animation
@Test
    public void simulatesAccessibilityOfItemsBeingPushedOut() throws Throwable {
        Intent intent = new Intent();
        intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID, R.layout.horizontal_grid);
        intent.putExtra(GridActivity.EXTRA_NUM_ITEMS, 100);
        intent.putExtra(GridActivity.EXTRA_STAGGERED, false);
        mOrientation = BaseGridView.HORIZONTAL;
        mNumRows = 3;
        initActivity(intent);
        final HashSet<View> moveAnimationViews = new HashSet();
        mActivity.mImportantForAccessibilityListener =
                new GridActivity.ImportantForAccessibilityListener() {
            RecyclerView.LayoutManager mLM = mGridView.getLayoutManager();
            @Override
            public void onImportantForAccessibilityChanged(View view, int newValue) {
                if (moveAnimationViews.contains(view)) {
                    AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain();
                    mLM.onInitializeAccessibilityNodeInfoForItem(
                            null, null, view, info);
                }
            }
        };
        final int lastPos = mGridView.getChildAdapterPosition(
                mGridView.getChildAt(mGridView.getChildCount() - 1));
        final int numItemsToPushOut = mNumRows;
        for (int i = 0; i < numItemsToPushOut; i++) {
            moveAnimationViews.add(
                    mGridView.getChildAt(mGridView.getChildCount() - 1 - i));
        }
        mActivityTestRule.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mGridView.setItemAnimator(new DefaultItemAnimator() {
                    @Override
                    public void onMoveFinished(RecyclerView.ViewHolder item) {
                        moveAnimationViews.remove(item.itemView);
                    }
                });
                mGridView.getLayoutManager().setItemPrefetchEnabled(false);
            }
        });
        mActivityTestRule.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                final int[] newItems = new int[numItemsToPushOut];
                final int newItemValue = mActivity.mItemLengths[0] + 1;
                for (int i = 0; i < newItems.length; i++) {
                    newItems[i] = newItemValue;
                }
                mActivity.addItems(lastPos - numItemsToPushOut + 1, newItems);
            }
        });
        while (moveAnimationViews.size() != 0) {
            Thread.sleep(100);
        }
    }
@ Test public void simulatesAccessibilityOfItemsBeingPushedOut ( ) throws Throwable { Intent intent = new Intent ( ) ; intent . putExtra ( GridActivity . EXTRA_LAYOUT_RESOURCE_ID , R . layout . horizontal_grid ) ; intent . putExtra ( GridActivity . EXTRA_NUM_ITEMS , 100 ) ; intent . putExtra ( GridActivity . EXTRA_STAGGERED , false ) ; mOrientation = BaseGridView . HORIZONTAL ; mNumRows = 3 ; initActivity ( intent ) ; final HashSet < View > moveAnimationViews = new HashSet ( ) ; mActivity . mImportantForAccessibilityListener = new GridActivity . ImportantForAccessibilityListener ( ) { RecyclerView . LayoutManager mLM = mGridView . getLayoutManager ( ) ; @ Override public void onImportantForAccessibilityChanged ( View view , int newValue ) { if ( moveAnimationViews . contains ( view ) ) { AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat . obtain ( ) ; mLM . onInitializeAccessibilityNodeInfoForItem ( null , null , view , info ) ; } } } ; final int lastPos = mGridView . getChildAdapterPosition ( mGridView . getChildAt ( mGridView . getChildCount ( ) - 1 ) ) ; final int numItemsToPushOut = mNumRows ; for ( int i = 0 ; i < numItemsToPushOut ; i ++ ) { moveAnimationViews . add ( mGridView . getChildAt ( mGridView . getChildCount ( ) - 1 - i ) ) ; } mActivityTestRule . runOnUiThread ( new Runnable ( ) { @ Override public void run ( ) { mGridView . setItemAnimator ( new DefaultItemAnimator ( ) { @ Override public void onMoveFinished ( RecyclerView . ViewHolder item ) { moveAnimationViews . remove ( item . itemView ) ; } } ) ; mGridView . getLayoutManager ( ) . setItemPrefetchEnabled ( false ) ; } } ) ; mActivityTestRule . runOnUiThread ( new Runnable ( ) { @ Override public void run ( ) { final int [ ] newItems = new int [ numItemsToPushOut ] ; final int newItemValue = mActivity . mItemLengths [ 0 ] + 1 ; for ( int i = 0 ; i < newItems . length ; i ++ ) { newItems [ i ] = newItemValue ; } mActivity . addItems ( lastPos - numItemsToPushOut + 1 , newItems ) ; } } ) ; while ( moveAnimationViews . size ( ) != 0 ) { Thread . sleep ( 100 ) ; } }
This test simulates talkback by calling setImportanceForAccessibility at end of animation
This test simulates talkback by calling setImportanceForAccessibility at end of animation
// simulates talkack, having setImportantForAccessibility to call // onInitializeAccessibilityNodeInfoForItem() for the DISAPPEARING items.