1/*
2 * Copyright (C) 2016 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
17package android.support.v7.widget;
18
19import static android.view.View.MeasureSpec.AT_MOST;
20import static android.view.View.MeasureSpec.EXACTLY;
21import static android.view.View.MeasureSpec.makeMeasureSpec;
22
23import static org.hamcrest.CoreMatchers.is;
24import static org.hamcrest.CoreMatchers.notNullValue;
25import static org.hamcrest.MatcherAssert.assertThat;
26
27import android.graphics.Rect;
28import android.support.test.runner.AndroidJUnit4;
29import android.test.suitebuilder.annotation.MediumTest;
30import android.view.View;
31
32import org.junit.Test;
33import org.junit.runner.RunWith;
34
35/**
36 * Tests scroll to position with wrap content to make sure that LayoutManagers can keep track of
37 * the position if layout is called multiple times.
38 */
39@MediumTest
40@RunWith(AndroidJUnit4.class)
41public class ScrollToPositionWithAutoMeasure extends BaseRecyclerViewInstrumentationTest {
42    @Test
43    public void testLinearLayoutManager() throws Throwable {
44        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
45        llm.ensureLayoutState();
46        test(llm, llm.mOrientationHelper);
47    }
48
49    @Test
50    public void testGridLayoutManager() throws Throwable {
51        GridLayoutManager glm = new GridLayoutManager(getActivity(), 3);
52        glm.ensureLayoutState();
53        test(glm, glm.mOrientationHelper);
54    }
55
56    @Test
57    public void testStaggeredGridLayoutManager() throws Throwable {
58        StaggeredGridLayoutManager sglm = new StaggeredGridLayoutManager(3,
59                StaggeredGridLayoutManager.VERTICAL);
60        test(sglm, sglm.mPrimaryOrientation);
61    }
62
63    public void test(final RecyclerView.LayoutManager llm,
64            final OrientationHelper orientationHelper) throws Throwable {
65        final RecyclerView recyclerView = new RecyclerView(getActivity());
66        recyclerView.setLayoutManager(llm);
67        recyclerView.setAdapter(new TestAdapter(1000));
68        setRecyclerView(recyclerView);
69        getInstrumentation().waitForIdleSync();
70        assertThat("Test sanity", recyclerView.getChildCount() > 0, is(true));
71        runTestOnUiThread(new Runnable() {
72            @Override
73            public void run() {
74                View lastChild = llm.getChildAt(llm.getChildCount() - 1);
75                int lastChildPos = recyclerView.getChildAdapterPosition(lastChild);
76                int targetPos = lastChildPos * 2;
77                llm.scrollToPosition(targetPos);
78                recyclerView.measure(
79                        makeMeasureSpec(recyclerView.getWidth(), EXACTLY),
80                        makeMeasureSpec(recyclerView.getHeight(), AT_MOST));
81                assertThat(recyclerView.findViewHolderForAdapterPosition(targetPos),
82                        notNullValue());
83                // make sure it is still visible from top at least
84                int size = orientationHelper.getDecoratedMeasurement(
85                        recyclerView.findViewHolderForAdapterPosition(targetPos).itemView);
86                recyclerView.measure(
87                        makeMeasureSpec(recyclerView.getWidth(), EXACTLY),
88                        makeMeasureSpec(size + 1, EXACTLY));
89                recyclerView.layout(0, 0, recyclerView.getMeasuredWidth(),
90                        recyclerView.getMeasuredHeight());
91                assertThat(recyclerView.findViewHolderForAdapterPosition(targetPos),
92                        notNullValue());
93                RecyclerView.ViewHolder viewHolder =
94                        recyclerView.findViewHolderForAdapterPosition(targetPos);
95                assertThat(viewHolder, notNullValue());
96                Rect viewBounds = new Rect();
97                llm.getDecoratedBoundsWithMargins(viewHolder.itemView, viewBounds);
98                Rect rvBounds = new Rect(0, 0, llm.getWidth(), llm.getHeight());
99                assertThat(rvBounds + " vs " + viewBounds, rvBounds.contains(viewBounds), is(true));
100            }
101        });
102    }
103}