RecyclerViewTest.java revision aa6e1d7fa3c1fb0f501ed58332b4cc584522df7f
10194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta/*
20194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta * Copyright (C) 2015 The Android Open Source Project
30194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta *
40194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta * Licensed under the Apache License, Version 2.0 (the "License");
50194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta * you may not use this file except in compliance with the License.
60194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta * You may obtain a copy of the License at
70194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta *
80194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta *      http://www.apache.org/licenses/LICENSE-2.0
90194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta *
100194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta * Unless required by applicable law or agreed to in writing, software
110194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta * distributed under the License is distributed on an "AS IS" BASIS,
120194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta * See the License for the specific language governing permissions and
140194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta * limitations under the License.
150194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta */
160194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
170194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptapackage android.support.v7.widget.test;
180194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
190a017072206f93474ccd2706e7983c2ff778b904Yigit Boyarimport org.junit.Rule;
200a017072206f93474ccd2706e7983c2ff778b904Yigit Boyarimport org.junit.Test;
210a017072206f93474ccd2706e7983c2ff778b904Yigit Boyarimport org.junit.runner.RunWith;
220a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar
230194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.app.Activity;
240a017072206f93474ccd2706e7983c2ff778b904Yigit Boyarimport android.app.Instrumentation;
25aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banesimport android.os.Build;
260a017072206f93474ccd2706e7983c2ff778b904Yigit Boyarimport android.support.test.InstrumentationRegistry;
270a017072206f93474ccd2706e7983c2ff778b904Yigit Boyarimport android.support.test.rule.ActivityTestRule;
280a017072206f93474ccd2706e7983c2ff778b904Yigit Boyarimport android.support.test.runner.AndroidJUnit4;
290194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.support.v7.recyclerview.test.CustomLayoutManager;
300194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.support.v7.recyclerview.test.R;
310194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.support.v7.widget.GridLayoutManager;
32ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyarimport android.support.v7.widget.LinearLayoutManager;
330194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.support.v7.widget.RecyclerView;
34ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyarimport android.support.v7.widget.StaggeredGridLayoutManager;
35f1b288ec2104488f4a92e911b0ab80c8f0f3e9d1Yigit Boyarimport android.test.suitebuilder.annotation.SmallTest;
360194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.widget.LinearLayout;
370194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
38aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banesimport static org.junit.Assert.assertFalse;
390a017072206f93474ccd2706e7983c2ff778b904Yigit Boyarimport static org.junit.Assert.assertEquals;
400a017072206f93474ccd2706e7983c2ff778b904Yigit Boyarimport static org.junit.Assert.assertNotNull;
410a017072206f93474ccd2706e7983c2ff778b904Yigit Boyarimport static org.junit.Assert.assertTrue;
420a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar
43f1b288ec2104488f4a92e911b0ab80c8f0f3e9d1Yigit Boyar@SmallTest
440a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar@RunWith(AndroidJUnit4.class)
450a017072206f93474ccd2706e7983c2ff778b904Yigit Boyarpublic class RecyclerViewTest {
460194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
470a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    @Rule
480a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    public ActivityTestRule<RecyclerViewTestActivity> mActivityRule
490a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar            = new ActivityTestRule<>(RecyclerViewTestActivity.class);
500194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
510194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta    private void setContentView(final int layoutId) throws Throwable {
520a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar        final Activity activity = mActivityRule.getActivity();
530a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
540194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta            @Override
550194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta            public void run() {
560194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                activity.setContentView(layoutId);
570194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta            }
580194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        });
590194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta    }
600194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
610a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    @Test
620a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    public void savedStateAccess() throws ClassNotFoundException {
63ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar        // this class should be accessible outside RecyclerView package
64ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar        assertNotNull(RecyclerView.SavedState.class);
65ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar        assertNotNull(LinearLayoutManager.SavedState.class);
66ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar        assertNotNull(GridLayoutManager.SavedState.class);
67ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar        assertNotNull(StaggeredGridLayoutManager.SavedState.class);
68ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar    }
69ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar
700a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    @Test
710a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    public void inflation() throws Throwable {
720194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        setContentView(R.layout.inflation_test);
730194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        getInstrumentation().waitForIdleSync();
740194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        RecyclerView view = (RecyclerView) getActivity().findViewById(R.id.recyclerView);
750194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        RecyclerView.LayoutManager layoutManager = view.getLayoutManager();
760194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertNotNull("LayoutManager not created.", layoutManager);
770194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Incorrect LayoutManager created",
780194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                layoutManager.getClass().getName(), GridLayoutManager.class.getName());
790194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        GridLayoutManager gridLayoutManager = ((GridLayoutManager) layoutManager);
800194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Incorrect span count.", 3, gridLayoutManager.getSpanCount());
810194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Expected horizontal orientation.",
820194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                LinearLayout.HORIZONTAL, gridLayoutManager.getOrientation());
830194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertTrue("Expected reversed layout", gridLayoutManager.getReverseLayout());
840194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
850194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        view = (RecyclerView) getActivity().findViewById(R.id.recyclerView2);
860194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        layoutManager = view.getLayoutManager();
870194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertNotNull("LayoutManager not created.", layoutManager);
880194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Incorrect LayoutManager created",
890194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                layoutManager.getClass().getName(),
900194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                CustomLayoutManager.class.getName());
910194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        CustomLayoutManager customLayoutManager =
920194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                (CustomLayoutManager) layoutManager;
930194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Expected vertical orientation.",
940194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                LinearLayout.VERTICAL, customLayoutManager.getOrientation());
950194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertTrue("Expected items to be stacked from end", customLayoutManager.getStackFromEnd());
960194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
970194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        view = (RecyclerView) getActivity().findViewById(R.id.recyclerView3);
980194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        layoutManager = view.getLayoutManager();
990194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertNotNull("LayoutManager not created.", layoutManager);
1000194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Incorrect LayoutManager created",
1010194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                layoutManager.getClass().getName(),
1020194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                CustomLayoutManager.LayoutManager.class.getName());
1030194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
104ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.recyclerView4);
105ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar        layoutManager = view.getLayoutManager();
106ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar        assertNotNull("LayoutManager not created.", layoutManager);
107ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar        assertEquals("Incorrect LayoutManager created",
108ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar                "android.support.v7.recyclerview.test.PrivateLayoutManager",
109ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar                layoutManager.getClass().getName());
110ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar
111aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes        view = (RecyclerView) getActivity().findViewById(R.id.recyclerView5);
112aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes        assertTrue("Incorrect default nested scrolling value", view.isNestedScrollingEnabled());
113aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes
114aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes        if (Build.VERSION.SDK_INT >= 21) {
115aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes            view = (RecyclerView) getActivity().findViewById(R.id.recyclerView6);
116aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes            assertFalse("Incorrect explicit nested scrolling value",
117aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes                    view.isNestedScrollingEnabled());
118aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes        }
1190194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta    }
1200a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar
1210a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    private Activity getActivity() {
1220a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar        return mActivityRule.getActivity();
1230a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    }
1240a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar
1250a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    private Instrumentation getInstrumentation() {
1260a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar        return InstrumentationRegistry.getInstrumentation();
1270a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    }
1280194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta}
129