GlifListLayoutTest.java revision 180360409c9e4e9163c670ff48663244b4057eaf
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 android.content.Context; 20import android.graphics.drawable.Drawable; 21import android.graphics.drawable.InsetDrawable; 22import android.os.Build; 23import android.test.InstrumentationTestCase; 24import android.test.suitebuilder.annotation.SmallTest; 25import android.view.ContextThemeWrapper; 26import android.view.LayoutInflater; 27import android.view.View; 28import android.widget.ArrayAdapter; 29import android.widget.HeaderViewListAdapter; 30import android.widget.ListAdapter; 31import android.widget.ListView; 32import android.widget.TextView; 33 34import com.android.setupwizardlib.GlifListLayout; 35 36public class GlifListLayoutTest extends InstrumentationTestCase { 37 38 private Context mContext; 39 40 @Override 41 protected void setUp() throws Exception { 42 super.setUp(); 43 mContext = new ContextThemeWrapper(getInstrumentation().getContext(), 44 R.style.SuwThemeGlif_Light); 45 } 46 47 @SmallTest 48 public void testDefaultTemplate() { 49 GlifListLayout layout = new GlifListLayout(mContext); 50 assertListTemplateInflated(layout); 51 } 52 53 @SmallTest 54 public void testAddView() { 55 GlifListLayout layout = new GlifListLayout(mContext); 56 TextView tv = new TextView(mContext); 57 try { 58 layout.addView(tv); 59 fail("Adding view to ListLayout should throw"); 60 } catch (UnsupportedOperationException e) { 61 // Expected exception 62 } 63 } 64 65 @SmallTest 66 public void testInflateFromXml() { 67 LayoutInflater inflater = LayoutInflater.from(mContext); 68 GlifListLayout layout = (GlifListLayout) 69 inflater.inflate(R.layout.test_glif_list_layout, null); 70 assertListTemplateInflated(layout); 71 } 72 73 @SmallTest 74 public void testGetListView() { 75 GlifListLayout layout = new GlifListLayout(mContext); 76 assertListTemplateInflated(layout); 77 assertNotNull("getListView should not be null", layout.getListView()); 78 } 79 80 @SmallTest 81 public void testAdapter() { 82 GlifListLayout layout = new GlifListLayout(mContext); 83 assertListTemplateInflated(layout); 84 85 final ArrayAdapter<String> adapter = 86 new ArrayAdapter<>(mContext, android.R.layout.simple_list_item_1); 87 adapter.add("Abracadabra"); 88 layout.setAdapter(adapter); 89 90 final ListAdapter gotAdapter = layout.getAdapter(); 91 if (gotAdapter instanceof HeaderViewListAdapter) { 92 assertSame("Adapter got from GlifListLayout should be same as set", 93 adapter, ((HeaderViewListAdapter) gotAdapter).getWrappedAdapter()); 94 } else { 95 assertSame("Adapter got from GlifListLayout should be same as set", 96 adapter, gotAdapter); 97 } 98 } 99 100 @SmallTest 101 public void testDividerInset() { 102 GlifListLayout layout = new GlifListLayout(mContext); 103 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 104 layout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR); 105 } 106 assertListTemplateInflated(layout); 107 108 layout.setDividerInset(10); 109 assertEquals("Divider inset should be 10", 10, layout.getDividerInset()); 110 111 final Drawable divider = layout.getDivider(); 112 assertTrue("Divider should be instance of InsetDrawable", divider instanceof InsetDrawable); 113 } 114 115 private void assertListTemplateInflated(GlifListLayout layout) { 116 View title = layout.findViewById(R.id.suw_layout_title); 117 assertNotNull("@id/suw_layout_title should not be null", title); 118 119 View icon = layout.findViewById(R.id.suw_layout_icon); 120 assertNotNull("@id/suw_layout_icon should not be null", icon); 121 122 View listView = layout.findViewById(android.R.id.list); 123 assertTrue("@android:id/list should be a ListView", listView instanceof ListView); 124 } 125} 126