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