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.content.Context;
20import android.graphics.drawable.Drawable;
21import android.graphics.drawable.InsetDrawable;
22import android.os.Build;
23import android.support.v7.widget.RecyclerView;
24import android.test.InstrumentationTestCase;
25import android.test.suitebuilder.annotation.SmallTest;
26import android.view.ContextThemeWrapper;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30
31import com.android.setupwizardlib.GlifPreferenceLayout;
32
33public class GlifPreferenceLayoutTest extends InstrumentationTestCase {
34
35    private Context mContext;
36
37    @Override
38    protected void setUp() throws Exception {
39        super.setUp();
40        mContext = new ContextThemeWrapper(getInstrumentation().getContext(),
41                R.style.SuwThemeGlif_Light);
42    }
43
44    @SmallTest
45    public void testDefaultTemplate() {
46        GlifPreferenceLayout layout = new TestLayout(mContext);
47        assertPreferenceTemplateInflated(layout);
48    }
49
50    @SmallTest
51    public void testGetRecyclerView() {
52        GlifPreferenceLayout layout = new TestLayout(mContext);
53        assertPreferenceTemplateInflated(layout);
54        assertNotNull("getRecyclerView should not be null", layout.getRecyclerView());
55    }
56
57    @SmallTest
58    public void testOnCreateRecyclerView() {
59        GlifPreferenceLayout layout = new TestLayout(mContext);
60        assertPreferenceTemplateInflated(layout);
61        final RecyclerView recyclerView = layout.onCreateRecyclerView(LayoutInflater.from(mContext),
62                layout, null /* savedInstanceState */);
63        assertNotNull("RecyclerView created should not be null", recyclerView);
64    }
65
66    @SmallTest
67    public void testDividerInset() {
68        GlifPreferenceLayout layout = new TestLayout(mContext);
69        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
70            layout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
71        }
72        assertPreferenceTemplateInflated(layout);
73
74        layout.addView(layout.onCreateRecyclerView(LayoutInflater.from(mContext), layout,
75                null /* savedInstanceState */));
76
77        layout.setDividerInset(10);
78        assertEquals("Divider inset should be 10", 10, layout.getDividerInset());
79
80        final Drawable divider = layout.getDivider();
81        assertTrue("Divider should be instance of InsetDrawable", divider instanceof InsetDrawable);
82    }
83
84    private void assertPreferenceTemplateInflated(GlifPreferenceLayout layout) {
85        View contentContainer = layout.findViewById(R.id.suw_layout_content);
86        assertTrue("@id/suw_layout_content should be a ViewGroup",
87                contentContainer instanceof ViewGroup);
88
89        if (layout instanceof TestLayout) {
90            assertNotNull("Header text view should not be null",
91                    ((TestLayout) layout).findManagedViewById(R.id.suw_layout_title));
92            assertNotNull("Icon view should not be null",
93                    ((TestLayout) layout).findManagedViewById(R.id.suw_layout_icon));
94        }
95    }
96
97    // Make some methods public for testing
98    public static class TestLayout extends GlifPreferenceLayout {
99
100        public TestLayout(Context context) {
101            super(context);
102        }
103
104        @Override
105        public View findManagedViewById(int id) {
106            return super.findManagedViewById(id);
107        }
108    }
109}
110