• 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.notification;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.ArgumentMatchers.anyInt;
24 import static org.mockito.ArgumentMatchers.nullable;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28 
29 import android.app.admin.DevicePolicyManager;
30 import android.content.BroadcastReceiver;
31 import android.content.ContentResolver;
32 import android.content.Context;
33 import android.content.Intent;
34 import android.content.IntentFilter;
35 import android.provider.Settings.Global;
36 
37 import androidx.fragment.app.FragmentActivity;
38 import androidx.preference.DropDownPreference;
39 import androidx.preference.PreferenceScreen;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.RobolectricTestRunner;
47 import org.robolectric.RuntimeEnvironment;
48 import org.robolectric.annotation.Config;
49 
50 @RunWith(RobolectricTestRunner.class)
51 @Config(shadows = {
52         com.android.settings.testutils.shadow.ShadowFragment.class,
53 })
54 public class DockAudioMediaPreferenceControllerTest {
55 
56     @Mock
57     private PreferenceScreen mScreen;
58     @Mock(answer = RETURNS_DEEP_STUBS)
59     private FragmentActivity mActivity;
60     @Mock
61     private ContentResolver mContentResolver;
62     @Mock
63     private SoundSettings mSetting;
64     @Mock(answer = RETURNS_DEEP_STUBS)
65     private Context mContext;
66 
67     private DockAudioMediaPreferenceController mController;
68     private DropDownPreference mPreference;
69 
70     @Before
setUp()71     public void setUp() {
72         MockitoAnnotations.initMocks(this);
73         doReturn(mock(DevicePolicyManager.class)).when(mContext)
74                 .getSystemService(Context.DEVICE_POLICY_SERVICE);
75         when(mSetting.getActivity()).thenReturn(mActivity);
76         when(mActivity.getContentResolver()).thenReturn(mContentResolver);
77         when(mActivity.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
78             .thenReturn(true);
79         when(mActivity.getResources().getString(anyInt())).thenReturn("test string");
80         mPreference = new DropDownPreference(RuntimeEnvironment.application);
81         mController = new DockAudioMediaPreferenceController(mContext, mSetting, null);
82         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
83         doReturn(mScreen).when(mSetting).getPreferenceScreen();
84         fakeDockState(Intent.EXTRA_DOCK_STATE_LE_DESK);
85     }
86 
87     @Test
isAvailable_hasDockSettings_shouldReturnTrue()88     public void isAvailable_hasDockSettings_shouldReturnTrue() {
89         when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
90             .thenReturn(true);
91 
92         assertThat(mController.isAvailable()).isTrue();
93     }
94 
95     @Test
isAvailable_noDockSettings_shouldReturnFalse()96     public void isAvailable_noDockSettings_shouldReturnFalse() {
97         when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
98             .thenReturn(false);
99 
100         assertThat(mController.isAvailable()).isFalse();
101     }
102 
103     @Test
isAvailable_undocked_shouldReturnFalse()104     public void isAvailable_undocked_shouldReturnFalse() {
105         when(mContext.registerReceiver(nullable(BroadcastReceiver.class),
106             any(IntentFilter.class))).thenReturn(null);
107         when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
108             .thenReturn(true);
109 
110         assertThat(mController.isAvailable()).isFalse();
111     }
112 
113     @Test
isAvailable_highEndDock_shouldReturnFalse()114     public void isAvailable_highEndDock_shouldReturnFalse() {
115         fakeDockState(Intent.EXTRA_DOCK_STATE_HE_DESK);
116         when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
117             .thenReturn(true);
118 
119         assertThat(mController.isAvailable()).isFalse();
120     }
121 
122     @Test
isAvailable_lowEndDock_shouldReturnTrue()123     public void isAvailable_lowEndDock_shouldReturnTrue() {
124         fakeDockState(Intent.EXTRA_DOCK_STATE_LE_DESK);
125         when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
126             .thenReturn(true);
127 
128         assertThat(mController.isAvailable()).isTrue();
129     }
130 
131     @Test
displayPreference_dockAudioDisabled_shouldSelectFirstItem()132     public void displayPreference_dockAudioDisabled_shouldSelectFirstItem() {
133         Global.putInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 0);
134 
135         mController.displayPreference(mScreen);
136 
137         assertThat(mPreference.getValue()).isEqualTo("0");
138     }
139 
140     @Test
displayPreference_dockAudioEnabled_shouldSelectSecondItem()141     public void displayPreference_dockAudioEnabled_shouldSelectSecondItem() {
142         Global.putInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 1);
143 
144         mController.displayPreference(mScreen);
145 
146         assertThat(mPreference.getValue()).isEqualTo("1");
147     }
148 
149     @Test
onPreferenceChanged_firstItemSelected_shouldDisableDockAudio()150     public void onPreferenceChanged_firstItemSelected_shouldDisableDockAudio() {
151         mController.displayPreference(mScreen);
152 
153         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, "0");
154 
155         assertThat(Global.getInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 0))
156             .isEqualTo(0);
157     }
158 
159     @Test
onPreferenceChanged_secondItemSelected_shouldEnableDockAudio()160     public void onPreferenceChanged_secondItemSelected_shouldEnableDockAudio() {
161         mController.displayPreference(mScreen);
162 
163         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, "1");
164 
165         assertThat(Global.getInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 0))
166             .isEqualTo(1);
167     }
168 
fakeDockState(int dockState)169     private void fakeDockState(int dockState) {
170         Intent intent = new Intent(Intent.ACTION_DOCK_EVENT);
171         intent.putExtra(Intent.EXTRA_DOCK_STATE, dockState);
172         when(mContext.registerReceiver(nullable(BroadcastReceiver.class),
173             any(IntentFilter.class))).thenReturn(intent);
174     }
175 }
176