RecyclerViewTest.java revision 42e7d6fafcde7bfe261dd7d8d75ee53ca0cd6790
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
19754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport static org.junit.Assert.assertEquals;
20754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport static org.junit.Assert.assertFalse;
21754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport static org.junit.Assert.assertNotNull;
22754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport static org.junit.Assert.assertTrue;
230a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar
240194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.app.Activity;
250a017072206f93474ccd2706e7983c2ff778b904Yigit Boyarimport android.app.Instrumentation;
26aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banesimport android.os.Build;
270a017072206f93474ccd2706e7983c2ff778b904Yigit Boyarimport android.support.test.InstrumentationRegistry;
28754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport android.support.test.filters.SmallTest;
290a017072206f93474ccd2706e7983c2ff778b904Yigit Boyarimport android.support.test.rule.ActivityTestRule;
300a017072206f93474ccd2706e7983c2ff778b904Yigit Boyarimport android.support.test.runner.AndroidJUnit4;
310194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.support.v7.recyclerview.test.CustomLayoutManager;
320194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.support.v7.recyclerview.test.R;
330194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.support.v7.widget.GridLayoutManager;
34ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyarimport android.support.v7.widget.LinearLayoutManager;
350194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.support.v7.widget.RecyclerView;
36ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyarimport android.support.v7.widget.StaggeredGridLayoutManager;
37d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyarimport android.view.ViewGroup;
380194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.widget.LinearLayout;
390194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
40754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport org.junit.Rule;
41754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport org.junit.Test;
42754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport org.junit.runner.RunWith;
430a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar
44f1b288ec2104488f4a92e911b0ab80c8f0f3e9d1Yigit Boyar@SmallTest
450a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar@RunWith(AndroidJUnit4.class)
460a017072206f93474ccd2706e7983c2ff778b904Yigit Boyarpublic class RecyclerViewTest {
470194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
480a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    @Rule
4942e7d6fafcde7bfe261dd7d8d75ee53ca0cd6790Aurimas Liutikas    public ActivityTestRule<RecyclerViewTestActivity> mActivityRule =
5042e7d6fafcde7bfe261dd7d8d75ee53ca0cd6790Aurimas Liutikas            new ActivityTestRule<>(RecyclerViewTestActivity.class);
510194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
520194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta    private void setContentView(final int layoutId) throws Throwable {
530a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar        final Activity activity = mActivityRule.getActivity();
5442e7d6fafcde7bfe261dd7d8d75ee53ca0cd6790Aurimas Liutikas        mActivityRule.runOnUiThread(new Runnable() {
550194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta            @Override
560194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta            public void run() {
570194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                activity.setContentView(layoutId);
580194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta            }
590194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        });
600194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta    }
610194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
620a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    @Test
630a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    public void savedStateAccess() throws ClassNotFoundException {
64ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar        // this class should be accessible outside RecyclerView package
65ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar        assertNotNull(RecyclerView.SavedState.class);
66ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar        assertNotNull(LinearLayoutManager.SavedState.class);
67ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar        assertNotNull(GridLayoutManager.SavedState.class);
68ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar        assertNotNull(StaggeredGridLayoutManager.SavedState.class);
69ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar    }
70ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar
710a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    @Test
720a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    public void inflation() throws Throwable {
730194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        setContentView(R.layout.inflation_test);
740194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        getInstrumentation().waitForIdleSync();
75f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar        RecyclerView view;
76f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.clipToPaddingUndefined);
77f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar        assertTrue(view.getLayoutManager().getClipToPadding());
78f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.clipToPaddingYes);
79f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar        assertTrue(view.getLayoutManager().getClipToPadding());
80f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.clipToPaddingNo);
81f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar        assertFalse(view.getLayoutManager().getClipToPadding());
82f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar
83f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.recyclerView);
840194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        RecyclerView.LayoutManager layoutManager = view.getLayoutManager();
850194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertNotNull("LayoutManager not created.", layoutManager);
860194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Incorrect LayoutManager created",
870194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                layoutManager.getClass().getName(), GridLayoutManager.class.getName());
880194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        GridLayoutManager gridLayoutManager = ((GridLayoutManager) layoutManager);
890194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Incorrect span count.", 3, gridLayoutManager.getSpanCount());
900194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Expected horizontal orientation.",
910194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                LinearLayout.HORIZONTAL, gridLayoutManager.getOrientation());
920194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertTrue("Expected reversed layout", gridLayoutManager.getReverseLayout());
930194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
940194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        view = (RecyclerView) getActivity().findViewById(R.id.recyclerView2);
950194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        layoutManager = view.getLayoutManager();
960194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertNotNull("LayoutManager not created.", layoutManager);
970194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Incorrect LayoutManager created",
980194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                layoutManager.getClass().getName(),
990194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                CustomLayoutManager.class.getName());
1000194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        CustomLayoutManager customLayoutManager =
1010194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                (CustomLayoutManager) layoutManager;
1020194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Expected vertical orientation.",
1030194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                LinearLayout.VERTICAL, customLayoutManager.getOrientation());
1040194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertTrue("Expected items to be stacked from end", customLayoutManager.getStackFromEnd());
1050194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
1060194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        view = (RecyclerView) getActivity().findViewById(R.id.recyclerView3);
1070194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        layoutManager = view.getLayoutManager();
1080194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertNotNull("LayoutManager not created.", layoutManager);
1090194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Incorrect LayoutManager created",
1100194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                layoutManager.getClass().getName(),
1110194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                CustomLayoutManager.LayoutManager.class.getName());
1120194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
113ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.recyclerView4);
114ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar        layoutManager = view.getLayoutManager();
115ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar        assertNotNull("LayoutManager not created.", layoutManager);
116ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar        assertEquals("Incorrect LayoutManager created",
117ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar                "android.support.v7.recyclerview.test.PrivateLayoutManager",
118ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar                layoutManager.getClass().getName());
119ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar
120aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes        view = (RecyclerView) getActivity().findViewById(R.id.recyclerView5);
121aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes        assertTrue("Incorrect default nested scrolling value", view.isNestedScrollingEnabled());
122aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes
123aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes        if (Build.VERSION.SDK_INT >= 21) {
124aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes            view = (RecyclerView) getActivity().findViewById(R.id.recyclerView6);
125aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes            assertFalse("Incorrect explicit nested scrolling value",
126aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes                    view.isNestedScrollingEnabled());
127aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes        }
128d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar
129d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.focusability_undefined);
130d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar        assertEquals(ViewGroup.FOCUS_AFTER_DESCENDANTS, view.getDescendantFocusability());
131d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar
132d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.focusability_after);
133d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar        assertEquals(ViewGroup.FOCUS_AFTER_DESCENDANTS, view.getDescendantFocusability());
134d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar
135d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.focusability_before);
136d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar        assertEquals(ViewGroup.FOCUS_BEFORE_DESCENDANTS, view.getDescendantFocusability());
137d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar
138d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.focusability_block);
139d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar        assertEquals(ViewGroup.FOCUS_BLOCK_DESCENDANTS, view.getDescendantFocusability());
1400194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta    }
1410a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar
1420a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    private Activity getActivity() {
1430a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar        return mActivityRule.getActivity();
1440a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    }
1450a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar
1460a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    private Instrumentation getInstrumentation() {
1470a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar        return InstrumentationRegistry.getInstrumentation();
1480a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    }
1490194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta}
150