Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2007 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 android.widget;
     18 
     19 import android.test.suitebuilder.annotation.Suppress;
     20 import com.google.android.collect.Lists;
     21 
     22 import junit.framework.Assert;
     23 
     24 import android.content.Context;
     25 import android.content.res.Resources;
     26 import android.test.InstrumentationTestCase;
     27 import android.test.mock.MockContext;
     28 import android.test.suitebuilder.annotation.MediumTest;
     29 import android.view.View;
     30 import android.view.ViewGroup;
     31 import android.widget.ArrayAdapter;
     32 import android.widget.ListView;
     33 
     34 import java.util.List;
     35 
     36 @Suppress // Failing.
     37 public class ListViewTest extends InstrumentationTestCase {
     38 
     39     /**
     40      * If a view in a ListView requests a layout it should be remeasured.
     41      */
     42     @MediumTest
     43     public void testRequestLayout() throws Exception {
     44         MockContext context = new MockContext2();
     45         ListView listView = new ListView(context);
     46         List<String> items = Lists.newArrayList("hello");
     47         Adapter<String> adapter = new Adapter<String>(context, 0, items);
     48         listView.setAdapter(adapter);
     49 
     50         int measureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY);
     51 
     52         adapter.notifyDataSetChanged();
     53         listView.measure(measureSpec, measureSpec);
     54         listView.layout(0, 0, 100, 100);
     55 
     56         MockView childView = (MockView) listView.getChildAt(0);
     57 
     58         childView.requestLayout();
     59         childView.onMeasureCalled = false;
     60         listView.measure(measureSpec, measureSpec);
     61         listView.layout(0, 0, 100, 100);
     62         Assert.assertTrue(childView.onMeasureCalled);
     63     }
     64 
     65     /**
     66      * The list view should handle the disappearance of the only selected item, even when that item
     67      * was selected before its disappearance.
     68      *
     69      */
     70     @MediumTest
     71     public void testNoSelectableItems() throws Exception {
     72         MockContext context = new MockContext2();
     73         ListView listView = new ListView(context);
     74         // We use a header as the unselectable item to remain after the selectable one is removed.
     75         listView.addHeaderView(new View(context), null, false);
     76         List<String> items = Lists.newArrayList("hello");
     77         Adapter<String> adapter = new Adapter<String>(context, 0, items);
     78         listView.setAdapter(adapter);
     79 
     80         listView.setSelection(1);
     81 
     82         int measureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY);
     83 
     84         adapter.notifyDataSetChanged();
     85         listView.measure(measureSpec, measureSpec);
     86         listView.layout(0, 0, 100, 100);
     87 
     88         items.remove(0);
     89 
     90         adapter.notifyDataSetChanged();
     91         listView.measure(measureSpec, measureSpec);
     92         listView.layout(0, 0, 100, 100);
     93     }
     94 
     95     private class MockContext2 extends MockContext {
     96 
     97         @Override
     98         public Resources getResources() {
     99             return getInstrumentation().getTargetContext().getResources();
    100         }
    101 
    102         @Override
    103         public Resources.Theme getTheme() {
    104             return getInstrumentation().getTargetContext().getTheme();
    105         }
    106 
    107         @Override
    108         public Object getSystemService(String name) {
    109             if (Context.LAYOUT_INFLATER_SERVICE.equals(name)) {
    110                 return getInstrumentation().getTargetContext().getSystemService(name);
    111             }
    112             return super.getSystemService(name);
    113         }
    114     }
    115 
    116     private class MockView extends View {
    117 
    118         public boolean onMeasureCalled = false;
    119 
    120         public MockView(Context context) {
    121             super(context);
    122         }
    123 
    124         @Override
    125         protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    126             super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    127             onMeasureCalled = true;
    128         }
    129     }
    130 
    131     private class Adapter<T> extends ArrayAdapter<T> {
    132 
    133         public Adapter(Context context, int resource, List<T> objects) {
    134             super(context, resource, objects);
    135         }
    136 
    137         @Override
    138         public View getView(int position, View convertView, ViewGroup parent) {
    139             return new MockView(getContext());
    140         }
    141     }
    142 
    143 }
    144