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 17 package com.android.setupwizardlib.test; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 import static org.junit.Assert.assertTrue; 22 23 import android.content.Context; 24 import android.graphics.drawable.Drawable; 25 import android.graphics.drawable.InsetDrawable; 26 import android.os.Build; 27 import android.support.test.InstrumentationRegistry; 28 import android.support.test.filters.SmallTest; 29 import android.support.test.runner.AndroidJUnit4; 30 import android.support.v7.widget.RecyclerView; 31 import android.view.ContextThemeWrapper; 32 import android.view.LayoutInflater; 33 import android.view.View; 34 import android.view.ViewGroup; 35 36 import com.android.setupwizardlib.SetupWizardPreferenceLayout; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 @RunWith(AndroidJUnit4.class) 43 @SmallTest 44 public class SetupWizardPreferenceLayoutTest { 45 46 private Context mContext; 47 48 @Before 49 public void setUp() throws Exception { 50 mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(), 51 R.style.SuwThemeMaterial_Light); 52 } 53 54 @Test 55 public void testDefaultTemplate() { 56 SetupWizardPreferenceLayout layout = new SetupWizardPreferenceLayout(mContext); 57 assertPreferenceTemplateInflated(layout); 58 } 59 60 @Test 61 public void testGetRecyclerView() { 62 SetupWizardPreferenceLayout layout = new SetupWizardPreferenceLayout(mContext); 63 assertPreferenceTemplateInflated(layout); 64 assertNotNull("getRecyclerView should not be null", layout.getRecyclerView()); 65 } 66 67 @Test 68 public void testOnCreateRecyclerView() { 69 SetupWizardPreferenceLayout layout = new SetupWizardPreferenceLayout(mContext); 70 assertPreferenceTemplateInflated(layout); 71 final RecyclerView recyclerView = layout.onCreateRecyclerView(LayoutInflater.from(mContext), 72 layout, null /* savedInstanceState */); 73 assertNotNull("RecyclerView created should not be null", recyclerView); 74 } 75 76 @Test 77 public void testDividerInset() { 78 SetupWizardPreferenceLayout layout = new SetupWizardPreferenceLayout(mContext); 79 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 80 layout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR); 81 } 82 assertPreferenceTemplateInflated(layout); 83 84 layout.addView(layout.onCreateRecyclerView(LayoutInflater.from(mContext), layout, 85 null /* savedInstanceState */)); 86 87 layout.setDividerInset(10); 88 assertEquals("Divider inset should be 10", 10, layout.getDividerInset()); 89 90 final Drawable divider = layout.getDivider(); 91 assertTrue("Divider should be instance of InsetDrawable", divider instanceof InsetDrawable); 92 } 93 94 private void assertPreferenceTemplateInflated(SetupWizardPreferenceLayout layout) { 95 View contentContainer = layout.findViewById(R.id.suw_layout_content); 96 assertTrue("@id/suw_layout_content should be a ViewGroup", 97 contentContainer instanceof ViewGroup); 98 99 assertNotNull("Header text view should not be null", 100 layout.findManagedViewById(R.id.suw_layout_title)); 101 assertNotNull("Decoration view should not be null", 102 layout.findManagedViewById(R.id.suw_layout_decor)); 103 } 104 } 105