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
17package com.android.settings.notification;
18
19import android.app.Activity;
20import android.content.ContentResolver;
21import android.content.Context;
22import android.support.v7.preference.DropDownPreference;
23import android.support.v7.preference.PreferenceScreen;
24import android.provider.Settings.Global;
25
26import com.android.settings.testutils.SettingsRobolectricTestRunner;
27import com.android.settings.TestConfig;
28
29import org.junit.Before;
30import org.junit.Test;
31import org.junit.runner.RunWith;
32import org.mockito.Mock;
33import org.mockito.MockitoAnnotations;
34import org.robolectric.annotation.Config;
35import org.robolectric.shadows.ShadowApplication;
36
37import static com.google.common.truth.Truth.assertThat;
38import static org.mockito.Answers.RETURNS_DEEP_STUBS;
39import static org.mockito.Matchers.anyInt;
40import static org.mockito.Mockito.doReturn;
41import static org.mockito.Mockito.when;
42
43@RunWith(SettingsRobolectricTestRunner.class)
44@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
45public 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
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
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
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
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
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
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
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