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