1// CHECKSTYLE:OFF Generated code 2/* This file is auto-generated from GuidedStepFragmentTest.java. DO NOT MODIFY. */ 3 4/* 5 * Copyright (C) 2016 The Android Open Source Project 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19package android.support.v17.leanback.app; 20 21import static org.junit.Assert.assertEquals; 22import static org.junit.Assert.assertFalse; 23import static org.junit.Assert.assertTrue; 24import static org.mockito.ArgumentMatchers.nullable; 25import static org.mockito.Mockito.any; 26import static org.mockito.Mockito.doAnswer; 27import static org.mockito.Mockito.timeout; 28import static org.mockito.Mockito.times; 29import static org.mockito.Mockito.verify; 30 31import android.os.Bundle; 32import android.support.test.filters.LargeTest; 33import android.support.test.runner.AndroidJUnit4; 34import android.support.v17.leanback.testutils.PollingCheck; 35import android.support.v17.leanback.widget.GuidedAction; 36import android.support.v17.leanback.widget.GuidedActionsStylist; 37import android.view.KeyEvent; 38import android.view.LayoutInflater; 39import android.view.View; 40import android.view.ViewGroup; 41 42import org.junit.Test; 43import org.junit.runner.RunWith; 44import org.mockito.ArgumentCaptor; 45import org.mockito.invocation.InvocationOnMock; 46import org.mockito.stubbing.Answer; 47 48import java.util.ArrayList; 49import java.util.List; 50 51@LargeTest 52@RunWith(AndroidJUnit4.class) 53public class GuidedStepSupportFragmentTest extends GuidedStepSupportFragmentTestBase { 54 55 private static final int ON_DESTROY_TIMEOUT = 5000; 56 57 @Test 58 public void nextAndBack() throws Throwable { 59 final String firstFragmentName = generateMethodTestName("first"); 60 final String secondFragmentName = generateMethodTestName("second"); 61 GuidedStepTestSupportFragment.Provider first = mockProvider(firstFragmentName); 62 doAnswer(new Answer<Void>() { 63 @Override 64 public Void answer(InvocationOnMock invocation) { 65 List actions = (List) invocation.getArguments()[0]; 66 actions.add(new GuidedAction.Builder().id(1000).title("OK").build()); 67 return null; 68 } 69 }).when(first).onCreateActions(any(List.class), nullable(Bundle.class)); 70 doAnswer(new Answer<Void>() { 71 @Override 72 public Void answer(InvocationOnMock invocation) { 73 GuidedAction action = (GuidedAction) invocation.getArguments()[0]; 74 GuidedStepTestSupportFragment.Provider obj = (GuidedStepTestSupportFragment.Provider) 75 invocation.getMock(); 76 if (action.getId() == 1000) { 77 GuidedStepSupportFragment.add(obj.getFragmentManager(), 78 new GuidedStepTestSupportFragment(secondFragmentName)); 79 } 80 return null; 81 } 82 }).when(first).onGuidedActionClicked(any(GuidedAction.class)); 83 84 GuidedStepTestSupportFragment.Provider second = mockProvider(secondFragmentName); 85 86 GuidedStepSupportFragmentTestActivity activity = launchTestActivity(firstFragmentName); 87 verify(first, times(1)).onCreate(nullable(Bundle.class)); 88 verify(first, times(1)).onCreateGuidance(nullable(Bundle.class)); 89 verify(first, times(1)).onCreateActions(any(List.class), nullable(Bundle.class)); 90 verify(first, times(1)).onCreateButtonActions(any(List.class), nullable(Bundle.class)); 91 verify(first, times(1)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class), 92 nullable(Bundle.class), any(View.class)); 93 verify(first, times(1)).onViewStateRestored(nullable(Bundle.class)); 94 verify(first, times(1)).onStart(); 95 verify(first, times(1)).onResume(); 96 97 sendKey(KeyEvent.KEYCODE_DPAD_CENTER); 98 verify(first, times(1)).onGuidedActionClicked(any(GuidedAction.class)); 99 100 PollingCheck.waitFor(new EnterTransitionFinish(second)); 101 verify(first, times(1)).onPause(); 102 verify(first, times(1)).onStop(); 103 verify(first, times(1)).onDestroyView(); 104 verify(second, times(1)).onCreate(nullable(Bundle.class)); 105 verify(second, times(1)).onCreateGuidance(nullable(Bundle.class)); 106 verify(second, times(1)).onCreateActions(any(List.class), nullable(Bundle.class)); 107 verify(second, times(1)).onCreateButtonActions(any(List.class), nullable(Bundle.class)); 108 verify(second, times(1)).onCreateView(any(LayoutInflater.class), nullable(ViewGroup.class), 109 nullable(Bundle.class), any(View.class)); 110 verify(second, times(1)).onViewStateRestored(nullable(Bundle.class)); 111 verify(second, times(1)).onStart(); 112 verify(second, times(1)).onResume(); 113 114 sendKey(KeyEvent.KEYCODE_BACK); 115 116 PollingCheck.waitFor(new EnterTransitionFinish(first)); 117 verify(second, times(1)).onPause(); 118 verify(second, times(1)).onStop(); 119 verify(second, times(1)).onDestroyView(); 120 verify(second, times(1)).onDestroy(); 121 verify(first, times(1)).onCreateActions(any(List.class), nullable(Bundle.class)); 122 verify(first, times(2)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class), 123 nullable(Bundle.class), any(View.class)); 124 verify(first, times(2)).onViewStateRestored(nullable(Bundle.class)); 125 verify(first, times(2)).onStart(); 126 verify(first, times(2)).onResume(); 127 128 sendKey(KeyEvent.KEYCODE_BACK); 129 PollingCheck.waitFor(new PollingCheck.ActivityDestroy(activity)); 130 verify(first, timeout(ON_DESTROY_TIMEOUT).times(1)).onDestroy(); 131 assertTrue(activity.isDestroyed()); 132 } 133 134 @Test 135 public void restoreFragments() throws Throwable { 136 final String firstFragmentName = generateMethodTestName("first"); 137 final String secondFragmentName = generateMethodTestName("second"); 138 GuidedStepTestSupportFragment.Provider first = mockProvider(firstFragmentName); 139 doAnswer(new Answer<Void>() { 140 @Override 141 public Void answer(InvocationOnMock invocation) { 142 List actions = (List) invocation.getArguments()[0]; 143 actions.add(new GuidedAction.Builder().id(1000).title("OK").build()); 144 actions.add(new GuidedAction.Builder().id(1001).editable(true).title("text") 145 .build()); 146 actions.add(new GuidedAction.Builder().id(1002).editable(true).title("text") 147 .autoSaveRestoreEnabled(false).build()); 148 return null; 149 } 150 }).when(first).onCreateActions(any(List.class), nullable(Bundle.class)); 151 doAnswer(new Answer<Void>() { 152 @Override 153 public Void answer(InvocationOnMock invocation) { 154 GuidedAction action = (GuidedAction) invocation.getArguments()[0]; 155 GuidedStepTestSupportFragment.Provider obj = (GuidedStepTestSupportFragment.Provider) 156 invocation.getMock(); 157 if (action.getId() == 1000) { 158 GuidedStepSupportFragment.add(obj.getFragmentManager(), 159 new GuidedStepTestSupportFragment(secondFragmentName)); 160 } 161 return null; 162 } 163 }).when(first).onGuidedActionClicked(any(GuidedAction.class)); 164 165 GuidedStepTestSupportFragment.Provider second = mockProvider(secondFragmentName); 166 167 final GuidedStepSupportFragmentTestActivity activity = launchTestActivity(firstFragmentName); 168 first.getFragment().findActionById(1001).setTitle("modified text"); 169 first.getFragment().findActionById(1002).setTitle("modified text"); 170 sendKey(KeyEvent.KEYCODE_DPAD_CENTER); 171 PollingCheck.waitFor(new EnterTransitionFinish(second)); 172 173 activityTestRule.runOnUiThread(new Runnable() { 174 @Override 175 public void run() { 176 activity.recreate(); 177 } 178 }); 179 PollingCheck.waitFor(new EnterTransitionFinish(second)); 180 verify(first, times(1)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class), 181 nullable(Bundle.class), any(View.class)); 182 verify(first, times(1)).onDestroy(); 183 verify(second, times(2)).onCreate(nullable(Bundle.class)); 184 verify(second, times(2)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class), 185 nullable(Bundle.class), any(View.class)); 186 verify(second, times(1)).onDestroy(); 187 188 sendKey(KeyEvent.KEYCODE_BACK); 189 PollingCheck.waitFor(new EnterTransitionFinish(first)); 190 verify(second, times(2)).onPause(); 191 verify(second, times(2)).onStop(); 192 verify(second, times(2)).onDestroyView(); 193 verify(second, times(2)).onDestroy(); 194 assertEquals("modified text", first.getFragment().findActionById(1001).getTitle()); 195 assertEquals("text", first.getFragment().findActionById(1002).getTitle()); 196 verify(first, times(2)).onCreate(nullable(Bundle.class)); 197 verify(first, times(2)).onCreateActions(any(List.class), nullable(Bundle.class)); 198 verify(first, times(2)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class), 199 nullable(Bundle.class), any(View.class)); 200 } 201 202 203 @Test 204 public void finishGuidedStepSupportFragment_finishes_activity() throws Throwable { 205 final String firstFragmentName = generateMethodTestName("first"); 206 GuidedStepTestSupportFragment.Provider first = mockProvider(firstFragmentName); 207 doAnswer(new Answer<Void>() { 208 @Override 209 public Void answer(InvocationOnMock invocation) { 210 List actions = (List) invocation.getArguments()[0]; 211 actions.add(new GuidedAction.Builder().id(1001).title("Finish activity").build()); 212 return null; 213 } 214 }).when(first).onCreateActions(any(List.class), nullable(Bundle.class)); 215 doAnswer(new Answer<Void>() { 216 @Override 217 public Void answer(InvocationOnMock invocation) { 218 GuidedAction action = (GuidedAction) invocation.getArguments()[0]; 219 GuidedStepTestSupportFragment.Provider obj = (GuidedStepTestSupportFragment.Provider) 220 invocation.getMock(); 221 if (action.getId() == 1001) { 222 obj.getFragment().finishGuidedStepSupportFragments(); 223 } 224 return null; 225 } 226 }).when(first).onGuidedActionClicked(any(GuidedAction.class)); 227 228 final GuidedStepSupportFragmentTestActivity activity = launchTestActivity(firstFragmentName); 229 230 View viewFinish = first.getFragment().getActionItemView(0); 231 assertTrue(viewFinish.hasFocus()); 232 sendKey(KeyEvent.KEYCODE_DPAD_CENTER); 233 PollingCheck.waitFor(new PollingCheck.ActivityDestroy(activity)); 234 verify(first, timeout(ON_DESTROY_TIMEOUT).times(1)).onDestroy(); 235 } 236 237 @Test 238 public void finishGuidedStepSupportFragment_finishes_fragments() throws Throwable { 239 final String firstFragmentName = generateMethodTestName("first"); 240 GuidedStepTestSupportFragment.Provider first = mockProvider(firstFragmentName); 241 doAnswer(new Answer<Void>() { 242 @Override 243 public Void answer(InvocationOnMock invocation) { 244 List actions = (List) invocation.getArguments()[0]; 245 actions.add(new GuidedAction.Builder().id(1001).title("Finish fragments").build()); 246 return null; 247 } 248 }).when(first).onCreateActions(any(List.class), nullable(Bundle.class)); 249 doAnswer(new Answer<Void>() { 250 @Override 251 public Void answer(InvocationOnMock invocation) { 252 GuidedAction action = (GuidedAction) invocation.getArguments()[0]; 253 GuidedStepTestSupportFragment.Provider obj = (GuidedStepTestSupportFragment.Provider) 254 invocation.getMock(); 255 if (action.getId() == 1001) { 256 obj.getFragment().finishGuidedStepSupportFragments(); 257 } 258 return null; 259 } 260 }).when(first).onGuidedActionClicked(any(GuidedAction.class)); 261 262 final GuidedStepSupportFragmentTestActivity activity = launchTestActivity(firstFragmentName, 263 false /*asRoot*/); 264 265 View viewFinish = first.getFragment().getActionItemView(0); 266 assertTrue(viewFinish.hasFocus()); 267 sendKey(KeyEvent.KEYCODE_DPAD_CENTER); 268 269 // fragment should be destroyed, activity should not destroyed 270 waitOnDestroy(first, 1); 271 assertFalse(activity.isDestroyed()); 272 } 273 274 @Test 275 public void subActions() throws Throwable { 276 final String firstFragmentName = generateMethodTestName("first"); 277 final String secondFragmentName = generateMethodTestName("second"); 278 final boolean[] expandSubActionInOnCreateView = new boolean[] {false}; 279 GuidedStepTestSupportFragment.Provider first = mockProvider(firstFragmentName); 280 doAnswer(new Answer<Void>() { 281 @Override 282 public Void answer(InvocationOnMock invocation) { 283 GuidedStepTestSupportFragment.Provider obj = (GuidedStepTestSupportFragment.Provider) 284 invocation.getMock(); 285 if (expandSubActionInOnCreateView[0]) { 286 obj.getFragment().expandAction(obj.getFragment().findActionById(1000), false); 287 } 288 return null; 289 } 290 }).when(first).onCreateView(any(LayoutInflater.class), any(ViewGroup.class), 291 nullable(Bundle.class), any(View.class)); 292 doAnswer(new Answer<Void>() { 293 @Override 294 public Void answer(InvocationOnMock invocation) { 295 List actions = (List) invocation.getArguments()[0]; 296 List<GuidedAction> subActions = new ArrayList<GuidedAction>(); 297 subActions.add(new GuidedAction.Builder().id(2000).title("item1").build()); 298 subActions.add(new GuidedAction.Builder().id(2001).title("item2").build()); 299 actions.add(new GuidedAction.Builder().id(1000).subActions(subActions) 300 .title("list").build()); 301 return null; 302 } 303 }).when(first).onCreateActions(any(List.class), nullable(Bundle.class)); 304 doAnswer(new Answer<Boolean>() { 305 @Override 306 public Boolean answer(InvocationOnMock invocation) { 307 GuidedStepTestSupportFragment.Provider obj = (GuidedStepTestSupportFragment.Provider) 308 invocation.getMock(); 309 GuidedAction action = (GuidedAction) invocation.getArguments()[0]; 310 if (action.getId() == 2000) { 311 return true; 312 } else if (action.getId() == 2001) { 313 GuidedStepSupportFragment.add(obj.getFragmentManager(), 314 new GuidedStepTestSupportFragment(secondFragmentName)); 315 return false; 316 } 317 return false; 318 } 319 }).when(first).onSubGuidedActionClicked(any(GuidedAction.class)); 320 321 GuidedStepTestSupportFragment.Provider second = mockProvider(secondFragmentName); 322 323 final GuidedStepSupportFragmentTestActivity activity = launchTestActivity(firstFragmentName); 324 325 // after clicked, it sub actions list should expand 326 View viewForList = first.getFragment().getActionItemView(0); 327 assertTrue(viewForList.hasFocus()); 328 sendKey(KeyEvent.KEYCODE_DPAD_CENTER); 329 PollingCheck.waitFor(new ExpandTransitionFinish(first)); 330 assertFalse(viewForList.hasFocus()); 331 332 sendKey(KeyEvent.KEYCODE_DPAD_CENTER); 333 ArgumentCaptor<GuidedAction> actionCapture = ArgumentCaptor.forClass(GuidedAction.class); 334 verify(first, times(1)).onSubGuidedActionClicked(actionCapture.capture()); 335 assertEquals(2000, actionCapture.getValue().getId()); 336 // after clicked a sub action, it sub actions list should close 337 PollingCheck.waitFor(new ExpandTransitionFinish(first)); 338 assertTrue(viewForList.hasFocus()); 339 340 sendKey(KeyEvent.KEYCODE_DPAD_CENTER); 341 PollingCheck.waitFor(new ExpandTransitionFinish(first)); 342 343 assertFalse(viewForList.hasFocus()); 344 sendKey(KeyEvent.KEYCODE_DPAD_DOWN); 345 sendKey(KeyEvent.KEYCODE_DPAD_CENTER); 346 ArgumentCaptor<GuidedAction> actionCapture2 = ArgumentCaptor.forClass(GuidedAction.class); 347 verify(first, times(2)).onSubGuidedActionClicked(actionCapture2.capture()); 348 assertEquals(2001, actionCapture2.getValue().getId()); 349 350 PollingCheck.waitFor(new EnterTransitionFinish(second)); 351 verify(second, times(1)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class), 352 nullable(Bundle.class), any(View.class)); 353 354 // test expand sub action when return to first fragment 355 expandSubActionInOnCreateView[0] = true; 356 sendKey(KeyEvent.KEYCODE_BACK); 357 PollingCheck.waitFor(new EnterTransitionFinish(first)); 358 verify(first, times(2)).onCreateView(any(LayoutInflater.class), any(ViewGroup.class), 359 nullable(Bundle.class), any(View.class)); 360 assertTrue(first.getFragment().isExpanded()); 361 362 sendKey(KeyEvent.KEYCODE_BACK); 363 PollingCheck.waitFor(new ExpandTransitionFinish(first)); 364 assertFalse(first.getFragment().isExpanded()); 365 366 sendKey(KeyEvent.KEYCODE_BACK); 367 PollingCheck.waitFor(new PollingCheck.ActivityDestroy(activity)); 368 verify(first, timeout(ON_DESTROY_TIMEOUT).times(1)).onDestroy(); 369 } 370 371 @Test 372 public void setActionsWhenSubActionsExpanded() throws Throwable { 373 final String firstFragmentName = generateMethodTestName("first"); 374 GuidedStepTestSupportFragment.Provider first = mockProvider(firstFragmentName); 375 doAnswer(new Answer<Void>() { 376 @Override 377 public Void answer(InvocationOnMock invocation) { 378 List actions = (List) invocation.getArguments()[0]; 379 List<GuidedAction> subActions = new ArrayList<GuidedAction>(); 380 subActions.add(new GuidedAction.Builder().id(2000).title("item1").build()); 381 actions.add(new GuidedAction.Builder().id(1000).subActions(subActions) 382 .title("list").build()); 383 return null; 384 } 385 }).when(first).onCreateActions(any(List.class), nullable(Bundle.class)); 386 doAnswer(new Answer<Boolean>() { 387 @Override 388 public Boolean answer(InvocationOnMock invocation) { 389 GuidedStepTestSupportFragment.Provider obj = (GuidedStepTestSupportFragment.Provider) 390 invocation.getMock(); 391 GuidedAction action = (GuidedAction) invocation.getArguments()[0]; 392 if (action.getId() == 2000) { 393 List<GuidedAction> newActions = new ArrayList<GuidedAction>(); 394 newActions.add(new GuidedAction.Builder().id(1001).title("item2").build()); 395 obj.getFragment().setActions(newActions); 396 return false; 397 } 398 return false; 399 } 400 }).when(first).onSubGuidedActionClicked(any(GuidedAction.class)); 401 402 final GuidedStepSupportFragmentTestActivity activity = launchTestActivity(firstFragmentName); 403 404 // after clicked, it sub actions list should expand 405 View firstView = first.getFragment().getActionItemView(0); 406 assertTrue(firstView.hasFocus()); 407 sendKey(KeyEvent.KEYCODE_DPAD_CENTER); 408 PollingCheck.waitFor(new ExpandTransitionFinish(first)); 409 assertFalse(firstView.hasFocus()); 410 411 sendKey(KeyEvent.KEYCODE_DPAD_CENTER); 412 ArgumentCaptor<GuidedAction> actionCapture = ArgumentCaptor.forClass(GuidedAction.class); 413 verify(first, times(1)).onSubGuidedActionClicked(actionCapture.capture()); 414 // after clicked a sub action, whole action list is replaced. 415 PollingCheck.waitFor(new ExpandTransitionFinish(first)); 416 assertFalse(first.getFragment().isExpanded()); 417 View newFirstView = first.getFragment().getActionItemView(0); 418 assertTrue(newFirstView.hasFocus()); 419 assertTrue(newFirstView.getVisibility() == View.VISIBLE); 420 GuidedActionsStylist.ViewHolder vh = (GuidedActionsStylist.ViewHolder) first.getFragment() 421 .getGuidedActionsStylist().getActionsGridView().getChildViewHolder(newFirstView); 422 assertEquals(1001, vh.getAction().getId()); 423 424 } 425 426 @Test 427 public void buttonActionsRtl() throws Throwable { 428 final String firstFragmentName = generateMethodTestName("first"); 429 GuidedStepTestSupportFragment.Provider first = mockProvider(firstFragmentName); 430 doAnswer(new Answer<Void>() { 431 @Override 432 public Void answer(InvocationOnMock invocation) { 433 List actions = (List) invocation.getArguments()[0]; 434 actions.add(new GuidedAction.Builder().id(1000).title("action").build()); 435 return null; 436 } 437 }).when(first).onCreateActions(any(List.class), nullable(Bundle.class)); 438 doAnswer(new Answer<Void>() { 439 @Override 440 public Void answer(InvocationOnMock invocation) { 441 List actions = (List) invocation.getArguments()[0]; 442 actions.add(new GuidedAction.Builder().id(1001).title("button action").build()); 443 return null; 444 } 445 }).when(first).onCreateButtonActions(any(List.class), nullable(Bundle.class)); 446 447 final GuidedStepSupportFragmentTestActivity activity = launchTestActivity(firstFragmentName, 448 true, View.LAYOUT_DIRECTION_RTL); 449 450 assertEquals(View.LAYOUT_DIRECTION_RTL, first.getFragment().getView().getLayoutDirection()); 451 View firstView = first.getFragment().getActionItemView(0); 452 assertTrue(firstView.hasFocus()); 453 } 454} 455