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.google.common.truth.Truth.assertThat;
     20 import static org.mockito.Mockito.spy;
     21 import static org.mockito.Mockito.verify;
     22 import static org.mockito.Mockito.when;
     23 
     24 import android.arch.lifecycle.LifecycleOwner;
     25 import android.bluetooth.BluetoothCodecConfig;
     26 import android.content.Context;
     27 import android.support.v7.preference.ListPreference;
     28 import android.support.v7.preference.PreferenceScreen;
     29 
     30 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     31 import com.android.settingslib.core.lifecycle.Lifecycle;
     32 
     33 import org.junit.Before;
     34 import org.junit.Test;
     35 import org.junit.runner.RunWith;
     36 import org.mockito.Mock;
     37 import org.mockito.MockitoAnnotations;
     38 import org.robolectric.RuntimeEnvironment;
     39 
     40 @RunWith(SettingsRobolectricTestRunner.class)
     41 public class BluetoothAudioChannelModePreferenceControllerTest {
     42 
     43     @Mock
     44     private BluetoothCodecConfig mBluetoothCodecConfig;
     45     @Mock
     46     private ListPreference mPreference;
     47     @Mock
     48     private PreferenceScreen mScreen;
     49     @Mock
     50     private BluetoothA2dpConfigStore mBluetoothA2dpConfigStore;
     51 
     52     /**
     53      * 0: Use System Selection (Default)
     54      * 1: Mono
     55      * 2: Stereo
     56      */
     57     private String[] mListValues;
     58     private Context mContext;
     59     private BluetoothAudioChannelModePreferenceController mController;
     60     private LifecycleOwner mLifecycleOwner;
     61     private Lifecycle mLifecycle;
     62 
     63     @Before
     64     public void setup() {
     65         MockitoAnnotations.initMocks(this);
     66         mContext = RuntimeEnvironment.application;
     67         mLifecycleOwner = () -> mLifecycle;
     68         mLifecycle = new Lifecycle(mLifecycleOwner);
     69         mController = spy(new BluetoothAudioChannelModePreferenceController(mContext,
     70                 mLifecycle, mBluetoothA2dpConfigStore));
     71         mListValues = mController.getListValues();
     72         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
     73         mController.displayPreference(mScreen);
     74     }
     75 
     76     @Test
     77     public void writeConfigurationValues_option2_shouldWriteOption2ToSharedStore() {
     78         when(mPreference.findIndexOfValue(mListValues[2])).thenReturn(2);
     79         mController.writeConfigurationValues(mListValues[2]);
     80 
     81         verify(mBluetoothA2dpConfigStore).setChannelMode(BluetoothCodecConfig.CHANNEL_MODE_STEREO);
     82     }
     83 
     84     @Test
     85     public void writeConfigurationValues_default_shouldSetDefault() {
     86         when(mPreference.findIndexOfValue(mListValues[0])).thenReturn(0);
     87         mController.writeConfigurationValues(mListValues[0]);
     88 
     89         verify(mBluetoothA2dpConfigStore).setChannelMode(BluetoothCodecConfig.CHANNEL_MODE_NONE);
     90     }
     91 
     92     @Test
     93     public void getCurrentA2dpSettingIndex_option2_shouldReturnSecondIndex() {
     94         when(mBluetoothCodecConfig.getChannelMode())
     95             .thenReturn(BluetoothCodecConfig.CHANNEL_MODE_STEREO);
     96 
     97         final int index = mController.getCurrentA2dpSettingIndex(mBluetoothCodecConfig);
     98 
     99         assertThat(index).isEqualTo(2);
    100     }
    101 
    102     @Test
    103     public void getCurrentA2dpSettingIndex_unknownOption_shouldReturnDefault() {
    104         when(mBluetoothCodecConfig.getCodecType()).thenReturn(1381391835);
    105 
    106         final int index = mController.getCurrentA2dpSettingIndex(mBluetoothCodecConfig);
    107 
    108         assertThat(index).isEqualTo(0);
    109     }
    110 }
    111