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.Global;
25
26import com.android.settings.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.Mockito.doReturn;
39import static org.mockito.Mockito.when;
40
41@RunWith(SettingsRobolectricTestRunner.class)
42@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
43public class ChargingSoundPreferenceControllerTest {
44
45    @Mock
46    private PreferenceScreen mScreen;
47    @Mock
48    private Activity mActivity;
49    @Mock
50    private ContentResolver mContentResolver;
51    @Mock
52    private SoundSettings mSetting;
53    @Mock
54    private Context mContext;
55
56    private ChargingSoundPreferenceController mController;
57    private SwitchPreference mPreference;
58
59    @Before
60    public void setUp() {
61        MockitoAnnotations.initMocks(this);
62        when(mSetting.getActivity()).thenReturn(mActivity);
63        when(mActivity.getContentResolver()).thenReturn(mContentResolver);
64        mPreference = new SwitchPreference(ShadowApplication.getInstance().getApplicationContext());
65        mController = new ChargingSoundPreferenceController(mContext, mSetting, null);
66        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
67        doReturn(mScreen).when(mSetting).getPreferenceScreen();
68    }
69
70    @Test
71    public void isAvailable_isAlwaysTrue() {
72        assertThat(mController.isAvailable()).isTrue();
73    }
74
75    @Test
76    public void displayPreference_chargingSoundEnabled_shouldCheckedPreference() {
77        Global.putInt(mContentResolver, Global.CHARGING_SOUNDS_ENABLED, 1);
78
79        mController.displayPreference(mScreen);
80
81        assertThat(mPreference.isChecked()).isTrue();
82    }
83
84    @Test
85    public void displayPreference_chargingSoundDisabled_shouldUncheckedPreference() {
86        Global.putInt(mContentResolver, Global.CHARGING_SOUNDS_ENABLED, 0);
87
88        mController.displayPreference(mScreen);
89
90        assertThat(mPreference.isChecked()).isFalse();
91    }
92
93    @Test
94    public void onPreferenceChanged_preferenceChecked_shouldEnabledChargingSound() {
95        mController.displayPreference(mScreen);
96
97        mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, true);
98
99        assertThat(Global.getInt(mContentResolver, Global.CHARGING_SOUNDS_ENABLED, 1))
100            .isEqualTo(1);
101    }
102
103    @Test
104    public void onPreferenceChanged_preferenceUnchecked_shouldDisabledChargingSound() {
105        mController.displayPreference(mScreen);
106
107        mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, false);
108
109        assertThat(Global.getInt(mContentResolver, Global.CHARGING_SOUNDS_ENABLED, 1))
110            .isEqualTo(0);
111    }
112}
113