RecyclerViewTest.java revision ceb1d0ab9e22f5a48b72e9850f713be60311c516
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 190194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.app.Activity; 200194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.support.v7.recyclerview.test.CustomLayoutManager; 210194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.support.v7.recyclerview.test.R; 220194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.support.v7.widget.GridLayoutManager; 23ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyarimport android.support.v7.widget.LinearLayoutManager; 240194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.support.v7.widget.RecyclerView; 25ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyarimport android.support.v7.widget.StaggeredGridLayoutManager; 260194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.test.ActivityInstrumentationTestCase2; 270194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptaimport android.widget.LinearLayout; 280194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta 290194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Guptapublic class RecyclerViewTest extends ActivityInstrumentationTestCase2<RecyclerViewTestActivity> { 300194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta 310194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta public RecyclerViewTest() { 320194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta super("android.support.v7.widget.test", RecyclerViewTestActivity.class); 330194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta } 340194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta 350194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta private void setContentView(final int layoutId) throws Throwable { 360194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta final Activity activity = getActivity(); 370194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta runTestOnUiThread(new Runnable() { 380194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta @Override 390194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta public void run() { 400194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta activity.setContentView(layoutId); 410194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta } 420194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta }); 430194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta } 440194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta 45ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar public void testSavedStateAccess() throws ClassNotFoundException { 46ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar // this class should be accessible outside RecyclerView package 47ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar assertNotNull(RecyclerView.SavedState.class); 48ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar assertNotNull(LinearLayoutManager.SavedState.class); 49ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar assertNotNull(GridLayoutManager.SavedState.class); 50ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar assertNotNull(StaggeredGridLayoutManager.SavedState.class); 51ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar } 52ceb1d0ab9e22f5a48b72e9850f713be60311c516Yigit Boyar 530194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta public void testInflation() throws Throwable { 540194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta setContentView(R.layout.inflation_test); 550194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta getInstrumentation().waitForIdleSync(); 560194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta RecyclerView view = (RecyclerView) getActivity().findViewById(R.id.recyclerView); 570194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta RecyclerView.LayoutManager layoutManager = view.getLayoutManager(); 580194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta assertNotNull("LayoutManager not created.", layoutManager); 590194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta assertEquals("Incorrect LayoutManager created", 600194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta layoutManager.getClass().getName(), GridLayoutManager.class.getName()); 610194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta GridLayoutManager gridLayoutManager = ((GridLayoutManager) layoutManager); 620194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta assertEquals("Incorrect span count.", 3, gridLayoutManager.getSpanCount()); 630194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta assertEquals("Expected horizontal orientation.", 640194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta LinearLayout.HORIZONTAL, gridLayoutManager.getOrientation()); 650194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta assertTrue("Expected reversed layout", gridLayoutManager.getReverseLayout()); 660194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta 670194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta view = (RecyclerView) getActivity().findViewById(R.id.recyclerView2); 680194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta layoutManager = view.getLayoutManager(); 690194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta assertNotNull("LayoutManager not created.", layoutManager); 700194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta assertEquals("Incorrect LayoutManager created", 710194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta layoutManager.getClass().getName(), 720194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta CustomLayoutManager.class.getName()); 730194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta CustomLayoutManager customLayoutManager = 740194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta (CustomLayoutManager) layoutManager; 750194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta assertEquals("Expected vertical orientation.", 760194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta LinearLayout.VERTICAL, customLayoutManager.getOrientation()); 770194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta assertTrue("Expected items to be stacked from end", customLayoutManager.getStackFromEnd()); 780194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta 790194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta view = (RecyclerView) getActivity().findViewById(R.id.recyclerView3); 800194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta layoutManager = view.getLayoutManager(); 810194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta assertNotNull("LayoutManager not created.", layoutManager); 820194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta assertEquals("Incorrect LayoutManager created", 830194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta layoutManager.getClass().getName(), 840194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta CustomLayoutManager.LayoutManager.class.getName()); 850194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta 86ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar view = (RecyclerView) getActivity().findViewById(R.id.recyclerView4); 87ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar layoutManager = view.getLayoutManager(); 88ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar assertNotNull("LayoutManager not created.", layoutManager); 89ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar assertEquals("Incorrect LayoutManager created", 90ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar "android.support.v7.recyclerview.test.PrivateLayoutManager", 91ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar layoutManager.getClass().getName()); 92ec494eb6490aae36981351d80c778031efcf2ebeYigit Boyar 930194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta } 940194ed84ad6f1d3d489db52b9431fa93a7697b50Deepanshu Gupta} 95