Home | History | Annotate | Download | only in gestures
      1 /*
      2  * Copyright (C) 2018 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.gestures;
     18 
     19 import static android.provider.Settings.Secure.VOLUME_HUSH_GESTURE;
     20 import static android.provider.Settings.Secure.VOLUME_HUSH_MUTE;
     21 import static android.provider.Settings.Secure.VOLUME_HUSH_OFF;
     22 import static android.provider.Settings.Secure.VOLUME_HUSH_VIBRATE;
     23 
     24 import static com.google.common.truth.Truth.assertThat;
     25 
     26 import static junit.framework.Assert.assertEquals;
     27 
     28 import static org.mockito.Matchers.anyInt;
     29 import static org.mockito.Mockito.doReturn;
     30 import static org.mockito.Mockito.mock;
     31 import static org.mockito.Mockito.spy;
     32 import static org.mockito.Mockito.verify;
     33 import static org.mockito.Mockito.when;
     34 
     35 import android.content.ContentResolver;
     36 import android.content.Context;
     37 import android.content.SharedPreferences;
     38 import android.provider.Settings;
     39 import android.support.v7.preference.ListPreference;
     40 import android.support.v7.preference.Preference;
     41 
     42 import com.android.internal.hardware.AmbientDisplayConfiguration;
     43 import com.android.settings.R;
     44 import com.android.settings.dashboard.suggestions.SuggestionFeatureProviderImpl;
     45 import com.android.settings.search.InlinePayload;
     46 import com.android.settings.search.InlineSwitchPayload;
     47 import com.android.settings.search.ResultPayload;
     48 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     49 
     50 import org.junit.Before;
     51 import org.junit.Test;
     52 import org.junit.runner.RunWith;
     53 import org.mockito.Answers;
     54 import org.mockito.Mock;
     55 import org.mockito.MockitoAnnotations;
     56 import org.robolectric.RuntimeEnvironment;
     57 
     58 @RunWith(SettingsRobolectricTestRunner.class)
     59 public class PreventRingingPreferenceControllerTest {
     60 
     61     private static final String KEY_PICK_UP = "gesture_prevent_ringing";
     62 
     63     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     64     private Context mContext;
     65 
     66     private PreventRingingPreferenceController mController;
     67 
     68     @Before
     69     public void setUp() {
     70         MockitoAnnotations.initMocks(this);
     71         mController = new PreventRingingPreferenceController(mContext, KEY_PICK_UP);
     72     }
     73 
     74     @Test
     75     public void testIsAvailable_configIsTrue_shouldReturnTrue() {
     76         when(mContext.getResources().getBoolean(
     77                 com.android.internal.R.bool.config_volumeHushGestureEnabled)).thenReturn(true);
     78 
     79         assertThat(mController.isAvailable()).isTrue();
     80     }
     81 
     82     @Test
     83     public void testIsAvailable_configIsFalse_shouldReturnFalse() {
     84         when(mContext.getResources().getBoolean(
     85                 com.android.internal.R.bool.config_volumeHushGestureEnabled)).thenReturn(false);
     86 
     87         assertThat(mController.isAvailable()).isFalse();
     88     }
     89 
     90     @Test
     91     public void testGetSummary_mute() {
     92         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
     93                 Settings.Secure.VOLUME_HUSH_MUTE);
     94         assertEquals(mContext.getString(R.string.prevent_ringing_option_mute_summary),
     95                 mController.getSummary());
     96     }
     97 
     98     @Test
     99     public void testGetSummary_vibrate() {
    100         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
    101                 Settings.Secure.VOLUME_HUSH_VIBRATE);
    102         assertEquals(mContext.getString(R.string.prevent_ringing_option_vibrate_summary),
    103                 mController.getSummary());
    104     }
    105     @Test
    106     public void testGetSummary_other() {
    107         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
    108                 7);
    109         assertEquals(mContext.getString(R.string.prevent_ringing_option_none_summary),
    110                 mController.getSummary());
    111     }
    112 
    113     @Test
    114     public void testUpdateState_mute() {
    115         ListPreference pref = mock(ListPreference.class);
    116         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
    117                 Settings.Secure.VOLUME_HUSH_MUTE);
    118         mController.updateState(pref);
    119         verify(pref).setValue(String.valueOf(Settings.Secure.VOLUME_HUSH_MUTE));
    120     }
    121 
    122     @Test
    123     public void testUpdateState_vibrate() {
    124         ListPreference pref = mock(ListPreference.class);
    125         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
    126                 Settings.Secure.VOLUME_HUSH_VIBRATE);
    127         mController.updateState(pref);
    128         verify(pref).setValue(String.valueOf(Settings.Secure.VOLUME_HUSH_VIBRATE));
    129     }
    130 
    131     @Test
    132     public void testUpdateState_other() {
    133         ListPreference pref = mock(ListPreference.class);
    134         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
    135                 7);
    136         mController.updateState(pref);
    137         verify(pref).setValue(String.valueOf(Settings.Secure.VOLUME_HUSH_OFF));
    138     }
    139 
    140     @Test
    141     public void testUpdateState_parentPage() {
    142         Preference pref = mock(Preference.class);
    143         // verify no exception
    144         mController.updateState(pref);
    145     }
    146 
    147     @Test
    148     public void testOnPreferenceChange() {
    149         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.VOLUME_HUSH_GESTURE,
    150                 7);
    151 
    152         mController.onPreferenceChange(mock(Preference.class), String.valueOf(VOLUME_HUSH_MUTE));
    153 
    154         assertEquals(VOLUME_HUSH_MUTE, Settings.Secure.getInt(mContext.getContentResolver(),
    155                 VOLUME_HUSH_GESTURE, VOLUME_HUSH_OFF));
    156     }
    157 }
    158