Merge UP1A.230905.019

Merged-In: Ifc048311746c027e3683cdcf65f1079d04cf7c56
Change-Id: I2a988e3da0958f31323f95588e1ac66482186ecf
This commit is contained in:
Xin Li
2023-08-31 11:53:03 -07:00
15 changed files with 320 additions and 29 deletions

View File

@@ -0,0 +1,104 @@
/*
* Copyright (C) 2023 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.internal.notification.NotificationAccessConfirmationActivityContract.EXTRA_COMPONENT_NAME;
import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.Shadows.shadowOf;
import android.annotation.Nullable;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.widget.TextView;
import com.android.settings.R;
import com.google.common.base.Strings;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class NotificationAccessConfirmationActivityTest {
@Test
public void start_showsDialog() {
ComponentName cn = new ComponentName("com.example", "com.example.SomeService");
installPackage(cn.getPackageName(), "X");
NotificationAccessConfirmationActivity activity = startActivityWithIntent(cn);
assertThat(activity.isFinishing()).isFalse();
assertThat(getDialogText(activity)).isEqualTo(
activity.getString(R.string.notification_listener_security_warning_summary, "X"));
}
@Test
public void start_withMissingPackage_finishes() {
ComponentName cn = new ComponentName("com.example", "com.example.SomeService");
NotificationAccessConfirmationActivity activity = startActivityWithIntent(cn);
assertThat(getDialogText(activity)).isNull();
assertThat(activity.isFinishing()).isTrue();
}
@Test
public void start_componentNameTooLong_finishes() {
ComponentName longCn = new ComponentName("com.example", Strings.repeat("Blah", 150));
installPackage(longCn.getPackageName(), "<Unused>");
NotificationAccessConfirmationActivity activity = startActivityWithIntent(longCn);
assertThat(getDialogText(activity)).isNull();
assertThat(activity.isFinishing()).isTrue();
}
private static NotificationAccessConfirmationActivity startActivityWithIntent(
ComponentName cn) {
return Robolectric.buildActivity(
NotificationAccessConfirmationActivity.class,
new Intent().putExtra(EXTRA_COMPONENT_NAME, cn))
.setup()
.get();
}
private static void installPackage(String packageName, String appName) {
PackageInfo pi = new PackageInfo();
pi.packageName = packageName;
pi.applicationInfo = new ApplicationInfo();
pi.applicationInfo.packageName = packageName;
pi.applicationInfo.name = appName;
shadowOf(RuntimeEnvironment.application.getPackageManager()).installPackage(pi);
}
@Nullable
private static String getDialogText(Activity activity) {
TextView tv = activity.getWindow().findViewById(android.R.id.message);
CharSequence text = (tv != null ? tv.getText() : null);
return text != null ? text.toString() : null;
}
}

View File

@@ -49,6 +49,8 @@ public class VolumeSeekBarPreferenceControllerTest {
@Mock
private VolumeSeekBarPreference.Callback mCallback;
@Mock
private VolumeSeekBarPreference.Listener mListener;
@Mock
private AudioHelper mHelper;
private VolumeSeekBarPreferenceControllerTestable mController;
@@ -59,7 +61,7 @@ public class VolumeSeekBarPreferenceControllerTest {
when(mScreen.findPreference(nullable(String.class))).thenReturn(mPreference);
when(mPreference.getKey()).thenReturn("key");
mController = new VolumeSeekBarPreferenceControllerTestable(mContext, mCallback, true,
mPreference.getKey());
mPreference.getKey(), mListener);
mController.setAudioHelper(mHelper);
}
@@ -70,18 +72,20 @@ public class VolumeSeekBarPreferenceControllerTest {
verify(mPreference).setCallback(mCallback);
verify(mPreference).setStream(VolumeSeekBarPreferenceControllerTestable.AUDIO_STREAM);
verify(mPreference).setMuteIcon(VolumeSeekBarPreferenceControllerTestable.MUTE_ICON);
verify(mPreference).setListener(mListener);
}
@Test
public void displayPreference_notAvailable_shouldNotUpdatePreference() {
mController = new VolumeSeekBarPreferenceControllerTestable(mContext, mCallback, false,
mPreference.getKey());
mPreference.getKey(), mListener);
mController.displayPreference(mScreen);
verify(mPreference, never()).setCallback(any(VolumeSeekBarPreference.Callback.class));
verify(mPreference, never()).setStream(anyInt());
verify(mPreference, never()).setMuteIcon(anyInt());
verify(mPreference, never()).setListener(mListener);
}
@Test
@@ -157,10 +161,12 @@ public class VolumeSeekBarPreferenceControllerTest {
private boolean mAvailable;
VolumeSeekBarPreferenceControllerTestable(Context context,
VolumeSeekBarPreference.Callback callback, boolean available, String key) {
VolumeSeekBarPreference.Callback callback, boolean available, String key,
VolumeSeekBarPreference.Listener listener) {
super(context, key);
setCallback(callback);
mAvailable = available;
mVolumePreferenceListener = listener;
}
@Override

View File

@@ -18,11 +18,14 @@ package com.android.settings.notification;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.media.AudioManager;
import android.preference.SeekBarVolumizer;
import android.widget.SeekBar;
import org.junit.Before;
import org.junit.Test;
@@ -34,18 +37,28 @@ import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class VolumeSeekBarPreferenceTest {
private static final CharSequence CONTENT_DESCRIPTION = "TEST";
@Mock
private AudioManager mAudioManager;
@Mock
private VolumeSeekBarPreference mPreference;
@Mock
private Context mContext;
@Mock
private SeekBar mSeekBar;
@Mock
private SeekBarVolumizer mVolumizer;
private VolumeSeekBarPreference.Listener mListener;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager);
doCallRealMethod().when(mPreference).updateContentDescription(CONTENT_DESCRIPTION);
mPreference.mSeekBar = mSeekBar;
mPreference.mAudioManager = mAudioManager;
mPreference.mVolumizer = mVolumizer;
mListener = () -> mPreference.updateContentDescription(CONTENT_DESCRIPTION);
}
@Test
@@ -65,4 +78,24 @@ public class VolumeSeekBarPreferenceTest {
verify(mPreference).setMin(min);
verify(mPreference).setProgress(progress);
}
@Test
public void init_listenerIsCalled() {
doCallRealMethod().when(mPreference).setListener(mListener);
doCallRealMethod().when(mPreference).init();
mPreference.setListener(mListener);
mPreference.init();
verify(mPreference).updateContentDescription(CONTENT_DESCRIPTION);
}
@Test
public void init_listenerNotSet_noException() {
doCallRealMethod().when(mPreference).init();
mPreference.init();
verify(mPreference, never()).updateContentDescription(CONTENT_DESCRIPTION);
}
}

View File

@@ -83,6 +83,36 @@ public class ApprovalPreferenceControllerTest {
}
@Test
public void updateState_enabled() {
when(mAppOpsManager.noteOpNoThrow(anyInt(), anyInt(), anyString())).thenReturn(
AppOpsManager.MODE_ALLOWED);
when(mNm.isNotificationListenerAccessGranted(mCn)).thenReturn(true);
RestrictedSwitchPreference pref = new RestrictedSwitchPreference(
mContext);
pref.setAppOps(mAppOpsManager);
mController.updateState(pref);
assertThat(pref.isEnabled()).isTrue();
}
@Test
public void updateState_invalidCn_disabled() {
ComponentName longCn = new ComponentName("com.example.package",
com.google.common.base.Strings.repeat("Blah", 150));
mController.setCn(longCn);
when(mAppOpsManager.noteOpNoThrow(anyInt(), anyInt(), anyString())).thenReturn(
AppOpsManager.MODE_ALLOWED);
RestrictedSwitchPreference pref = new RestrictedSwitchPreference(
mContext);
pref.setAppOps(mAppOpsManager);
mController.updateState(pref);
assertThat(pref.isEnabled()).isFalse();
}
@Test
public void updateState_checked() {
when(mAppOpsManager.noteOpNoThrow(anyInt(), anyInt(), anyString())).thenReturn(