This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
/
PlaybackSupportFragmentTest.java
514 lines (440 loc) · 22.6 KB
/
PlaybackSupportFragmentTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.leanback.app;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import androidx.leanback.media.PlaybackControlGlue;
import androidx.leanback.media.PlaybackGlue;
import androidx.leanback.testutils.PollingCheck;
import androidx.leanback.widget.ControlButtonPresenterSelector;
import androidx.leanback.widget.ListRow;
import androidx.leanback.widget.OnItemViewClickedListener;
import androidx.leanback.widget.OnItemViewSelectedListener;
import androidx.leanback.widget.PlaybackControlsRow;
import androidx.leanback.widget.PlaybackControlsRowPresenter;
import androidx.leanback.widget.Presenter;
import androidx.leanback.widget.Row;
import androidx.leanback.widget.RowPresenter;
import androidx.leanback.widget.SparseArrayObjectAdapter;
import androidx.test.filters.FlakyTest;
import androidx.test.filters.LargeTest;
import androidx.test.filters.MediumTest;
import androidx.test.filters.Suppress;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
@MediumTest
@RunWith(AndroidJUnit4.class)
public class PlaybackSupportFragmentTest extends SingleSupportFragmentTestBase {
private static final String TAG = "PlaybackSupportFragmentTest";
private static final long ACTIVITY_LOAD_MS = 1000;
private static final long WAIT_FOR_NO_EVENT_MS = 500;
private static final float MINIMAL_FADEOUT_TRANSLATION = 50f;
private static final float DELTA = 1f;
@Test
public void testDetachCalledWhenDestroyFragment() throws Throwable {
final SingleSupportFragmentTestActivity activity =
launchAndWaitActivity(PlaybackTestSupportFragment.class, ACTIVITY_LOAD_MS);
final PlaybackTestSupportFragment fragment = (PlaybackTestSupportFragment) activity.getTestFragment();
PlaybackGlue glue = fragment.getGlue();
activityTestRule.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.finish();
}
});
PollingCheck.waitFor(new PollingCheck.PollingCheckCondition() {
@Override
public boolean canProceed() {
return fragment.mDestroyCalled;
}
});
assertNull(glue.getHost());
}
@Test
public void testSelectedListener() throws Throwable {
SingleSupportFragmentTestActivity activity =
launchAndWaitActivity(PlaybackTestSupportFragment.class, ACTIVITY_LOAD_MS);
PlaybackTestSupportFragment fragment = (PlaybackTestSupportFragment) activity.getTestFragment();
assertTrue(fragment.getView().hasFocus());
OnItemViewSelectedListener selectedListener = Mockito.mock(
OnItemViewSelectedListener.class);
fragment.setOnItemViewSelectedListener(selectedListener);
PlaybackControlsRow controlsRow = fragment.getGlue().getControlsRow();
SparseArrayObjectAdapter primaryActionsAdapter = (SparseArrayObjectAdapter)
controlsRow.getPrimaryActionsAdapter();
PlaybackControlsRow.MultiAction playPause = (PlaybackControlsRow.MultiAction)
primaryActionsAdapter.lookup(PlaybackControlGlue.ACTION_PLAY_PAUSE);
PlaybackControlsRow.MultiAction rewind = (PlaybackControlsRow.MultiAction)
primaryActionsAdapter.lookup(PlaybackControlGlue.ACTION_REWIND);
PlaybackControlsRow.MultiAction thumbsUp = (PlaybackControlsRow.MultiAction)
primaryActionsAdapter.lookup(PlaybackControlGlue.ACTION_CUSTOM_LEFT_FIRST);
ArgumentCaptor<Presenter.ViewHolder> itemVHCaptor =
ArgumentCaptor.forClass(Presenter.ViewHolder.class);
ArgumentCaptor<Object> itemCaptor = ArgumentCaptor.forClass(Object.class);
ArgumentCaptor<RowPresenter.ViewHolder> rowVHCaptor =
ArgumentCaptor.forClass(RowPresenter.ViewHolder.class);
ArgumentCaptor<Row> rowCaptor = ArgumentCaptor.forClass(Row.class);
// First navigate left within PlaybackControlsRow items.
verify(selectedListener, times(0)).onItemSelected(any(Presenter.ViewHolder.class),
any(Object.class), any(RowPresenter.ViewHolder.class), any(Row.class));
sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
verify(selectedListener, times(1)).onItemSelected(itemVHCaptor.capture(),
itemCaptor.capture(), rowVHCaptor.capture(), rowCaptor.capture());
assertSame("Same controls row should be passed to the listener", controlsRow,
rowCaptor.getValue());
assertSame("The selected action should be rewind", rewind, itemCaptor.getValue());
sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
verify(selectedListener, times(2)).onItemSelected(itemVHCaptor.capture(),
itemCaptor.capture(), rowVHCaptor.capture(), rowCaptor.capture());
assertSame("Same controls row should be passed to the listener", controlsRow,
rowCaptor.getValue());
assertSame("The selected action should be thumbsUp", thumbsUp, itemCaptor.getValue());
// Now navigate down to a ListRow item.
ListRow listRow0 = (ListRow) fragment.getAdapter().get(1);
sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
waitForScrollIdle(fragment.getVerticalGridView());
verify(selectedListener, times(3)).onItemSelected(itemVHCaptor.capture(),
itemCaptor.capture(), rowVHCaptor.capture(), rowCaptor.capture());
assertSame("Same list row should be passed to the listener", listRow0,
rowCaptor.getValue());
// Depending on the focusSearch algorithm, one of the items in the first ListRow must be
// selected.
boolean listRowItemPassed = (itemCaptor.getValue() == listRow0.getAdapter().get(0)
|| itemCaptor.getValue() == listRow0.getAdapter().get(1));
assertTrue("None of the items in the first ListRow are passed to the selected listener.",
listRowItemPassed);
}
@Test
public void testClickedListener() throws Throwable {
SingleSupportFragmentTestActivity activity =
launchAndWaitActivity(PlaybackTestSupportFragment.class, ACTIVITY_LOAD_MS);
PlaybackTestSupportFragment fragment = (PlaybackTestSupportFragment) activity.getTestFragment();
assertTrue(fragment.getView().hasFocus());
OnItemViewClickedListener clickedListener = Mockito.mock(OnItemViewClickedListener.class);
fragment.setOnItemViewClickedListener(clickedListener);
PlaybackControlsRow controlsRow = fragment.getGlue().getControlsRow();
SparseArrayObjectAdapter primaryActionsAdapter = (SparseArrayObjectAdapter)
controlsRow.getPrimaryActionsAdapter();
PlaybackControlsRow.MultiAction playPause = (PlaybackControlsRow.MultiAction)
primaryActionsAdapter.lookup(PlaybackControlGlue.ACTION_PLAY_PAUSE);
PlaybackControlsRow.MultiAction rewind = (PlaybackControlsRow.MultiAction)
primaryActionsAdapter.lookup(PlaybackControlGlue.ACTION_REWIND);
PlaybackControlsRow.MultiAction thumbsUp = (PlaybackControlsRow.MultiAction)
primaryActionsAdapter.lookup(PlaybackControlGlue.ACTION_CUSTOM_LEFT_FIRST);
ArgumentCaptor<Presenter.ViewHolder> itemVHCaptor =
ArgumentCaptor.forClass(Presenter.ViewHolder.class);
ArgumentCaptor<Object> itemCaptor = ArgumentCaptor.forClass(Object.class);
ArgumentCaptor<RowPresenter.ViewHolder> rowVHCaptor =
ArgumentCaptor.forClass(RowPresenter.ViewHolder.class);
ArgumentCaptor<Row> rowCaptor = ArgumentCaptor.forClass(Row.class);
// First navigate left within PlaybackControlsRow items.
verify(clickedListener, times(0)).onItemClicked(any(Presenter.ViewHolder.class),
any(Object.class), any(RowPresenter.ViewHolder.class), any(Row.class));
sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
verify(clickedListener, times(1)).onItemClicked(itemVHCaptor.capture(),
itemCaptor.capture(), rowVHCaptor.capture(), rowCaptor.capture());
assertSame("Same controls row should be passed to the listener", controlsRow,
rowCaptor.getValue());
assertSame("The clicked action should be playPause", playPause, itemCaptor.getValue());
sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
verify(clickedListener, times(1)).onItemClicked(any(Presenter.ViewHolder.class),
any(Object.class), any(RowPresenter.ViewHolder.class), any(Row.class));
sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
verify(clickedListener, times(2)).onItemClicked(itemVHCaptor.capture(),
itemCaptor.capture(), rowVHCaptor.capture(), rowCaptor.capture());
assertSame("Same controls row should be passed to the listener", controlsRow,
rowCaptor.getValue());
assertSame("The clicked action should be rewind", rewind, itemCaptor.getValue());
sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
verify(clickedListener, times(2)).onItemClicked(any(Presenter.ViewHolder.class),
any(Object.class), any(RowPresenter.ViewHolder.class), any(Row.class));
sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
verify(clickedListener, times(3)).onItemClicked(itemVHCaptor.capture(),
itemCaptor.capture(), rowVHCaptor.capture(), rowCaptor.capture());
assertSame("Same controls row should be passed to the listener", controlsRow,
rowCaptor.getValue());
assertSame("The clicked action should be thumbsUp", thumbsUp, itemCaptor.getValue());
// Now navigate down to a ListRow item.
ListRow listRow0 = (ListRow) fragment.getAdapter().get(1);
sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
waitForScrollIdle(fragment.getVerticalGridView());
verify(clickedListener, times(3)).onItemClicked(any(Presenter.ViewHolder.class),
any(Object.class), any(RowPresenter.ViewHolder.class), any(Row.class));
sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
verify(clickedListener, times(4)).onItemClicked(itemVHCaptor.capture(),
itemCaptor.capture(), rowVHCaptor.capture(), rowCaptor.capture());
assertSame("Same list row should be passed to the listener", listRow0,
rowCaptor.getValue());
boolean listRowItemPassed = (itemCaptor.getValue() == listRow0.getAdapter().get(0)
|| itemCaptor.getValue() == listRow0.getAdapter().get(1));
assertTrue("None of the items in the first ListRow are passed to the click listener.",
listRowItemPassed);
}
@FlakyTest
@Suppress
@Test
public void alignmentRowToBottom() throws Throwable {
SingleSupportFragmentTestActivity activity =
launchAndWaitActivity(PlaybackTestSupportFragment.class, ACTIVITY_LOAD_MS);
final PlaybackTestSupportFragment fragment = (PlaybackTestSupportFragment) activity.getTestFragment();
assertTrue(fragment.getAdapter().size() > 2);
View playRow = fragment.getVerticalGridView().getChildAt(0);
assertTrue(playRow.hasFocus());
assertEquals(playRow.getResources().getDimensionPixelSize(
androidx.leanback.R.dimen.lb_playback_controls_padding_bottom),
fragment.getVerticalGridView().getHeight() - playRow.getBottom());
activityTestRule.runOnUiThread(new Runnable() {
@Override
public void run() {
fragment.getVerticalGridView().setSelectedPositionSmooth(
fragment.getAdapter().size() - 1);
}
});
waitForScrollIdle(fragment.getVerticalGridView());
View lastRow = fragment.getVerticalGridView().getChildAt(
fragment.getVerticalGridView().getChildCount() - 1);
assertEquals(fragment.getAdapter().size() - 1,
fragment.getVerticalGridView().getChildAdapterPosition(lastRow));
assertTrue(lastRow.hasFocus());
assertEquals(lastRow.getResources().getDimensionPixelSize(
androidx.leanback.R.dimen.lb_playback_controls_padding_bottom),
fragment.getVerticalGridView().getHeight() - lastRow.getBottom());
}
public static class PurePlaybackSupportFragment extends PlaybackSupportFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setFadingEnabled(false);
PlaybackControlsRow row = new PlaybackControlsRow();
SparseArrayObjectAdapter primaryAdapter = new SparseArrayObjectAdapter(
new ControlButtonPresenterSelector());
primaryAdapter.set(0, new PlaybackControlsRow.SkipPreviousAction(getActivity()));
primaryAdapter.set(1, new PlaybackControlsRow.PlayPauseAction(getActivity()));
primaryAdapter.set(2, new PlaybackControlsRow.SkipNextAction(getActivity()));
row.setPrimaryActionsAdapter(primaryAdapter);
row.setSecondaryActionsAdapter(null);
setPlaybackRow(row);
setPlaybackRowPresenter(new PlaybackControlsRowPresenter());
}
}
@Test
public void setupRowAndPresenterWithoutGlue() {
SingleSupportFragmentTestActivity activity =
launchAndWaitActivity(PurePlaybackSupportFragment.class, ACTIVITY_LOAD_MS);
final PurePlaybackSupportFragment fragment = (PurePlaybackSupportFragment)
activity.getTestFragment();
assertTrue(fragment.getAdapter().size() == 1);
View playRow = fragment.getVerticalGridView().getChildAt(0);
assertTrue(playRow.hasFocus());
assertEquals(playRow.getResources().getDimensionPixelSize(
androidx.leanback.R.dimen.lb_playback_controls_padding_bottom),
fragment.getVerticalGridView().getHeight() - playRow.getBottom());
}
public static class ControlGlueFragment extends PlaybackSupportFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int[] ffspeeds = new int[] {PlaybackControlGlue.PLAYBACK_SPEED_FAST_L0,
PlaybackControlGlue.PLAYBACK_SPEED_FAST_L1};
PlaybackGlue glue = new PlaybackControlGlue(
getActivity(), ffspeeds) {
@Override
public boolean hasValidMedia() {
return true;
}
@Override
public boolean isMediaPlaying() {
return false;
}
@Override
public CharSequence getMediaTitle() {
return "Title";
}
@Override
public CharSequence getMediaSubtitle() {
return "SubTitle";
}
@Override
public int getMediaDuration() {
return 100;
}
@Override
public Drawable getMediaArt() {
return null;
}
@Override
public long getSupportedActions() {
return PlaybackControlGlue.ACTION_PLAY_PAUSE;
}
@Override
public int getCurrentSpeedId() {
return PlaybackControlGlue.PLAYBACK_SPEED_PAUSED;
}
@Override
public int getCurrentPosition() {
return 50;
}
};
glue.setHost(new PlaybackSupportFragmentGlueHost(this));
}
}
@Test
public void setupWithControlGlue() throws Throwable {
SingleSupportFragmentTestActivity activity =
launchAndWaitActivity(ControlGlueFragment.class, ACTIVITY_LOAD_MS);
final ControlGlueFragment fragment = (ControlGlueFragment)
activity.getTestFragment();
assertTrue(fragment.getAdapter().size() == 1);
View playRow = fragment.getVerticalGridView().getChildAt(0);
assertTrue(playRow.hasFocus());
assertEquals(playRow.getResources().getDimensionPixelSize(
androidx.leanback.R.dimen.lb_playback_controls_padding_bottom),
fragment.getVerticalGridView().getHeight() - playRow.getBottom());
}
public static class AutohidePlaybackSupportFragment extends PlaybackSupportFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PlaybackControlsRow row = new PlaybackControlsRow();
SparseArrayObjectAdapter primaryAdapter = new SparseArrayObjectAdapter(
new ControlButtonPresenterSelector());
row.setPrimaryActionsAdapter(primaryAdapter);
row.setSecondaryActionsAdapter(null);
setPlaybackRow(row);
setPlaybackRowPresenter(new PlaybackControlsRowPresenter());
}
}
@LargeTest
@Test
public void autohideAfterPlaying() throws Throwable {
SingleSupportFragmentTestActivity activity =
launchAndWaitActivity(AutohidePlaybackSupportFragment.class, ACTIVITY_LOAD_MS);
final AutohidePlaybackSupportFragment fragment = (AutohidePlaybackSupportFragment)
activity.getTestFragment();
assertTrue(fragment.getAdapter().size() == 1);
fragment.mAutohideTimerAfterPlayingInMs = 100;
fragment.mAutohideTimerAfterTickleInMs = 0;
activityTestRule.runOnUiThread(new Runnable() {
public void run() {
fragment.setControlsOverlayAutoHideEnabled(true);
}
});
waitForSlideOut(fragment);
activityTestRule.runOnUiThread(new Runnable() {
public void run() {
fragment.setControlsOverlayAutoHideEnabled(false);
}
});
waitForSlideIn(fragment);
}
@Test
public void noAutohideWhenControlsOverlayAutoHideDisabled() throws Throwable {
SingleSupportFragmentTestActivity activity =
launchAndWaitActivity(AutohidePlaybackSupportFragment.class, ACTIVITY_LOAD_MS);
final AutohidePlaybackSupportFragment fragment = (AutohidePlaybackSupportFragment)
activity.getTestFragment();
assertEquals(1, fragment.getAdapter().size());
// by default it's enabled when fragment is resumed
assertTrue(fragment.isControlsOverlayAutoHideEnabled());
fragment.mAutohideTimerAfterPlayingInMs = 100;
activityTestRule.runOnUiThread(new Runnable() {
public void run() {
fragment.setControlsOverlayAutoHideEnabled(false);
}
});
assertNoSlideOut(fragment);
}
@Test
public void noAutohideAfterTickle() throws Throwable {
SingleSupportFragmentTestActivity activity =
launchAndWaitActivity(AutohidePlaybackSupportFragment.class, ACTIVITY_LOAD_MS);
final AutohidePlaybackSupportFragment fragment = (AutohidePlaybackSupportFragment)
activity.getTestFragment();
assertTrue(fragment.getAdapter().size() == 1);
fragment.mAutohideTimerAfterPlayingInMs = 100;
// When mAutohideTimerAfterTickleInMs is 0, no autohide after tickle
fragment.mAutohideTimerAfterTickleInMs = 0;
activityTestRule.runOnUiThread(new Runnable() {
public void run() {
fragment.tickle();
}
});
assertNoSlideOut(fragment);
}
@Test
public void autohideAfterTickle() throws Throwable {
SingleSupportFragmentTestActivity activity =
launchAndWaitActivity(AutohidePlaybackSupportFragment.class, ACTIVITY_LOAD_MS);
final AutohidePlaybackSupportFragment fragment = (AutohidePlaybackSupportFragment)
activity.getTestFragment();
assertTrue(fragment.getAdapter().size() == 1);
fragment.mAutohideTimerAfterPlayingInMs = 100;
// When mAutohideTimerAfterTickleInMs > 0, autohide after tickle
fragment.mAutohideTimerAfterTickleInMs = 100;
activityTestRule.runOnUiThread(new Runnable() {
public void run() {
fragment.tickle();
}
});
waitForSlideOut(fragment);
}
private static void waitForSlideOut(final PlaybackSupportFragment fragment) {
PollingCheck.waitFor(new PollingCheck.PollingCheckCondition() {
@Override
public boolean canProceed() {
return fragment.getVerticalGridView().findViewHolderForAdapterPosition(0)
.itemView.getTranslationY() > MINIMAL_FADEOUT_TRANSLATION;
}
});
}
private static void waitForSlideIn(final PlaybackSupportFragment fragment) {
PollingCheck.waitFor(new PollingCheck.PollingCheckCondition() {
@Override
public boolean canProceed() {
return fragment.getVerticalGridView().findViewHolderForAdapterPosition(0)
.itemView.getTranslationY() < DELTA;
}
});
}
private static void assertNoSlideOut(final PlaybackSupportFragment fragment) {
try {
PollingCheck.waitFor(WAIT_FOR_NO_EVENT_MS, new PollingCheck.PollingCheckCondition() {
@Override
public boolean canProceed() {
return fragment.getVerticalGridView().findViewHolderForAdapterPosition(0)
.itemView.getTranslationY() > DELTA;
}
});
} catch (AssertionError error) {
// if timeout, then we are good
return;
}
fail("unexpected faded out");
}
}