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 static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertNotNull;
21import static org.junit.Assert.assertSame;
22import static org.junit.Assert.assertTrue;
23import static org.junit.Assert.fail;
24
25import android.content.Context;
26import android.graphics.drawable.Drawable;
27import android.graphics.drawable.InsetDrawable;
28import android.os.Build;
29import android.support.test.InstrumentationRegistry;
30import android.support.test.filters.SmallTest;
31import android.support.test.runner.AndroidJUnit4;
32import android.support.v7.widget.RecyclerView;
33import android.support.v7.widget.RecyclerView.Adapter;
34import android.view.ContextThemeWrapper;
35import android.view.LayoutInflater;
36import android.view.View;
37import android.view.View.MeasureSpec;
38import android.view.ViewGroup;
39
40import com.android.setupwizardlib.GlifRecyclerLayout;
41
42import org.junit.Before;
43import org.junit.Test;
44import org.junit.runner.RunWith;
45
46@RunWith(AndroidJUnit4.class)
47@SmallTest
48public class GlifRecyclerLayoutTest {
49
50    private Context mContext;
51
52    @Before
53    public void setUp() throws Exception {
54        mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(),
55                R.style.SuwThemeGlif_Light);
56    }
57
58    @Test
59    public void testDefaultTemplate() {
60        GlifRecyclerLayout layout = new GlifRecyclerLayout(mContext);
61        assertRecyclerTemplateInflated(layout);
62    }
63
64    @Test
65    public void testInflateFromXml() {
66        LayoutInflater inflater = LayoutInflater.from(mContext);
67        GlifRecyclerLayout layout = (GlifRecyclerLayout)
68                inflater.inflate(R.layout.test_glif_recycler_layout, null);
69        assertRecyclerTemplateInflated(layout);
70    }
71
72    @Test
73    public void testGetRecyclerView() {
74        GlifRecyclerLayout layout = new GlifRecyclerLayout(mContext);
75        assertRecyclerTemplateInflated(layout);
76        assertNotNull("getRecyclerView should not be null", layout.getRecyclerView());
77    }
78
79    @Test
80    public void testAdapter() {
81        GlifRecyclerLayout layout = new GlifRecyclerLayout(mContext);
82        assertRecyclerTemplateInflated(layout);
83
84        final RecyclerView.Adapter adapter = createTestAdapter(1);
85        layout.setAdapter(adapter);
86
87        final RecyclerView.Adapter gotAdapter = layout.getAdapter();
88        // Note: The wrapped adapter should be returned, not the HeaderAdapter.
89        assertSame("Adapter got from GlifRecyclerLayout should be same as set",
90                adapter, gotAdapter);
91    }
92
93    @Test
94    public void testLayout() {
95        GlifRecyclerLayout layout = new GlifRecyclerLayout(mContext);
96        assertRecyclerTemplateInflated(layout);
97
98        layout.setAdapter(createTestAdapter(3));
99
100        layout.measure(
101                MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY),
102                MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY));
103        layout.layout(0, 0, 500, 500);
104        // Test that the layout code doesn't crash.
105    }
106
107    @Test
108    public void testDividerInset() {
109        GlifRecyclerLayout layout = new GlifRecyclerLayout(mContext);
110        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
111            layout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
112        }
113        assertRecyclerTemplateInflated(layout);
114
115        layout.setDividerInset(10);
116        assertEquals("Divider inset should be 10", 10, layout.getDividerInset());
117
118        final Drawable divider = layout.getDivider();
119        assertTrue("Divider should be instance of InsetDrawable", divider instanceof InsetDrawable);
120    }
121
122    @Test
123    public void testTemplateWithNoRecyclerView() {
124        try {
125            new GlifRecyclerLayout(mContext, R.layout.suw_glif_template);
126            fail("Creating GlifRecyclerLayout with no recycler view should throw exception");
127        } catch (Exception e) {
128            // pass
129        }
130    }
131
132    private void assertRecyclerTemplateInflated(GlifRecyclerLayout layout) {
133        View recyclerView = layout.findViewById(R.id.suw_recycler_view);
134        assertTrue("@id/suw_recycler_view should be a RecyclerView",
135                recyclerView instanceof RecyclerView);
136
137        assertNotNull("Header text view should not be null",
138                layout.findManagedViewById(R.id.suw_layout_title));
139        assertNotNull("Icon view should not be null",
140                layout.findManagedViewById(R.id.suw_layout_icon));
141    }
142
143    private Adapter createTestAdapter(final int itemCount) {
144        return new RecyclerView.Adapter() {
145            @Override
146            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {
147                return new RecyclerView.ViewHolder(new View(parent.getContext())) {};
148            }
149
150            @Override
151            public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
152            }
153
154            @Override
155            public int getItemCount() {
156                return itemCount;
157            }
158        };
159    }
160}
161