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 android.content.Context;
     20 import android.content.SharedPreferences;
     21 import android.provider.Settings;
     22 
     23 import com.android.settings.SettingsRobolectricTestRunner;
     24 import com.android.settings.TestConfig;
     25 
     26 import org.junit.Before;
     27 import org.junit.Test;
     28 import org.junit.runner.RunWith;
     29 import org.mockito.Answers;
     30 import org.mockito.Mock;
     31 import org.mockito.MockitoAnnotations;
     32 import org.robolectric.RuntimeEnvironment;
     33 import org.robolectric.annotation.Config;
     34 import org.robolectric.util.ReflectionHelpers;
     35 
     36 import static com.google.common.truth.Truth.assertThat;
     37 
     38 @RunWith(SettingsRobolectricTestRunner.class)
     39 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     40 public class DevelopmentSettingsEnablerTest {
     41 
     42     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     43     private SharedPreferences mDevelopmentPreferences;
     44     private Context mContext;
     45     private DevelopmentSettingsEnabler mEnabler;
     46 
     47     @Before
     48     public void setUp() {
     49         MockitoAnnotations.initMocks(this);
     50         mContext = RuntimeEnvironment.application;
     51         mEnabler = new DevelopmentSettingsEnabler(mContext, null);
     52         ReflectionHelpers.setField(mEnabler, "mDevelopmentPreferences", mDevelopmentPreferences);
     53     }
     54 
     55     @Test
     56     public void onResume_shouldReadStateFromSettingProvider() {
     57         Settings.Global.putInt(mContext.getContentResolver(),
     58                 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
     59 
     60         mEnabler.onResume();
     61 
     62         assertThat(mEnabler.getLastEnabledState()).isTrue();
     63 
     64         Settings.Global.putInt(mContext.getContentResolver(),
     65                 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0);
     66 
     67         mEnabler.onResume();
     68 
     69         assertThat(mEnabler.getLastEnabledState()).isFalse();
     70     }
     71 
     72     @Test
     73     public void disable_shouldChangeSettingProviderValue() {
     74         Settings.Global.putInt(mContext.getContentResolver(),
     75                 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
     76 
     77         mEnabler.disableDevelopmentSettings();
     78 
     79         assertThat(mEnabler.getLastEnabledState()).isFalse();
     80         assertThat(Settings.Global.getInt(mContext.getContentResolver(),
     81                 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1)).isEqualTo(0);
     82     }
     83 
     84     @Test
     85     public void enable_shouldChangeSettingProviderValue() {
     86         Settings.Global.putInt(mContext.getContentResolver(),
     87                 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0);
     88 
     89         mEnabler.enableDevelopmentSettings();
     90 
     91         assertThat(mEnabler.getLastEnabledState()).isTrue();
     92         assertThat(Settings.Global.getInt(mContext.getContentResolver(),
     93                 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0)).isEqualTo(1);
     94     }
     95 }
     96