ListViewTest.java revision 1d3165f10b12165f02b7015ac1a817c5f60e6399
1package android.widget; 2 3import com.google.android.collect.Lists; 4 5import junit.framework.Assert; 6 7import android.content.Context; 8import android.content.res.Resources; 9import android.test.InstrumentationTestCase; 10import android.test.mock.MockContext; 11import android.test.suitebuilder.annotation.MediumTest; 12import android.view.View; 13import android.view.ViewGroup; 14import android.widget.ArrayAdapter; 15import android.widget.ListView; 16 17import java.util.List; 18 19/** 20 * Copyright (C) 2007 The Android Open Source Project 21 * 22 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 23 * in compliance with the License. You may obtain a copy of the License at 24 * 25 * http://www.apache.org/licenses/LICENSE-2.0 26 * 27 * Unless required by applicable law or agreed to in writing, software distributed under the 28 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 29 * express or implied. See the License for the specific language governing permissions and 30 * limitations under the License. 31 */ 32 33public class ListViewTest extends InstrumentationTestCase { 34 35 /** 36 * If a view in a ListView requests a layout it should be remeasured. 37 */ 38 @MediumTest 39 public void testRequestLayout() throws Exception { 40 MockContext context = new MockContext2(); 41 ListView listView = new ListView(context); 42 List<String> items = Lists.newArrayList("hello"); 43 Adapter<String> adapter = new Adapter<String>(context, 0, items); 44 listView.setAdapter(adapter); 45 46 int measureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY); 47 48 adapter.notifyDataSetChanged(); 49 listView.measure(measureSpec, measureSpec); 50 listView.layout(0, 0, 100, 100); 51 52 MockView childView = (MockView) listView.getChildAt(0); 53 54 childView.requestLayout(); 55 childView.onMeasureCalled = false; 56 listView.measure(measureSpec, measureSpec); 57 listView.layout(0, 0, 100, 100); 58 Assert.assertTrue(childView.onMeasureCalled); 59 } 60 61 /** 62 * The list view should handle the disappearance of the only selected item, even when that item 63 * was selected before its disappearance. 64 * 65 */ 66 @MediumTest 67 public void testNoSelectableItems() throws Exception { 68 MockContext context = new MockContext2(); 69 ListView listView = new ListView(context); 70 // We use a header as the unselectable item to remain after the selectable one is removed. 71 listView.addHeaderView(new View(context), null, false); 72 List<String> items = Lists.newArrayList("hello"); 73 Adapter<String> adapter = new Adapter<String>(context, 0, items); 74 listView.setAdapter(adapter); 75 76 listView.setSelection(1); 77 78 int measureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY); 79 80 adapter.notifyDataSetChanged(); 81 listView.measure(measureSpec, measureSpec); 82 listView.layout(0, 0, 100, 100); 83 84 items.remove(0); 85 86 adapter.notifyDataSetChanged(); 87 listView.measure(measureSpec, measureSpec); 88 listView.layout(0, 0, 100, 100); 89 } 90 91 private class MockContext2 extends MockContext { 92 93 @Override 94 public Resources getResources() { 95 return getInstrumentation().getTargetContext().getResources(); 96 } 97 98 @Override 99 public Resources.Theme getTheme() { 100 return getInstrumentation().getTargetContext().getTheme(); 101 } 102 103 @Override 104 public Object getSystemService(String name) { 105 if (Context.LAYOUT_INFLATER_SERVICE.equals(name)) { 106 return getInstrumentation().getTargetContext().getSystemService(name); 107 } 108 return super.getSystemService(name); 109 } 110 } 111 112 private class MockView extends View { 113 114 public boolean onMeasureCalled = false; 115 116 public MockView(Context context) { 117 super(context); 118 } 119 120 @Override 121 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 122 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 123 onMeasureCalled = true; 124 } 125 } 126 127 private class Adapter<T> extends ArrayAdapter<T> { 128 129 public Adapter(Context context, int resource, List<T> objects) { 130 super(context, resource, objects); 131 } 132 133 @Override 134 public View getView(int position, View convertView, ViewGroup parent) { 135 return new MockView(getContext()); 136 } 137 } 138 139} 140