• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.doReturn;
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.os.UserManager;
27 import android.provider.Settings;
28 import android.support.v14.preference.SwitchPreference;
29 import android.support.v7.preference.PreferenceScreen;
30 
31 import com.android.settings.testutils.SettingsRobolectricTestRunner;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.robolectric.RuntimeEnvironment;
39 
40 @RunWith(SettingsRobolectricTestRunner.class)
41 public class AdbPreferenceControllerTest {
42 
43     @Mock
44     private SwitchPreference mPreference;
45     @Mock
46     private PreferenceScreen mPreferenceScreen;
47     @Mock
48     private UserManager mUserManager;
49     @Mock
50     private DevelopmentSettingsDashboardFragment mFragment;
51 
52     private Context mContext;
53     private AdbPreferenceController mController;
54 
55     @Before
setup()56     public void setup() {
57         MockitoAnnotations.initMocks(this);
58         mContext = RuntimeEnvironment.application;
59         mController = spy(new AdbPreferenceController(mContext, mFragment));
60         doReturn(true).when(mController).isAvailable();
61         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
62             .thenReturn(mPreference);
63         mController.displayPreference(mPreferenceScreen);
64     }
65 
66     @Test
onDeveloperOptionsDisabled_shouldDisablePreference()67     public void onDeveloperOptionsDisabled_shouldDisablePreference() {
68         mController.onDeveloperOptionsDisabled();
69         final int mode = Settings.System.getInt(mContext.getContentResolver(),
70                 Settings.Global.ADB_ENABLED, -1);
71 
72         assertThat(mode).isEqualTo(AdbPreferenceController.ADB_SETTING_OFF);
73         verify(mPreference).setEnabled(false);
74         verify(mPreference).setChecked(false);
75     }
76 
77     @Test
onAdbDialogConfirmed_shouldEnableAdbSetting()78     public void onAdbDialogConfirmed_shouldEnableAdbSetting() {
79         mController.onAdbDialogConfirmed();
80         final int mode = Settings.System.getInt(mContext.getContentResolver(),
81                 Settings.Global.ADB_ENABLED, -1);
82 
83         assertThat(mode).isEqualTo(AdbPreferenceController.ADB_SETTING_ON);
84     }
85 
86     @Test
onAdbDialogDismissed_preferenceShouldNotBeChecked()87     public void onAdbDialogDismissed_preferenceShouldNotBeChecked() {
88         Settings.System.putInt(mContext.getContentResolver(), Settings.Global.ADB_ENABLED,
89                 AdbPreferenceController.ADB_SETTING_OFF);
90         mController.onAdbDialogDismissed();
91 
92         verify(mPreference).setChecked(false);
93     }
94 }
95