Home | History | Annotate | Download | only in client
      1 /*
      2  * Copyright 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.support.mediacompat.client;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertNotNull;
     21 import static org.junit.Assert.assertNull;
     22 
     23 import android.os.Bundle;
     24 import android.os.Parcel;
     25 import android.support.test.filters.SmallTest;
     26 import android.support.test.runner.AndroidJUnit4;
     27 import android.support.v4.media.session.MediaSessionCompat;
     28 import android.support.v4.media.session.PlaybackStateCompat;
     29 
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     32 
     33 import java.util.ArrayList;
     34 
     35 /**
     36  * Test {@link PlaybackStateCompat}.
     37  */
     38 @RunWith(AndroidJUnit4.class)
     39 public class PlaybackStateCompatTest {
     40 
     41     private static final long TEST_POSITION = 20000L;
     42     private static final long TEST_BUFFERED_POSITION = 15000L;
     43     private static final long TEST_UPDATE_TIME = 100000L;
     44     private static final long TEST_ACTIONS = PlaybackStateCompat.ACTION_PLAY
     45             | PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_SEEK_TO;
     46     private static final long TEST_QUEUE_ITEM_ID = 23L;
     47     private static final float TEST_PLAYBACK_SPEED = 3.0f;
     48     private static final float TEST_PLAYBACK_SPEED_ON_REWIND = -2.0f;
     49     private static final float DELTA = 1e-7f;
     50 
     51     private static final int TEST_ERROR_CODE =
     52             PlaybackStateCompat.ERROR_CODE_AUTHENTICATION_EXPIRED;
     53     private static final String TEST_ERROR_MSG = "test-error-msg";
     54     private static final String TEST_CUSTOM_ACTION = "test-custom-action";
     55     private static final String TEST_CUSTOM_ACTION_NAME = "test-custom-action-name";
     56     private static final int TEST_ICON_RESOURCE_ID = android.R.drawable.ic_media_next;
     57 
     58     private static final String EXTRAS_KEY = "test-key";
     59     private static final String EXTRAS_VALUE = "test-value";
     60 
     61     /**
     62      * Test default values of {@link PlaybackStateCompat}.
     63      */
     64     @Test
     65     @SmallTest
     66     public void testBuilder() {
     67         PlaybackStateCompat state = new PlaybackStateCompat.Builder().build();
     68 
     69         assertEquals(new ArrayList<PlaybackStateCompat.CustomAction>(), state.getCustomActions());
     70         assertEquals(0, state.getState());
     71         assertEquals(0L, state.getPosition());
     72         assertEquals(0L, state.getBufferedPosition());
     73         assertEquals(0.0f, state.getPlaybackSpeed(), DELTA);
     74         assertEquals(0L, state.getActions());
     75         assertEquals(0, state.getErrorCode());
     76         assertNull(state.getErrorMessage());
     77         assertEquals(0L, state.getLastPositionUpdateTime());
     78         assertEquals(MediaSessionCompat.QueueItem.UNKNOWN_ID, state.getActiveQueueItemId());
     79         assertNull(state.getExtras());
     80     }
     81 
     82     /**
     83      * Test following setter methods of {@link PlaybackStateCompat.Builder}:
     84      * {@link PlaybackStateCompat.Builder#setState(int, long, float)}
     85      * {@link PlaybackStateCompat.Builder#setActions(long)}
     86      * {@link PlaybackStateCompat.Builder#setActiveQueueItemId(long)}
     87      * {@link PlaybackStateCompat.Builder#setBufferedPosition(long)}
     88      * {@link PlaybackStateCompat.Builder#setErrorMessage(CharSequence)}
     89      * {@link PlaybackStateCompat.Builder#setExtras(Bundle)}
     90      */
     91     @Test
     92     @SmallTest
     93     public void testBuilder_setterMethods() {
     94         Bundle extras = new Bundle();
     95         extras.putString(EXTRAS_KEY, EXTRAS_VALUE);
     96 
     97         PlaybackStateCompat state = new PlaybackStateCompat.Builder()
     98                 .setState(PlaybackStateCompat.STATE_PLAYING, TEST_POSITION, TEST_PLAYBACK_SPEED)
     99                 .setActions(TEST_ACTIONS)
    100                 .setActiveQueueItemId(TEST_QUEUE_ITEM_ID)
    101                 .setBufferedPosition(TEST_BUFFERED_POSITION)
    102                 .setErrorMessage(TEST_ERROR_CODE, TEST_ERROR_MSG)
    103                 .setExtras(extras)
    104                 .build();
    105         assertEquals(PlaybackStateCompat.STATE_PLAYING, state.getState());
    106         assertEquals(TEST_POSITION, state.getPosition());
    107         assertEquals(TEST_PLAYBACK_SPEED, state.getPlaybackSpeed(), DELTA);
    108         assertEquals(TEST_ACTIONS, state.getActions());
    109         assertEquals(TEST_QUEUE_ITEM_ID, state.getActiveQueueItemId());
    110         assertEquals(TEST_BUFFERED_POSITION, state.getBufferedPosition());
    111         assertEquals(TEST_ERROR_CODE, state.getErrorCode());
    112         assertEquals(TEST_ERROR_MSG, state.getErrorMessage().toString());
    113         assertNotNull(state.getExtras());
    114         assertEquals(EXTRAS_VALUE, state.getExtras().get(EXTRAS_KEY));
    115     }
    116 
    117     /**
    118      * Test {@link PlaybackStateCompat.Builder#setState(int, long, float, long)}.
    119      */
    120     @Test
    121     @SmallTest
    122     public void testBuilder_setStateWithUpdateTime() {
    123         PlaybackStateCompat state = new PlaybackStateCompat.Builder()
    124                 .setState(
    125                         PlaybackStateCompat.STATE_REWINDING,
    126                         TEST_POSITION,
    127                         TEST_PLAYBACK_SPEED_ON_REWIND,
    128                         TEST_UPDATE_TIME)
    129                 .build();
    130         assertEquals(PlaybackStateCompat.STATE_REWINDING, state.getState());
    131         assertEquals(TEST_POSITION, state.getPosition());
    132         assertEquals(TEST_PLAYBACK_SPEED_ON_REWIND, state.getPlaybackSpeed(), DELTA);
    133         assertEquals(TEST_UPDATE_TIME, state.getLastPositionUpdateTime());
    134     }
    135 
    136     /**
    137      * Test {@link PlaybackStateCompat.Builder#addCustomAction(String, String, int)}.
    138      */
    139     @Test
    140     @SmallTest
    141     public void testBuilder_addCustomAction() {
    142         ArrayList<PlaybackStateCompat.CustomAction> actions = new ArrayList<>();
    143         PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder();
    144 
    145         for (int i = 0; i < 5; i++) {
    146             actions.add(new PlaybackStateCompat.CustomAction.Builder(
    147                     TEST_CUSTOM_ACTION + i, TEST_CUSTOM_ACTION_NAME + i, TEST_ICON_RESOURCE_ID + i)
    148                     .build());
    149             builder.addCustomAction(
    150                     TEST_CUSTOM_ACTION + i, TEST_CUSTOM_ACTION_NAME + i, TEST_ICON_RESOURCE_ID + i);
    151         }
    152 
    153         PlaybackStateCompat state = builder.build();
    154         assertEquals(actions.size(), state.getCustomActions().size());
    155         for (int i = 0; i < actions.size(); i++) {
    156             assertCustomActionEquals(actions.get(i), state.getCustomActions().get(i));
    157         }
    158     }
    159 
    160     /**
    161      * Test {@link PlaybackStateCompat.Builder#addCustomAction(PlaybackStateCompat.CustomAction)}.
    162      */
    163     @Test
    164     @SmallTest
    165     public void testBuilder_addCustomActionWithCustomActionObject() {
    166         Bundle extras = new Bundle();
    167         extras.putString(EXTRAS_KEY, EXTRAS_VALUE);
    168 
    169         ArrayList<PlaybackStateCompat.CustomAction> actions = new ArrayList<>();
    170         PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder();
    171 
    172         for (int i = 0; i < 5; i++) {
    173             actions.add(new PlaybackStateCompat.CustomAction.Builder(
    174                     TEST_CUSTOM_ACTION + i, TEST_CUSTOM_ACTION_NAME + i, TEST_ICON_RESOURCE_ID + i)
    175                     .setExtras(extras)
    176                     .build());
    177             builder.addCustomAction(new PlaybackStateCompat.CustomAction.Builder(
    178                     TEST_CUSTOM_ACTION + i, TEST_CUSTOM_ACTION_NAME + i, TEST_ICON_RESOURCE_ID + i)
    179                     .setExtras(extras)
    180                     .build());
    181         }
    182 
    183         PlaybackStateCompat state = builder.build();
    184         assertEquals(actions.size(), state.getCustomActions().size());
    185         for (int i = 0; i < actions.size(); i++) {
    186             assertCustomActionEquals(actions.get(i), state.getCustomActions().get(i));
    187         }
    188     }
    189 
    190     /**
    191      * Test {@link PlaybackStateCompat#writeToParcel(Parcel, int)}.
    192      */
    193     @Test
    194     @SmallTest
    195     public void testWriteToParcel() {
    196         Bundle extras = new Bundle();
    197         extras.putString(EXTRAS_KEY, EXTRAS_VALUE);
    198 
    199         PlaybackStateCompat.Builder builder =
    200                 new PlaybackStateCompat.Builder()
    201                         .setState(PlaybackStateCompat.STATE_CONNECTING, TEST_POSITION,
    202                                 TEST_PLAYBACK_SPEED, TEST_UPDATE_TIME)
    203                         .setActions(TEST_ACTIONS)
    204                         .setActiveQueueItemId(TEST_QUEUE_ITEM_ID)
    205                         .setBufferedPosition(TEST_BUFFERED_POSITION)
    206                         .setErrorMessage(TEST_ERROR_CODE, TEST_ERROR_MSG)
    207                         .setExtras(extras);
    208 
    209         for (int i = 0; i < 5; i++) {
    210             builder.addCustomAction(
    211                     new PlaybackStateCompat.CustomAction.Builder(
    212                             TEST_CUSTOM_ACTION + i,
    213                             TEST_CUSTOM_ACTION_NAME + i,
    214                             TEST_ICON_RESOURCE_ID + i)
    215                             .setExtras(extras)
    216                             .build());
    217         }
    218         PlaybackStateCompat state = builder.build();
    219 
    220         Parcel parcel = Parcel.obtain();
    221         state.writeToParcel(parcel, 0);
    222         parcel.setDataPosition(0);
    223 
    224         PlaybackStateCompat stateOut = PlaybackStateCompat.CREATOR.createFromParcel(parcel);
    225         assertEquals(PlaybackStateCompat.STATE_CONNECTING, stateOut.getState());
    226         assertEquals(TEST_POSITION, stateOut.getPosition());
    227         assertEquals(TEST_PLAYBACK_SPEED, stateOut.getPlaybackSpeed(), DELTA);
    228         assertEquals(TEST_UPDATE_TIME, stateOut.getLastPositionUpdateTime());
    229         assertEquals(TEST_BUFFERED_POSITION, stateOut.getBufferedPosition());
    230         assertEquals(TEST_ACTIONS, stateOut.getActions());
    231         assertEquals(TEST_QUEUE_ITEM_ID, stateOut.getActiveQueueItemId());
    232         assertEquals(TEST_ERROR_CODE, stateOut.getErrorCode());
    233         assertEquals(TEST_ERROR_MSG, stateOut.getErrorMessage());
    234         assertNotNull(stateOut.getExtras());
    235         assertEquals(EXTRAS_VALUE, stateOut.getExtras().get(EXTRAS_KEY));
    236 
    237         assertEquals(state.getCustomActions().size(), stateOut.getCustomActions().size());
    238         for (int i = 0; i < state.getCustomActions().size(); i++) {
    239             assertCustomActionEquals(
    240                     state.getCustomActions().get(i), stateOut.getCustomActions().get(i));
    241         }
    242         parcel.recycle();
    243     }
    244 
    245     /**
    246      * Test {@link PlaybackStateCompat#describeContents()}.
    247      */
    248     @Test
    249     @SmallTest
    250     public void testDescribeContents() {
    251         assertEquals(0, new PlaybackStateCompat.Builder().build().describeContents());
    252     }
    253 
    254     /**
    255      * Test {@link PlaybackStateCompat.CustomAction}.
    256      */
    257     @Test
    258     @SmallTest
    259     public void testCustomAction() {
    260         Bundle extras = new Bundle();
    261         extras.putString(EXTRAS_KEY, EXTRAS_VALUE);
    262 
    263         // Test Builder/Getters
    264         PlaybackStateCompat.CustomAction customAction = new PlaybackStateCompat.CustomAction
    265                 .Builder(TEST_CUSTOM_ACTION, TEST_CUSTOM_ACTION_NAME, TEST_ICON_RESOURCE_ID)
    266                 .setExtras(extras)
    267                 .build();
    268         assertEquals(TEST_CUSTOM_ACTION, customAction.getAction());
    269         assertEquals(TEST_CUSTOM_ACTION_NAME, customAction.getName().toString());
    270         assertEquals(TEST_ICON_RESOURCE_ID, customAction.getIcon());
    271         assertEquals(EXTRAS_VALUE, customAction.getExtras().get(EXTRAS_KEY));
    272 
    273         // Test describeContents
    274         assertEquals(0, customAction.describeContents());
    275 
    276         // Test writeToParcel
    277         Parcel parcel = Parcel.obtain();
    278         customAction.writeToParcel(parcel, 0);
    279         parcel.setDataPosition(0);
    280 
    281         assertCustomActionEquals(
    282                 customAction, PlaybackStateCompat.CustomAction.CREATOR.createFromParcel(parcel));
    283         parcel.recycle();
    284     }
    285 
    286     private void assertCustomActionEquals(PlaybackStateCompat.CustomAction action1,
    287             PlaybackStateCompat.CustomAction action2) {
    288         assertEquals(action1.getAction(), action2.getAction());
    289         assertEquals(action1.getName(), action2.getName());
    290         assertEquals(action1.getIcon(), action2.getIcon());
    291 
    292         // To be the same, two extras should be both null or both not null.
    293         assertEquals(action1.getExtras() != null, action2.getExtras() != null);
    294         if (action1.getExtras() != null) {
    295             assertEquals(action1.getExtras().get(EXTRAS_KEY), action2.getExtras().get(EXTRAS_KEY));
    296         }
    297     }
    298 }
    299