Home | History | Annotate | Download | only in notification
      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.notification;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 import static org.mockito.Mockito.doReturn;
     21 import static org.mockito.Mockito.spy;
     22 import static org.mockito.Mockito.when;
     23 
     24 import android.app.Activity;
     25 import android.content.ContentResolver;
     26 import android.content.Context;
     27 import android.provider.Settings.Global;
     28 import android.support.v14.preference.SwitchPreference;
     29 import android.support.v7.preference.PreferenceScreen;
     30 
     31 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     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 import org.robolectric.annotation.Config;
     40 
     41 @RunWith(SettingsRobolectricTestRunner.class)
     42 public class ChargingSoundPreferenceControllerTest {
     43 
     44     @Mock
     45     private PreferenceScreen mScreen;
     46     @Mock
     47     private Activity mActivity;
     48     @Mock
     49     private ContentResolver mContentResolver;
     50     @Mock
     51     private SoundSettings mSetting;
     52 
     53     private Context mContext;
     54     private ChargingSoundPreferenceController mController;
     55     private SwitchPreference mPreference;
     56 
     57     @Before
     58     public void setUp() {
     59         MockitoAnnotations.initMocks(this);
     60         mContext = spy(RuntimeEnvironment.application);
     61         when(mSetting.getActivity()).thenReturn(mActivity);
     62         when(mActivity.getContentResolver()).thenReturn(mContentResolver);
     63         mPreference = new SwitchPreference(RuntimeEnvironment.application);
     64         mController = new ChargingSoundPreferenceController(mContext, mSetting, null);
     65         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
     66         doReturn(mScreen).when(mSetting).getPreferenceScreen();
     67     }
     68 
     69     @Test
     70     public void isAvailable_byDefault_isTrue() {
     71         assertThat(mController.isAvailable()).isTrue();
     72     }
     73 
     74     @Test
     75     @Config(qualifiers = "mcc999")
     76     public void isAvailable_whenNotVisible_isFalse() {
     77         assertThat(mController.isAvailable()).isFalse();
     78     }
     79 
     80     @Test
     81     public void displayPreference_chargingSoundEnabled_shouldCheckedPreference() {
     82         Global.putInt(mContentResolver, Global.CHARGING_SOUNDS_ENABLED, 1);
     83 
     84         mController.displayPreference(mScreen);
     85 
     86         assertThat(mPreference.isChecked()).isTrue();
     87     }
     88 
     89     @Test
     90     public void displayPreference_chargingSoundDisabled_shouldUncheckedPreference() {
     91         Global.putInt(mContentResolver, Global.CHARGING_SOUNDS_ENABLED, 0);
     92 
     93         mController.displayPreference(mScreen);
     94 
     95         assertThat(mPreference.isChecked()).isFalse();
     96     }
     97 
     98     @Test
     99     public void onPreferenceChanged_preferenceChecked_shouldEnabledChargingSound() {
    100         mController.displayPreference(mScreen);
    101 
    102         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, true);
    103 
    104         assertThat(Global.getInt(mContentResolver, Global.CHARGING_SOUNDS_ENABLED, 1))
    105             .isEqualTo(1);
    106     }
    107 
    108     @Test
    109     public void onPreferenceChanged_preferenceUnchecked_shouldDisabledChargingSound() {
    110         mController.displayPreference(mScreen);
    111 
    112         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, false);
    113 
    114         assertThat(Global.getInt(mContentResolver, Global.CHARGING_SOUNDS_ENABLED, 1))
    115             .isEqualTo(0);
    116     }
    117 }
    118