Merge "Create a page to manage dnd permission for individual app"

This commit is contained in:
Fan Zhang
2019-03-19 17:14:07 +00:00
committed by Android (Google) Code Review
12 changed files with 635 additions and 284 deletions

View File

@@ -18,26 +18,41 @@ package com.android.settings.applications.specialaccess.zenaccess;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import android.app.NotificationManager;
import android.content.Context;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.ShadowNotificationManager;
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;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowActivityManager;
@RunWith(RobolectricTestRunner.class)
public class ZenAccessControllerTest {
private static final String TEST_PKG = "com.test.package";
private FakeFeatureFactory mFeatureFactory;
private Context mContext;
private ZenAccessController mController;
private ShadowActivityManager mActivityManager;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mFeatureFactory = FakeFeatureFactory.setupForTest();
mController = new ZenAccessController(mContext, "key");
mActivityManager = Shadow.extract(mContext.getSystemService(Context.ACTIVITY_SERVICE));
}
@@ -52,4 +67,32 @@ public class ZenAccessControllerTest {
mActivityManager.setIsLowRamDevice(true);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void logSpecialPermissionChange() {
ZenAccessController.logSpecialPermissionChange(true, "app", mContext);
verify(mFeatureFactory.metricsFeatureProvider).action(any(Context.class),
eq(MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_DND_ALLOW),
eq("app"));
ZenAccessController.logSpecialPermissionChange(false, "app", mContext);
verify(mFeatureFactory.metricsFeatureProvider).action(any(Context.class),
eq(MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_DND_DENY),
eq("app"));
}
@Test
@Config(shadows = ShadowNotificationManager.class)
public void hasAccess_granted_yes() {
final ShadowNotificationManager snm = Shadow.extract(mContext.getSystemService(
NotificationManager.class));
snm.setNotificationPolicyAccessGrantedForPackage(TEST_PKG);
assertThat(ZenAccessController.hasAccess(mContext, TEST_PKG)).isTrue();
}
@Test
@Config(shadows = ShadowNotificationManager.class)
public void hasAccess_notGranted_no() {
assertThat(ZenAccessController.hasAccess(mContext, TEST_PKG)).isFalse();
}
}

View File

@@ -0,0 +1,109 @@
/*
* Copyright (C) 2019 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.applications.specialaccess.zenaccess;
import static androidx.lifecycle.Lifecycle.Event.ON_START;
import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.app.ActivityManager;
import android.content.Context;
import android.provider.Settings;
import androidx.lifecycle.LifecycleOwner;
import com.android.settingslib.core.lifecycle.Lifecycle;
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.RuntimeEnvironment;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowActivityManager;
@RunWith(RobolectricTestRunner.class)
public class ZenAccessSettingObserverMixinTest {
@Mock
private ZenAccessSettingObserverMixin.Listener mListener;
private Context mContext;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle;
private ZenAccessSettingObserverMixin mMixin;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
mMixin = new ZenAccessSettingObserverMixin(mContext, mListener);
mLifecycle.addObserver(mMixin);
}
@Test
public void onStart_lowMemory_shouldNotRegisterListener() {
final ShadowActivityManager sam = Shadow.extract(
mContext.getSystemService(ActivityManager.class));
sam.setIsLowRamDevice(true);
mLifecycle.handleLifecycleEvent(ON_START);
mContext.getContentResolver().notifyChange(Settings.Secure.getUriFor(
Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES), null);
verify(mListener, never()).onZenAccessPolicyChanged();
}
@Test
public void onStart_highMemory_shouldRegisterListener() {
final ShadowActivityManager sam = Shadow.extract(
mContext.getSystemService(ActivityManager.class));
sam.setIsLowRamDevice(false);
mLifecycle.handleLifecycleEvent(ON_START);
mContext.getContentResolver().notifyChange(Settings.Secure.getUriFor(
Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES), null);
verify(mListener).onZenAccessPolicyChanged();
}
@Test
public void onStop_shouldUnregisterListener() {
final ShadowActivityManager sam = Shadow.extract(
mContext.getSystemService(ActivityManager.class));
sam.setIsLowRamDevice(false);
mLifecycle.handleLifecycleEvent(ON_START);
mLifecycle.handleLifecycleEvent(ON_STOP);
mContext.getContentResolver().notifyChange(Settings.Secure.getUriFor(
Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES), null);
verify(mListener, never()).onZenAccessPolicyChanged();
}
}

View File

@@ -1,63 +0,0 @@
/*
* Copyright (C) 2017 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.eq;
import static org.mockito.Mockito.verify;
import android.content.Context;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.testutils.FakeFeatureFactory;
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.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class ZenAccessSettingsTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
private FakeFeatureFactory mFeatureFactory;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mFeatureFactory = FakeFeatureFactory.setupForTest();
}
@Test
public void logSpecialPermissionChange() {
ZenAccessSettings.logSpecialPermissionChange(true, "app", mContext);
verify(mFeatureFactory.metricsFeatureProvider).action(any(Context.class),
eq(MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_DND_ALLOW),
eq("app"));
ZenAccessSettings.logSpecialPermissionChange(false, "app", mContext);
verify(mFeatureFactory.metricsFeatureProvider).action(any(Context.class),
eq(MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_DND_DENY),
eq("app"));
}
}

View File

@@ -19,15 +19,19 @@ package com.android.settings.testutils.shadow;
import android.app.NotificationManager;
import android.net.Uri;
import android.service.notification.ZenModeConfig;
import android.util.ArraySet;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import java.util.Set;
@Implements(NotificationManager.class)
public class ShadowNotificationManager {
private int mZenMode;
private ZenModeConfig mZenModeConfig;
private Set<String> mNotificationPolicyGrantedPackages = new ArraySet<>();
@Implementation
protected void setZenMode(int mode, Uri conditionId, String reason) {
@@ -39,6 +43,11 @@ public class ShadowNotificationManager {
return mZenMode;
}
@Implementation
protected boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {
return mNotificationPolicyGrantedPackages.contains(pkg);
}
@Implementation
public ZenModeConfig getZenModeConfig() {
return mZenModeConfig;
@@ -47,4 +56,8 @@ public class ShadowNotificationManager {
public void setZenModeConfig(ZenModeConfig config) {
mZenModeConfig = config;
}
public void setNotificationPolicyAccessGrantedForPackage(String pkg) {
mNotificationPolicyGrantedPackages.add(pkg);
}
}