Merge "Refactor SoundSettings to use more preference controller."

This commit is contained in:
TreeHugger Robot
2016-12-08 00:30:25 +00:00
committed by Android (Google) Code Review
21 changed files with 918 additions and 198 deletions

View File

@@ -28,7 +28,6 @@ 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;

View File

@@ -0,0 +1,53 @@
/*
* 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.RingtoneManager;
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.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 AlarmRingtonePreferenceControllerTest {
@Mock
private Context mContext;
private AlarmRingtonePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mController = new AlarmRingtonePreferenceController(mContext);
}
@Test
public void getRingtoneType_shouldReturnAlarm() {
assertThat(mController.getRingtoneType()).isEqualTo(RingtoneManager.TYPE_ALARM);
}
}

View File

@@ -25,7 +25,6 @@ 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;

View File

@@ -28,7 +28,6 @@ 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;

View File

@@ -0,0 +1,124 @@
/*
* 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.content.pm.PackageManager;
import android.os.UserManager;
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 static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.eq;
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 EmergencyBroadcastPreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock
private AccountRestrictionHelper mAccountHelper;
@Mock
private PackageManager mPackageManager;
@Mock
private UserManager mUserManager;
@Mock
private RestrictedPreference mPreference;
private EmergencyBroadcastPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
mController = new EmergencyBroadcastPreferenceController(mContext, mAccountHelper);
}
@Test
public void updateState_shouldCheckRestriction() {
mController.updateState(mPreference);
verify(mPreference).checkRestrictionAndSetDisabled(anyString());
}
@Test
public void isAvailable_notAdminUser_shouldReturnFalse() {
when(mUserManager.isAdminUser()).thenReturn(false);
when(mContext.getResources().getBoolean(
com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true);
when(mPackageManager.getApplicationEnabledSetting(anyString()))
.thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
when(mAccountHelper.hasBaseUserRestriction(anyString(), anyInt())).thenReturn(false);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_hasConfigCellBroadcastRestriction_shouldReturnFalse() {
when(mUserManager.isAdminUser()).thenReturn(true);
when(mContext.getResources().getBoolean(
com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true);
when(mPackageManager.getApplicationEnabledSetting(anyString()))
.thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
when(mAccountHelper.hasBaseUserRestriction(
eq(UserManager.DISALLOW_CONFIG_CELL_BROADCASTS), anyInt())).thenReturn(true);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_cellBroadcastAppLinkDisabled_shouldReturnFalse() {
when(mUserManager.isAdminUser()).thenReturn(true);
when(mContext.getResources().getBoolean(
com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(false);
when(mPackageManager.getApplicationEnabledSetting(anyString()))
.thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
when(mAccountHelper.hasBaseUserRestriction(anyString(), anyInt())).thenReturn(false);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_cellBroadcastReceiverDisabled_shouldReturnFalse() {
when(mUserManager.isAdminUser()).thenReturn(true);
when(mContext.getResources().getBoolean(
com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true);
when(mPackageManager.getApplicationEnabledSetting(anyString()))
.thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
when(mAccountHelper.hasBaseUserRestriction(anyString(), anyInt())).thenReturn(false);
assertThat(mController.isAvailable()).isFalse();
}
}

View File

@@ -25,7 +25,6 @@ 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;

View File

@@ -0,0 +1,53 @@
/*
* 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.RingtoneManager;
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.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 NotificationRingtonePreferenceControllerTest {
@Mock
private Context mContext;
private NotificationRingtonePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mController = new NotificationRingtonePreferenceController(mContext);
}
@Test
public void getRingtoneType_shouldReturnNotification() {
assertThat(mController.getRingtoneType()).isEqualTo(RingtoneManager.TYPE_NOTIFICATION);
}
}

View File

@@ -27,7 +27,6 @@ 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;

View File

@@ -0,0 +1,75 @@
/*
* 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.RingtoneManager;
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.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 PhoneRingtonePreferenceControllerTest {
@Mock
private TelephonyManager mTelephonyManager;
private Context mContext;
private PhoneRingtonePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowContext = ShadowApplication.getInstance();
shadowContext.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager);
mContext = shadowContext.getApplicationContext();
mController = new PhoneRingtonePreferenceController(mContext);
}
@Test
public void isAvailable_notVoiceCapable_shouldReturnFalse() {
when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_VoiceCapable_shouldReturnTrue() {
when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void getRingtoneType_shouldReturnRingtone() {
assertThat(mController.getRingtoneType()).isEqualTo(RingtoneManager.TYPE_RINGTONE);
}
}

View File

@@ -30,7 +30,6 @@ 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;

View File

@@ -0,0 +1,85 @@
/*
* 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.support.v7.preference.Preference;
import android.media.RingtoneManager;
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.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class RingtonePreferenceControllerBaseTest {
@Mock
private Context mContext;
private RingtonePreferenceControllerBase mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mController = new RingtonePreferenceControllerBaseTestable(mContext);
}
@Test
public void isAlwaysAvailable() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void updateState_shouldSetSummary() {
Preference preference = mock(Preference.class);
mController.updateState(preference);
verify(preference).setSummary(anyString());
}
private class RingtonePreferenceControllerBaseTestable extends
RingtonePreferenceControllerBase {
RingtonePreferenceControllerBaseTestable(Context context) {
super(context);
}
@Override
public String getPreferenceKey() {
return null;
}
@Override
public int getRingtoneType() {
return RingtoneManager.TYPE_RINGTONE;
}
}
}

View File

@@ -0,0 +1,109 @@
/*
* 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.provider.Settings;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.support.v7.preference.TwoStatePreference;
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.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import static android.provider.Settings.System.VIBRATE_WHEN_RINGING;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
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 VibrateWhenRingPreferenceControllerTest {
@Mock
private Context mContext;
@Mock
private PreferenceScreen mScreen;
@Mock
private TelephonyManager mTelephonyManager;
private VibrateWhenRingPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
mController = new VibrateWhenRingPreferenceController(mContext);
}
@Test
public void display_voiceCapable_shouldDisplay() {
when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
when(mScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mock(Preference.class));
mController.displayPreference(mScreen);
verify(mScreen, never()).removePreference(any(Preference.class));
}
@Test
public void display_notVoiceCapable_shouldNotDisplay() {
when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
when(mScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mock(Preference.class));
mController.displayPreference(mScreen);
verify(mScreen).removePreference(any(Preference.class));
}
@Test
public void updateState_settingIsOn_preferenceShouldBeChecked() {
final TwoStatePreference preference = mock(TwoStatePreference.class);
final Context context = ShadowApplication.getInstance().getApplicationContext();
Settings.System.putInt(context.getContentResolver(), VIBRATE_WHEN_RINGING, 1);
mController = new VibrateWhenRingPreferenceController(context);
mController.updateState(preference);
verify(preference).setChecked(true);
}
@Test
public void updateState_settingIsOff_preferenceShouldNotBeChecked() {
final TwoStatePreference preference = mock(TwoStatePreference.class);
final Context context = ShadowApplication.getInstance().getApplicationContext();
Settings.System.putInt(context.getContentResolver(), VIBRATE_WHEN_RINGING, 0);
mController = new VibrateWhenRingPreferenceController(context);
mController.updateState(preference);
verify(preference).setChecked(false);
}
}

View File

@@ -27,7 +27,6 @@ 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;

View File

@@ -24,7 +24,6 @@ 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;