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 static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertSame; 21import static org.mockito.Mockito.inOrder; 22import static org.mockito.Mockito.mock; 23 24import android.database.DataSetObserver; 25import android.support.test.filters.SmallTest; 26import android.support.test.runner.AndroidJUnit4; 27 28import com.android.setupwizardlib.items.Item; 29import com.android.setupwizardlib.items.ItemAdapter; 30import com.android.setupwizardlib.items.ItemGroup; 31import com.android.setupwizardlib.items.ItemHierarchy; 32 33import org.junit.Before; 34import org.junit.Test; 35import org.junit.runner.RunWith; 36import org.mockito.InOrder; 37 38import java.util.Arrays; 39import java.util.HashSet; 40 41@RunWith(AndroidJUnit4.class) 42@SmallTest 43public class ItemAdapterTest { 44 45 private Item[] mItems = new Item[5]; 46 private ItemGroup mItemGroup = new ItemGroup(); 47 48 @Before 49 public void setUp() throws Exception { 50 for (int i = 0; i < 5; i++) { 51 Item item = new Item(); 52 item.setTitle("TestTitle" + i); 53 item.setId(i); 54 item.setLayoutResource(((i % 3) + 1) * 10); 55 mItems[i] = item; 56 mItemGroup.addChild(item); 57 } 58 } 59 60 @Test 61 public void testAdapter() { 62 ItemAdapter adapter = new ItemAdapter(mItemGroup); 63 assertEquals("Adapter should have 5 items", 5, adapter.getCount()); 64 assertEquals("Adapter should return the first item", mItems[0], adapter.getItem(0)); 65 assertEquals("ID should be same as position", 2, adapter.getItemId(2)); 66 67 // Each test item has its own layout resource, and therefore its own view type 68 assertEquals("Should have 3 different view types", 3, adapter.getViewTypeCount()); 69 HashSet<Integer> viewTypes = new HashSet<>(3); 70 viewTypes.add(adapter.getItemViewType(0)); 71 viewTypes.add(adapter.getItemViewType(1)); 72 viewTypes.add(adapter.getItemViewType(2)); 73 74 assertEquals("View types should be 0, 1, 2", 75 new HashSet<>(Arrays.asList(0, 1, 2)), viewTypes); 76 } 77 78 @Test 79 public void testGetRootItemHierarchy() { 80 ItemAdapter adapter = new ItemAdapter(mItemGroup); 81 ItemHierarchy root = adapter.getRootItemHierarchy(); 82 assertSame("Root item hierarchy should be mItemGroup", mItemGroup, root); 83 } 84 85 @Test 86 public void testAdapterNotifications() { 87 ItemAdapter adapter = new ItemAdapter(mItemGroup); 88 final DataSetObserver observer = mock(DataSetObserver.class); 89 adapter.registerDataSetObserver(observer); 90 final InOrder inOrder = inOrder(observer); 91 92 mItems[0].setTitle("Child 1"); 93 inOrder.verify(observer).onChanged(); 94 95 mItemGroup.removeChild(mItems[1]); 96 inOrder.verify(observer).onChanged(); 97 98 mItemGroup.addChild(mItems[1]); 99 inOrder.verify(observer).onChanged(); 100 } 101} 102