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.v14.preference.SwitchPreference;
23import android.support.v7.preference.PreferenceScreen;
24import android.provider.Settings.System;
25import android.telephony.TelephonyManager;
26
27import com.android.settings.SettingsRobolectricTestRunner;
28import com.android.settings.TestConfig;
29
30import org.junit.Before;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33import org.mockito.Mock;
34import org.mockito.MockitoAnnotations;
35import org.robolectric.annotation.Config;
36import org.robolectric.shadows.ShadowApplication;
37
38import static com.google.common.truth.Truth.assertThat;
39import static org.mockito.Mockito.doReturn;
40import static org.mockito.Mockito.when;
41
42@RunWith(SettingsRobolectricTestRunner.class)
43@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
44public class DialPadTonePreferenceControllerTest {
45
46    @Mock
47    private TelephonyManager mTelephonyManager;
48    @Mock
49    private PreferenceScreen mScreen;
50    @Mock
51    private Activity mActivity;
52    @Mock
53    private ContentResolver mContentResolver;
54    @Mock
55    private SoundSettings mSetting;
56    @Mock
57    private Context mContext;
58
59    private DialPadTonePreferenceController mController;
60    private SwitchPreference mPreference;
61
62    @Before
63    public void setUp() {
64        MockitoAnnotations.initMocks(this);
65        when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
66        when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
67        when(mSetting.getActivity()).thenReturn(mActivity);
68        when(mActivity.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
69        when(mActivity.getContentResolver()).thenReturn(mContentResolver);
70        mPreference = new SwitchPreference(ShadowApplication.getInstance().getApplicationContext());
71        mController = new DialPadTonePreferenceController(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_voiceCapable_shouldReturnTrue() {
78        assertThat(mController.isAvailable()).isTrue();
79    }
80
81    @Test
82    public void isAvailable_notVoiceCapable_shouldReturnFalse() {
83        when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
84
85        assertThat(mController.isAvailable()).isFalse();
86    }
87
88    @Test
89    public void displayPreference_dialToneEnabled_shouldCheckedPreference() {
90        System.putInt(mContentResolver, System.DTMF_TONE_WHEN_DIALING, 1);
91
92        mController.displayPreference(mScreen);
93
94        assertThat(mPreference.isChecked()).isTrue();
95    }
96
97    @Test
98    public void displayPreference_dialToneDisabled_shouldUncheckedPreference() {
99        System.putInt(mContentResolver, System.DTMF_TONE_WHEN_DIALING, 0);
100
101        mController.displayPreference(mScreen);
102
103        assertThat(mPreference.isChecked()).isFalse();
104    }
105
106    @Test
107    public void onPreferenceChanged_preferenceChecked_shouldEnabledDialTone() {
108        mController.displayPreference(mScreen);
109
110        mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, true);
111
112        assertThat(System.getInt(mContentResolver, System.DTMF_TONE_WHEN_DIALING, 1)).isEqualTo(1);
113    }
114
115    @Test
116    public void onPreferenceChanged_preferenceUnchecked_shouldDisabledDialTone() {
117        mController.displayPreference(mScreen);
118
119        mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, false);
120
121        assertThat(System.getInt(mContentResolver, System.DTMF_TONE_WHEN_DIALING, 1)).isEqualTo(0);
122    }
123}
124