Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.car.ui.toolbar; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
Ram Parameswaran | 05fec07 | 2019-10-17 13:24:27 -0700 | [diff] [blame] | 21 | import static org.mockito.Mockito.spy; |
| 22 | import static org.mockito.Mockito.when; |
| 23 | |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Ram Parameswaran | 05fec07 | 2019-10-17 13:24:27 -0700 | [diff] [blame] | 25 | import android.content.res.Resources; |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 26 | import android.view.View; |
| 27 | import android.view.ViewGroup; |
| 28 | |
| 29 | import com.android.car.ui.CarUiRobolectricTestRunner; |
| 30 | import com.android.car.ui.R; |
Cole Faust | b283015 | 2019-10-10 14:49:43 -0700 | [diff] [blame] | 31 | import com.android.car.ui.utils.ShadowTypeface; |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 32 | |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
| 36 | import org.robolectric.Robolectric; |
| 37 | import org.robolectric.RuntimeEnvironment; |
| 38 | import org.robolectric.android.controller.ActivityController; |
| 39 | import org.robolectric.annotation.Config; |
| 40 | |
| 41 | import java.util.Arrays; |
| 42 | import java.util.Collections; |
| 43 | import java.util.List; |
| 44 | |
| 45 | @RunWith(CarUiRobolectricTestRunner.class) |
Cole Faust | b283015 | 2019-10-10 14:49:43 -0700 | [diff] [blame] | 46 | @Config(qualifiers = "land", shadows = ShadowTypeface.class) |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 47 | public class ToolbarTest { |
| 48 | |
| 49 | private Context mContext; |
Ram Parameswaran | 05fec07 | 2019-10-17 13:24:27 -0700 | [diff] [blame] | 50 | private Resources mResources; |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 51 | private ActivityController<TestActivity> mActivityController; |
| 52 | private TestActivity mActivity; |
| 53 | private Toolbar mToolbar; |
| 54 | |
| 55 | @Before |
| 56 | public void setUp() { |
| 57 | mContext = RuntimeEnvironment.application; |
Ram Parameswaran | 05fec07 | 2019-10-17 13:24:27 -0700 | [diff] [blame] | 58 | mResources = mContext.getResources(); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 59 | mActivityController = Robolectric.buildActivity(TestActivity.class); |
| 60 | mActivityController.setup(); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 61 | mActivity = mActivityController.get(); |
| 62 | mToolbar = mActivity.findViewById(R.id.toolbar); |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void getters_nochanges_shouldReturnDefaults() { |
| 67 | assertThat(mToolbar.getBackgroundShown()).isEqualTo(true); |
| 68 | assertThat(mToolbar.getShowMenuItemsWhileSearching()).isEqualTo(false); |
| 69 | assertThat(mToolbar.getState()).isEquivalentAccordingToCompareTo(Toolbar.State.HOME); |
| 70 | assertThat(mToolbar.getNavButtonMode()).isEquivalentAccordingToCompareTo( |
| 71 | Toolbar.NavButtonMode.BACK); |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void setState_subpage_shouldCauseGetStateToReturnSubpage() { |
| 76 | mToolbar.setState(Toolbar.State.SUBPAGE); |
| 77 | |
| 78 | assertThat(mToolbar.getState()).isEquivalentAccordingToCompareTo(Toolbar.State.SUBPAGE); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void configurationChange_shouldNotLoseProperties() { |
| 83 | mToolbar.setTitle("Foo"); |
| 84 | mToolbar.setSearchHint("Foo2"); |
| 85 | mToolbar.setBackgroundShown(false); |
| 86 | mToolbar.setShowMenuItemsWhileSearching(true); |
| 87 | mToolbar.setState(Toolbar.State.SUBPAGE); |
| 88 | mToolbar.setNavButtonMode(Toolbar.NavButtonMode.CLOSE); |
| 89 | |
| 90 | // TODO this is supposed to change the configuration, but doesn't |
| 91 | RuntimeEnvironment.setQualifiers("+port"); |
| 92 | mActivityController.configurationChange(); |
| 93 | mActivity = mActivityController.get(); |
| 94 | mToolbar = mActivity.findViewById(R.id.toolbar); |
| 95 | |
| 96 | assertThat(mToolbar.getTitle().toString()).isEqualTo("Foo"); |
| 97 | assertThat(mToolbar.getSearchHint().toString()).isEqualTo("Foo2"); |
| 98 | assertThat(mToolbar.getBackgroundShown()).isEqualTo(false); |
| 99 | assertThat(mToolbar.getShowMenuItemsWhileSearching()).isEqualTo(true); |
| 100 | assertThat(mToolbar.getState()).isEquivalentAccordingToCompareTo(Toolbar.State.SUBPAGE); |
| 101 | assertThat(mToolbar.getNavButtonMode()).isEquivalentAccordingToCompareTo( |
| 102 | Toolbar.NavButtonMode.CLOSE); |
| 103 | } |
| 104 | |
| 105 | @Test |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 106 | public void showLogo_whenSet_andStateIsHome() { |
| 107 | mToolbar.setState(Toolbar.State.HOME); |
| 108 | mToolbar.setLogo(R.drawable.test_ic_launcher); |
| 109 | |
Cole Faust | db6866c | 2019-10-09 15:56:07 -0700 | [diff] [blame] | 110 | assertThat(mToolbar.findViewById(R.id.car_ui_toolbar_logo).isShown()).isTrue(); |
Roberto Perez | 90c45bb | 2019-10-21 17:30:48 -0700 | [diff] [blame] | 111 | assertThat(mToolbar.findViewById(R.id.car_ui_toolbar_title_logo).isShown()).isFalse(); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Test |
Roberto Perez | 90c45bb | 2019-10-21 17:30:48 -0700 | [diff] [blame] | 115 | public void showTitleLogo_whenSet_andStateIsNotHome() { |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 116 | mToolbar.setState(Toolbar.State.SUBPAGE); |
| 117 | mToolbar.setLogo(R.drawable.test_ic_launcher); |
| 118 | |
Cole Faust | db6866c | 2019-10-09 15:56:07 -0700 | [diff] [blame] | 119 | assertThat(mToolbar.findViewById(R.id.car_ui_toolbar_logo).isShown()).isFalse(); |
Roberto Perez | 90c45bb | 2019-10-21 17:30:48 -0700 | [diff] [blame] | 120 | assertThat(mToolbar.findViewById(R.id.car_ui_toolbar_title_logo).isShown()).isTrue(); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Test |
Roberto Perez | 90c45bb | 2019-10-21 17:30:48 -0700 | [diff] [blame] | 124 | public void hideLogo_andTitleLogo_whenNotSet_andStateIsHome() { |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 125 | mToolbar.setState(Toolbar.State.HOME); |
| 126 | mToolbar.setLogo(0); |
| 127 | |
Cole Faust | db6866c | 2019-10-09 15:56:07 -0700 | [diff] [blame] | 128 | assertThat(mToolbar.findViewById(R.id.car_ui_toolbar_logo).isShown()).isFalse(); |
Roberto Perez | 90c45bb | 2019-10-21 17:30:48 -0700 | [diff] [blame] | 129 | assertThat(mToolbar.findViewById(R.id.car_ui_toolbar_title_logo).isShown()).isFalse(); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Test |
Roberto Perez | 90c45bb | 2019-10-21 17:30:48 -0700 | [diff] [blame] | 133 | public void hideLogo_andTitleLogo_whenNotSet_andStateIsNotHome() { |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 134 | mToolbar.setState(Toolbar.State.SUBPAGE); |
| 135 | mToolbar.setLogo(0); |
| 136 | |
Cole Faust | db6866c | 2019-10-09 15:56:07 -0700 | [diff] [blame] | 137 | assertThat(mToolbar.findViewById(R.id.car_ui_toolbar_logo).isShown()).isFalse(); |
Roberto Perez | 90c45bb | 2019-10-21 17:30:48 -0700 | [diff] [blame] | 138 | assertThat(mToolbar.findViewById(R.id.car_ui_toolbar_title_logo).isShown()).isFalse(); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Test |
| 142 | public void registerOnBackListener_whenBackIsPressed_shouldCallListener() { |
| 143 | mToolbar.setState(Toolbar.State.SUBPAGE); |
| 144 | Mutable<Integer> timesBackPressed = new Mutable<>(0); |
| 145 | Toolbar.OnBackListener listener = () -> { |
| 146 | timesBackPressed.value++; |
| 147 | return false; |
| 148 | }; |
| 149 | |
| 150 | mToolbar.registerOnBackListener(listener); |
| 151 | pressBack(); |
| 152 | |
| 153 | assertThat(timesBackPressed.value).isEqualTo(1); |
| 154 | assertThat(mActivity.getTimesBackPressed()).isEqualTo(1); |
| 155 | } |
| 156 | |
| 157 | @Test |
| 158 | public void registerOnBackListener_whenAListenerReturnsTrue_shouldSuppressBack() { |
| 159 | mToolbar.setState(Toolbar.State.SUBPAGE); |
| 160 | |
| 161 | mToolbar.registerOnBackListener(() -> true); |
| 162 | pressBack(); |
| 163 | mToolbar.registerOnBackListener(() -> false); |
| 164 | pressBack(); |
| 165 | |
| 166 | assertThat(mActivity.getTimesBackPressed()).isEqualTo(0); |
| 167 | } |
| 168 | |
| 169 | @Test |
Ram Parameswaran | 05fec07 | 2019-10-17 13:24:27 -0700 | [diff] [blame] | 170 | public void testState_twoRow_withTitle_withTabs() { |
| 171 | mockResources(); |
| 172 | when(mResources.getBoolean(R.bool.car_ui_toolbar_tabs_on_second_row)).thenReturn(true); |
| 173 | |
| 174 | Toolbar toolbar = new Toolbar(mContext); |
| 175 | assertThat(toolbar.isTabsInSecondRow()).isTrue(); |
| 176 | |
| 177 | // Set title and tabs for toolbar. |
| 178 | toolbar.setTitle("Test title"); |
| 179 | toolbar.addTab(new TabLayout.Tab(mContext.getDrawable(R.drawable.test_ic_launcher), "Foo")); |
| 180 | toolbar.addTab(new TabLayout.Tab(mContext.getDrawable(R.drawable.test_ic_launcher), "Foo")); |
| 181 | |
| 182 | // Toolbar should display two rows, showing both title and tabs. |
| 183 | assertThat(toolbar.findViewById(R.id.car_ui_toolbar_tabs).getVisibility()).isEqualTo( |
| 184 | View.VISIBLE); |
| 185 | assertThat(toolbar.findViewById(R.id.car_ui_toolbar_title).getVisibility()).isEqualTo( |
| 186 | View.VISIBLE); |
| 187 | } |
| 188 | |
| 189 | @Test |
| 190 | public void testState_twoRow_withTitle() { |
| 191 | mockResources(); |
| 192 | when(mResources.getBoolean(R.bool.car_ui_toolbar_tabs_on_second_row)).thenReturn(true); |
| 193 | |
| 194 | Toolbar toolbar = new Toolbar(mContext); |
| 195 | assertThat(toolbar.isTabsInSecondRow()).isTrue(); |
| 196 | |
| 197 | toolbar.setTitle("Test title"); |
| 198 | |
| 199 | // Toolbar should display two rows, but no tabs are set so they should not be visible. |
| 200 | assertThat(toolbar.findViewById(R.id.car_ui_toolbar_title).getVisibility()).isEqualTo( |
| 201 | View.VISIBLE); |
| 202 | assertThat(toolbar.findViewById(R.id.car_ui_toolbar_tabs).getVisibility()).isNotEqualTo( |
| 203 | View.VISIBLE); |
| 204 | } |
| 205 | |
| 206 | @Test |
| 207 | public void testState_twoRow_withTabs() { |
| 208 | mockResources(); |
| 209 | when(mResources.getBoolean(R.bool.car_ui_toolbar_tabs_on_second_row)).thenReturn(true); |
| 210 | |
| 211 | Toolbar toolbar = new Toolbar(mContext); |
| 212 | assertThat(toolbar.isTabsInSecondRow()).isTrue(); |
| 213 | toolbar.addTab(new TabLayout.Tab(mContext.getDrawable(R.drawable.test_ic_launcher), "Foo")); |
| 214 | toolbar.addTab(new TabLayout.Tab(mContext.getDrawable(R.drawable.test_ic_launcher), "Foo")); |
| 215 | |
| 216 | // Toolbar should display two rows with an empty title and tabs. |
| 217 | assertThat(toolbar.findViewById(R.id.car_ui_toolbar_tabs).getVisibility()).isEqualTo( |
| 218 | View.VISIBLE); |
| 219 | assertThat(toolbar.findViewById(R.id.car_ui_toolbar_title).getVisibility()).isEqualTo( |
| 220 | View.VISIBLE); |
| 221 | } |
| 222 | |
| 223 | @Test |
| 224 | public void testState_oneRow_withTitle_withTabs() { |
| 225 | mockResources(); |
| 226 | when(mResources.getBoolean(R.bool.car_ui_toolbar_tabs_on_second_row)).thenReturn(false); |
| 227 | |
| 228 | Toolbar toolbar = new Toolbar(mContext); |
| 229 | assertThat(toolbar.isTabsInSecondRow()).isFalse(); |
| 230 | |
| 231 | // Set title and tabs for toolbar. |
| 232 | toolbar.setTitle("Test title"); |
| 233 | toolbar.addTab(new TabLayout.Tab(mContext.getDrawable(R.drawable.test_ic_launcher), "Foo")); |
| 234 | toolbar.addTab(new TabLayout.Tab(mContext.getDrawable(R.drawable.test_ic_launcher), "Foo")); |
| 235 | |
| 236 | // With only one row available, toolbar will only show tabs and not the title. |
| 237 | assertThat(toolbar.findViewById(R.id.car_ui_toolbar_tabs).getVisibility()).isEqualTo( |
| 238 | View.VISIBLE); |
| 239 | assertThat(toolbar.findViewById(R.id.car_ui_toolbar_title).getVisibility()).isNotEqualTo( |
| 240 | View.VISIBLE); |
| 241 | } |
| 242 | |
| 243 | @Test |
| 244 | public void testState_oneRow_withTitle() { |
| 245 | mockResources(); |
| 246 | when(mResources.getBoolean(R.bool.car_ui_toolbar_tabs_on_second_row)).thenReturn(false); |
| 247 | |
| 248 | |
| 249 | Toolbar toolbar = new Toolbar(mContext); |
| 250 | assertThat(toolbar.isTabsInSecondRow()).isFalse(); |
| 251 | |
| 252 | toolbar.setTitle("Test title"); |
| 253 | |
| 254 | // Toolbar should display one row with the title and no tabs. |
| 255 | assertThat(toolbar.findViewById(R.id.car_ui_toolbar_tabs).getVisibility()).isNotEqualTo( |
| 256 | View.VISIBLE); |
| 257 | assertThat(toolbar.findViewById(R.id.car_ui_toolbar_title).getVisibility()).isEqualTo( |
| 258 | View.VISIBLE); |
| 259 | } |
| 260 | |
| 261 | @Test |
| 262 | public void testState_oneRow_withTabs() { |
| 263 | mockResources(); |
| 264 | when(mResources.getBoolean(R.bool.car_ui_toolbar_tabs_on_second_row)).thenReturn(false); |
| 265 | |
| 266 | |
| 267 | Toolbar toolbar = new Toolbar(mContext); |
| 268 | assertThat(toolbar.isTabsInSecondRow()).isFalse(); |
| 269 | |
| 270 | toolbar.addTab(new TabLayout.Tab(mContext.getDrawable(R.drawable.test_ic_launcher), "Foo")); |
| 271 | toolbar.addTab(new TabLayout.Tab(mContext.getDrawable(R.drawable.test_ic_launcher), "Foo")); |
| 272 | |
| 273 | // Toolbar should display one row with only tabs. |
| 274 | assertThat(toolbar.findViewById(R.id.car_ui_toolbar_tabs).getVisibility()).isEqualTo( |
| 275 | View.VISIBLE); |
| 276 | assertThat(toolbar.findViewById(R.id.car_ui_toolbar_title).getVisibility()).isNotEqualTo( |
| 277 | View.VISIBLE); |
| 278 | } |
| 279 | |
| 280 | @Test |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 281 | public void registerOnBackListener_whenListenerRegisteredTwice_shouldntCallListenerTwice() { |
| 282 | mToolbar.setState(Toolbar.State.SUBPAGE); |
| 283 | Mutable<Integer> timesBackPressed = new Mutable<>(0); |
| 284 | Toolbar.OnBackListener listener = () -> { |
| 285 | timesBackPressed.value++; |
| 286 | return false; |
| 287 | }; |
| 288 | |
| 289 | // Registering a second time shouldn't do anything |
| 290 | mToolbar.registerOnBackListener(listener); |
| 291 | mToolbar.registerOnBackListener(listener); |
| 292 | pressBack(); |
| 293 | |
| 294 | assertThat(timesBackPressed.value).isEqualTo(1); |
| 295 | } |
| 296 | |
| 297 | @Test |
| 298 | public void unregisterOnBackListener_previouslyRegisteredListener_shouldUnregister() { |
| 299 | mToolbar.setState(Toolbar.State.SUBPAGE); |
| 300 | Mutable<Integer> timesBackPressed = new Mutable<>(0); |
| 301 | Toolbar.OnBackListener listener = () -> { |
| 302 | timesBackPressed.value++; |
| 303 | return false; |
| 304 | }; |
| 305 | |
| 306 | mToolbar.registerOnBackListener(listener); |
| 307 | mToolbar.unregisterOnBackListener(listener); |
| 308 | pressBack(); |
| 309 | |
| 310 | assertThat(timesBackPressed.value).isEqualTo(0); |
| 311 | } |
| 312 | |
| 313 | @Test |
Cole Faust | 4ad34c8 | 2019-11-19 17:55:11 -0800 | [diff] [blame] | 314 | public void menuItems_builder_id() { |
| 315 | MenuItem item = MenuItem.builder(mContext) |
| 316 | .setId(5) |
| 317 | .build(); |
| 318 | |
| 319 | assertThat(item.getId()).isEqualTo(5); |
| 320 | } |
| 321 | |
| 322 | @Test |
| 323 | public void menuItems_setId_shouldWork() { |
| 324 | MenuItem item = MenuItem.builder(mContext).build(); |
| 325 | |
| 326 | assertThat(item.getId()).isEqualTo(0); |
| 327 | |
| 328 | item.setId(7); |
| 329 | |
| 330 | assertThat(item.getId()).isEqualTo(7); |
| 331 | } |
| 332 | |
| 333 | @Test |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 334 | public void menuItems_whenClicked_shouldCallListener() { |
Cole Faust | 9de2199 | 2019-11-22 10:45:25 -0800 | [diff] [blame] | 335 | assertThat(getMenuItemCount()).isEqualTo(0); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 336 | |
| 337 | Mutable<Boolean> button1Clicked = new Mutable<>(false); |
| 338 | Mutable<Boolean> button2Clicked = new Mutable<>(false); |
| 339 | mToolbar.setMenuItems(Arrays.asList( |
| 340 | createMenuItem(i -> button1Clicked.value = true), |
| 341 | createMenuItem(i -> button2Clicked.value = true))); |
| 342 | |
Cole Faust | 9de2199 | 2019-11-22 10:45:25 -0800 | [diff] [blame] | 343 | assertThat(getMenuItemCount()).isEqualTo(2); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 344 | |
Cole Faust | db6866c | 2019-10-09 15:56:07 -0700 | [diff] [blame] | 345 | getMenuItemView(0).performClick(); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 346 | |
| 347 | assertThat(button1Clicked.value).isTrue(); |
| 348 | |
Cole Faust | db6866c | 2019-10-09 15:56:07 -0700 | [diff] [blame] | 349 | getMenuItemView(1).performClick(); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 350 | |
| 351 | assertThat(button2Clicked.value).isTrue(); |
| 352 | } |
| 353 | |
| 354 | @Test |
| 355 | public void menuItems_null_shouldRemoveExistingMenuItems() { |
| 356 | mToolbar.setMenuItems(Arrays.asList( |
Ram Parameswaran | 05fec07 | 2019-10-17 13:24:27 -0700 | [diff] [blame] | 357 | createMenuItem(i -> { |
| 358 | }), |
| 359 | createMenuItem(i -> { |
| 360 | }))); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 361 | |
Cole Faust | 9de2199 | 2019-11-22 10:45:25 -0800 | [diff] [blame] | 362 | assertThat(getMenuItemCount()).isEqualTo(2); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 363 | |
| 364 | mToolbar.setMenuItems(null); |
| 365 | |
Cole Faust | 9de2199 | 2019-11-22 10:45:25 -0800 | [diff] [blame] | 366 | assertThat(getMenuItemCount()).isEqualTo(0); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | @Test |
| 370 | public void menuItems_setVisibility_shouldDefaultToShown() { |
Ram Parameswaran | 05fec07 | 2019-10-17 13:24:27 -0700 | [diff] [blame] | 371 | MenuItem item = createMenuItem(i -> { |
| 372 | }); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 373 | mToolbar.setMenuItems(Collections.singletonList(item)); |
| 374 | |
Cole Faust | db6866c | 2019-10-09 15:56:07 -0700 | [diff] [blame] | 375 | assertThat(getMenuItemView(0).isShown()).isTrue(); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | @Test |
| 379 | public void menuItems_setVisibility_shouldHide() { |
Ram Parameswaran | 05fec07 | 2019-10-17 13:24:27 -0700 | [diff] [blame] | 380 | MenuItem item = createMenuItem(i -> { |
| 381 | }); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 382 | mToolbar.setMenuItems(Collections.singletonList(item)); |
| 383 | |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 384 | item.setVisible(false); |
Cole Faust | db6866c | 2019-10-09 15:56:07 -0700 | [diff] [blame] | 385 | assertThat(getMenuItemView(0).isShown()).isFalse(); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | @Test |
| 389 | public void menuItems_setVisibility_shouldReshowAfterHiding() { |
Ram Parameswaran | 05fec07 | 2019-10-17 13:24:27 -0700 | [diff] [blame] | 390 | MenuItem item = createMenuItem(i -> { |
| 391 | }); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 392 | mToolbar.setMenuItems(Collections.singletonList(item)); |
| 393 | |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 394 | item.setVisible(false); |
| 395 | item.setVisible(true); |
Cole Faust | db6866c | 2019-10-09 15:56:07 -0700 | [diff] [blame] | 396 | assertThat(getMenuItemView(0).isShown()).isTrue(); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | @Test |
| 400 | public void menuItems_equalItems_shouldntRecreateViews() { |
| 401 | List<MenuItem> menuItems = Arrays.asList( |
Ram Parameswaran | 05fec07 | 2019-10-17 13:24:27 -0700 | [diff] [blame] | 402 | createMenuItem(i -> { |
| 403 | }), |
| 404 | createMenuItem(i -> { |
| 405 | })); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 406 | mToolbar.setMenuItems(menuItems); |
| 407 | |
Cole Faust | 9de2199 | 2019-11-22 10:45:25 -0800 | [diff] [blame] | 408 | assertThat(getMenuItemCount()).isEqualTo(2); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 409 | |
Cole Faust | db6866c | 2019-10-09 15:56:07 -0700 | [diff] [blame] | 410 | View firstMenuItemView = getMenuItemView(0); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 411 | |
| 412 | mToolbar.setMenuItems(menuItems); |
| 413 | |
Cole Faust | db6866c | 2019-10-09 15:56:07 -0700 | [diff] [blame] | 414 | assertThat(firstMenuItemView).isSameAs(getMenuItemView(0)); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | @Test |
| 418 | public void menuItems_searchScreen_shouldHideMenuItems() { |
| 419 | mToolbar.setMenuItems(Arrays.asList( |
Cole Faust | 042a8a6 | 2019-11-22 14:11:58 -0800 | [diff] [blame^] | 420 | MenuItem.builder(mContext).setToSearch().build(), |
Ram Parameswaran | 05fec07 | 2019-10-17 13:24:27 -0700 | [diff] [blame] | 421 | createMenuItem(i -> { |
| 422 | }))); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 423 | |
| 424 | mToolbar.setShowMenuItemsWhileSearching(false); |
| 425 | mToolbar.setState(Toolbar.State.SEARCH); |
| 426 | |
Cole Faust | db6866c | 2019-10-09 15:56:07 -0700 | [diff] [blame] | 427 | assertThat(getMenuItemView(0).isShown()).isFalse(); |
| 428 | assertThat(getMenuItemView(1).isShown()).isFalse(); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | @Test |
| 432 | public void menuItems_showMenuItemsWhileSearching() { |
| 433 | mToolbar.setMenuItems(Arrays.asList( |
Cole Faust | 042a8a6 | 2019-11-22 14:11:58 -0800 | [diff] [blame^] | 434 | MenuItem.builder(mContext).setToSearch().build(), |
Ram Parameswaran | 05fec07 | 2019-10-17 13:24:27 -0700 | [diff] [blame] | 435 | createMenuItem(i -> { |
| 436 | }))); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 437 | |
| 438 | mToolbar.setShowMenuItemsWhileSearching(true); |
| 439 | mToolbar.setState(Toolbar.State.SEARCH); |
| 440 | |
Cole Faust | db6866c | 2019-10-09 15:56:07 -0700 | [diff] [blame] | 441 | assertThat(getMenuItemView(0).isShown()).isFalse(); |
| 442 | assertThat(getMenuItemView(1).isShown()).isTrue(); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | private MenuItem createMenuItem(MenuItem.OnClickListener listener) { |
Cole Faust | 042a8a6 | 2019-11-22 14:11:58 -0800 | [diff] [blame^] | 446 | return MenuItem.builder(mContext) |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 447 | .setTitle("Button!") |
| 448 | .setOnClickListener(listener) |
| 449 | .build(); |
| 450 | } |
| 451 | |
Ram Parameswaran | 05fec07 | 2019-10-17 13:24:27 -0700 | [diff] [blame] | 452 | private void mockResources() { |
| 453 | mContext = spy(RuntimeEnvironment.application); |
| 454 | mResources = spy(mContext.getResources()); |
| 455 | when(mContext.getResources()).thenReturn(mResources); |
| 456 | } |
| 457 | |
Cole Faust | 9de2199 | 2019-11-22 10:45:25 -0800 | [diff] [blame] | 458 | private int getMenuItemCount() { |
| 459 | return mToolbar.getMenuItems().size(); |
Cole Faust | db6866c | 2019-10-09 15:56:07 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | private View getMenuItemView(int index) { |
| 463 | return ((ViewGroup) mToolbar |
| 464 | .findViewById(R.id.car_ui_toolbar_menu_items_container)) |
| 465 | .getChildAt(index); |
| 466 | } |
| 467 | |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 468 | private void pressBack() { |
Cole Faust | db6866c | 2019-10-09 15:56:07 -0700 | [diff] [blame] | 469 | mToolbar.findViewById(R.id.car_ui_toolbar_nav_icon_container).performClick(); |
Cole Faust | d4e193b | 2019-10-03 09:49:26 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | private static class Mutable<T> { |
| 473 | public T value; |
| 474 | |
| 475 | Mutable(T value) { |
| 476 | this.value = value; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | } |