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