Home | History | Annotate | Download | only in development
      1 /*
      2  * Copyright (C) 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 com.android.settings.development;
     18 
     19 import static com.android.settings.development.AbstractBluetoothA2dpPreferenceController.STREAMING_LABEL_ID;
     20 import static org.mockito.ArgumentMatchers.any;
     21 import static org.mockito.Mockito.doNothing;
     22 import static org.mockito.Mockito.doReturn;
     23 import static org.mockito.Mockito.never;
     24 import static org.mockito.Mockito.spy;
     25 import static org.mockito.Mockito.verify;
     26 import static org.mockito.Mockito.when;
     27 
     28 import android.arch.lifecycle.LifecycleOwner;
     29 import android.bluetooth.BluetoothA2dp;
     30 import android.bluetooth.BluetoothCodecConfig;
     31 import android.content.Context;
     32 import android.support.v7.preference.ListPreference;
     33 import android.support.v7.preference.PreferenceScreen;
     34 
     35 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     36 import com.android.settingslib.core.lifecycle.Lifecycle;
     37 
     38 import org.junit.Before;
     39 import org.junit.Test;
     40 import org.junit.runner.RunWith;
     41 import org.mockito.Mock;
     42 import org.mockito.MockitoAnnotations;
     43 import org.robolectric.RuntimeEnvironment;
     44 
     45 @RunWith(SettingsRobolectricTestRunner.class)
     46 public class AbstractBluetoothA2dpPreferenceControllerTest {
     47 
     48     @Mock
     49     private BluetoothA2dp mBluetoothA2dp;
     50     @Mock
     51     private BluetoothCodecConfig mBluetoothCodecConfig;
     52     @Mock
     53     private ListPreference mPreference;
     54     @Mock
     55     private PreferenceScreen mScreen;
     56     @Mock
     57     private BluetoothA2dpConfigStore mBluetoothA2dpConfigStore;
     58 
     59     private LifecycleOwner mLifecycleOwner;
     60     private Lifecycle mLifecycle;
     61     private Context mContext;
     62     private AbstractBluetoothA2dpPreferenceController mController;
     63 
     64     @Before
     65     public void setup() {
     66         MockitoAnnotations.initMocks(this);
     67         mContext = RuntimeEnvironment.application;
     68         mLifecycleOwner = () -> mLifecycle;
     69         mLifecycle = new Lifecycle(mLifecycleOwner);
     70         mController = spy(new AbstractBluetoothA2dpPreferenceControllerImpl(mContext, mLifecycle,
     71                 mBluetoothA2dpConfigStore));
     72         doReturn(mBluetoothCodecConfig).when(mController).getCodecConfig(null);
     73         doNothing().when(mController).setCodecConfigPreference(any(), any());
     74         when(mBluetoothA2dpConfigStore.createCodecConfig()).thenReturn(mBluetoothCodecConfig);
     75         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
     76         mController.displayPreference(mScreen);
     77     }
     78 
     79     @Test
     80     public void onPreferenceChange_bluetoothConnected_shouldUpdateCodec() {
     81         mController.onBluetoothServiceConnected(mBluetoothA2dp);
     82 
     83         mController.onPreferenceChange(mPreference, "" /* new value */);
     84 
     85         verify(mController).setCodecConfigPreference(any(), any());
     86     }
     87 
     88     @Test
     89     public void onPreferenceChange_bluetoothNotConnected_shouldNotUpdateCodec() {
     90         mController.onBluetoothServiceDisconnected();
     91 
     92         mController.onPreferenceChange(mPreference, "" /* new value */);
     93 
     94         verify(mController, never()).setCodecConfigPreference(any(), any());
     95     }
     96 
     97     @Test
     98     public void updateState_option2Set_shouldUpdateToOption2() {
     99         when(mBluetoothCodecConfig.getSampleRate()).thenReturn(
    100                 BluetoothCodecConfig.SAMPLE_RATE_48000);
    101 
    102         doReturn(2).when(mController).getCurrentA2dpSettingIndex(any());
    103         mController.updateState(mPreference);
    104 
    105         verify(mPreference).setValue(mController.getListValues()[2]);
    106         verify(mPreference).setSummary(mContext.getString(STREAMING_LABEL_ID,
    107             mController.getListSummaries()[2]));
    108     }
    109 
    110     @Test
    111     public void onBluetoothServiceConnected_shouldUpdateState() {
    112         mController.onBluetoothServiceConnected(mBluetoothA2dp);
    113 
    114         verify(mController).updateState(mPreference);
    115     }
    116 
    117     private static class AbstractBluetoothA2dpPreferenceControllerImpl
    118         extends AbstractBluetoothA2dpPreferenceController {
    119 
    120         private AbstractBluetoothA2dpPreferenceControllerImpl(Context context,
    121                 Lifecycle lifecycle, BluetoothA2dpConfigStore store) {
    122             super(context, lifecycle, store);
    123         }
    124 
    125         @Override
    126         public String getPreferenceKey() {
    127             return null;
    128         }
    129 
    130         @Override
    131         protected String[] getListValues() {
    132             return new String[]{"1", "2", "3"};
    133         }
    134 
    135         @Override
    136         protected String[] getListSummaries() {
    137             return new String[]{"foo", "bar", "foobar"};
    138         }
    139 
    140         @Override
    141         protected void writeConfigurationValues(Object newValue) {
    142         }
    143 
    144         @Override
    145         protected int getCurrentA2dpSettingIndex(BluetoothCodecConfig config) {
    146             return 0;
    147         }
    148 
    149         @Override
    150         protected int getDefaultIndex() {
    151             return 0;
    152         }
    153     }
    154 }
    155