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