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