10194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta/*
2ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas * Copyright 2018 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
17ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikaspackage androidx.recyclerview.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;
31c95a6f1f125ad3a7e1f9f79bccf4b2603bc40ebaAurimas Liutikasimport android.view.ViewGroup;
32c95a6f1f125ad3a7e1f9f79bccf4b2603bc40ebaAurimas Liutikasimport android.widget.LinearLayout;
33ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas
34ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.recyclerview.widget.GridLayoutManager;
35ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.recyclerview.widget.LinearLayoutManager;
36ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.recyclerview.widget.RecyclerView;
37ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.recyclerview.widget.StaggeredGridLayoutManager;
380194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
39754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport org.junit.Rule;
40754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport org.junit.Test;
41754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport org.junit.runner.RunWith;
420a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar
43f1b288ec2104488f4a92e911b0ab80c8f0f3e9d1Yigit Boyar@SmallTest
440a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar@RunWith(AndroidJUnit4.class)
450a017072206f93474ccd2706e7983c2ff778b904Yigit Boyarpublic class RecyclerViewTest {
460194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
470a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    @Rule
4842e7d6fafcde7bfe261dd7d8d75ee53ca0cd6790Aurimas Liutikas    public ActivityTestRule<RecyclerViewTestActivity> mActivityRule =
4942e7d6fafcde7bfe261dd7d8d75ee53ca0cd6790Aurimas Liutikas            new ActivityTestRule<>(RecyclerViewTestActivity.class);
500194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
510194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta    private void setContentView(final int layoutId) throws Throwable {
520a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar        final Activity activity = mActivityRule.getActivity();
5342e7d6fafcde7bfe261dd7d8d75ee53ca0cd6790Aurimas Liutikas        mActivityRule.runOnUiThread(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();
74f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar        RecyclerView view;
75f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.clipToPaddingUndefined);
76f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar        assertTrue(view.getLayoutManager().getClipToPadding());
77f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.clipToPaddingYes);
78f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar        assertTrue(view.getLayoutManager().getClipToPadding());
79f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.clipToPaddingNo);
80f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar        assertFalse(view.getLayoutManager().getClipToPadding());
81f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar
82f3c39cb89a925a2315354116bd012a2e41cebac8Yigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.recyclerView);
830194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        RecyclerView.LayoutManager layoutManager = view.getLayoutManager();
840194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertNotNull("LayoutManager not created.", layoutManager);
850194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Incorrect LayoutManager created",
860194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                layoutManager.getClass().getName(), GridLayoutManager.class.getName());
870194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        GridLayoutManager gridLayoutManager = ((GridLayoutManager) layoutManager);
880194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Incorrect span count.", 3, gridLayoutManager.getSpanCount());
890194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Expected horizontal orientation.",
900194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                LinearLayout.HORIZONTAL, gridLayoutManager.getOrientation());
910194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertTrue("Expected reversed layout", gridLayoutManager.getReverseLayout());
920194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
930194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        view = (RecyclerView) getActivity().findViewById(R.id.recyclerView2);
940194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        layoutManager = view.getLayoutManager();
950194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertNotNull("LayoutManager not created.", layoutManager);
960194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Incorrect LayoutManager created",
970194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                layoutManager.getClass().getName(),
980194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                CustomLayoutManager.class.getName());
990194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        CustomLayoutManager customLayoutManager =
1000194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                (CustomLayoutManager) layoutManager;
1010194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Expected vertical orientation.",
1020194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                LinearLayout.VERTICAL, customLayoutManager.getOrientation());
1030194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertTrue("Expected items to be stacked from end", customLayoutManager.getStackFromEnd());
1040194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
1050194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        view = (RecyclerView) getActivity().findViewById(R.id.recyclerView3);
1060194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        layoutManager = view.getLayoutManager();
1070194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertNotNull("LayoutManager not created.", layoutManager);
1080194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta        assertEquals("Incorrect LayoutManager created",
1090194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                layoutManager.getClass().getName(),
1100194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta                CustomLayoutManager.LayoutManager.class.getName());
1110194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta
112ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.recyclerView4);
113ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar        layoutManager = view.getLayoutManager();
114ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar        assertNotNull("LayoutManager not created.", layoutManager);
115ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar        assertEquals("Incorrect LayoutManager created",
116ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas                "androidx.recyclerview.test.PrivateLayoutManager",
117ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar                layoutManager.getClass().getName());
118ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar
119aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes        view = (RecyclerView) getActivity().findViewById(R.id.recyclerView5);
120aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes        assertTrue("Incorrect default nested scrolling value", view.isNestedScrollingEnabled());
121aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes
122aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes        if (Build.VERSION.SDK_INT >= 21) {
123aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes            view = (RecyclerView) getActivity().findViewById(R.id.recyclerView6);
124aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes            assertFalse("Incorrect explicit nested scrolling value",
125aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes                    view.isNestedScrollingEnabled());
126aa6e1d7fa3c1fb0f501ed58332b4cc584522df7fChris Banes        }
127d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar
128d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.focusability_undefined);
129d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar        assertEquals(ViewGroup.FOCUS_AFTER_DESCENDANTS, view.getDescendantFocusability());
130d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar
131d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.focusability_after);
132d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar        assertEquals(ViewGroup.FOCUS_AFTER_DESCENDANTS, view.getDescendantFocusability());
133d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar
134d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.focusability_before);
135d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar        assertEquals(ViewGroup.FOCUS_BEFORE_DESCENDANTS, view.getDescendantFocusability());
136d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar
137d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar        view = (RecyclerView) getActivity().findViewById(R.id.focusability_block);
138d8d42d52f8ae53107ffa849b86496650182e24b8Yigit Boyar        assertEquals(ViewGroup.FOCUS_BLOCK_DESCENDANTS, view.getDescendantFocusability());
1390194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta    }
1400a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar
1410a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    private Activity getActivity() {
1420a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar        return mActivityRule.getActivity();
1430a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    }
1440a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar
1450a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    private Instrumentation getInstrumentation() {
1460a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar        return InstrumentationRegistry.getInstrumentation();
1470a017072206f93474ccd2706e7983c2ff778b904Yigit Boyar    }
1480194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta}
149