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