blob: dacefa252d4a7ed9db7ef1bb23cc5732c3cef8ad [file] [log] [blame]
Cole Faustd4e193b2019-10-03 09:49:26 -07001/*
2 * Copyright 2019 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.car.ui.toolbar;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import android.content.Context;
22import android.view.View;
23import android.view.ViewGroup;
24
25import com.android.car.ui.CarUiRobolectricTestRunner;
26import com.android.car.ui.R;
27
28import org.junit.Before;
29import org.junit.Test;
30import org.junit.runner.RunWith;
31import org.robolectric.Robolectric;
32import org.robolectric.RuntimeEnvironment;
33import org.robolectric.android.controller.ActivityController;
34import org.robolectric.annotation.Config;
35
36import java.util.Arrays;
37import java.util.Collections;
38import java.util.List;
39
40@RunWith(CarUiRobolectricTestRunner.class)
41@Config(qualifiers = "land")
42public class ToolbarTest {
43
44 private Context mContext;
45 private ActivityController<TestActivity> mActivityController;
46 private TestActivity mActivity;
47 private Toolbar mToolbar;
48
49 @Before
50 public void setUp() {
51 mContext = RuntimeEnvironment.application;
52
53 mActivityController = Robolectric.buildActivity(TestActivity.class);
54 mActivityController.setup();
55
56 mActivity = mActivityController.get();
57 mToolbar = mActivity.findViewById(R.id.toolbar);
58 }
59
60 @Test
61 public void getters_nochanges_shouldReturnDefaults() {
62 assertThat(mToolbar.getBackgroundShown()).isEqualTo(true);
63 assertThat(mToolbar.getShowMenuItemsWhileSearching()).isEqualTo(false);
64 assertThat(mToolbar.getState()).isEquivalentAccordingToCompareTo(Toolbar.State.HOME);
65 assertThat(mToolbar.getNavButtonMode()).isEquivalentAccordingToCompareTo(
66 Toolbar.NavButtonMode.BACK);
67 }
68
69 @Test
70 public void setState_subpage_shouldCauseGetStateToReturnSubpage() {
71 mToolbar.setState(Toolbar.State.SUBPAGE);
72
73 assertThat(mToolbar.getState()).isEquivalentAccordingToCompareTo(Toolbar.State.SUBPAGE);
74 }
75
76 @Test
77 public void configurationChange_shouldNotLoseProperties() {
78 mToolbar.setTitle("Foo");
79 mToolbar.setSearchHint("Foo2");
80 mToolbar.setBackgroundShown(false);
81 mToolbar.setShowMenuItemsWhileSearching(true);
82 mToolbar.setState(Toolbar.State.SUBPAGE);
83 mToolbar.setNavButtonMode(Toolbar.NavButtonMode.CLOSE);
84
85 // TODO this is supposed to change the configuration, but doesn't
86 RuntimeEnvironment.setQualifiers("+port");
87 mActivityController.configurationChange();
88 mActivity = mActivityController.get();
89 mToolbar = mActivity.findViewById(R.id.toolbar);
90
91 assertThat(mToolbar.getTitle().toString()).isEqualTo("Foo");
92 assertThat(mToolbar.getSearchHint().toString()).isEqualTo("Foo2");
93 assertThat(mToolbar.getBackgroundShown()).isEqualTo(false);
94 assertThat(mToolbar.getShowMenuItemsWhileSearching()).isEqualTo(true);
95 assertThat(mToolbar.getState()).isEquivalentAccordingToCompareTo(Toolbar.State.SUBPAGE);
96 assertThat(mToolbar.getNavButtonMode()).isEquivalentAccordingToCompareTo(
97 Toolbar.NavButtonMode.CLOSE);
98 }
99
100 @Test
101 public void setCustomView_shouldInflateViewIntoToolbar() {
102 mToolbar.setCustomView(R.layout.test_custom_view);
103
104 View v = mToolbar.findViewById(R.id.text_box_1);
105
106 assertThat(v).isNotNull();
107 assertThat(mToolbar.getState()).isEquivalentAccordingToCompareTo(
108 Toolbar.State.SUBPAGE_CUSTOM);
109 }
110
111 @Test
112 public void showLogo_whenSet_andStateIsHome() {
113 mToolbar.setState(Toolbar.State.HOME);
114 mToolbar.setLogo(R.drawable.test_ic_launcher);
115
116 assertThat(mToolbar.findViewById(R.id.logo).isShown()).isTrue();
117 }
118
119 @Test
120 public void hideLogo_whenSet_andStateIsNotHome() {
121 mToolbar.setState(Toolbar.State.SUBPAGE);
122 mToolbar.setLogo(R.drawable.test_ic_launcher);
123
124 assertThat(mToolbar.findViewById(R.id.logo).isShown()).isFalse();
125 }
126
127 @Test
128 public void hideLogo_whenNotSet_andStateIsHome() {
129 mToolbar.setState(Toolbar.State.HOME);
130 mToolbar.setLogo(0);
131
132 assertThat(mToolbar.findViewById(R.id.logo).isShown()).isFalse();
133 }
134
135 @Test
136 public void hideLogo_whenNotSet_andStateIsNotHome() {
137 mToolbar.setState(Toolbar.State.SUBPAGE);
138 mToolbar.setLogo(0);
139
140 assertThat(mToolbar.findViewById(R.id.logo).isShown()).isFalse();
141 }
142
143 @Test
144 public void registerOnBackListener_whenBackIsPressed_shouldCallListener() {
145 mToolbar.setState(Toolbar.State.SUBPAGE);
146 Mutable<Integer> timesBackPressed = new Mutable<>(0);
147 Toolbar.OnBackListener listener = () -> {
148 timesBackPressed.value++;
149 return false;
150 };
151
152 mToolbar.registerOnBackListener(listener);
153 pressBack();
154
155 assertThat(timesBackPressed.value).isEqualTo(1);
156 assertThat(mActivity.getTimesBackPressed()).isEqualTo(1);
157 }
158
159 @Test
160 public void registerOnBackListener_whenAListenerReturnsTrue_shouldSuppressBack() {
161 mToolbar.setState(Toolbar.State.SUBPAGE);
162
163 mToolbar.registerOnBackListener(() -> true);
164 pressBack();
165 mToolbar.registerOnBackListener(() -> false);
166 pressBack();
167
168 assertThat(mActivity.getTimesBackPressed()).isEqualTo(0);
169 }
170
171 @Test
172 public void registerOnBackListener_whenListenerRegisteredTwice_shouldntCallListenerTwice() {
173 mToolbar.setState(Toolbar.State.SUBPAGE);
174 Mutable<Integer> timesBackPressed = new Mutable<>(0);
175 Toolbar.OnBackListener listener = () -> {
176 timesBackPressed.value++;
177 return false;
178 };
179
180 // Registering a second time shouldn't do anything
181 mToolbar.registerOnBackListener(listener);
182 mToolbar.registerOnBackListener(listener);
183 pressBack();
184
185 assertThat(timesBackPressed.value).isEqualTo(1);
186 }
187
188 @Test
189 public void unregisterOnBackListener_previouslyRegisteredListener_shouldUnregister() {
190 mToolbar.setState(Toolbar.State.SUBPAGE);
191 Mutable<Integer> timesBackPressed = new Mutable<>(0);
192 Toolbar.OnBackListener listener = () -> {
193 timesBackPressed.value++;
194 return false;
195 };
196
197 mToolbar.registerOnBackListener(listener);
198 mToolbar.unregisterOnBackListener(listener);
199 pressBack();
200
201 assertThat(timesBackPressed.value).isEqualTo(0);
202 }
203
204 @Test
205 public void menuItems_whenClicked_shouldCallListener() {
206 assertThat(((ViewGroup) mToolbar.findViewById(R.id.menu_items_container)).getChildCount())
207 .isEqualTo(0);
208
209 Mutable<Boolean> button1Clicked = new Mutable<>(false);
210 Mutable<Boolean> button2Clicked = new Mutable<>(false);
211 mToolbar.setMenuItems(Arrays.asList(
212 createMenuItem(i -> button1Clicked.value = true),
213 createMenuItem(i -> button2Clicked.value = true)));
214
215 assertThat(((ViewGroup) mToolbar.findViewById(R.id.menu_items_container)).getChildCount())
216 .isEqualTo(2);
217
218 ((ViewGroup) mToolbar.findViewById(R.id.menu_items_container))
219 .getChildAt(0).performClick();
220
221 assertThat(button1Clicked.value).isTrue();
222
223 ((ViewGroup) mToolbar.findViewById(R.id.menu_items_container))
224 .getChildAt(1).performClick();
225
226 assertThat(button2Clicked.value).isTrue();
227 }
228
229 @Test
230 public void menuItems_null_shouldRemoveExistingMenuItems() {
231 mToolbar.setMenuItems(Arrays.asList(
232 createMenuItem(i -> { }),
233 createMenuItem(i -> { })));
234
235 assertThat(((ViewGroup) mToolbar.findViewById(R.id.menu_items_container)).getChildCount())
236 .isEqualTo(2);
237
238 mToolbar.setMenuItems(null);
239
240 assertThat(((ViewGroup) mToolbar.findViewById(R.id.menu_items_container)).getChildCount())
241 .isEqualTo(0);
242 }
243
244 @Test
245 public void menuItems_setVisibility_shouldDefaultToShown() {
246 MenuItem item = createMenuItem(i -> { });
247 mToolbar.setMenuItems(Collections.singletonList(item));
248
249 View itemView = ((ViewGroup) mToolbar.findViewById(R.id.menu_items_container))
250 .getChildAt(0);
251
252 assertThat(itemView.isShown()).isTrue();
253 }
254
255 @Test
256 public void menuItems_setVisibility_shouldHide() {
257 MenuItem item = createMenuItem(i -> { });
258 mToolbar.setMenuItems(Collections.singletonList(item));
259
260 View itemView = ((ViewGroup) mToolbar.findViewById(R.id.menu_items_container))
261 .getChildAt(0);
262
263 item.setVisible(false);
264 assertThat(itemView.isShown()).isFalse();
265 }
266
267 @Test
268 public void menuItems_setVisibility_shouldReshowAfterHiding() {
269 MenuItem item = createMenuItem(i -> { });
270 mToolbar.setMenuItems(Collections.singletonList(item));
271
272 View itemView = ((ViewGroup) mToolbar.findViewById(R.id.menu_items_container))
273 .getChildAt(0);
274
275 item.setVisible(false);
276 item.setVisible(true);
277 assertThat(itemView.isShown()).isTrue();
278 }
279
280 @Test
281 public void menuItems_equalItems_shouldntRecreateViews() {
282 List<MenuItem> menuItems = Arrays.asList(
283 createMenuItem(i -> { }),
284 createMenuItem(i -> { }));
285 mToolbar.setMenuItems(menuItems);
286
287 assertThat(((ViewGroup) mToolbar.findViewById(R.id.menu_items_container)).getChildCount())
288 .isEqualTo(2);
289
290 View firstMenuItemView = ((ViewGroup) mToolbar.findViewById(R.id.menu_items_container))
291 .getChildAt(0);
292
293 mToolbar.setMenuItems(menuItems);
294
295 assertThat(firstMenuItemView).isSameAs(((ViewGroup) mToolbar
296 .findViewById(R.id.menu_items_container)).getChildAt(0));
297 }
298
299 @Test
300 public void menuItems_searchScreen_shouldHideMenuItems() {
301 mToolbar.setMenuItems(Arrays.asList(
302 MenuItem.Builder.createSearch(mContext, i -> { }),
303 createMenuItem(i -> { })));
304
305 mToolbar.setShowMenuItemsWhileSearching(false);
306 mToolbar.setState(Toolbar.State.SEARCH);
307
308 assertThat(((ViewGroup) mToolbar.findViewById(R.id.menu_items_container))
309 .getChildAt(0).isShown()).isFalse();
310 assertThat(((ViewGroup) mToolbar.findViewById(R.id.menu_items_container))
311 .getChildAt(1).isShown()).isFalse();
312 }
313
314 @Test
315 public void menuItems_showMenuItemsWhileSearching() {
316 mToolbar.setMenuItems(Arrays.asList(
317 MenuItem.Builder.createSearch(mContext, i -> { }),
318 createMenuItem(i -> { })));
319
320 mToolbar.setShowMenuItemsWhileSearching(true);
321 mToolbar.setState(Toolbar.State.SEARCH);
322
323 assertThat(((ViewGroup) mToolbar.findViewById(R.id.menu_items_container))
324 .getChildAt(0).isShown()).isFalse();
325 assertThat(((ViewGroup) mToolbar.findViewById(R.id.menu_items_container))
326 .getChildAt(1).isShown()).isTrue();
327 }
328
329 private MenuItem createMenuItem(MenuItem.OnClickListener listener) {
330 return new MenuItem.Builder(mContext)
331 .setTitle("Button!")
332 .setOnClickListener(listener)
333 .build();
334 }
335
336 private void pressBack() {
337 mToolbar.findViewById(R.id.nav_icon_container).performClick();
338 }
339
340 private static class Mutable<T> {
341 public T value;
342
343 Mutable(T value) {
344 this.value = value;
345 }
346 }
347
348}