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 com.android.setupwizardlib.test; 18 19import static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertNotNull; 21import static org.junit.Assert.assertSame; 22import static org.junit.Assert.assertTrue; 23import static org.junit.Assert.fail; 24 25import android.content.Context; 26import android.graphics.drawable.Drawable; 27import android.graphics.drawable.InsetDrawable; 28import android.os.Build; 29import android.support.test.InstrumentationRegistry; 30import android.support.test.filters.SmallTest; 31import android.support.test.runner.AndroidJUnit4; 32import android.support.v7.widget.RecyclerView; 33import android.support.v7.widget.RecyclerView.Adapter; 34import android.support.v7.widget.RecyclerView.ViewHolder; 35import android.view.ContextThemeWrapper; 36import android.view.LayoutInflater; 37import android.view.View; 38import android.view.View.MeasureSpec; 39import android.view.ViewGroup; 40 41import com.android.setupwizardlib.SetupWizardRecyclerLayout; 42 43import org.junit.Before; 44import org.junit.Test; 45import org.junit.runner.RunWith; 46 47@RunWith(AndroidJUnit4.class) 48@SmallTest 49public class SetupWizardRecyclerLayoutTest { 50 51 private Context mContext; 52 53 @Before 54 public void setUp() throws Exception { 55 mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(), 56 R.style.SuwThemeMaterial_Light); 57 } 58 59 @Test 60 public void testDefaultTemplate() { 61 SetupWizardRecyclerLayout layout = new SetupWizardRecyclerLayout(mContext); 62 assertRecyclerTemplateInflated(layout); 63 } 64 65 @Test 66 public void testInflateFromXml() { 67 LayoutInflater inflater = LayoutInflater.from(mContext); 68 SetupWizardRecyclerLayout layout = (SetupWizardRecyclerLayout) 69 inflater.inflate(R.layout.test_recycler_layout, null); 70 assertRecyclerTemplateInflated(layout); 71 } 72 73 @Test 74 public void testGetRecyclerView() { 75 SetupWizardRecyclerLayout layout = new SetupWizardRecyclerLayout(mContext); 76 assertRecyclerTemplateInflated(layout); 77 assertNotNull("getRecyclerView should not be null", layout.getRecyclerView()); 78 } 79 80 @Test 81 public void testAdapter() { 82 SetupWizardRecyclerLayout layout = new SetupWizardRecyclerLayout(mContext); 83 assertRecyclerTemplateInflated(layout); 84 85 final Adapter adapter = createTestAdapter(1); 86 layout.setAdapter(adapter); 87 88 final Adapter gotAdapter = layout.getAdapter(); 89 // Note: The wrapped adapter should be returned, not the HeaderAdapter. 90 assertSame("Adapter got from SetupWizardLayout should be same as set", 91 adapter, gotAdapter); 92 } 93 94 @Test 95 public void testLayout() { 96 SetupWizardRecyclerLayout layout = new SetupWizardRecyclerLayout(mContext); 97 assertRecyclerTemplateInflated(layout); 98 99 layout.setAdapter(createTestAdapter(3)); 100 101 layout.measure( 102 MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY), 103 MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY)); 104 layout.layout(0, 0, 500, 500); 105 // Test that the layout code doesn't crash. 106 } 107 108 @Test 109 public void testDividerInset() { 110 SetupWizardRecyclerLayout layout = new SetupWizardRecyclerLayout(mContext); 111 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 112 layout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR); 113 } 114 assertRecyclerTemplateInflated(layout); 115 116 layout.setDividerInset(10); 117 assertEquals("Divider inset should be 10", 10, layout.getDividerInset()); 118 119 final Drawable divider = layout.getDivider(); 120 assertTrue("Divider should be instance of InsetDrawable", divider instanceof InsetDrawable); 121 } 122 123 @Test 124 public void testTemplateWithNoRecyclerView() { 125 try { 126 new SetupWizardRecyclerLayout( 127 mContext, 128 R.layout.suw_glif_template, 129 R.id.suw_recycler_view); 130 fail("Creating SetupWizardRecyclerLayout with no recycler view should throw exception"); 131 } catch (Exception e) { 132 // pass 133 } 134 } 135 136 private void assertRecyclerTemplateInflated(SetupWizardRecyclerLayout layout) { 137 View recyclerView = layout.findViewById(R.id.suw_recycler_view); 138 assertTrue("@id/suw_recycler_view should be a RecyclerView", 139 recyclerView instanceof RecyclerView); 140 141 assertNotNull("Header text view should not be null", 142 layout.findManagedViewById(R.id.suw_layout_title)); 143 assertNotNull("Decoration view should not be null", 144 layout.findManagedViewById(R.id.suw_layout_decor)); 145 } 146 147 private Adapter createTestAdapter(final int itemCount) { 148 return new Adapter() { 149 @Override 150 public ViewHolder onCreateViewHolder(ViewGroup parent, int position) { 151 return new ViewHolder(new View(parent.getContext())) {}; 152 } 153 154 @Override 155 public void onBindViewHolder(ViewHolder viewHolder, int position) { 156 } 157 158 @Override 159 public int getItemCount() { 160 return itemCount; 161 } 162 }; 163 } 164} 165