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.BluetoothAbsoluteVolumePreferenceController.BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY;
     20 import static com.google.common.truth.Truth.assertThat;
     21 import static org.mockito.Mockito.verify;
     22 import static org.mockito.Mockito.when;
     23 
     24 import android.content.Context;
     25 import android.os.SystemProperties;
     26 import android.support.v14.preference.SwitchPreference;
     27 import android.support.v7.preference.PreferenceScreen;
     28 
     29 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     30 
     31 import org.junit.Before;
     32 import org.junit.Test;
     33 import org.junit.runner.RunWith;
     34 import org.mockito.Mock;
     35 import org.mockito.MockitoAnnotations;
     36 import org.robolectric.RuntimeEnvironment;
     37 
     38 @RunWith(SettingsRobolectricTestRunner.class)
     39 public class BluetoothAbsoluteVolumePreferenceControllerTest {
     40 
     41     @Mock
     42     private SwitchPreference mPreference;
     43     @Mock
     44     private PreferenceScreen mPreferenceScreen;
     45 
     46     private Context mContext;
     47     private BluetoothAbsoluteVolumePreferenceController mController;
     48 
     49     @Before
     50     public void setup() {
     51         MockitoAnnotations.initMocks(this);
     52         mContext = RuntimeEnvironment.application;
     53         mController = new BluetoothAbsoluteVolumePreferenceController(mContext);
     54         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
     55             .thenReturn(mPreference);
     56         mController.displayPreference(mPreferenceScreen);
     57     }
     58 
     59     @Test
     60     public void onPreferenceChanged_settingEnabled_shouldTurnOnBluetoothDisableAbsoluteVolume() {
     61         mController.onPreferenceChange(mPreference, true /* new value */);
     62 
     63         final boolean mode = SystemProperties.getBoolean(
     64                 BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY, false /* default */);
     65 
     66         assertThat(mode).isTrue();
     67     }
     68 
     69     @Test
     70     public void onPreferenceChanged_settingDisabled_shouldTurnOffBluetoothDisableAbsoluteVolume() {
     71         mController.onPreferenceChange(mPreference, false /* new value */);
     72 
     73         final boolean mode = SystemProperties.getBoolean(
     74                 BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY, false /* default */);
     75 
     76         assertThat(mode).isFalse();
     77     }
     78 
     79     @Test
     80     public void updateState_settingEnabled_preferenceShouldBeChecked() {
     81         SystemProperties.set(BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY, Boolean.toString(true));
     82         mController.updateState(mPreference);
     83 
     84         verify(mPreference).setChecked(true);
     85     }
     86 
     87     @Test
     88     public void updateState_settingDisabled_preferenceShouldNotBeChecked() {
     89         SystemProperties.set(BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY, Boolean.toString(false));
     90         mController.updateState(mPreference);
     91 
     92         verify(mPreference).setChecked(false);
     93     }
     94 
     95     @Test
     96     public void onDeveloperOptionsDisabled_shouldDisablePreference() {
     97         mController.onDeveloperOptionsDisabled();
     98 
     99         final boolean mode = SystemProperties.getBoolean(
    100                 BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY, false /* default */);
    101 
    102         assertThat(mode).isFalse();
    103         verify(mPreference).setEnabled(false);
    104         verify(mPreference).setChecked(false);
    105     }
    106 }
    107