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 android.app.Activity;
     20 import android.content.ContentResolver;
     21 import android.content.Context;
     22 import android.media.AudioManager;
     23 import android.support.v14.preference.SwitchPreference;
     24 import android.support.v7.preference.PreferenceScreen;
     25 import android.provider.Settings.System;
     26 
     27 import com.android.settings.SettingsRobolectricTestRunner;
     28 import com.android.settings.TestConfig;
     29 
     30 import org.junit.Before;
     31 import org.junit.Test;
     32 import org.junit.runner.RunWith;
     33 import org.mockito.Mock;
     34 import org.mockito.MockitoAnnotations;
     35 import org.robolectric.Robolectric;
     36 import org.robolectric.annotation.Config;
     37 import org.robolectric.shadows.ShadowApplication;
     38 
     39 import static com.google.common.truth.Truth.assertThat;
     40 import static org.mockito.Mockito.doReturn;
     41 import static org.mockito.Mockito.verify;
     42 import static org.mockito.Mockito.when;
     43 
     44 @RunWith(SettingsRobolectricTestRunner.class)
     45 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     46 public class TouchSoundPreferenceControllerTest {
     47 
     48     @Mock
     49     private AudioManager mAudioManager;
     50     @Mock
     51     private PreferenceScreen mScreen;
     52     @Mock
     53     private Activity mActivity;
     54     @Mock
     55     private ContentResolver mContentResolver;
     56     @Mock
     57     private SoundSettings mSetting;
     58     @Mock
     59     private Context mContext;
     60 
     61     private TouchSoundPreferenceController mController;
     62     private SwitchPreference mPreference;
     63 
     64     @Before
     65     public void setUp() {
     66         MockitoAnnotations.initMocks(this);
     67         when(mActivity.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager);
     68         when(mSetting.getActivity()).thenReturn(mActivity);
     69         when(mActivity.getContentResolver()).thenReturn(mContentResolver);
     70         mPreference = new SwitchPreference(ShadowApplication.getInstance().getApplicationContext());
     71         mController = new TouchSoundPreferenceController(mContext, mSetting, null);
     72         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
     73         doReturn(mScreen).when(mSetting).getPreferenceScreen();
     74     }
     75 
     76     @Test
     77     public void isAvailable_isAlwaysTrue() {
     78         assertThat(mController.isAvailable()).isTrue();
     79     }
     80 
     81     @Test
     82     public void displayPreference_soundEffectEnabled_shouldCheckedPreference() {
     83         System.putInt(mContentResolver, System.SOUND_EFFECTS_ENABLED, 1);
     84 
     85         mController.displayPreference(mScreen);
     86 
     87         assertThat(mPreference.isChecked()).isTrue();
     88     }
     89 
     90     @Test
     91     public void displayPreference_soundEffectDisabled_shouldUncheckedPreference() {
     92         System.putInt(mContentResolver, System.SOUND_EFFECTS_ENABLED, 0);
     93 
     94         mController.displayPreference(mScreen);
     95 
     96         assertThat(mPreference.isChecked()).isFalse();
     97     }
     98 
     99     @Test
    100     public void onPreferenceChanged_preferenceChecked_shouldEnabledSoundEffect() {
    101         mController.displayPreference(mScreen);
    102 
    103         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, true);
    104 
    105         assertThat(System.getInt(mContentResolver, System.SOUND_EFFECTS_ENABLED, 1)).isEqualTo(1);
    106     }
    107 
    108     @Test
    109     public void onPreferenceChanged_preferenceUnchecked_shouldDisabledSoundEffect() {
    110         mController.displayPreference(mScreen);
    111 
    112         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, false);
    113 
    114         assertThat(System.getInt(mContentResolver, System.SOUND_EFFECTS_ENABLED, 1)).isEqualTo(0);
    115     }
    116 }
    117