Home | History | Annotate | Download | only in accessibility
      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.accessibility;
     18 
     19 import static com.android.settings.accessibility.MagnificationPreferenceFragment.OFF;
     20 import static com.android.settings.accessibility.MagnificationPreferenceFragment.ON;
     21 import static com.android.settings.search.ResultPayload.Availability.AVAILABLE;
     22 
     23 import static com.google.common.truth.Truth.assertThat;
     24 
     25 import static org.mockito.Mockito.spy;
     26 
     27 import android.content.Context;
     28 import android.content.res.Resources;
     29 import android.provider.Settings;
     30 import android.support.v7.preference.Preference;
     31 
     32 import com.android.settings.R;
     33 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     34 
     35 import org.junit.After;
     36 import org.junit.Before;
     37 import org.junit.Test;
     38 import org.junit.runner.RunWith;
     39 import org.mockito.MockitoAnnotations;
     40 import org.robolectric.RuntimeEnvironment;
     41 import org.robolectric.annotation.Config;
     42 import org.robolectric.annotation.Implementation;
     43 import org.robolectric.annotation.Implements;
     44 import org.robolectric.annotation.Resetter;
     45 
     46 @RunWith(SettingsRobolectricTestRunner.class)
     47 public class MagnificationNavbarPreferenceControllerTest {
     48 
     49     private Context mContext;
     50     private MagnificationNavbarPreferenceController mController;
     51     private Preference mPreference;
     52 
     53     @Before
     54     public void setUp() {
     55         MockitoAnnotations.initMocks(this);
     56         mContext = spy(RuntimeEnvironment.application);
     57         mController = new MagnificationNavbarPreferenceController(mContext, "test_key");
     58         mPreference = new Preference(mContext);
     59         mController.updateState(mPreference);
     60     }
     61 
     62     @After
     63     public void tearDown() {
     64         ShadowMagnificationPreferenceFragment.reset();
     65     }
     66 
     67     @Test
     68     @Config(shadows = ShadowMagnificationPreferenceFragment.class)
     69     public void isAvailable_unsupported_shouldNotBeAvailable() {
     70         ShadowMagnificationPreferenceFragment.setApplicable(false);
     71 
     72         assertThat(mController.getAvailabilityStatus())
     73                 .isNotEqualTo(AVAILABLE);
     74     }
     75 
     76     @Test
     77     @Config(shadows = ShadowMagnificationPreferenceFragment.class)
     78     public void isAvailable_supported_shouldBeAvailable() {
     79         ShadowMagnificationPreferenceFragment.setApplicable(true);
     80 
     81         assertThat(mController.getAvailabilityStatus())
     82                 .isEqualTo(AVAILABLE);
     83     }
     84 
     85     @Test
     86     public void updateState_shouldRefreshSummary() {
     87         Settings.System.putInt(mContext.getContentResolver(),
     88                 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED, ON);
     89         mController.updateState(mPreference);
     90         assertThat(mPreference.getSummary())
     91                 .isEqualTo(mContext.getText(R.string.accessibility_feature_state_on));
     92 
     93         Settings.System.putInt(mContext.getContentResolver(),
     94                 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED, OFF);
     95         mController.updateState(mPreference);
     96         assertThat(mPreference.getSummary())
     97                 .isEqualTo(mContext.getText(R.string.accessibility_feature_state_off));
     98     }
     99 
    100     @Test
    101     public void updateState_shouldRefreshSummarySuw() {
    102         mController.setIsFromSUW(true);
    103         mController.updateState(mPreference);
    104         assertThat(mPreference.getSummary())
    105                 .isEqualTo(mContext.getString(R.string.
    106                         accessibility_screen_magnification_navbar_short_summary));
    107     }
    108 
    109     @Test
    110     public void isChecked_enabled() {
    111         Settings.System.putInt(mContext.getContentResolver(),
    112                 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED, ON);
    113 
    114         assertThat(mController.isChecked()).isTrue();
    115     }
    116 
    117     @Test
    118     public void isChecked_disabled() {
    119         Settings.System.putInt(mContext.getContentResolver(),
    120                 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED, OFF);
    121 
    122         assertThat(mController.isChecked()).isFalse();
    123     }
    124 
    125     @Test
    126     public void setChecked_enabled() {
    127         mController.setChecked(true);
    128 
    129         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
    130                 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED, -1))
    131                 .isEqualTo(ON);
    132     }
    133 
    134     @Test
    135     public void setChecked_disabled() {
    136         mController.setChecked(false);
    137 
    138         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
    139                 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED, -1))
    140                 .isEqualTo(OFF);
    141     }
    142 
    143     @Implements(MagnificationPreferenceFragment.class)
    144     public static class ShadowMagnificationPreferenceFragment {
    145         private static boolean sIsApplicable;
    146 
    147         @Resetter
    148         static void reset() {
    149             sIsApplicable = false;
    150         }
    151 
    152         @Implementation
    153         static boolean isApplicable(Resources res) {
    154             return sIsApplicable;
    155         }
    156 
    157         static void setApplicable(boolean applicable) {
    158             sIsApplicable = applicable;
    159         }
    160     }
    161 
    162     @Test
    163     public void isSliceableCorrectKey_returnsTrue() {
    164         final MagnificationNavbarPreferenceController controller =
    165                 new MagnificationNavbarPreferenceController(mContext,
    166                         "screen_magnification_navbar_preference_screen");
    167         assertThat(controller.isSliceable()).isTrue();
    168     }
    169 
    170     @Test
    171     public void isSliceableIncorrectKey_returnsFalse() {
    172         final MagnificationNavbarPreferenceController controller =
    173                 new MagnificationNavbarPreferenceController(mContext, "bad_key");
    174         assertThat(controller.isSliceable()).isFalse();
    175     }
    176 }
    177