ItemAdapterTest.java revision 960c0ea0b1d36904beef0f01715dd43a211e88ca
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.test.AndroidTestCase; 20import android.test.suitebuilder.annotation.SmallTest; 21 22import com.android.setupwizardlib.items.Item; 23import com.android.setupwizardlib.items.ItemAdapter; 24import com.android.setupwizardlib.items.ItemGroup; 25import com.android.setupwizardlib.items.ItemHierarchy; 26 27import java.util.Arrays; 28import java.util.HashSet; 29 30public class ItemAdapterTest extends AndroidTestCase { 31 32 private Item[] mItems = new Item[5]; 33 private ItemGroup mItemGroup = new ItemGroup(); 34 35 @Override 36 protected void setUp() throws Exception { 37 super.setUp(); 38 for (int i = 0; i < 5; i++) { 39 Item item = new Item(); 40 item.setTitle("TestTitle" + i); 41 item.setId(i); 42 item.setLayoutResource(((i % 3) + 1) * 10); 43 mItems[i] = item; 44 mItemGroup.addChild(item); 45 } 46 } 47 48 @SmallTest 49 public void testAdapter() { 50 ItemAdapter adapter = new ItemAdapter(mItemGroup); 51 assertEquals("Adapter should have 5 items", 5, adapter.getCount()); 52 assertEquals("Adapter should return the first item", mItems[0], adapter.getItem(0)); 53 assertEquals("ID should be same as position", 2, adapter.getItemId(2)); 54 55 // Each test item has its own layout resource, and therefore its own view type 56 assertEquals("Should have 3 different view types", 3, adapter.getViewTypeCount()); 57 HashSet<Integer> viewTypes = new HashSet<>(3); 58 viewTypes.add(adapter.getItemViewType(0)); 59 viewTypes.add(adapter.getItemViewType(1)); 60 viewTypes.add(adapter.getItemViewType(2)); 61 62 assertEquals("View types should be 0, 1, 2", 63 new HashSet<>(Arrays.asList(0, 1, 2)), viewTypes); 64 } 65 66 @SmallTest 67 public void testGetRootItemHierarchy() { 68 ItemAdapter adapter = new ItemAdapter(mItemGroup); 69 ItemHierarchy root = adapter.getRootItemHierarchy(); 70 assertSame("Root item hierarchy should be mItemGroup", mItemGroup, root); 71 } 72} 73