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.content.Context;
20import android.graphics.drawable.Drawable;
21import android.graphics.drawable.InsetDrawable;
22import android.os.Build;
23import android.test.InstrumentationTestCase;
24import android.test.suitebuilder.annotation.SmallTest;
25import android.view.ContextThemeWrapper;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.widget.ArrayAdapter;
29import android.widget.HeaderViewListAdapter;
30import android.widget.ListAdapter;
31import android.widget.ListView;
32import android.widget.TextView;
33
34import com.android.setupwizardlib.GlifListLayout;
35
36public class GlifListLayoutTest extends InstrumentationTestCase {
37
38    private Context mContext;
39
40    @Override
41    protected void setUp() throws Exception {
42        super.setUp();
43        mContext = new ContextThemeWrapper(getInstrumentation().getContext(),
44                R.style.SuwThemeGlif_Light);
45    }
46
47    @SmallTest
48    public void testDefaultTemplate() {
49        GlifListLayout layout = new GlifListLayout(mContext);
50        assertListTemplateInflated(layout);
51    }
52
53    @SmallTest
54    public void testAddView() {
55        GlifListLayout layout = new GlifListLayout(mContext);
56        TextView tv = new TextView(mContext);
57        try {
58            layout.addView(tv);
59            fail("Adding view to ListLayout should throw");
60        } catch (UnsupportedOperationException e) {
61            // Expected exception
62        }
63    }
64
65    @SmallTest
66    public void testInflateFromXml() {
67        LayoutInflater inflater = LayoutInflater.from(mContext);
68        GlifListLayout layout = (GlifListLayout)
69                inflater.inflate(R.layout.test_glif_list_layout, null);
70        assertListTemplateInflated(layout);
71    }
72
73    @SmallTest
74    public void testGetListView() {
75        GlifListLayout layout = new GlifListLayout(mContext);
76        assertListTemplateInflated(layout);
77        assertNotNull("getListView should not be null", layout.getListView());
78    }
79
80    @SmallTest
81    public void testAdapter() {
82        GlifListLayout layout = new GlifListLayout(mContext);
83        assertListTemplateInflated(layout);
84
85        final ArrayAdapter<String> adapter =
86                new ArrayAdapter<>(mContext, android.R.layout.simple_list_item_1);
87        adapter.add("Abracadabra");
88        layout.setAdapter(adapter);
89
90        final ListAdapter gotAdapter = layout.getAdapter();
91        // Note: the wrapped adapter should be returned directly, not the HeaderViewListAdapter.
92        assertSame("Adapter got from GlifListLayout should be same as set",
93                adapter, gotAdapter);
94    }
95
96    @SmallTest
97    public void testDividerInset() {
98        GlifListLayout layout = new GlifListLayout(mContext);
99        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
100            layout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
101        }
102        assertListTemplateInflated(layout);
103
104        layout.setDividerInset(10);
105        assertEquals("Divider inset should be 10", 10, layout.getDividerInset());
106
107        final Drawable divider = layout.getDivider();
108        assertTrue("Divider should be instance of InsetDrawable", divider instanceof InsetDrawable);
109    }
110
111    private void assertListTemplateInflated(GlifListLayout layout) {
112        View title = layout.findViewById(R.id.suw_layout_title);
113        assertNotNull("@id/suw_layout_title should not be null", title);
114
115        View icon = layout.findViewById(R.id.suw_layout_icon);
116        assertNotNull("@id/suw_layout_icon should not be null", icon);
117
118        View listView = layout.findViewById(android.R.id.list);
119        assertTrue("@android:id/list should be a ListView", listView instanceof ListView);
120    }
121}
122