Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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.cts;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertFalse;
     21 import static org.junit.Assert.assertTrue;
     22 import static org.mockito.Mockito.mock;
     23 import static org.mockito.Mockito.reset;
     24 import static org.mockito.Mockito.times;
     25 import static org.mockito.Mockito.verify;
     26 import static org.mockito.Mockito.verifyZeroInteractions;
     27 
     28 import android.database.DataSetObserver;
     29 import android.support.test.filters.SmallTest;
     30 import android.support.test.runner.AndroidJUnit4;
     31 import android.view.View;
     32 import android.view.ViewGroup;
     33 import android.widget.BaseExpandableListAdapter;
     34 
     35 import org.junit.Test;
     36 import org.junit.runner.RunWith;
     37 
     38 /**
     39  * Test {@link BaseExpandableListAdapter}.
     40  */
     41 @SmallTest
     42 @RunWith(AndroidJUnit4.class)
     43 public class BaseExpandableListAdapterTest {
     44     @Test
     45     public void testDefaults() {
     46         // Test child type / group type APIs for the default values documented in the method
     47         // Javadocs
     48         BaseExpandableListAdapter adapter = new MockBaseExpandableListAdapter();
     49         assertEquals(1, adapter.getGroupTypeCount());
     50         assertEquals(0, adapter.getGroupType(0));
     51         assertEquals(1, adapter.getChildTypeCount());
     52         assertEquals(0, adapter.getChildType(0, 0));
     53     }
     54 
     55     @Test
     56     public void testAreAllItemsEnabled() {
     57         BaseExpandableListAdapter adapter = new MockBaseExpandableListAdapter();
     58         assertTrue(adapter.areAllItemsEnabled());
     59     }
     60 
     61     @Test
     62     public void testGetCombinedId() {
     63         BaseExpandableListAdapter adapter = new MockBaseExpandableListAdapter();
     64 
     65         long childID = adapter.getCombinedChildId(10, 100);
     66         long groupID = adapter.getCombinedGroupId(10);
     67 
     68         // there should be no clash in group and child IDs
     69         assertTrue(childID != groupID);
     70 
     71         childID = adapter.getCombinedChildId(0, 0);
     72         groupID = adapter.getCombinedGroupId(0);
     73         assertTrue(childID != groupID);
     74     }
     75 
     76     @Test
     77     public void testIsEmpty() {
     78         MockBaseExpandableListAdapter adapter = new MockBaseExpandableListAdapter();
     79         assertTrue(adapter.isEmpty());
     80         adapter.setGroupCount(10);
     81         assertFalse(adapter.isEmpty());
     82     }
     83 
     84     @Test
     85     public void testNotifyDataSetChanged() {
     86         BaseExpandableListAdapter adapter = new MockBaseExpandableListAdapter();
     87         DataSetObserver mockDataSetObserver = mock(DataSetObserver.class);
     88         adapter.registerDataSetObserver(mockDataSetObserver);
     89 
     90         verifyZeroInteractions(mockDataSetObserver);
     91         adapter.notifyDataSetChanged();
     92         verify(mockDataSetObserver, times(1)).onChanged();
     93     }
     94 
     95     @Test
     96     public void testNotifyDataSetInvalidated() {
     97         BaseExpandableListAdapter adapter = new MockBaseExpandableListAdapter();
     98         DataSetObserver mockDataSetObserver = mock(DataSetObserver.class);
     99         adapter.registerDataSetObserver(mockDataSetObserver);
    100 
    101         verifyZeroInteractions(mockDataSetObserver);
    102         adapter.notifyDataSetInvalidated();
    103         verify(mockDataSetObserver, times(1)).onInvalidated();
    104     }
    105 
    106     @Test
    107     public void testOnGroupCollapsed() {
    108         BaseExpandableListAdapter adapter = new MockBaseExpandableListAdapter();
    109         // this function is non-operation.
    110         adapter.onGroupCollapsed(0);
    111     }
    112 
    113     @Test
    114     public void testOnGroupExpanded() {
    115         BaseExpandableListAdapter adapter = new MockBaseExpandableListAdapter();
    116         // this function is non-operation.
    117         adapter.onGroupExpanded(0);
    118     }
    119 
    120     @Test
    121     public void testDataSetObserver() {
    122         BaseExpandableListAdapter adapter = new MockBaseExpandableListAdapter();
    123         DataSetObserver mockDataSetObserver = mock(DataSetObserver.class);
    124         adapter.registerDataSetObserver(mockDataSetObserver);
    125 
    126         verifyZeroInteractions(mockDataSetObserver);
    127         adapter.notifyDataSetChanged();
    128         verify(mockDataSetObserver, times(1)).onChanged();
    129 
    130         reset(mockDataSetObserver);
    131         verifyZeroInteractions(mockDataSetObserver);
    132         adapter.unregisterDataSetObserver(mockDataSetObserver);
    133         adapter.notifyDataSetChanged();
    134         verifyZeroInteractions(mockDataSetObserver);
    135     }
    136 
    137     private class MockBaseExpandableListAdapter extends BaseExpandableListAdapter {
    138         private int mGroupCount;
    139 
    140         public Object getChild(int groupPosition, int childPosition) {
    141             return null;
    142         }
    143 
    144         public long getChildId(int groupPosition, int childPosition) {
    145             return 0;
    146         }
    147 
    148         public View getChildView(int groupPosition, int childPosition,
    149                 boolean isLastChild, View convertView, ViewGroup parent) {
    150             return null;
    151         }
    152 
    153         public int getChildrenCount(int groupPosition) {
    154             return 0;
    155         }
    156 
    157         public Object getGroup(int groupPosition) {
    158             return null;
    159         }
    160 
    161         public int getGroupCount() {
    162             return mGroupCount;
    163         }
    164 
    165         public void setGroupCount(int count) {
    166             mGroupCount = count;
    167         }
    168 
    169         public long getGroupId(int groupPosition) {
    170             return 0;
    171         }
    172 
    173         public View getGroupView(int groupPosition, boolean isExpanded,
    174                 View convertView, ViewGroup parent) {
    175             return null;
    176         }
    177 
    178         public boolean hasStableIds() {
    179             return false;
    180         }
    181 
    182         public boolean isChildSelectable(int groupPosition, int childPosition) {
    183             return false;
    184         }
    185     }
    186 }
    187