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.graphics.drawable.Drawable;
20import android.graphics.drawable.ShapeDrawable;
21import android.test.AndroidTestCase;
22import android.test.suitebuilder.annotation.SmallTest;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.FrameLayout;
26import android.widget.ImageView;
27import android.widget.TextView;
28
29import com.android.setupwizardlib.R;
30import com.android.setupwizardlib.items.Item;
31
32public class ItemTest extends AndroidTestCase {
33
34    private TextView mTitleView;
35    private TextView mSummaryView;
36    private ImageView mIconView;
37    private FrameLayout mIconContainer;
38
39    @SmallTest
40    public void testOnBindView() {
41        Item item = new Item();
42        item.setTitle("TestTitle");
43        item.setSummary("TestSummary");
44        Drawable icon = new ShapeDrawable();
45        icon.setLevel(4);
46        item.setIcon(icon);
47        View view = createLayout();
48
49        mIconView.setImageLevel(1);
50        Drawable recycledIcon = new ShapeDrawable();
51        mIconView.setImageDrawable(recycledIcon);
52
53        item.onBindView(view);
54
55        assertEquals("Title should be \"TestTitle\"", "TestTitle", mTitleView.getText().toString());
56        assertEquals("Summary should be \"TestSummary\"", "TestSummary",
57                mSummaryView.getText().toString());
58        assertSame("Icon should be the icon shape drawable", icon, mIconView.getDrawable());
59        assertEquals("Recycled icon level should not change", 1, recycledIcon.getLevel());
60        assertEquals("Icon should be level 4", 4, icon.getLevel());
61    }
62
63    @SmallTest
64    public void testSingleLineItem() {
65        Item item = new Item();
66        item.setTitle("TestTitle");
67        View view = createLayout();
68
69        item.onBindView(view);
70
71        assertEquals("Title should be \"TestTitle\"", "TestTitle", mTitleView.getText().toString());
72        assertEquals("Summary should be gone", View.GONE, mSummaryView.getVisibility());
73        assertEquals("IconContainer should be gone", View.GONE, mIconContainer.getVisibility());
74    }
75
76    @SmallTest
77    public void testProperties() {
78        Item item = new Item();
79        item.setTitle("TestTitle");
80        item.setSummary("TestSummary");
81        item.setEnabled(false);
82        ShapeDrawable icon = new ShapeDrawable();
83        item.setIcon(icon);
84        item.setId(12345);
85        item.setLayoutResource(56789);
86
87        assertEquals("Title should be \"TestTitle\"", "TestTitle", item.getTitle());
88        assertEquals("Summary should be \"TestSummary\"", "TestSummary", item.getSummary());
89        assertFalse("Enabled should be false", item.isEnabled());
90        assertSame("Icon should be same as set", icon, item.getIcon());
91        assertEquals("ID should be 12345", 12345, item.getId());
92        assertEquals("Layout resource should be 56789", 56789, item.getLayoutResource());
93    }
94
95    @SmallTest
96    public void testDefaultValues() {
97        Item item = new Item();
98
99        assertNull("Default title should be null", item.getTitle());
100        assertNull("Default summary should be null", item.getSummary());
101        assertNull("Default icon should be null", item.getIcon());
102        assertTrue("Default enabled should be true", item.isEnabled());
103        assertEquals("Default ID should be 0", 0, item.getId());
104        assertEquals("Default layout resource should be R.layout.suw_items_text",
105                R.layout.suw_items_default, item.getLayoutResource());
106        assertTrue("Default visible should be true", item.isVisible());
107    }
108
109    @SmallTest
110    public void testHierarchyImplementation() {
111        Item item = new Item();
112        item.setId(12345);
113
114        assertEquals("getCount should be 1", 1, item.getCount());
115        assertSame("getItemAt should return itself", item, item.getItemAt(0));
116        assertSame("findItemById with same ID should return itself", item,
117                item.findItemById(12345));
118        assertNull("findItemById with different ID should return null", item.findItemById(34567));
119    }
120
121    @SmallTest
122    public void testVisible() {
123        Item item = new Item();
124        item.setVisible(false);
125
126        assertFalse("Item should not be visible", item.isVisible());
127        assertEquals("Item count should be 0 when not visible", 0, item.getCount());
128    }
129
130    private ViewGroup createLayout() {
131        ViewGroup root = new FrameLayout(mContext);
132
133        mTitleView = new TextView(mContext);
134        mTitleView.setId(R.id.suw_items_title);
135        root.addView(mTitleView);
136
137        mSummaryView = new TextView(mContext);
138        mSummaryView.setId(R.id.suw_items_summary);
139        root.addView(mSummaryView);
140
141        mIconContainer = new FrameLayout(mContext);
142        mIconContainer.setId(R.id.suw_items_icon_container);
143        root.addView(mIconContainer);
144
145        mIconView = new ImageView(mContext);
146        mIconView.setId(R.id.suw_items_icon);
147        mIconContainer.addView(mIconView);
148
149        return root;
150    }
151}
152