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