1/* 2 * Copyright (C) 2015 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.test; 18 19import static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertFalse; 21import static org.junit.Assert.assertNotNull; 22import static org.junit.Assert.assertTrue; 23 24import android.app.Activity; 25import android.app.Instrumentation; 26import android.os.Build; 27import android.support.test.InstrumentationRegistry; 28import android.support.test.filters.SmallTest; 29import android.support.test.rule.ActivityTestRule; 30import android.support.test.runner.AndroidJUnit4; 31import android.support.v7.recyclerview.test.CustomLayoutManager; 32import android.support.v7.recyclerview.test.R; 33import android.support.v7.widget.GridLayoutManager; 34import android.support.v7.widget.LinearLayoutManager; 35import android.support.v7.widget.RecyclerView; 36import android.support.v7.widget.StaggeredGridLayoutManager; 37import android.view.ViewGroup; 38import android.widget.LinearLayout; 39 40import org.junit.Rule; 41import org.junit.Test; 42import org.junit.runner.RunWith; 43 44@SmallTest 45@RunWith(AndroidJUnit4.class) 46public class RecyclerViewTest { 47 48 @Rule 49 public ActivityTestRule<RecyclerViewTestActivity> mActivityRule = 50 new ActivityTestRule<>(RecyclerViewTestActivity.class); 51 52 private void setContentView(final int layoutId) throws Throwable { 53 final Activity activity = mActivityRule.getActivity(); 54 mActivityRule.runOnUiThread(new Runnable() { 55 @Override 56 public void run() { 57 activity.setContentView(layoutId); 58 } 59 }); 60 } 61 62 @Test 63 public void savedStateAccess() throws ClassNotFoundException { 64 // this class should be accessible outside RecyclerView package 65 assertNotNull(RecyclerView.SavedState.class); 66 assertNotNull(LinearLayoutManager.SavedState.class); 67 assertNotNull(GridLayoutManager.SavedState.class); 68 assertNotNull(StaggeredGridLayoutManager.SavedState.class); 69 } 70 71 @Test 72 public void inflation() throws Throwable { 73 setContentView(R.layout.inflation_test); 74 getInstrumentation().waitForIdleSync(); 75 RecyclerView view; 76 view = (RecyclerView) getActivity().findViewById(R.id.clipToPaddingUndefined); 77 assertTrue(view.getLayoutManager().getClipToPadding()); 78 view = (RecyclerView) getActivity().findViewById(R.id.clipToPaddingYes); 79 assertTrue(view.getLayoutManager().getClipToPadding()); 80 view = (RecyclerView) getActivity().findViewById(R.id.clipToPaddingNo); 81 assertFalse(view.getLayoutManager().getClipToPadding()); 82 83 view = (RecyclerView) getActivity().findViewById(R.id.recyclerView); 84 RecyclerView.LayoutManager layoutManager = view.getLayoutManager(); 85 assertNotNull("LayoutManager not created.", layoutManager); 86 assertEquals("Incorrect LayoutManager created", 87 layoutManager.getClass().getName(), GridLayoutManager.class.getName()); 88 GridLayoutManager gridLayoutManager = ((GridLayoutManager) layoutManager); 89 assertEquals("Incorrect span count.", 3, gridLayoutManager.getSpanCount()); 90 assertEquals("Expected horizontal orientation.", 91 LinearLayout.HORIZONTAL, gridLayoutManager.getOrientation()); 92 assertTrue("Expected reversed layout", gridLayoutManager.getReverseLayout()); 93 94 view = (RecyclerView) getActivity().findViewById(R.id.recyclerView2); 95 layoutManager = view.getLayoutManager(); 96 assertNotNull("LayoutManager not created.", layoutManager); 97 assertEquals("Incorrect LayoutManager created", 98 layoutManager.getClass().getName(), 99 CustomLayoutManager.class.getName()); 100 CustomLayoutManager customLayoutManager = 101 (CustomLayoutManager) layoutManager; 102 assertEquals("Expected vertical orientation.", 103 LinearLayout.VERTICAL, customLayoutManager.getOrientation()); 104 assertTrue("Expected items to be stacked from end", customLayoutManager.getStackFromEnd()); 105 106 view = (RecyclerView) getActivity().findViewById(R.id.recyclerView3); 107 layoutManager = view.getLayoutManager(); 108 assertNotNull("LayoutManager not created.", layoutManager); 109 assertEquals("Incorrect LayoutManager created", 110 layoutManager.getClass().getName(), 111 CustomLayoutManager.LayoutManager.class.getName()); 112 113 view = (RecyclerView) getActivity().findViewById(R.id.recyclerView4); 114 layoutManager = view.getLayoutManager(); 115 assertNotNull("LayoutManager not created.", layoutManager); 116 assertEquals("Incorrect LayoutManager created", 117 "android.support.v7.recyclerview.test.PrivateLayoutManager", 118 layoutManager.getClass().getName()); 119 120 view = (RecyclerView) getActivity().findViewById(R.id.recyclerView5); 121 assertTrue("Incorrect default nested scrolling value", view.isNestedScrollingEnabled()); 122 123 if (Build.VERSION.SDK_INT >= 21) { 124 view = (RecyclerView) getActivity().findViewById(R.id.recyclerView6); 125 assertFalse("Incorrect explicit nested scrolling value", 126 view.isNestedScrollingEnabled()); 127 } 128 129 view = (RecyclerView) getActivity().findViewById(R.id.focusability_undefined); 130 assertEquals(ViewGroup.FOCUS_AFTER_DESCENDANTS, view.getDescendantFocusability()); 131 132 view = (RecyclerView) getActivity().findViewById(R.id.focusability_after); 133 assertEquals(ViewGroup.FOCUS_AFTER_DESCENDANTS, view.getDescendantFocusability()); 134 135 view = (RecyclerView) getActivity().findViewById(R.id.focusability_before); 136 assertEquals(ViewGroup.FOCUS_BEFORE_DESCENDANTS, view.getDescendantFocusability()); 137 138 view = (RecyclerView) getActivity().findViewById(R.id.focusability_block); 139 assertEquals(ViewGroup.FOCUS_BLOCK_DESCENDANTS, view.getDescendantFocusability()); 140 } 141 142 private Activity getActivity() { 143 return mActivityRule.getActivity(); 144 } 145 146 private Instrumentation getInstrumentation() { 147 return InstrumentationRegistry.getInstrumentation(); 148 } 149} 150