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.Answers.RETURNS_DEEP_STUBS;
     21 import static org.mockito.Mockito.doReturn;
     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 
     40 @RunWith(SettingsRobolectricTestRunner.class)
     41 public class DockingSoundPreferenceControllerTest {
     42 
     43     @Mock
     44     private PreferenceScreen mScreen;
     45     @Mock(answer = RETURNS_DEEP_STUBS)
     46     private Activity mActivity;
     47     @Mock
     48     private ContentResolver mContentResolver;
     49     @Mock
     50     private SoundSettings mSetting;
     51     @Mock(answer = RETURNS_DEEP_STUBS)
     52     private Context mContext;
     53 
     54     private DockingSoundPreferenceController mController;
     55     private SwitchPreference mPreference;
     56 
     57     @Before
     58     public void setUp() {
     59         MockitoAnnotations.initMocks(this);
     60         when(mSetting.getActivity()).thenReturn(mActivity);
     61         when(mActivity.getContentResolver()).thenReturn(mContentResolver);
     62         mPreference = new SwitchPreference(RuntimeEnvironment.application);
     63         when(mActivity.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
     64             .thenReturn(true);
     65         mController = new DockingSoundPreferenceController(mContext, mSetting, null);
     66         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
     67         doReturn(mScreen).when(mSetting).getPreferenceScreen();
     68     }
     69 
     70     @Test
     71     public void isAvailable_hasDockSettings_shouldReturnTrue() {
     72         when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
     73             .thenReturn(true);
     74 
     75         assertThat(mController.isAvailable()).isTrue();
     76     }
     77 
     78     @Test
     79     public void isAvailable_noDockSettings_shouldReturnFalse() {
     80         when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
     81             .thenReturn(false);
     82 
     83         assertThat(mController.isAvailable()).isFalse();
     84     }
     85 
     86     @Test
     87     public void displayPreference_dockingSoundEnabled_shouldCheckedPreference() {
     88         Global.putInt(mContentResolver, Global.DOCK_SOUNDS_ENABLED, 1);
     89 
     90         mController.displayPreference(mScreen);
     91 
     92         assertThat(mPreference.isChecked()).isTrue();
     93     }
     94 
     95     @Test
     96     public void displayPreference_dockingSoundDisabled_shouldUncheckedPreference() {
     97         Global.putInt(mContentResolver, Global.DOCK_SOUNDS_ENABLED, 0);
     98 
     99         mController.displayPreference(mScreen);
    100 
    101         assertThat(mPreference.isChecked()).isFalse();
    102     }
    103 
    104     @Test
    105     public void onPreferenceChanged_preferenceChecked_shouldEnabledDockingSound() {
    106         mController.displayPreference(mScreen);
    107 
    108         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, true);
    109 
    110         assertThat(Global.getInt(mContentResolver, Global.DOCK_SOUNDS_ENABLED, 1)).isEqualTo(1);
    111     }
    112 
    113     @Test
    114     public void onPreferenceChanged_preferenceUnchecked_shouldDisabledDockingSound() {
    115         mController.displayPreference(mScreen);
    116 
    117         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, false);
    118 
    119         assertThat(Global.getInt(mContentResolver, Global.DOCK_SOUNDS_ENABLED, 1)).isEqualTo(0);
    120     }
    121 }
    122