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.Mockito.doReturn;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 import android.media.AudioManager;
28 import android.os.UserManager;
29 import android.preference.SeekBarVolumizer;
30 
31 import com.android.settings.R;
32 import com.android.settings.testutils.XmlTestUtils;
33 import com.android.settings.testutils.shadow.ShadowAudioHelper;
34 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
35 import com.android.settings.testutils.shadow.ShadowDeviceConfig;
36 import com.android.settings.testutils.shadow.ShadowUserManager;
37 
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.robolectric.RobolectricTestRunner;
41 import org.robolectric.RuntimeEnvironment;
42 import org.robolectric.annotation.Config;
43 import org.robolectric.util.ReflectionHelpers;
44 
45 import java.util.List;
46 
47 @RunWith(RobolectricTestRunner.class)
48 public class SoundSettingsTest {
49 
50     @Test
51     @Config(shadows = {ShadowUserManager.class, ShadowAudioHelper.class, ShadowDeviceConfig.class,
52             ShadowBluetoothAdapter.class})
getNonIndexableKeys_existInXmlLayout()53     public void getNonIndexableKeys_existInXmlLayout() {
54         final Context context = spy(RuntimeEnvironment.application);
55         AudioManager audioManager = mock(AudioManager.class);
56         doReturn(audioManager).when(context).getSystemService(Context.AUDIO_SERVICE);
57 
58         UserManager userManager = mock(UserManager.class);
59         when(userManager.isAdminUser()).thenReturn(false);
60         doReturn(userManager).when(context).getSystemService(Context.USER_SERVICE);
61 
62         final List<String> niks =
63             SoundSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(context);
64         SoundSettings settings = new SoundSettings();
65         final int xmlId = settings.getPreferenceScreenResId();
66         final List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(context, xmlId);
67         keys.addAll(XmlTestUtils.getKeysFromPreferenceXml(context, R.xml.zen_mode_settings));
68         // Add keys with hidden resources
69         keys.add("alarm_volume");
70         keys.add("ring_volume");
71         keys.add("notification_volume");
72 
73         assertThat(keys).containsAtLeastElementsIn(niks);
74     }
75 
76     @Test
onStreamValueChanged_shouldRepostStopSampleMessage()77     public void onStreamValueChanged_shouldRepostStopSampleMessage() {
78         final SoundSettings settings = new SoundSettings();
79         ReflectionHelpers.setField(
80                 settings.mVolumeCallback, "mCurrent", mock(SeekBarVolumizer.class));
81 
82         settings.mVolumeCallback.onStreamValueChanged(0, 5);
83 
84         assertThat(settings.mHandler.hasMessages(SoundSettings.STOP_SAMPLE)).isTrue();
85     }
86 }
87