• 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.anyInt;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26 
27 import android.app.admin.DevicePolicyManager;
28 import android.content.ContentResolver;
29 import android.content.Context;
30 import android.provider.Settings.Global;
31 
32 import androidx.fragment.app.FragmentActivity;
33 import androidx.preference.DropDownPreference;
34 import androidx.preference.PreferenceScreen;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.RuntimeEnvironment;
43 
44 @RunWith(RobolectricTestRunner.class)
45 public class DockAudioMediaPreferenceControllerTest {
46 
47     @Mock
48     private PreferenceScreen mScreen;
49     @Mock(answer = RETURNS_DEEP_STUBS)
50     private FragmentActivity mActivity;
51     @Mock
52     private ContentResolver mContentResolver;
53     @Mock
54     private SoundSettings mSetting;
55     @Mock(answer = RETURNS_DEEP_STUBS)
56     private Context mContext;
57 
58     private DockAudioMediaPreferenceController mController;
59     private DropDownPreference mPreference;
60 
61     @Before
setUp()62     public void setUp() {
63         MockitoAnnotations.initMocks(this);
64         doReturn(mock(DevicePolicyManager.class)).when(mContext)
65                 .getSystemService(Context.DEVICE_POLICY_SERVICE);
66         when(mSetting.getActivity()).thenReturn(mActivity);
67         when(mActivity.getContentResolver()).thenReturn(mContentResolver);
68         when(mActivity.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
69             .thenReturn(true);
70         when(mActivity.getResources().getString(anyInt())).thenReturn("test string");
71         mPreference = new DropDownPreference(RuntimeEnvironment.application);
72         mController = new DockAudioMediaPreferenceController(mContext, mSetting, null);
73         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
74         doReturn(mScreen).when(mSetting).getPreferenceScreen();
75     }
76 
77     @Test
isAvailable_hasDockSettings_shouldReturnTrue()78     public void isAvailable_hasDockSettings_shouldReturnTrue() {
79         when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
80             .thenReturn(true);
81 
82         assertThat(mController.isAvailable()).isTrue();
83     }
84 
85     @Test
isAvailable_noDockSettings_shouldReturnFalse()86     public void isAvailable_noDockSettings_shouldReturnFalse() {
87         when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
88             .thenReturn(false);
89 
90         assertThat(mController.isAvailable()).isFalse();
91     }
92 
93     @Test
displayPreference_dockAudioDisabled_shouldSelectFirstItem()94     public void displayPreference_dockAudioDisabled_shouldSelectFirstItem() {
95         Global.putInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 0);
96 
97         mController.displayPreference(mScreen);
98 
99         assertThat(mPreference.getValue()).isEqualTo("0");
100     }
101 
102     @Test
displayPreference_dockAudioEnabled_shouldSelectSecondItem()103     public void displayPreference_dockAudioEnabled_shouldSelectSecondItem() {
104         Global.putInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 1);
105 
106         mController.displayPreference(mScreen);
107 
108         assertThat(mPreference.getValue()).isEqualTo("1");
109     }
110 
111     @Test
onPreferenceChanged_firstItemSelected_shouldDisableDockAudio()112     public void onPreferenceChanged_firstItemSelected_shouldDisableDockAudio() {
113         mController.displayPreference(mScreen);
114 
115         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, "0");
116 
117         assertThat(Global.getInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 0))
118             .isEqualTo(0);
119     }
120 
121     @Test
onPreferenceChanged_secondItemSelected_shouldEnableDockAudio()122     public void onPreferenceChanged_secondItemSelected_shouldEnableDockAudio() {
123         mController.displayPreference(mScreen);
124 
125         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, "1");
126 
127         assertThat(Global.getInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 0))
128             .isEqualTo(1);
129     }
130 }
131