Make work profile sounds section as its own entry

Also rephrase the strings by request.

Bug: 174964721
Test: manual
Change-Id: I760249cd48832f2739ab7ca33706cd11dd34a6b5
This commit is contained in:
Yanting Yang
2021-03-09 14:35:21 +08:00
parent 21cf6c04db
commit e14a3a3022
12 changed files with 991 additions and 50 deletions

View File

@@ -0,0 +1,189 @@
/*
* Copyright (C) 2021 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 static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.UserHandle;
import android.os.UserManager;
import android.telephony.TelephonyManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.TwoStatePreference;
import com.android.settings.DefaultRingtonePreference;
import com.android.settings.R;
import com.android.settings.RingtonePreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class SoundWorkSettingsControllerTest {
private static final String KEY_WORK_USE_PERSONAL_SOUNDS = "work_use_personal_sounds";
private static final String KEY_WORK_PHONE_RINGTONE = "work_ringtone";
private static final String KEY_WORK_NOTIFICATION_RINGTONE = "work_notification_ringtone";
private static final String KEY_WORK_ALARM_RINGTONE = "work_alarm_ringtone";
@Mock
private Context mContext;
@Mock
private PreferenceScreen mScreen;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private AudioHelper mAudioHelper;
@Mock
private SoundWorkSettings mFragment;
private SoundWorkSettingsController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
when(mScreen.findPreference(KEY_WORK_USE_PERSONAL_SOUNDS))
.thenReturn(mock(TwoStatePreference.class));
when(mScreen.findPreference(KEY_WORK_PHONE_RINGTONE))
.thenReturn(mock(DefaultRingtonePreference.class));
when(mScreen.findPreference(KEY_WORK_NOTIFICATION_RINGTONE))
.thenReturn(mock(DefaultRingtonePreference.class));
when(mScreen.findPreference(KEY_WORK_ALARM_RINGTONE))
.thenReturn(mock(DefaultRingtonePreference.class));
mController = new SoundWorkSettingsController(mContext, mFragment, null, mAudioHelper);
}
@Test
public void isAvailable_managedProfileAndNotSingleVolume_shouldReturnTrue() {
when(mAudioHelper.getManagedProfileId(nullable(UserManager.class)))
.thenReturn(UserHandle.myUserId());
when(mAudioHelper.isSingleVolume()).thenReturn(false);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_noManagedProfile_shouldReturnFalse() {
when(mAudioHelper.getManagedProfileId(nullable(UserManager.class)))
.thenReturn(UserHandle.USER_NULL);
when(mAudioHelper.isSingleVolume()).thenReturn(false);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_singleVolume_shouldReturnFalse() {
when(mAudioHelper.getManagedProfileId(nullable(UserManager.class)))
.thenReturn(UserHandle.myUserId());
when(mAudioHelper.isSingleVolume()).thenReturn(true);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void onPreferenceChange_shouldUpdateSummary() {
final Preference preference = mock(Preference.class);
when(preference.getKey()).thenReturn(KEY_WORK_PHONE_RINGTONE);
mController.onPreferenceChange(preference, "hello");
verify(preference).setSummary(nullable(String.class));
}
@Test
public void onResume_noVoiceCapability_shouldHidePhoneRingtone() {
when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
mController = new SoundWorkSettingsController(mContext, mFragment, null, mAudioHelper);
when(mAudioHelper.getManagedProfileId(nullable(UserManager.class)))
.thenReturn(UserHandle.myUserId());
when(mAudioHelper.isUserUnlocked(nullable(UserManager.class), anyInt())).thenReturn(true);
when(mAudioHelper.isSingleVolume()).thenReturn(false);
when(mAudioHelper.createPackageContextAsUser(anyInt())).thenReturn(mContext);
// Precondition: work profile is available.
assertThat(mController.isAvailable()).isTrue();
mController.displayPreference(mScreen);
mController.onResume();
verify((Preference) mScreen.findPreference(KEY_WORK_PHONE_RINGTONE)).setVisible(false);
}
@Test
public void onResume_availableButLocked_shouldRedactPreferences() {
final String notAvailable = "(not available)";
when(mContext.getString(R.string.managed_profile_not_available_label))
.thenReturn(notAvailable);
// Given a device with a managed profile:
when(mAudioHelper.isSingleVolume()).thenReturn(false);
when(mAudioHelper.createPackageContextAsUser(anyInt())).thenReturn(mContext);
when(mAudioHelper.getManagedProfileId(nullable(UserManager.class)))
.thenReturn(UserHandle.myUserId());
when(mAudioHelper.isUserUnlocked(nullable(UserManager.class), anyInt())).thenReturn(false);
// When resumed:
mController.displayPreference(mScreen);
mController.onResume();
// Sound preferences should explain that the profile isn't available yet.
verify((Preference) mScreen.findPreference(KEY_WORK_PHONE_RINGTONE))
.setSummary(eq(notAvailable));
verify((Preference) mScreen.findPreference(KEY_WORK_NOTIFICATION_RINGTONE))
.setSummary(eq(notAvailable));
verify((Preference) mScreen.findPreference(KEY_WORK_ALARM_RINGTONE))
.setSummary(eq(notAvailable));
}
@Test
public void onResume_shouldSetUserIdToPreference() {
final int managedProfileUserId = 10;
when(mAudioHelper.getManagedProfileId(nullable(UserManager.class)))
.thenReturn(managedProfileUserId);
when(mAudioHelper.isUserUnlocked(nullable(UserManager.class), anyInt())).thenReturn(true);
when(mAudioHelper.isSingleVolume()).thenReturn(false);
when(mAudioHelper.createPackageContextAsUser(anyInt())).thenReturn(mContext);
mController.displayPreference(mScreen);
mController.onResume();
verify((RingtonePreference) mScreen.findPreference(KEY_WORK_PHONE_RINGTONE))
.setUserId(managedProfileUserId);
verify((RingtonePreference) mScreen.findPreference(KEY_WORK_NOTIFICATION_RINGTONE))
.setUserId(managedProfileUserId);
verify((RingtonePreference) mScreen.findPreference(KEY_WORK_ALARM_RINGTONE))
.setUserId(managedProfileUserId);
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright (C) 2021 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 static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.os.UserHandle;
import androidx.fragment.app.FragmentActivity;
import androidx.preference.Preference;
import com.android.settings.DefaultRingtonePreference;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class SoundWorkSettingsTest {
@Mock
private FragmentActivity mActivity;
@Mock
private MetricsFeatureProvider mMetricsFeatureProvider;
private FakeFeatureFactory mFeatureFactory;
private SoundWorkSettings mFragment;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mFragment = spy(new SoundWorkSettings());
when(mFragment.getActivity()).thenReturn(mActivity);
mFeatureFactory = FakeFeatureFactory.setupForTest();
mMetricsFeatureProvider = mFeatureFactory.getMetricsFeatureProvider();
ReflectionHelpers.setField(mFragment, "mMetricsFeatureProvider", mMetricsFeatureProvider);
}
@Test
public void onPreferenceTreeClick_isRingtonePreference_shouldStartActivity() {
final DefaultRingtonePreference ringtonePreference = mock(DefaultRingtonePreference.class);
when(mMetricsFeatureProvider.logClickedPreference(any(Preference.class),
anyInt())).thenReturn(true);
mFragment.onPreferenceTreeClick(ringtonePreference);
verify(mActivity).startActivityForResultAsUser(any(), anyInt(), any(),
any(UserHandle.class));
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2021 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 static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.DISABLED_FOR_USER;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.os.UserHandle;
import android.util.FeatureFlagUtils;
import com.android.settings.core.FeatureFlags;
import com.android.settings.testutils.shadow.ShadowAudioHelper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@Config(shadows = ShadowAudioHelper.class)
@RunWith(RobolectricTestRunner.class)
public class WorkSoundsPreferenceControllerTest {
private Context mContext;
private WorkSoundsPreferenceController mController;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mController = new WorkSoundsPreferenceController(mContext, "test_key");
}
@After
public void tearDown() {
ShadowAudioHelper.reset();
}
@Test
public void getAvailabilityStatus_supportWorkProfileSound_shouldReturnAvailable() {
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.SILKY_HOME, true);
ShadowAudioHelper.setIsSingleVolume(false);
ShadowAudioHelper.setManagedProfileId(UserHandle.USER_CURRENT);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailabilityStatus_notSupportWorkProfileSound_shouldReturnDisabled() {
ShadowAudioHelper.setIsSingleVolume(true);
ShadowAudioHelper.setManagedProfileId(UserHandle.USER_NULL);
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
}
}

View File

@@ -23,17 +23,35 @@ import com.android.settings.notification.AudioHelper;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;
@Implements(AudioHelper.class)
public class ShadowAudioHelper {
private static boolean sIsSingleVolume = true;
private static int sManagedProfileId = UserHandle.USER_CURRENT;
@Resetter
public static void reset() {
sIsSingleVolume = true;
sManagedProfileId = UserHandle.USER_CURRENT;
}
public static void setIsSingleVolume(boolean isSingleVolume) {
sIsSingleVolume = isSingleVolume;
}
public static void setManagedProfileId(int managedProfileId) {
sManagedProfileId = managedProfileId;
}
@Implementation
protected boolean isSingleVolume() {
return true;
return sIsSingleVolume;
}
@Implementation
protected int getManagedProfileId(UserManager um) {
return UserHandle.USER_CURRENT;
return sManagedProfileId;
}
}