106976317cb7365affab2e991c1f3456738919098Matthew Fritze/*
206976317cb7365affab2e991c1f3456738919098Matthew Fritze * Copyright (C) 2017 The Android Open Source Project
306976317cb7365affab2e991c1f3456738919098Matthew Fritze *
406976317cb7365affab2e991c1f3456738919098Matthew Fritze * Licensed under the Apache License, Version 2.0 (the "License");
506976317cb7365affab2e991c1f3456738919098Matthew Fritze * you may not use this file except in compliance with the License.
606976317cb7365affab2e991c1f3456738919098Matthew Fritze * You may obtain a copy of the License at
706976317cb7365affab2e991c1f3456738919098Matthew Fritze *
806976317cb7365affab2e991c1f3456738919098Matthew Fritze *      http://www.apache.org/licenses/LICENSE-2.0
906976317cb7365affab2e991c1f3456738919098Matthew Fritze *
1006976317cb7365affab2e991c1f3456738919098Matthew Fritze * Unless required by applicable law or agreed to in writing, software
1106976317cb7365affab2e991c1f3456738919098Matthew Fritze * distributed under the License is distributed on an "AS IS" BASIS,
1206976317cb7365affab2e991c1f3456738919098Matthew Fritze * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1306976317cb7365affab2e991c1f3456738919098Matthew Fritze * See the License for the specific language governing permissions and
1406976317cb7365affab2e991c1f3456738919098Matthew Fritze * limitations under the License.
1506976317cb7365affab2e991c1f3456738919098Matthew Fritze */
1606976317cb7365affab2e991c1f3456738919098Matthew Fritze
1706976317cb7365affab2e991c1f3456738919098Matthew Fritzepackage com.android.settings.notification;
1806976317cb7365affab2e991c1f3456738919098Matthew Fritze
1922a39c2b93bc66db71238274a7683d329232d124James Lemieuximport static com.google.common.truth.Truth.assertThat;
2022a39c2b93bc66db71238274a7683d329232d124James Lemieuximport static org.mockito.Mockito.doReturn;
2122a39c2b93bc66db71238274a7683d329232d124James Lemieuximport static org.mockito.Mockito.mock;
2222a39c2b93bc66db71238274a7683d329232d124James Lemieuximport static org.mockito.Mockito.spy;
2322a39c2b93bc66db71238274a7683d329232d124James Lemieuximport static org.mockito.Mockito.when;
2406976317cb7365affab2e991c1f3456738919098Matthew Fritze
2522a39c2b93bc66db71238274a7683d329232d124James Lemieuximport android.content.Context;
2606976317cb7365affab2e991c1f3456738919098Matthew Fritzeimport android.media.AudioManager;
27de08eaf43748255d1e778e6648dc442fb6202a12Doris Lingimport android.os.Handler;
2806976317cb7365affab2e991c1f3456738919098Matthew Fritzeimport android.os.UserManager;
2922a39c2b93bc66db71238274a7683d329232d124James Lemieux
30de08eaf43748255d1e778e6648dc442fb6202a12Doris Lingimport android.preference.SeekBarVolumizer;
3106976317cb7365affab2e991c1f3456738919098Matthew Fritzeimport com.android.settings.R;
329f1e911759dc6fedaac9fa65afb79f6a93022bf4Andrew Sappersteinimport com.android.settings.testutils.SettingsRobolectricTestRunner;
3306976317cb7365affab2e991c1f3456738919098Matthew Fritzeimport com.android.settings.testutils.XmlTestUtils;
3406976317cb7365affab2e991c1f3456738919098Matthew Fritzeimport com.android.settings.testutils.shadow.ShadowAudioHelper;
3506976317cb7365affab2e991c1f3456738919098Matthew Fritzeimport com.android.settings.testutils.shadow.ShadowUserManager;
3622a39c2b93bc66db71238274a7683d329232d124James Lemieux
3706976317cb7365affab2e991c1f3456738919098Matthew Fritzeimport org.junit.Test;
3806976317cb7365affab2e991c1f3456738919098Matthew Fritzeimport org.junit.runner.RunWith;
3906976317cb7365affab2e991c1f3456738919098Matthew Fritzeimport org.robolectric.RuntimeEnvironment;
4006976317cb7365affab2e991c1f3456738919098Matthew Fritzeimport org.robolectric.annotation.Config;
41de08eaf43748255d1e778e6648dc442fb6202a12Doris Lingimport org.robolectric.util.ReflectionHelpers;
4206976317cb7365affab2e991c1f3456738919098Matthew Fritze
4306976317cb7365affab2e991c1f3456738919098Matthew Fritzeimport java.util.List;
4406976317cb7365affab2e991c1f3456738919098Matthew Fritze
4506976317cb7365affab2e991c1f3456738919098Matthew Fritze@RunWith(SettingsRobolectricTestRunner.class)
4606976317cb7365affab2e991c1f3456738919098Matthew Fritzepublic class SoundSettingsTest {
4706976317cb7365affab2e991c1f3456738919098Matthew Fritze
4806976317cb7365affab2e991c1f3456738919098Matthew Fritze    @Test
4922a39c2b93bc66db71238274a7683d329232d124James Lemieux    @Config(shadows = {ShadowUserManager.class, ShadowAudioHelper.class})
50de08eaf43748255d1e778e6648dc442fb6202a12Doris Ling    public void getNonIndexableKeys_existInXmlLayout() {
5106976317cb7365affab2e991c1f3456738919098Matthew Fritze        final Context context = spy(RuntimeEnvironment.application);
5206976317cb7365affab2e991c1f3456738919098Matthew Fritze        AudioManager audioManager = mock(AudioManager.class);
5306976317cb7365affab2e991c1f3456738919098Matthew Fritze        doReturn(audioManager).when(context).getSystemService(Context.AUDIO_SERVICE);
5406976317cb7365affab2e991c1f3456738919098Matthew Fritze
5506976317cb7365affab2e991c1f3456738919098Matthew Fritze        UserManager userManager = mock(UserManager.class);
5606976317cb7365affab2e991c1f3456738919098Matthew Fritze        when(userManager.isAdminUser()).thenReturn(false);
5706976317cb7365affab2e991c1f3456738919098Matthew Fritze        doReturn(userManager).when(context).getSystemService(Context.USER_SERVICE);
5806976317cb7365affab2e991c1f3456738919098Matthew Fritze
5922a39c2b93bc66db71238274a7683d329232d124James Lemieux        final List<String> niks =
6022a39c2b93bc66db71238274a7683d329232d124James Lemieux            SoundSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(context);
616f60cddc9e4f9dfd932181765d6b75b2040b0ad7Doris Ling        SoundSettings settings = new SoundSettings();
626f60cddc9e4f9dfd932181765d6b75b2040b0ad7Doris Ling        final int xmlId = settings.getPreferenceScreenResId();
6306976317cb7365affab2e991c1f3456738919098Matthew Fritze        final List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(context, xmlId);
6422a39c2b93bc66db71238274a7683d329232d124James Lemieux        keys.addAll(XmlTestUtils.getKeysFromPreferenceXml(context, R.xml.zen_mode_settings));
6506976317cb7365affab2e991c1f3456738919098Matthew Fritze        // Add keys with hidden resources
6606976317cb7365affab2e991c1f3456738919098Matthew Fritze        keys.add("alarm_volume");
6706976317cb7365affab2e991c1f3456738919098Matthew Fritze        keys.add("ring_volume");
6806976317cb7365affab2e991c1f3456738919098Matthew Fritze        keys.add("notification_volume");
6906976317cb7365affab2e991c1f3456738919098Matthew Fritze
7006976317cb7365affab2e991c1f3456738919098Matthew Fritze        assertThat(keys).containsAllIn(niks);
7106976317cb7365affab2e991c1f3456738919098Matthew Fritze    }
72de08eaf43748255d1e778e6648dc442fb6202a12Doris Ling
73de08eaf43748255d1e778e6648dc442fb6202a12Doris Ling    @Test
74de08eaf43748255d1e778e6648dc442fb6202a12Doris Ling    public void onStreamValueChanged_shouldRepostStopSampleMessage() {
75de08eaf43748255d1e778e6648dc442fb6202a12Doris Ling        final SoundSettings settings = new SoundSettings();
76de08eaf43748255d1e778e6648dc442fb6202a12Doris Ling        final Handler handler = settings.mHandler;
77de08eaf43748255d1e778e6648dc442fb6202a12Doris Ling        ReflectionHelpers.setField(
78de08eaf43748255d1e778e6648dc442fb6202a12Doris Ling                settings.mVolumeCallback, "mCurrent", mock(SeekBarVolumizer.class));
79de08eaf43748255d1e778e6648dc442fb6202a12Doris Ling
80de08eaf43748255d1e778e6648dc442fb6202a12Doris Ling        settings.mVolumeCallback.onStreamValueChanged(0, 5);
81de08eaf43748255d1e778e6648dc442fb6202a12Doris Ling
82de08eaf43748255d1e778e6648dc442fb6202a12Doris Ling        assertThat(settings.mHandler.hasMessages(SoundSettings.STOP_SAMPLE)).isTrue();
83de08eaf43748255d1e778e6648dc442fb6202a12Doris Ling    }
84de08eaf43748255d1e778e6648dc442fb6202a12Doris Ling}
85