1/*
2 * Copyright (C) 2016 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.view.ViewGroup;
21import android.widget.Button;
22import android.widget.LinearLayout;
23
24import com.android.setupwizardlib.items.ButtonBarItem;
25import com.android.setupwizardlib.items.ButtonItem;
26import com.android.setupwizardlib.items.Item;
27import com.android.setupwizardlib.items.ItemHierarchy;
28
29public class ButtonBarItemTest extends AndroidTestCase {
30
31    private ButtonItem mChild1;
32    private ButtonItem mChild2;
33    private ButtonItem mChild3;
34
35    @Override
36    protected void setUp() throws Exception {
37        super.setUp();
38        mChild1 = new ButtonItem();
39        mChild2 = new ButtonItem();
40        mChild3 = new ButtonItem();
41    }
42
43    public void testFindItemById() {
44        ButtonBarItem item = new ButtonBarItem();
45        item.setId(888);
46
47        mChild1.setId(123);
48        mChild2.setId(456);
49        mChild3.setId(789);
50        item.addChild(mChild1);
51        item.addChild(mChild2);
52        item.addChild(mChild3);
53
54        assertEquals("Finding 123 should return child1", mChild1, item.findItemById(123));
55        assertEquals("Finding 456 should return child2", mChild2, item.findItemById(456));
56        assertEquals("Finding 789 should return child3", mChild3, item.findItemById(789));
57
58        assertEquals("Finding 888 should return ButtonBarItem itself", item,
59                item.findItemById(888));
60
61        assertNull("Finding 999 should return null", item.findItemById(999));
62    }
63
64    public void testBindEmpty() {
65        ButtonBarItem item = new ButtonBarItem();
66        final ViewGroup layout = createLayout();
67        item.onBindView(layout);
68
69        assertEquals("Binding empty ButtonBar should not create any children", 0,
70                layout.getChildCount());
71    }
72
73    public void testBind() {
74        ButtonBarItem item = new ButtonBarItem();
75
76        item.addChild(mChild1);
77        mChild1.setText("child1");
78        item.addChild(mChild2);
79        mChild2.setText("child2");
80        item.addChild(mChild3);
81        mChild3.setText("child3");
82
83        final ViewGroup layout = createLayout();
84        item.onBindView(layout);
85
86        assertEquals("Binding ButtonBar should create 3 children", 3, layout.getChildCount());
87        assertEquals("First button should have text \"child1\"", "child1",
88                ((Button) layout.getChildAt(0)).getText());
89        assertEquals("Second button should have text \"child2\"", "child2",
90                ((Button) layout.getChildAt(1)).getText());
91        assertEquals("Third button should have text \"child3\"", "child3",
92                ((Button) layout.getChildAt(2)).getText());
93    }
94
95    public void testAddInvalidChild() {
96        ButtonBarItem item = new ButtonBarItem();
97
98        ItemHierarchy invalidChild = new Item();
99
100        try {
101            item.addChild(invalidChild);
102            fail("Adding non ButtonItem to ButtonBarItem should throw exception");
103        } catch (UnsupportedOperationException e) {
104            // pass
105        }
106    }
107
108    private ViewGroup createLayout() {
109        return new LinearLayout(mContext);
110    }
111}
112