1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.support.v7.widget;
18 
19 import android.content.Context;
20 import android.support.test.InstrumentationRegistry;
21 import android.support.v4.view.ViewCompat;
22 import android.util.AttributeSet;
23 
24 import org.hamcrest.CoreMatchers;
25 import org.hamcrest.MatcherAssert;
26 
27 import java.util.concurrent.CountDownLatch;
28 import java.util.concurrent.TimeUnit;
29 
30 /**
31  * RecyclerView wrapper used in tests. This class can fake behavior like layout direction w/o
32  * playing with framework support.
33  */
34 public class WrappedRecyclerView extends RecyclerView {
35 
36     Boolean mFakeRTL;
37     private long mDrawingTimeOffsetMs;
38 
setFakeRTL(Boolean fakeRTL)39     public void setFakeRTL(Boolean fakeRTL) {
40         mFakeRTL = fakeRTL;
41     }
42 
setDrawingTimeOffset(long offsetMs)43     public void setDrawingTimeOffset(long offsetMs) {
44         mDrawingTimeOffsetMs = offsetMs;
45     }
46 
47     @Override
getDrawingTime()48     public long getDrawingTime() {
49         return super.getDrawingTime() + mDrawingTimeOffsetMs;
50     }
51 
WrappedRecyclerView(Context context)52     public WrappedRecyclerView(Context context) {
53         super(context);
54         init(context);
55     }
56 
WrappedRecyclerView(Context context, AttributeSet attrs)57     public WrappedRecyclerView(Context context, AttributeSet attrs) {
58         super(context, attrs);
59         init(context);
60     }
61 
WrappedRecyclerView(Context context, AttributeSet attrs, int defStyle)62     public WrappedRecyclerView(Context context, AttributeSet attrs, int defStyle) {
63         super(context, attrs, defStyle);
64         init(context);
65     }
66 
init(Context context)67     private void init(Context context) {
68         //initializeScrollbars(null);
69     }
70 
waitUntilLayout()71     public void waitUntilLayout() {
72         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
73         while (isLayoutRequested()) {
74             try {
75                 Thread.sleep(100);
76             } catch (InterruptedException e) {
77                 e.printStackTrace();
78             }
79         }
80     }
81 
waitUntilAnimations()82     public void waitUntilAnimations() throws InterruptedException {
83         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
84         final CountDownLatch latch = new CountDownLatch(1);
85         if (mItemAnimator == null || !mItemAnimator.isRunning(
86                 new ItemAnimator.ItemAnimatorFinishedListener() {
87                     @Override
88                     public void onAnimationsFinished() {
89                         latch.countDown();
90                     }
91                 })) {
92             latch.countDown();
93         }
94         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
95         MatcherAssert.assertThat("waiting too long for animations",
96                 latch.await(60, TimeUnit.SECONDS), CoreMatchers.is(true));
97     }
98 
99 
100     @Override
getLayoutDirection()101     public int getLayoutDirection() {
102         if (mFakeRTL == null) {
103             return super.getLayoutDirection();
104         }
105         //noinspection WrongConstant
106         return Boolean.TRUE.equals(mFakeRTL) ? ViewCompat.LAYOUT_DIRECTION_RTL
107                 : ViewCompat.LAYOUT_DIRECTION_LTR;
108     }
109 
110     @Override
setChildImportantForAccessibilityInternal(ViewHolder viewHolder, int importantForAccessibilityBeforeHidden)111     public boolean setChildImportantForAccessibilityInternal(ViewHolder viewHolder,
112             int importantForAccessibilityBeforeHidden) {
113         return super.setChildImportantForAccessibilityInternal(viewHolder,
114                 importantForAccessibilityBeforeHidden);
115     }
116 }