blob: 1a415c2a3b673cfdd330374596e0880bab44125b [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
Cole Faustdb6866c2019-10-09 15:56:07 -0700116 assertThat(mToolbar.findViewById(R.id.car_ui_toolbar_logo).isShown()).isTrue();
Cole Faustd4e193b2019-10-03 09:49:26 -0700117 }
118
119 @Test
120 public void hideLogo_whenSet_andStateIsNotHome() {
121 mToolbar.setState(Toolbar.State.SUBPAGE);
122 mToolbar.setLogo(R.drawable.test_ic_launcher);
123
Cole Faustdb6866c2019-10-09 15:56:07 -0700124 assertThat(mToolbar.findViewById(R.id.car_ui_toolbar_logo).isShown()).isFalse();
Cole Faustd4e193b2019-10-03 09:49:26 -0700125 }
126
127 @Test
128 public void hideLogo_whenNotSet_andStateIsHome() {
129 mToolbar.setState(Toolbar.State.HOME);
130 mToolbar.setLogo(0);
131
Cole Faustdb6866c2019-10-09 15:56:07 -0700132 assertThat(mToolbar.findViewById(R.id.car_ui_toolbar_logo).isShown()).isFalse();
Cole Faustd4e193b2019-10-03 09:49:26 -0700133 }
134
135 @Test
136 public void hideLogo_whenNotSet_andStateIsNotHome() {
137 mToolbar.setState(Toolbar.State.SUBPAGE);
138 mToolbar.setLogo(0);
139
Cole Faustdb6866c2019-10-09 15:56:07 -0700140 assertThat(mToolbar.findViewById(R.id.car_ui_toolbar_logo).isShown()).isFalse();
Cole Faustd4e193b2019-10-03 09:49:26 -0700141 }
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() {
Cole Faustdb6866c2019-10-09 15:56:07 -0700206 assertThat(getMenuItemViewCount()).isEqualTo(0);
Cole Faustd4e193b2019-10-03 09:49:26 -0700207
208 Mutable<Boolean> button1Clicked = new Mutable<>(false);
209 Mutable<Boolean> button2Clicked = new Mutable<>(false);
210 mToolbar.setMenuItems(Arrays.asList(
211 createMenuItem(i -> button1Clicked.value = true),
212 createMenuItem(i -> button2Clicked.value = true)));
213
Cole Faustdb6866c2019-10-09 15:56:07 -0700214 assertThat(getMenuItemViewCount()).isEqualTo(2);
Cole Faustd4e193b2019-10-03 09:49:26 -0700215
Cole Faustdb6866c2019-10-09 15:56:07 -0700216 getMenuItemView(0).performClick();
Cole Faustd4e193b2019-10-03 09:49:26 -0700217
218 assertThat(button1Clicked.value).isTrue();
219
Cole Faustdb6866c2019-10-09 15:56:07 -0700220 getMenuItemView(1).performClick();
Cole Faustd4e193b2019-10-03 09:49:26 -0700221
222 assertThat(button2Clicked.value).isTrue();
223 }
224
225 @Test
226 public void menuItems_null_shouldRemoveExistingMenuItems() {
227 mToolbar.setMenuItems(Arrays.asList(
228 createMenuItem(i -> { }),
229 createMenuItem(i -> { })));
230
Cole Faustdb6866c2019-10-09 15:56:07 -0700231 assertThat(getMenuItemViewCount()).isEqualTo(2);
Cole Faustd4e193b2019-10-03 09:49:26 -0700232
233 mToolbar.setMenuItems(null);
234
Cole Faustdb6866c2019-10-09 15:56:07 -0700235 assertThat(getMenuItemViewCount()).isEqualTo(0);
Cole Faustd4e193b2019-10-03 09:49:26 -0700236 }
237
238 @Test
239 public void menuItems_setVisibility_shouldDefaultToShown() {
240 MenuItem item = createMenuItem(i -> { });
241 mToolbar.setMenuItems(Collections.singletonList(item));
242
Cole Faustdb6866c2019-10-09 15:56:07 -0700243 assertThat(getMenuItemView(0).isShown()).isTrue();
Cole Faustd4e193b2019-10-03 09:49:26 -0700244 }
245
246 @Test
247 public void menuItems_setVisibility_shouldHide() {
248 MenuItem item = createMenuItem(i -> { });
249 mToolbar.setMenuItems(Collections.singletonList(item));
250
Cole Faustd4e193b2019-10-03 09:49:26 -0700251 item.setVisible(false);
Cole Faustdb6866c2019-10-09 15:56:07 -0700252 assertThat(getMenuItemView(0).isShown()).isFalse();
Cole Faustd4e193b2019-10-03 09:49:26 -0700253 }
254
255 @Test
256 public void menuItems_setVisibility_shouldReshowAfterHiding() {
257 MenuItem item = createMenuItem(i -> { });
258 mToolbar.setMenuItems(Collections.singletonList(item));
259
Cole Faustd4e193b2019-10-03 09:49:26 -0700260 item.setVisible(false);
261 item.setVisible(true);
Cole Faustdb6866c2019-10-09 15:56:07 -0700262 assertThat(getMenuItemView(0).isShown()).isTrue();
Cole Faustd4e193b2019-10-03 09:49:26 -0700263 }
264
265 @Test
266 public void menuItems_equalItems_shouldntRecreateViews() {
267 List<MenuItem> menuItems = Arrays.asList(
268 createMenuItem(i -> { }),
269 createMenuItem(i -> { }));
270 mToolbar.setMenuItems(menuItems);
271
Cole Faustdb6866c2019-10-09 15:56:07 -0700272 assertThat(getMenuItemViewCount()).isEqualTo(2);
Cole Faustd4e193b2019-10-03 09:49:26 -0700273
Cole Faustdb6866c2019-10-09 15:56:07 -0700274 View firstMenuItemView = getMenuItemView(0);
Cole Faustd4e193b2019-10-03 09:49:26 -0700275
276 mToolbar.setMenuItems(menuItems);
277
Cole Faustdb6866c2019-10-09 15:56:07 -0700278 assertThat(firstMenuItemView).isSameAs(getMenuItemView(0));
Cole Faustd4e193b2019-10-03 09:49:26 -0700279 }
280
281 @Test
282 public void menuItems_searchScreen_shouldHideMenuItems() {
283 mToolbar.setMenuItems(Arrays.asList(
284 MenuItem.Builder.createSearch(mContext, i -> { }),
285 createMenuItem(i -> { })));
286
287 mToolbar.setShowMenuItemsWhileSearching(false);
288 mToolbar.setState(Toolbar.State.SEARCH);
289
Cole Faustdb6866c2019-10-09 15:56:07 -0700290 assertThat(getMenuItemView(0).isShown()).isFalse();
291 assertThat(getMenuItemView(1).isShown()).isFalse();
Cole Faustd4e193b2019-10-03 09:49:26 -0700292 }
293
294 @Test
295 public void menuItems_showMenuItemsWhileSearching() {
296 mToolbar.setMenuItems(Arrays.asList(
297 MenuItem.Builder.createSearch(mContext, i -> { }),
298 createMenuItem(i -> { })));
299
300 mToolbar.setShowMenuItemsWhileSearching(true);
301 mToolbar.setState(Toolbar.State.SEARCH);
302
Cole Faustdb6866c2019-10-09 15:56:07 -0700303 assertThat(getMenuItemView(0).isShown()).isFalse();
304 assertThat(getMenuItemView(1).isShown()).isTrue();
Cole Faustd4e193b2019-10-03 09:49:26 -0700305 }
306
307 private MenuItem createMenuItem(MenuItem.OnClickListener listener) {
308 return new MenuItem.Builder(mContext)
309 .setTitle("Button!")
310 .setOnClickListener(listener)
311 .build();
312 }
313
Cole Faustdb6866c2019-10-09 15:56:07 -0700314 private int getMenuItemViewCount() {
315 return ((ViewGroup) mToolbar
316 .findViewById(R.id.car_ui_toolbar_menu_items_container))
317 .getChildCount();
318 }
319
320 private View getMenuItemView(int index) {
321 return ((ViewGroup) mToolbar
322 .findViewById(R.id.car_ui_toolbar_menu_items_container))
323 .getChildAt(index);
324 }
325
Cole Faustd4e193b2019-10-03 09:49:26 -0700326 private void pressBack() {
Cole Faustdb6866c2019-10-09 15:56:07 -0700327 mToolbar.findViewById(R.id.car_ui_toolbar_nav_icon_container).performClick();
Cole Faustd4e193b2019-10-03 09:49:26 -0700328 }
329
330 private static class Mutable<T> {
331 public T value;
332
333 Mutable(T value) {
334 this.value = value;
335 }
336 }
337
338}