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