Home | History | Annotate | Download | only in display
      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.display;
     18 
     19 import android.content.ContentResolver;
     20 import android.content.Context;
     21 import android.content.pm.PackageManager;
     22 import android.os.UserHandle;
     23 import android.provider.Settings;
     24 import android.support.v14.preference.SwitchPreference;
     25 
     26 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     27 import com.android.settings.TestConfig;
     28 import com.android.settings.testutils.FakeFeatureFactory;
     29 import com.android.settings.testutils.shadow.ShadowSystemSettings;
     30 import com.android.settingslib.core.lifecycle.Lifecycle;
     31 
     32 import org.junit.After;
     33 import org.junit.Before;
     34 import org.junit.Test;
     35 import org.junit.runner.RunWith;
     36 import org.mockito.Answers;
     37 import org.mockito.Mock;
     38 import org.mockito.MockitoAnnotations;
     39 import org.robolectric.RuntimeEnvironment;
     40 import org.robolectric.annotation.Config;
     41 
     42 import static com.google.common.truth.Truth.assertThat;
     43 import static org.mockito.Matchers.anyInt;
     44 import static org.mockito.Matchers.anyString;
     45 import static org.mockito.Mockito.when;
     46 
     47 @RunWith(SettingsRobolectricTestRunner.class)
     48 @Config(
     49     manifest = TestConfig.MANIFEST_PATH,
     50     sdk = TestConfig.SDK_VERSION,
     51     shadows = ShadowSystemSettings.class
     52 )
     53 public class AutoRotatePreferenceControllerTest {
     54 
     55     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     56     private Context mContext;
     57     @Mock
     58     private PackageManager mPackageManager;
     59     private Lifecycle mLifecycle;
     60     private SwitchPreference mPreference;
     61     private ContentResolver mContentResolver;
     62     private AutoRotatePreferenceController mController;
     63 
     64     @Before
     65     public void setUp() {
     66         MockitoAnnotations.initMocks(this);
     67         FakeFeatureFactory.setupForTest(mContext);
     68         mContentResolver = RuntimeEnvironment.application.getContentResolver();
     69         mLifecycle = new Lifecycle();
     70         mPreference = new SwitchPreference(RuntimeEnvironment.application);
     71         when(mContext.getPackageManager()).thenReturn(mPackageManager);
     72         when(mContext.getContentResolver()).thenReturn(mContentResolver);
     73 
     74         mController = new AutoRotatePreferenceController(mContext, mLifecycle);
     75     }
     76 
     77     @After
     78     public void tearDown() {
     79         ShadowSystemSettings.reset();
     80     }
     81 
     82     @Test
     83     public void isAvailableWhenPolicyAllows() {
     84         assertThat(mController.isAvailable()).isFalse();
     85 
     86         when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
     87         when(mContext.getResources().getBoolean(anyInt())).thenReturn(true);
     88         Settings.System.putInt(mContentResolver,
     89                 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0);
     90 
     91         assertThat(mController.isAvailable()).isTrue();
     92     }
     93 
     94     @Test
     95     public void updatePreference_settingsIsOff_shouldTurnOffToggle() {
     96         Settings.System.putIntForUser(mContentResolver,
     97                 Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT);
     98 
     99         mController.updateState(mPreference);
    100 
    101         assertThat(mPreference.isChecked()).isFalse();
    102     }
    103 
    104     @Test
    105     public void updatePreference_settingsIsOn_shouldTurnOnToggle() {
    106         Settings.System.putIntForUser(mContentResolver,
    107                 Settings.System.ACCELEROMETER_ROTATION, 1, UserHandle.USER_CURRENT);
    108 
    109         mController.updateState(mPreference);
    110 
    111         assertThat(mPreference.isChecked()).isTrue();
    112     }
    113 }
    114