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