ItemGroupTest.java revision ab45bdf67a01ba13efb45334cc43f9632de6f034
1/*
2 * Copyright (C) 2017 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.items;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertNull;
21import static org.junit.Assert.assertSame;
22import static org.mockito.Matchers.eq;
23import static org.mockito.Mockito.inOrder;
24import static org.mockito.Mockito.verify;
25import static org.mockito.Mockito.verifyNoMoreInteractions;
26
27import com.android.setupwizardlib.BuildConfig;
28import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
29
30import org.junit.Before;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33import org.mockito.InOrder;
34import org.mockito.Mock;
35import org.mockito.MockitoAnnotations;
36import org.robolectric.annotation.Config;
37
38@RunWith(SuwLibRobolectricTestRunner.class)
39@Config(
40        constants = BuildConfig.class,
41        sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK })
42public class ItemGroupTest {
43
44    private static final Item CHILD_1 = new EqualsItem("Child 1");
45    private static final Item CHILD_2 = new EqualsItem("Child 2");
46    private static final Item CHILD_3 = new EqualsItem("Child 3");
47    private static final Item CHILD_4 = new EqualsItem("Child 4");
48
49    private ItemGroup mItemGroup;
50
51    @Mock
52    private ItemHierarchy.Observer mObserver;
53
54    @Before
55    public void setUp() {
56        MockitoAnnotations.initMocks(this);
57        mItemGroup = new ItemGroup();
58        mItemGroup.registerObserver(mObserver);
59    }
60
61    @Test
62    public void testGroup() {
63        mItemGroup.addChild(CHILD_1);
64        mItemGroup.addChild(CHILD_2);
65
66        assertSame("Item at position 0 should be child1", CHILD_1, mItemGroup.getItemAt(0));
67        assertSame("Item at position 1 should be child2", CHILD_2, mItemGroup.getItemAt(1));
68        assertEquals("Should have 2 children", 2, mItemGroup.getCount());
69
70        final InOrder inOrder = inOrder(mObserver);
71        inOrder.verify(mObserver).onItemRangeInserted(eq(mItemGroup), eq(0), eq(1));
72        inOrder.verify(mObserver).onItemRangeInserted(eq(mItemGroup), eq(1), eq(1));
73    }
74
75    @Test
76    public void testRemoveChild() {
77        mItemGroup.addChild(CHILD_1);
78        mItemGroup.addChild(CHILD_2);
79        mItemGroup.addChild(CHILD_3);
80
81        mItemGroup.removeChild(CHILD_2);
82
83        assertSame("Item at position 0 should be child1", CHILD_1, mItemGroup.getItemAt(0));
84        assertSame("Item at position 1 should be child3", CHILD_3, mItemGroup.getItemAt(1));
85        assertEquals("Should have 2 children", 2, mItemGroup.getCount());
86
87        verify(mObserver).onItemRangeRemoved(eq(mItemGroup), eq(1), eq(1));
88    }
89
90    @Test
91    public void testClear() {
92        mItemGroup.addChild(CHILD_1);
93        mItemGroup.addChild(CHILD_2);
94
95        mItemGroup.clear();
96
97        assertEquals("Should have 0 child", 0, mItemGroup.getCount());
98
99        verify(mObserver).onItemRangeRemoved(eq(mItemGroup), eq(0), eq(2));
100    }
101
102    @Test
103    public void testNestedGroup() {
104        ItemGroup parentGroup = new ItemGroup();
105        ItemGroup childGroup = new ItemGroup();
106        parentGroup.registerObserver(mObserver);
107
108        parentGroup.addChild(CHILD_1);
109        childGroup.addChild(CHILD_2);
110        childGroup.addChild(CHILD_3);
111        parentGroup.addChild(childGroup);
112        parentGroup.addChild(CHILD_4);
113
114        assertSame("Position 0 should be child 1", CHILD_1, parentGroup.getItemAt(0));
115        assertSame("Position 1 should be child 2", CHILD_2, parentGroup.getItemAt(1));
116        assertSame("Position 2 should be child 3", CHILD_3, parentGroup.getItemAt(2));
117        assertSame("Position 3 should be child 4", CHILD_4, parentGroup.getItemAt(3));
118
119        final InOrder inOrder = inOrder(mObserver);
120        inOrder.verify(mObserver).onItemRangeInserted(eq(parentGroup), eq(0), eq(1));
121        inOrder.verify(mObserver).onItemRangeInserted(eq(parentGroup), eq(1), eq(2));
122        inOrder.verify(mObserver).onItemRangeInserted(eq(parentGroup), eq(3), eq(1));
123        verifyNoMoreInteractions(mObserver);
124    }
125
126    @Test
127    public void testNotifyChange() {
128        mItemGroup.addChild(CHILD_1);
129        mItemGroup.addChild(CHILD_2);
130
131        CHILD_2.setTitle("Child 2 modified");
132
133        verify(mObserver).onItemRangeChanged(eq(mItemGroup), eq(1), eq(1));
134    }
135
136    @Test
137    public void testEmptyChildGroup() {
138        ItemGroup parentGroup = new ItemGroup();
139        ItemGroup childGroup = new ItemGroup();
140
141        parentGroup.addChild(CHILD_1);
142        parentGroup.addChild(childGroup);
143        parentGroup.addChild(CHILD_2);
144
145        assertSame("Position 0 should be child 1", CHILD_1, parentGroup.getItemAt(0));
146        assertSame("Position 1 should be child 2", CHILD_2, parentGroup.getItemAt(1));
147    }
148
149    @Test
150    public void testFindItemById() {
151        CHILD_1.setId(12345);
152        CHILD_2.setId(23456);
153
154        mItemGroup.addChild(CHILD_1);
155        mItemGroup.addChild(CHILD_2);
156
157        assertSame("Find item 23456 should return child 2",
158                CHILD_2, mItemGroup.findItemById(23456));
159    }
160
161    @Test
162    public void testFindItemByIdNotFound() {
163        CHILD_1.setId(12345);
164        CHILD_2.setId(23456);
165
166        mItemGroup.addChild(CHILD_1);
167        mItemGroup.addChild(CHILD_2);
168
169        assertNull("ID not found should return null", mItemGroup.findItemById(56789));
170    }
171
172    /**
173     * This class will always return true on {@link #equals(Object)}. Used to ensure that ItemGroup
174     * is using identity rather than equals(). Be sure to use assertSame rather than assertEquals
175     * when comparing items of this class.
176     */
177    private static class EqualsItem extends Item {
178
179        EqualsItem(String name) {
180            setTitle(name);
181        }
182
183        @Override
184        public int hashCode() {
185            return 1;
186        }
187
188        @Override
189        public boolean equals(Object obj) {
190            return obj instanceof Item;
191        }
192
193        @Override
194        public String toString() {
195            return "EqualsItem{title=" + getTitle() + "}";
196        }
197    }
198}
199