Update SoundSettings to extend DashboardFragment.

- initial round of refactoring SoundSettings to use DashboardFragment.
- add controller for Cast, Do not disturb, Alarm volume, Media volume,
  Ring volume and Notification volume.

Bug: 32276590
Test: make RunSettingsRoboTests
Change-Id: I5c02a344bff5117bfce93d7ccac650fccc82d2b0
This commit is contained in:
Doris Ling
2016-11-30 17:33:50 -08:00
parent 7aad8a97a4
commit 1b3ec04748
19 changed files with 1448 additions and 193 deletions

View File

@@ -0,0 +1,115 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification;
import android.content.Context;
import android.os.UserManager;
import android.support.v7.preference.Preference;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.accounts.AccountRestrictionHelper;
import com.android.settingslib.RestrictedPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class AdjustVolumeRestrictedPreferenceControllerTest {
@Mock
private AccountRestrictionHelper mAccountHelper;
private Context mContext;
private AdjustVolumeRestrictedPreferenceControllerTestable mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowContext = ShadowApplication.getInstance();
mContext = spy(shadowContext.getApplicationContext());
mController =
new AdjustVolumeRestrictedPreferenceControllerTestable(mContext, mAccountHelper);
}
@Test
public void updateState_hasBaseRestriction_shouldDisable() {
RestrictedPreference preference = mock(RestrictedPreference.class);
when(mAccountHelper.hasBaseUserRestriction(
eq(UserManager.DISALLOW_ADJUST_VOLUME), anyInt())).thenReturn(true);
mController.updateState(preference);
assertThat(preference.isEnabled()).isFalse();
}
@Test
public void updateState_NoBaseRestriction_shouldCheckRestriction() {
RestrictedPreference preference = spy(new RestrictedPreference(mContext));
when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn(null);
when(mAccountHelper.hasBaseUserRestriction(
eq(UserManager.DISALLOW_ADJUST_VOLUME), anyInt())).thenReturn(false);
doCallRealMethod().when(mAccountHelper).enforceRestrictionOnPreference(
eq(preference), eq(UserManager.DISALLOW_ADJUST_VOLUME), anyInt());
mController.updateState(preference);
verify(preference).checkRestrictionAndSetDisabled(
eq(UserManager.DISALLOW_ADJUST_VOLUME), anyInt());
}
private class AdjustVolumeRestrictedPreferenceControllerTestable extends
AdjustVolumeRestrictedPreferenceController {
AdjustVolumeRestrictedPreferenceControllerTestable(Context context,
AccountRestrictionHelper helper) {
super(context, helper);
}
@Override
public String getPreferenceKey() {
return null;
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
return false;
}
@Override
public boolean isAvailable() {
return true;
}
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification;
import android.content.Context;
import android.media.AudioManager;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class AlarmVolumePreferenceControllerTest {
@Mock
private Context mContext;
@Mock
private AudioHelper mHelper;
private AlarmVolumePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mController = new AlarmVolumePreferenceController(mContext, null, null, mHelper);
}
@Test
public void isAvailable_singleVolume_shouldReturnFalse() {
when(mHelper.isSingleVolume()).thenReturn(true);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_notSingleVolume_shouldReturnTrue() {
when(mHelper.isSingleVolume()).thenReturn(false);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void getAudioStream_shouldReturnAlarm() {
assertThat(mController.getAudioStream()).isEqualTo(AudioManager.STREAM_ALARM);
}
}

View File

@@ -0,0 +1,84 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification;
import android.content.Context;
import android.telephony.TelephonyManager;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class CastPreferenceControllerTest {
@Mock
private TelephonyManager mTelephonyManager;
private Context mContext;
private CastPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowContext = ShadowApplication.getInstance();
shadowContext.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager);
mContext = shadowContext.getApplicationContext();
mController = new CastPreferenceController(mContext);
}
@Test
public void isAlwaysAvailable() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void updateNonIndexableKeys_voiceCapable_shouldNotUpdate() {
when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
final List<String> keys = new ArrayList<>();
mController.updateNonIndexableKeys(keys);
assertThat(keys).isEmpty();
}
@Test
public void updateNonIndexableKeys_voiceIncapable_shouldUpdate() {
when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
final List<String> keys = new ArrayList<>();
mController.updateNonIndexableKeys(keys);
assertThat(keys).isNotEmpty();
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification;
import android.content.Context;
import android.media.AudioManager;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class MediaVolumePreferenceControllerTest {
@Mock
private Context mContext;
private MediaVolumePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mController = new MediaVolumePreferenceController(mContext, null, null);
}
@Test
public void isAlwaysAvailable() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void getAudioStream_shouldReturnMusic() {
assertThat(mController.getAudioStream()).isEqualTo(AudioManager.STREAM_MUSIC);
}
}

View File

@@ -0,0 +1,93 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification;
import android.content.Context;
import android.media.AudioManager;
import android.os.Vibrator;
import android.telephony.TelephonyManager;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class NotificationVolumePreferenceControllerTest {
@Mock
private Context mContext;
@Mock
private AudioHelper mHelper;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private AudioManager mAudioManager;
@Mock
private Vibrator mVibrator;
private NotificationVolumePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager);
when(mContext.getSystemService(Context.VIBRATOR_SERVICE)).thenReturn(mVibrator);
mController = new NotificationVolumePreferenceController(mContext, null, null, mHelper);
}
@Test
public void isAvailable_singleVolume_shouldReturnFalse() {
when(mHelper.isSingleVolume()).thenReturn(true);
when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_voiceCapable_shouldReturnFalse() {
when(mHelper.isSingleVolume()).thenReturn(false);
when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_notSingleVolume_notVoiceCapable_shouldReturnTrue() {
when(mHelper.isSingleVolume()).thenReturn(false);
when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void getAudioStream_shouldReturnNotification() {
assertThat(mController.getAudioStream()).isEqualTo(AudioManager.STREAM_NOTIFICATION);
}
}

View File

@@ -0,0 +1,104 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Vibrator;
import android.telephony.TelephonyManager;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class RingVolumePreferenceControllerTest {
@Mock
private AudioHelper mHelper;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private AudioManager mAudioManager;
@Mock
private Vibrator mVibrator;
@Mock
private NotificationManager mNotificationManager;
@Mock
private ComponentName mSuppressor;
private Context mContext;
private RingVolumePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowContext = ShadowApplication.getInstance();
shadowContext.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager);
shadowContext.setSystemService(Context.AUDIO_SERVICE, mAudioManager);
shadowContext.setSystemService(Context.VIBRATOR_SERVICE, mVibrator);
shadowContext.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = shadowContext.getApplicationContext();
when(mNotificationManager.getEffectsSuppressor()).thenReturn(mSuppressor);
mController = new RingVolumePreferenceController(mContext, null, null, mHelper);
}
@Test
public void isAvailable_singleVolume_shouldReturnFalse() {
when(mHelper.isSingleVolume()).thenReturn(true);
when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_notVoiceCapable_shouldReturnFalse() {
when(mHelper.isSingleVolume()).thenReturn(false);
when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_notSingleVolume_VoiceCapable_shouldReturnTrue() {
when(mHelper.isSingleVolume()).thenReturn(false);
when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void getAudioStream_shouldReturnRing() {
assertThat(mController.getAudioStream()).isEqualTo(AudioManager.STREAM_RING);
}
}

View File

@@ -0,0 +1,153 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification;
import android.content.Context;
import android.preference.SeekBarVolumizer;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class VolumeSeekBarPreferenceControllerTest {
@Mock
private Context mContext;
@Mock
private PreferenceScreen mScreen;
@Mock
private VolumeSeekBarPreference mPreference;
@Mock
private VolumeSeekBarPreference.Callback mCallback;
private VolumeSeekBarPreferenceControllerTestable mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
mController =
new VolumeSeekBarPreferenceControllerTestable(mContext, mCallback);
}
@Test
public void displayPreference_available_shouldUpdatePreference() {
mController.displayPreference(mScreen);
verify(mPreference).setCallback(mCallback);
verify(mPreference).setStream(mController.AUDIO_STREAM);
verify(mPreference).setMuteIcon(mController.MUTE_ICON);
}
@Test
public void displayPreference_notAvailable_shouldNotUpdatePreference() {
mController =
new VolumeSeekBarPreferenceControllerTestable(mContext, mCallback, false);
mController.displayPreference(mScreen);
verify(mPreference, never()).setCallback(any(VolumeSeekBarPreference.Callback.class));
verify(mPreference, never()).setStream(anyInt());
verify(mPreference, never()).setMuteIcon(anyInt());
}
@Test
public void onResume_shouldResumePreference() {
mController.displayPreference(mScreen);
mController.onResume();
verify(mPreference).onActivityResume();
}
@Test
public void onPause_shouldPausePreference() {
mController.displayPreference(mScreen);
mController.onPause();
verify(mPreference).onActivityPause();
}
private class VolumeSeekBarPreferenceControllerTestable extends
VolumeSeekBarPreferenceController {
private final static int AUDIO_STREAM = 1;
private final static int MUTE_ICON = 2;
private boolean mAvailable;
VolumeSeekBarPreferenceControllerTestable(Context context,
VolumeSeekBarPreference.Callback callback) {
this(context, callback, true);
}
VolumeSeekBarPreferenceControllerTestable(Context context,
VolumeSeekBarPreference.Callback callback, boolean available) {
super(context, callback, null);
mAvailable = available;
}
@Override
public String getPreferenceKey() {
return null;
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
return false;
}
@Override
public boolean isAvailable() {
return mAvailable;
}
@Override
public int getAudioStream() {
return AUDIO_STREAM;
}
@Override
public int getMuteIcon() {
return MUTE_ICON;
}
private void setAvailable(boolean available) {
}
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification;
import android.content.Context;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class ZenModePreferenceControllerTest {
@Mock
private Context mContext;
private ZenModePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mController = new ZenModePreferenceController(mContext);
}
@Test
public void isAlwaysAvailable() {
assertThat(mController.isAvailable()).isTrue();
}
}