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.ItemGroup; 24 25public class ItemGroupTest extends AndroidTestCase { 26 27 public static final Item CHILD_1 = new Item(); 28 public static final Item CHILD_2 = new Item(); 29 public static final Item CHILD_3 = new Item(); 30 public static final Item CHILD_4 = new Item(); 31 32 @SmallTest 33 public void testGroup() { 34 ItemGroup itemGroup = new ItemGroup(); 35 itemGroup.addChild(CHILD_1); 36 itemGroup.addChild(CHILD_2); 37 38 assertSame("Item at position 0 should be child1", CHILD_1, itemGroup.getItemAt(0)); 39 assertSame("Item at position 1 should be child2", CHILD_2, itemGroup.getItemAt(1)); 40 assertEquals("Should have 2 children", 2, itemGroup.getCount()); 41 } 42 43 @SmallTest 44 public void testRemoveChild() { 45 ItemGroup itemGroup = new ItemGroup(); 46 itemGroup.addChild(CHILD_1); 47 itemGroup.addChild(CHILD_2); 48 49 itemGroup.removeChild(CHILD_1); 50 51 assertSame("Item at position 0 should be child2", CHILD_2, itemGroup.getItemAt(0)); 52 assertEquals("Should have 1 child", 1, itemGroup.getCount()); 53 } 54 55 @SmallTest 56 public void testClear() { 57 ItemGroup itemGroup = new ItemGroup(); 58 itemGroup.addChild(CHILD_1); 59 itemGroup.addChild(CHILD_2); 60 61 itemGroup.clear(); 62 63 assertEquals("Should have 0 child", 0, itemGroup.getCount()); 64 } 65 66 @SmallTest 67 public void testNestedGroup() { 68 ItemGroup parentGroup = new ItemGroup(); 69 ItemGroup childGroup = new ItemGroup(); 70 71 parentGroup.addChild(CHILD_1); 72 childGroup.addChild(CHILD_2); 73 childGroup.addChild(CHILD_3); 74 parentGroup.addChild(childGroup); 75 parentGroup.addChild(CHILD_4); 76 77 CHILD_1.setTitle("CHILD1"); 78 CHILD_2.setTitle("CHILD2"); 79 CHILD_3.setTitle("CHILD3"); 80 CHILD_4.setTitle("CHILD4"); 81 82 assertSame("Position 0 should be child 1", CHILD_1, parentGroup.getItemAt(0)); 83 assertSame("Position 1 should be child 2", CHILD_2, parentGroup.getItemAt(1)); 84 assertSame("Position 2 should be child 3", CHILD_3, parentGroup.getItemAt(2)); 85 assertSame("Position 3 should be child 4", CHILD_4, parentGroup.getItemAt(3)); 86 } 87 88 @SmallTest 89 public void testEmptyChildGroup() { 90 ItemGroup parentGroup = new ItemGroup(); 91 ItemGroup childGroup = new ItemGroup(); 92 93 parentGroup.addChild(CHILD_1); 94 parentGroup.addChild(childGroup); 95 parentGroup.addChild(CHILD_2); 96 97 assertSame("Position 0 should be child 1", CHILD_1, parentGroup.getItemAt(0)); 98 assertSame("Position 1 should be child 2", CHILD_2, parentGroup.getItemAt(1)); 99 } 100 101 @SmallTest 102 public void testFindItemById() { 103 ItemGroup itemGroup = new ItemGroup(); 104 CHILD_1.setId(12345); 105 CHILD_2.setId(23456); 106 107 itemGroup.addChild(CHILD_1); 108 itemGroup.addChild(CHILD_2); 109 110 assertSame("Find item 23456 should return child 2", CHILD_2, itemGroup.findItemById(23456)); 111 } 112 113 @SmallTest 114 public void testFindItemByIdNotFound() { 115 ItemGroup itemGroup = new ItemGroup(); 116 CHILD_1.setId(12345); 117 CHILD_2.setId(23456); 118 119 itemGroup.addChild(CHILD_1); 120 itemGroup.addChild(CHILD_2); 121 122 assertNull("ID not found should return null", itemGroup.findItemById(56789)); 123 } 124} 125