Home | History | Annotate | Download | only in development
      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.development;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 import static org.mockito.Mockito.verify;
     21 import static org.mockito.Mockito.when;
     22 
     23 import android.content.Context;
     24 import android.provider.Settings;
     25 import android.support.v14.preference.SwitchPreference;
     26 import android.support.v7.preference.PreferenceScreen;
     27 
     28 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     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.RuntimeEnvironment;
     36 
     37 @RunWith(SettingsRobolectricTestRunner.class)
     38 public class ShowTapsPreferenceControllerTest {
     39 
     40     @Mock
     41     private PreferenceScreen mScreen;
     42     @Mock
     43     private SwitchPreference mPreference;
     44 
     45     private Context mContext;
     46 
     47     private ShowTapsPreferenceController mController;
     48 
     49     @Before
     50     public void setUp() {
     51         MockitoAnnotations.initMocks(this);
     52         mContext = RuntimeEnvironment.application;
     53         mController = new ShowTapsPreferenceController(mContext);
     54         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
     55         mController.displayPreference(mScreen);
     56     }
     57 
     58     @Test
     59     public void updateState_showTapsEnabled_shouldCheckedPreference() {
     60         Settings.System.putInt(mContext.getContentResolver(),
     61                 Settings.System.SHOW_TOUCHES, ShowTapsPreferenceController.SETTING_VALUE_ON);
     62 
     63         mController.updateState(mPreference);
     64 
     65         verify(mPreference).setChecked(true);
     66     }
     67 
     68     @Test
     69     public void updateState_showTapsDisabled_shouldUncheckedPreference() {
     70         Settings.System.putInt(mContext.getContentResolver(),
     71                 Settings.System.SHOW_TOUCHES, ShowTapsPreferenceController.SETTING_VALUE_OFF);
     72 
     73         mController.updateState(mPreference);
     74 
     75         verify(mPreference).setChecked(false);
     76     }
     77 
     78     @Test
     79     public void onPreferenceChange_preferenceChecked_shouldEnableShowTaps() {
     80         mController.onPreferenceChange(mPreference, true /* new value */);
     81 
     82         final int showTapsMode = Settings.System.getInt(mContext.getContentResolver(),
     83                 Settings.System.SHOW_TOUCHES, -1 /* default */);
     84 
     85         assertThat(showTapsMode).isEqualTo(ShowTapsPreferenceController.SETTING_VALUE_ON);
     86     }
     87 
     88     @Test
     89     public void onPreferenceChange__preferenceUnchecked_shouldDisableShowTaps() {
     90         mController.onPreferenceChange(mPreference, false /* new value */);
     91 
     92         final int showTapsMode = Settings.System.getInt(mContext.getContentResolver(),
     93                 Settings.System.SHOW_TOUCHES, -1 /* default */);
     94 
     95         assertThat(showTapsMode).isEqualTo(ShowTapsPreferenceController.SETTING_VALUE_OFF);
     96     }
     97 
     98     @Test
     99     public void onDeveloperOptionsSwitchDisabled_preferenceShouldBeEnabled() {
    100         mController.onDeveloperOptionsSwitchDisabled();
    101 
    102         final int showTapsMode = Settings.System.getInt(mContext.getContentResolver(),
    103                 Settings.System.SHOW_TOUCHES, -1 /* default */);
    104 
    105         assertThat(showTapsMode).isEqualTo(ShowTapsPreferenceController.SETTING_VALUE_OFF);
    106         verify(mPreference).setEnabled(false);
    107         verify(mPreference).setChecked(false);
    108     }
    109 }
    110