• 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 android.app.Activity;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.support.v7.preference.DropDownPreference;
23 import android.support.v7.preference.PreferenceScreen;
24 import android.provider.Settings.Global;
25 
26 import com.android.settings.SettingsRobolectricTestRunner;
27 import com.android.settings.TestConfig;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.robolectric.annotation.Config;
35 import org.robolectric.shadows.ShadowApplication;
36 
37 import static com.google.common.truth.Truth.assertThat;
38 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
39 import static org.mockito.Matchers.anyInt;
40 import static org.mockito.Mockito.doReturn;
41 import static org.mockito.Mockito.when;
42 
43 @RunWith(SettingsRobolectricTestRunner.class)
44 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
45 public class DockAudioMediaPreferenceControllerTest {
46 
47     @Mock
48     private PreferenceScreen mScreen;
49     @Mock(answer = RETURNS_DEEP_STUBS)
50     private Activity 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         final Context appContext = ShadowApplication.getInstance().getApplicationContext();
65         when(mSetting.getActivity()).thenReturn(mActivity);
66         when(mActivity.getContentResolver()).thenReturn(mContentResolver);
67         when(mActivity.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
68             .thenReturn(true);
69         when(mActivity.getResources().getString(anyInt())).thenReturn("test string");
70         mPreference = new DropDownPreference(appContext);
71         mController = new DockAudioMediaPreferenceController(mContext, mSetting, null);
72         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
73         doReturn(mScreen).when(mSetting).getPreferenceScreen();
74     }
75 
76     @Test
isAvailable_hasDockSettings_shouldReturnTrue()77     public void isAvailable_hasDockSettings_shouldReturnTrue() {
78         when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
79             .thenReturn(true);
80 
81         assertThat(mController.isAvailable()).isTrue();
82     }
83 
84     @Test
isAvailable_noDockSettings_shouldReturnFalse()85     public void isAvailable_noDockSettings_shouldReturnFalse() {
86         when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings))
87             .thenReturn(false);
88 
89         assertThat(mController.isAvailable()).isFalse();
90     }
91 
92     @Test
displayPreference_dockAudioDisabled_shouldSelectFirstItem()93     public void displayPreference_dockAudioDisabled_shouldSelectFirstItem() {
94         Global.putInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 0);
95 
96         mController.displayPreference(mScreen);
97 
98         assertThat(mPreference.getValue()).isEqualTo("0");
99     }
100 
101     @Test
displayPreference_dockAudioEnabled_shouldSelectSecondItem()102     public void displayPreference_dockAudioEnabled_shouldSelectSecondItem() {
103         Global.putInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 1);
104 
105         mController.displayPreference(mScreen);
106 
107         assertThat(mPreference.getValue()).isEqualTo("1");
108     }
109 
110     @Test
onPreferenceChanged_firstItemSelected_shouldDisableDockAudio()111     public void onPreferenceChanged_firstItemSelected_shouldDisableDockAudio() {
112         mController.displayPreference(mScreen);
113 
114         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, "0");
115 
116         assertThat(Global.getInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 0))
117             .isEqualTo(0);
118     }
119 
120     @Test
onPreferenceChanged_secondItemSelected_shouldEnableDockAudio()121     public void onPreferenceChanged_secondItemSelected_shouldEnableDockAudio() {
122         mController.displayPreference(mScreen);
123 
124         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, "1");
125 
126         assertThat(Global.getInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 0))
127             .isEqualTo(1);
128     }
129 }
130