Home | History | Annotate | Download | only in app
      1 // CHECKSTYLE:OFF Generated code
      2 /* This file is auto-generated from PlaybackControlGlueTest.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 
     20 package android.support.v17.leanback.app;
     21 
     22 import static org.junit.Assert.assertEquals;
     23 import static org.junit.Assert.assertFalse;
     24 import static org.junit.Assert.assertTrue;
     25 import static org.mockito.Mockito.times;
     26 import static org.mockito.Mockito.verify;
     27 
     28 import android.content.Context;
     29 import android.graphics.drawable.Drawable;
     30 import android.support.test.InstrumentationRegistry;
     31 import android.support.test.filters.MediumTest;
     32 import android.support.test.runner.AndroidJUnit4;
     33 import android.support.v17.leanback.widget.OnItemViewClickedListener;
     34 import android.support.v17.leanback.widget.PlaybackControlsRow;
     35 import android.support.v17.leanback.widget.PlaybackRowPresenter;
     36 import android.support.v17.leanback.widget.Presenter;
     37 import android.support.v17.leanback.widget.Row;
     38 import android.support.v17.leanback.widget.RowPresenter;
     39 import android.support.v17.leanback.widget.SparseArrayObjectAdapter;
     40 import android.view.KeyEvent;
     41 import android.view.View;
     42 
     43 import org.junit.Assert;
     44 import org.junit.Before;
     45 import org.junit.Test;
     46 import org.junit.runner.RunWith;
     47 import org.mockito.Mockito;
     48 
     49 @RunWith(AndroidJUnit4.class)
     50 @MediumTest
     51 public class PlaybackControlSupportGlueTest {
     52 
     53 
     54     static class PlayControlGlueImpl extends PlaybackControlSupportGlue {
     55         int mSpeedId = PLAYBACK_SPEED_PAUSED;
     56         // number of times onRowChanged callback is called
     57         int mOnRowChangedCallCount = 0;
     58 
     59         PlayControlGlueImpl(Context context, int[] seekSpeeds) {
     60             super(context, seekSpeeds);
     61         }
     62 
     63         PlayControlGlueImpl(Context context, int[] ffSpeeds, int[] rwSpeeds) {
     64             super(context, ffSpeeds, rwSpeeds);
     65         }
     66 
     67         PlayControlGlueImpl(Context context, PlaybackOverlaySupportFragment fragment,
     68                                    int[] seekSpeeds) {
     69             super(context, fragment, seekSpeeds);
     70         }
     71 
     72         @Override
     73         public boolean hasValidMedia() {
     74             return true;
     75         }
     76 
     77         @Override
     78         public boolean isMediaPlaying() {
     79             return mSpeedId == PLAYBACK_SPEED_NORMAL;
     80         }
     81 
     82         @Override
     83         public CharSequence getMediaTitle() {
     84             return "DUMP TITLE";
     85         }
     86 
     87         @Override
     88         public CharSequence getMediaSubtitle() {
     89             return "DUMP SUBTITLE";
     90         }
     91 
     92         @Override
     93         public int getMediaDuration() {
     94             return 50000;
     95         }
     96 
     97         @Override
     98         public Drawable getMediaArt() {
     99             return null;
    100         }
    101 
    102         @Override
    103         public long getSupportedActions() {
    104             return ACTION_REWIND | ACTION_FAST_FORWARD | ACTION_PLAY_PAUSE;
    105         }
    106 
    107         @Override
    108         public int getCurrentSpeedId() {
    109             return mSpeedId;
    110         }
    111 
    112         @Override
    113         public int getCurrentPosition() {
    114             return 5000;
    115         }
    116 
    117         @Override
    118         protected void startPlayback(int speed) {
    119             mSpeedId = speed;
    120         }
    121 
    122         @Override
    123         protected void pausePlayback() {
    124             mSpeedId = PLAYBACK_SPEED_PAUSED;
    125         }
    126 
    127         @Override
    128         protected void skipToNext() {
    129         }
    130 
    131         @Override
    132         protected void skipToPrevious() {
    133         }
    134 
    135         @Override
    136         protected void onRowChanged(PlaybackControlsRow row) {
    137             mOnRowChangedCallCount++;
    138         }
    139 
    140         public void notifyMetaDataChanged() {
    141             onMetadataChanged();
    142             onStateChanged();
    143         }
    144 
    145         public int getOnRowChangedCallCount() {
    146             return mOnRowChangedCallCount;
    147         }
    148     }
    149 
    150     Context context;
    151     PlaybackControlSupportGlue glue;
    152 
    153     @Before
    154     public void setUp() {
    155         context = InstrumentationRegistry.getInstrumentation().getTargetContext();
    156         try {
    157             InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
    158                 @Override
    159                 public void run() {
    160                     glue = new PlayControlGlueImpl(context, new int[]{
    161                             PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L0,
    162                             PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L1,
    163                             PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L2
    164                     });
    165                 }
    166             });
    167         } catch (Throwable throwable) {
    168             Assert.fail(throwable.getMessage());
    169         }
    170     }
    171 
    172     @Test
    173     public void testFastForwardToMaxThenReset() {
    174         PlaybackControlsRow row = new PlaybackControlsRow();
    175         glue.setControlsRow(row);
    176         SparseArrayObjectAdapter adapter = (SparseArrayObjectAdapter)
    177                 row.getPrimaryActionsAdapter();
    178         PlaybackControlsRow.MultiAction playPause = (PlaybackControlsRow.MultiAction) adapter
    179                 .lookup(PlaybackControlSupportGlue.ACTION_PLAY_PAUSE);
    180         PlaybackControlsRow.MultiAction fastForward = (PlaybackControlsRow.MultiAction) adapter
    181                 .lookup(PlaybackControlSupportGlue.ACTION_FAST_FORWARD);
    182         PlaybackControlsRow.MultiAction rewind = (PlaybackControlsRow.MultiAction) adapter
    183                 .lookup(PlaybackControlSupportGlue.ACTION_REWIND);
    184 
    185         assertFalse(glue.isMediaPlaying());
    186         glue.onActionClicked(playPause);
    187         assertTrue(glue.isMediaPlaying());
    188         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    189         assertEquals(0, fastForward.getIndex());
    190         assertEquals(0, rewind.getIndex());
    191 
    192         // click multiple times to reach PLAYBACK_SPEED_FAST_L2
    193         glue.onActionClicked(fastForward);
    194         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L0, glue.getCurrentSpeedId());
    195         assertEquals(1, fastForward.getIndex());
    196         assertEquals(0, rewind.getIndex());
    197         glue.onActionClicked(fastForward);
    198         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L1, glue.getCurrentSpeedId());
    199         assertEquals(2, fastForward.getIndex());
    200         assertEquals(0, rewind.getIndex());
    201         glue.onActionClicked(fastForward);
    202         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L2, glue.getCurrentSpeedId());
    203         assertEquals(3, fastForward.getIndex());
    204         assertEquals(0, rewind.getIndex());
    205         glue.onActionClicked(fastForward);
    206         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L2, glue.getCurrentSpeedId());
    207         assertEquals(3, fastForward.getIndex());
    208         assertEquals(0, rewind.getIndex());
    209 
    210         // press playPause again put it back to play
    211         glue.onActionClicked(playPause);
    212         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    213         assertEquals(0, fastForward.getIndex());
    214         assertEquals(0, rewind.getIndex());
    215     }
    216 
    217     @Test
    218     public void testFastRewindToMaxThenReset() {
    219         PlaybackControlsRow row = new PlaybackControlsRow();
    220         glue.setControlsRow(row);
    221         SparseArrayObjectAdapter adapter = (SparseArrayObjectAdapter)
    222                 row.getPrimaryActionsAdapter();
    223         PlaybackControlsRow.MultiAction playPause = (PlaybackControlsRow.MultiAction) adapter
    224                 .lookup(PlaybackControlSupportGlue.ACTION_PLAY_PAUSE);
    225         PlaybackControlsRow.MultiAction fastForward = (PlaybackControlsRow.MultiAction) adapter
    226                 .lookup(PlaybackControlSupportGlue.ACTION_FAST_FORWARD);
    227         PlaybackControlsRow.MultiAction rewind = (PlaybackControlsRow.MultiAction) adapter
    228                 .lookup(PlaybackControlSupportGlue.ACTION_REWIND);
    229 
    230         assertFalse(glue.isMediaPlaying());
    231         glue.onActionClicked(playPause);
    232         assertTrue(glue.isMediaPlaying());
    233         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    234         assertEquals(0, fastForward.getIndex());
    235         assertEquals(0, rewind.getIndex());
    236 
    237         // click multiple times to reach PLAYBACK_SPEED_FAST_L2
    238         glue.onActionClicked(rewind);
    239         assertEquals(-PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L0, glue.getCurrentSpeedId());
    240         assertEquals(0, fastForward.getIndex());
    241         assertEquals(1, rewind.getIndex());
    242         glue.onActionClicked(rewind);
    243         assertEquals(-PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L1, glue.getCurrentSpeedId());
    244         assertEquals(0, fastForward.getIndex());
    245         assertEquals(2, rewind.getIndex());
    246         glue.onActionClicked(rewind);
    247         assertEquals(-PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L2, glue.getCurrentSpeedId());
    248         assertEquals(0, fastForward.getIndex());
    249         assertEquals(3, rewind.getIndex());
    250         glue.onActionClicked(rewind);
    251         assertEquals(-PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L2, glue.getCurrentSpeedId());
    252         assertEquals(0, fastForward.getIndex());
    253         assertEquals(3, rewind.getIndex());
    254 
    255         // press playPause again put it back to play
    256         glue.onActionClicked(playPause);
    257         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    258         assertEquals(0, fastForward.getIndex());
    259         assertEquals(0, rewind.getIndex());
    260     }
    261 
    262     @Test
    263     public void testFastForwardAbortKeyCodes() {
    264         PlaybackControlsRow row = new PlaybackControlsRow();
    265         glue.setControlsRow(row);
    266         SparseArrayObjectAdapter adapter = (SparseArrayObjectAdapter)
    267                 row.getPrimaryActionsAdapter();
    268         PlaybackControlsRow.MultiAction playPause = (PlaybackControlsRow.MultiAction) adapter
    269                 .lookup(PlaybackControlSupportGlue.ACTION_PLAY_PAUSE);
    270         PlaybackControlsRow.MultiAction fastForward = (PlaybackControlsRow.MultiAction) adapter
    271                 .lookup(PlaybackControlSupportGlue.ACTION_FAST_FORWARD);
    272         PlaybackControlsRow.MultiAction rewind = (PlaybackControlsRow.MultiAction) adapter
    273                 .lookup(PlaybackControlSupportGlue.ACTION_REWIND);
    274 
    275         glue.onActionClicked(playPause);
    276         assertTrue(glue.isMediaPlaying());
    277         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    278         assertEquals(0, fastForward.getIndex());
    279         assertEquals(0, rewind.getIndex());
    280 
    281         // Testing keycodes that will not abort seek
    282         final int[] noAbortSeekKeyCodes = new int[] {
    283                 KeyEvent.KEYCODE_DPAD_CENTER,
    284                 KeyEvent.KEYCODE_ENTER
    285         };
    286         for (int i = 0; i < noAbortSeekKeyCodes.length; i++) {
    287             glue.onActionClicked(fastForward);
    288             assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L0, glue.getCurrentSpeedId());
    289             assertEquals(1, fastForward.getIndex());
    290             assertEquals(0, rewind.getIndex());
    291             KeyEvent kv = new KeyEvent(KeyEvent.ACTION_DOWN, noAbortSeekKeyCodes[i]);
    292             glue.onKey(null, noAbortSeekKeyCodes[i], kv);
    293             assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L0, glue.getCurrentSpeedId());
    294             glue.onActionClicked(playPause);
    295             assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    296         }
    297 
    298         // Testing abortSeekKeyCodes
    299         final int[] abortSeekKeyCodes = new int[] {
    300             KeyEvent.KEYCODE_DPAD_UP,
    301             KeyEvent.KEYCODE_DPAD_DOWN,
    302             KeyEvent.KEYCODE_DPAD_RIGHT,
    303             KeyEvent.KEYCODE_DPAD_LEFT,
    304             KeyEvent.KEYCODE_BACK,
    305             KeyEvent.KEYCODE_ESCAPE
    306         };
    307         for (int i = 0; i < abortSeekKeyCodes.length; i++) {
    308             glue.onActionClicked(fastForward);
    309             assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L0, glue.getCurrentSpeedId());
    310             assertEquals(1, fastForward.getIndex());
    311             assertEquals(0, rewind.getIndex());
    312             KeyEvent kv = new KeyEvent(KeyEvent.ACTION_DOWN, abortSeekKeyCodes[i]);
    313             glue.onKey(null, abortSeekKeyCodes[i], kv);
    314             assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    315             assertEquals(0, fastForward.getIndex());
    316             assertEquals(0, rewind.getIndex());
    317         }
    318     }
    319 
    320     @Test
    321     public void testRewindAbortKeyCodes() {
    322         PlaybackControlsRow row = new PlaybackControlsRow();
    323         glue.setControlsRow(row);
    324         SparseArrayObjectAdapter adapter = (SparseArrayObjectAdapter)
    325                 row.getPrimaryActionsAdapter();
    326         PlaybackControlsRow.MultiAction playPause = (PlaybackControlsRow.MultiAction) adapter
    327                 .lookup(PlaybackControlSupportGlue.ACTION_PLAY_PAUSE);
    328         PlaybackControlsRow.MultiAction fastForward = (PlaybackControlsRow.MultiAction) adapter
    329                 .lookup(PlaybackControlSupportGlue.ACTION_FAST_FORWARD);
    330         PlaybackControlsRow.MultiAction rewind = (PlaybackControlsRow.MultiAction) adapter
    331                 .lookup(PlaybackControlSupportGlue.ACTION_REWIND);
    332 
    333         glue.onActionClicked(playPause);
    334         assertTrue(glue.isMediaPlaying());
    335         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    336         assertEquals(0, fastForward.getIndex());
    337         assertEquals(0, rewind.getIndex());
    338 
    339         // Testing keycodes that will not abort seek
    340         final int[] noAbortSeekKeyCodes = new int[] {
    341                 KeyEvent.KEYCODE_DPAD_CENTER,
    342                 KeyEvent.KEYCODE_ENTER
    343         };
    344         for (int i = 0; i < noAbortSeekKeyCodes.length; i++) {
    345             glue.onActionClicked(rewind);
    346             assertEquals(-PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L0, glue.getCurrentSpeedId());
    347             assertEquals(0, fastForward.getIndex());
    348             assertEquals(1, rewind.getIndex());
    349             KeyEvent kv = new KeyEvent(KeyEvent.ACTION_DOWN, noAbortSeekKeyCodes[i]);
    350             glue.onKey(null, noAbortSeekKeyCodes[i], kv);
    351             assertEquals(-PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L0, glue.getCurrentSpeedId());
    352             glue.onActionClicked(playPause);
    353             assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    354         }
    355 
    356         // Testing abortSeekKeyCodes
    357         final int[] abortSeekKeyCodes = new int[] {
    358                 KeyEvent.KEYCODE_DPAD_UP,
    359                 KeyEvent.KEYCODE_DPAD_DOWN,
    360                 KeyEvent.KEYCODE_DPAD_RIGHT,
    361                 KeyEvent.KEYCODE_DPAD_LEFT,
    362                 KeyEvent.KEYCODE_BACK,
    363                 KeyEvent.KEYCODE_ESCAPE
    364         };
    365         for (int i = 0; i < abortSeekKeyCodes.length; i++) {
    366             glue.onActionClicked(rewind);
    367             assertEquals(-PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L0, glue.getCurrentSpeedId());
    368             assertEquals(0, fastForward.getIndex());
    369             assertEquals(1, rewind.getIndex());
    370             KeyEvent kv = new KeyEvent(KeyEvent.ACTION_DOWN, abortSeekKeyCodes[i]);
    371             glue.onKey(null, abortSeekKeyCodes[i], kv);
    372             assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    373             assertEquals(0, fastForward.getIndex());
    374             assertEquals(0, rewind.getIndex());
    375         }
    376     }
    377 
    378     @Test
    379     public void testMediaPauseButtonOnFF() {
    380         PlaybackControlsRow row = new PlaybackControlsRow();
    381         glue.setControlsRow(row);
    382         SparseArrayObjectAdapter adapter = (SparseArrayObjectAdapter)
    383                 row.getPrimaryActionsAdapter();
    384         PlaybackControlsRow.MultiAction playPause = (PlaybackControlsRow.MultiAction) adapter
    385                 .lookup(PlaybackControlSupportGlue.ACTION_PLAY_PAUSE);
    386         PlaybackControlsRow.MultiAction fastForward = (PlaybackControlsRow.MultiAction) adapter
    387                 .lookup(PlaybackControlSupportGlue.ACTION_FAST_FORWARD);
    388 
    389         glue.onActionClicked(playPause);
    390         glue.onActionClicked(fastForward);
    391         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L0, glue.getCurrentSpeedId());
    392         glue.onKey(null, KeyEvent.KEYCODE_MEDIA_PAUSE, new KeyEvent(KeyEvent.ACTION_DOWN,
    393                 KeyEvent.KEYCODE_MEDIA_PAUSE));
    394         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_PAUSED, glue.getCurrentSpeedId());
    395     }
    396 
    397     @Test
    398     public void testMediaPauseButtonOnPlay() {
    399         PlaybackControlsRow row = new PlaybackControlsRow();
    400         glue.setControlsRow(row);
    401         SparseArrayObjectAdapter adapter = (SparseArrayObjectAdapter)
    402                 row.getPrimaryActionsAdapter();
    403         PlaybackControlsRow.MultiAction playPause = (PlaybackControlsRow.MultiAction) adapter
    404                 .lookup(PlaybackControlSupportGlue.ACTION_PLAY_PAUSE);
    405 
    406         glue.onActionClicked(playPause);
    407         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    408         glue.onKey(null, KeyEvent.KEYCODE_MEDIA_PAUSE, new KeyEvent(KeyEvent.ACTION_DOWN,
    409                 KeyEvent.KEYCODE_MEDIA_PAUSE));
    410         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_PAUSED, glue.getCurrentSpeedId());
    411     }
    412 
    413     @Test
    414     public void testMediaPauseButtonOnPause() {
    415         PlaybackControlsRow row = new PlaybackControlsRow();
    416         glue.setControlsRow(row);
    417         SparseArrayObjectAdapter adapter = (SparseArrayObjectAdapter)
    418                 row.getPrimaryActionsAdapter();
    419         PlaybackControlsRow.MultiAction playPause = (PlaybackControlsRow.MultiAction) adapter
    420                 .lookup(PlaybackControlSupportGlue.ACTION_PLAY_PAUSE);
    421 
    422         glue.onActionClicked(playPause);
    423         glue.onActionClicked(playPause);
    424         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_PAUSED, glue.getCurrentSpeedId());
    425         glue.onKey(null, KeyEvent.KEYCODE_MEDIA_PAUSE, new KeyEvent(KeyEvent.ACTION_DOWN,
    426                 KeyEvent.KEYCODE_MEDIA_PAUSE));
    427         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_PAUSED, glue.getCurrentSpeedId());
    428     }
    429 
    430     @Test
    431     public void testMediaPlayButtonOnFF() {
    432         PlaybackControlsRow row = new PlaybackControlsRow();
    433         glue.setControlsRow(row);
    434         SparseArrayObjectAdapter adapter = (SparseArrayObjectAdapter)
    435                 row.getPrimaryActionsAdapter();
    436         PlaybackControlsRow.MultiAction playPause = (PlaybackControlsRow.MultiAction) adapter
    437                 .lookup(PlaybackControlSupportGlue.ACTION_PLAY_PAUSE);
    438         PlaybackControlsRow.MultiAction fastForward = (PlaybackControlsRow.MultiAction) adapter
    439                 .lookup(PlaybackControlSupportGlue.ACTION_FAST_FORWARD);
    440 
    441         glue.onActionClicked(playPause);
    442         glue.onActionClicked(fastForward);
    443         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L0, glue.getCurrentSpeedId());
    444         glue.onKey(null, KeyEvent.KEYCODE_MEDIA_PLAY, new KeyEvent(KeyEvent.ACTION_DOWN,
    445                 KeyEvent.KEYCODE_MEDIA_PLAY));
    446         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    447     }
    448 
    449     @Test
    450     public void testMediaPlayButtonOnPlay() {
    451         PlaybackControlsRow row = new PlaybackControlsRow();
    452         glue.setControlsRow(row);
    453         SparseArrayObjectAdapter adapter = (SparseArrayObjectAdapter)
    454                 row.getPrimaryActionsAdapter();
    455         PlaybackControlsRow.MultiAction playPause = (PlaybackControlsRow.MultiAction) adapter
    456                 .lookup(PlaybackControlSupportGlue.ACTION_PLAY_PAUSE);
    457 
    458         glue.onActionClicked(playPause);
    459         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    460         glue.onKey(null, KeyEvent.KEYCODE_MEDIA_PLAY, new KeyEvent(KeyEvent.ACTION_DOWN,
    461                 KeyEvent.KEYCODE_MEDIA_PLAY));
    462         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    463     }
    464 
    465     @Test
    466     public void testMediaPlayButtonOnPause() {
    467         PlaybackControlsRow row = new PlaybackControlsRow();
    468         glue.setControlsRow(row);
    469         SparseArrayObjectAdapter adapter = (SparseArrayObjectAdapter)
    470                 row.getPrimaryActionsAdapter();
    471         PlaybackControlsRow.MultiAction playPause = (PlaybackControlsRow.MultiAction) adapter
    472                 .lookup(PlaybackControlSupportGlue.ACTION_PLAY_PAUSE);
    473 
    474         glue.onActionClicked(playPause);
    475         glue.onActionClicked(playPause);
    476         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_PAUSED, glue.getCurrentSpeedId());
    477         glue.onKey(null, KeyEvent.KEYCODE_MEDIA_PLAY, new KeyEvent(KeyEvent.ACTION_DOWN,
    478                 KeyEvent.KEYCODE_MEDIA_PLAY));
    479         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    480     }
    481 
    482     @Test
    483     public void testMediaPlayPauseButtonOnFF() {
    484         PlaybackControlsRow row = new PlaybackControlsRow();
    485         glue.setControlsRow(row);
    486         SparseArrayObjectAdapter adapter = (SparseArrayObjectAdapter)
    487                 row.getPrimaryActionsAdapter();
    488         PlaybackControlsRow.MultiAction playPause = (PlaybackControlsRow.MultiAction) adapter
    489                 .lookup(PlaybackControlSupportGlue.ACTION_PLAY_PAUSE);
    490         PlaybackControlsRow.MultiAction fastForward = (PlaybackControlsRow.MultiAction) adapter
    491                 .lookup(PlaybackControlSupportGlue.ACTION_FAST_FORWARD);
    492 
    493         glue.onActionClicked(playPause);
    494         glue.onActionClicked(fastForward);
    495         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L0, glue.getCurrentSpeedId());
    496         glue.onKey(null, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, new KeyEvent(KeyEvent.ACTION_DOWN,
    497                 KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
    498         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    499     }
    500 
    501     @Test
    502     public void testMediaPlayPauseButtonOnPlay() {
    503         PlaybackControlsRow row = new PlaybackControlsRow();
    504         glue.setControlsRow(row);
    505         SparseArrayObjectAdapter adapter = (SparseArrayObjectAdapter)
    506                 row.getPrimaryActionsAdapter();
    507         PlaybackControlsRow.MultiAction playPause = (PlaybackControlsRow.MultiAction) adapter
    508                 .lookup(PlaybackControlSupportGlue.ACTION_PLAY_PAUSE);
    509 
    510         glue.onActionClicked(playPause);
    511         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    512         glue.onKey(null, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, new KeyEvent(KeyEvent.ACTION_DOWN,
    513                 KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
    514         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_PAUSED, glue.getCurrentSpeedId());
    515     }
    516 
    517     @Test
    518     public void testMediaPlayPauseButtonOnPause() {
    519         PlaybackControlsRow row = new PlaybackControlsRow();
    520         glue.setControlsRow(row);
    521         SparseArrayObjectAdapter adapter = (SparseArrayObjectAdapter)
    522                 row.getPrimaryActionsAdapter();
    523         PlaybackControlsRow.MultiAction playPause = (PlaybackControlsRow.MultiAction) adapter
    524                 .lookup(PlaybackControlSupportGlue.ACTION_PLAY_PAUSE);
    525 
    526         glue.onActionClicked(playPause);
    527         glue.onActionClicked(playPause);
    528         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_PAUSED, glue.getCurrentSpeedId());
    529         glue.onKey(null, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, new KeyEvent(KeyEvent.ACTION_DOWN,
    530                 KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
    531         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    532     }
    533 
    534     @Test
    535     public void testOnItemClickedListener() {
    536         PlaybackControlsRow row = new PlaybackControlsRow();
    537         final PlaybackOverlaySupportFragment[] fragmentResult = new PlaybackOverlaySupportFragment[1];
    538         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
    539             @Override
    540             public void run() {
    541                 fragmentResult[0] = new PlaybackOverlaySupportFragment();
    542             }
    543         });
    544         PlaybackOverlaySupportFragment fragment = fragmentResult[0];
    545         glue.setHost(new PlaybackControlSupportGlue.PlaybackSupportGlueHostOld(fragment));
    546         glue.setControlsRow(row);
    547         SparseArrayObjectAdapter adapter = (SparseArrayObjectAdapter)
    548                 row.getPrimaryActionsAdapter();
    549         PlaybackControlsRow.MultiAction playPause = (PlaybackControlsRow.MultiAction) adapter
    550                 .lookup(PlaybackControlSupportGlue.ACTION_PLAY_PAUSE);
    551         OnItemViewClickedListener listener = Mockito.mock(OnItemViewClickedListener.class);
    552         glue.setOnItemViewClickedListener(listener);
    553 
    554         // create fake row ViewHolder and fade item ViewHolder
    555         View rowView = new View(context);
    556         View view = new View(context);
    557         PlaybackRowPresenter.ViewHolder rowVh = new PlaybackRowPresenter.ViewHolder(rowView);
    558         Presenter.ViewHolder vh = new Presenter.ViewHolder(view);
    559 
    560         // Initially media is paused
    561         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_PAUSED, glue.getCurrentSpeedId());
    562 
    563         // simulate a click inside PlaybackOverlaySupportFragment's PlaybackRow.
    564         fragment.getOnItemViewClickedListener().onItemClicked(vh, playPause, rowVh, row);
    565         verify(listener, times(0)).onItemClicked(vh, playPause, rowVh, row);
    566         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    567 
    568         // simulate a click on object other than PlaybackRow.
    569         Object regularItem = new Object();
    570         Row regularRow = new Row();
    571         RowPresenter.ViewHolder regularRowViewHolder = new RowPresenter.ViewHolder(rowView);
    572         Presenter.ViewHolder regularViewHOlder = new Presenter.ViewHolder(view);
    573         fragment.getOnItemViewClickedListener().onItemClicked(regularViewHOlder, regularItem,
    574                 regularRowViewHolder, regularRow);
    575         verify(listener, times(1)).onItemClicked(regularViewHOlder, regularItem,
    576                 regularRowViewHolder, regularRow);
    577         assertEquals(PlaybackControlSupportGlue.PLAYBACK_SPEED_NORMAL, glue.getCurrentSpeedId());
    578     }
    579 
    580     @Test
    581     public void testOnRowChangedCallback() throws Exception {
    582         final PlaybackOverlaySupportFragment[] fragmentResult = new
    583                 PlaybackOverlaySupportFragment[1];
    584         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
    585             @Override
    586             public void run() {
    587                 fragmentResult[0] = new PlaybackOverlaySupportFragment();
    588             }
    589         });
    590         PlaybackOverlaySupportFragment fragment = fragmentResult[0];
    591         PlayControlGlueImpl playbackGlue = new PlayControlGlueImpl(context, fragment,
    592                 new int[]{
    593                         PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L0,
    594                         PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L1,
    595                         PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L2
    596                 });
    597 
    598         // before any controls row is created the count is zero
    599         assertEquals(playbackGlue.getOnRowChangedCallCount(), 0);
    600         playbackGlue.createControlsRowAndPresenter();
    601         // after a controls row is created, onRowChanged() call back is called once
    602         assertEquals(playbackGlue.getOnRowChangedCallCount(), 1);
    603         assertEquals(3, playbackGlue.getControlsRow().getPrimaryActionsAdapter().size());
    604         playbackGlue.notifyMetaDataChanged();
    605         // onMetaDataChanged() calls updateRowMetadata which ends up calling
    606         // notifyPlaybackRowChanged on the old host and finally onRowChanged on the glue.
    607         assertEquals(playbackGlue.getOnRowChangedCallCount(), 2);
    608         assertEquals(3, playbackGlue.getControlsRow().getPrimaryActionsAdapter().size());
    609     }
    610 
    611 
    612     @Test
    613     public void testWithoutValidMedia() throws Exception {
    614         final PlaybackOverlaySupportFragment[] fragmentResult = new
    615                 PlaybackOverlaySupportFragment[1];
    616         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
    617             @Override
    618             public void run() {
    619                 fragmentResult[0] = new PlaybackOverlaySupportFragment();
    620             }
    621         });
    622         final boolean[] hasValidMedia = new boolean[] {false};
    623         PlaybackOverlaySupportFragment fragment = fragmentResult[0];
    624         PlayControlGlueImpl playbackGlue = new PlayControlGlueImpl(context, fragment,
    625                 new int[]{
    626                         PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L0,
    627                         PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L1,
    628                         PlaybackControlSupportGlue.PLAYBACK_SPEED_FAST_L2
    629                 }) {
    630             @Override
    631             public boolean hasValidMedia() {
    632                 return hasValidMedia[0];
    633             }
    634         };
    635 
    636         // before any controls row is created the count is zero
    637         assertEquals(playbackGlue.getOnRowChangedCallCount(), 0);
    638         playbackGlue.createControlsRowAndPresenter();
    639         // after a controls row is created, onRowChanged() call back is called once
    640         assertEquals(playbackGlue.getOnRowChangedCallCount(), 1);
    641         // enven hasValidMedia() is false, we should still have three buttons.
    642         assertEquals(3, playbackGlue.getControlsRow().getPrimaryActionsAdapter().size());
    643 
    644         hasValidMedia[0] = true;
    645         playbackGlue.notifyMetaDataChanged();
    646         // onMetaDataChanged() calls updateRowMetadata which ends up calling
    647         // notifyPlaybackRowChanged on the old host and finally onRowChanged on the glue.
    648         assertEquals(playbackGlue.getOnRowChangedCallCount(), 2);
    649         assertEquals(3, playbackGlue.getControlsRow().getPrimaryActionsAdapter().size());
    650     }
    651 
    652 }
    653