Merge "NAS settings change" into sc-dev

This commit is contained in:
Chloris Kuo
2021-04-20 02:03:27 +00:00
committed by Android (Google) Code Review
3 changed files with 56 additions and 5 deletions

View File

@@ -37,7 +37,7 @@ public class NotificationAssistantDialogFragment extends InstrumentedDialogFragm
final NotificationAssistantDialogFragment dialogFragment =
new NotificationAssistantDialogFragment();
final Bundle args = new Bundle();
args.putString(KEY_COMPONENT, cn.flattenToString());
args.putString(KEY_COMPONENT, cn == null ? "" : cn.flattenToString());
dialogFragment.setArguments(args);
dialogFragment.setTargetFragment(target, 0);

View File

@@ -19,6 +19,7 @@ package com.android.settings.notification;
import android.content.ComponentName;
import android.content.Context;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import androidx.fragment.app.Fragment;
@@ -30,6 +31,7 @@ import com.google.common.annotations.VisibleForTesting;
public class NotificationAssistantPreferenceController extends TogglePreferenceController {
private static final String TAG = "NASPreferenceController";
private static final int AVAILABLE = 1;
private final UserManager mUserManager;
private Fragment mFragment;
private int mUserId = UserHandle.myUserId();
@@ -41,6 +43,7 @@ public class NotificationAssistantPreferenceController extends TogglePreferenceC
super(context, preferenceKey);
mNotificationBackend = backend;
mFragment = fragment;
mUserManager = UserManager.get(context);
}
@Override
@@ -74,8 +77,10 @@ public class NotificationAssistantPreferenceController extends TogglePreferenceC
protected void setNotificationAssistantGranted(ComponentName cn) {
if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
Settings.Secure.NAS_SETTINGS_UPDATED, 0, mUserId) == 0) {
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.NAS_SETTINGS_UPDATED, 1, mUserId);
for (int profileId : mUserManager.getProfileIds(mUserId, false)) {
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.NAS_SETTINGS_UPDATED, 1, profileId);
}
mNotificationBackend.resetDefaultNotificationAssistant(cn != null);
}
mNotificationBackend.setNotificationAssistantGranted(cn);

View File

@@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
@@ -30,12 +31,15 @@ import static org.mockito.Mockito.when;
import android.content.ComponentName;
import android.content.Context;
import android.os.UserManager;
import android.provider.Settings;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.testutils.shadow.ShadowSecureSettings;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -43,6 +47,9 @@ import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
@RunWith(RobolectricTestRunner.class)
public class NotificationAssistantPreferenceControllerTest {
@@ -58,6 +65,8 @@ public class NotificationAssistantPreferenceControllerTest {
private FragmentTransaction mFragmentTransaction;
@Mock
private NotificationBackend mBackend;
@Mock
private UserManager mUserManager;
private NotificationAssistantPreferenceController mPreferenceController;
ComponentName mNASComponent = new ComponentName("a", "b");
@@ -65,12 +74,15 @@ public class NotificationAssistantPreferenceControllerTest {
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
ShadowApplication.getInstance().setSystemService(Context.USER_SERVICE, mUserManager);
doReturn(mContext).when(mFragment).getContext();
when(mFragment.getFragmentManager()).thenReturn(mFragmentManager);
when(mFragmentManager.beginTransaction()).thenReturn(mFragmentTransaction);
when(mBackend.getDefaultNotificationAssistant()).thenReturn(mNASComponent);
mPreferenceController = new NotificationAssistantPreferenceController(mContext,
mBackend, mFragment, KEY);
when(mUserManager.getProfileIds(eq(0), anyBoolean())).thenReturn(new int[] {0, 10});
when(mUserManager.getProfileIds(eq(20), anyBoolean())).thenReturn(new int[] {20});
}
@Test
@@ -96,14 +108,19 @@ public class NotificationAssistantPreferenceControllerTest {
}
@Test
public void testMigrationFromSetting_userEnable() throws Exception {
@Config(shadows = ShadowSecureSettings.class)
public void testMigrationFromSetting_userEnable_multiProfile() throws Exception {
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 0);
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 10);
//Test user enable for the first time
mPreferenceController.setNotificationAssistantGranted(mNASComponent);
assertEquals(1, Settings.Secure.getIntForUser(mContext.getContentResolver(),
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 0));
assertEquals(1, Settings.Secure.getIntForUser(mContext.getContentResolver(),
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 10));
verify(mBackend, times(1))
.resetDefaultNotificationAssistant(eq(true));
@@ -115,14 +132,43 @@ public class NotificationAssistantPreferenceControllerTest {
}
@Test
public void testMigrationFromSetting_userDisable() throws Exception {
@Config(shadows = ShadowSecureSettings.class)
public void testMigrationFromSetting_userEnable_multiUser() throws Exception {
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 0);
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 20);
//Test user 0 enable for the first time
mPreferenceController.setNotificationAssistantGranted(mNASComponent);
assertEquals(1, Settings.Secure.getIntForUser(mContext.getContentResolver(),
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 0));
assertEquals(0, Settings.Secure.getIntForUser(mContext.getContentResolver(),
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 20));
verify(mBackend, times(1))
.resetDefaultNotificationAssistant(eq(true));
//Test user enable again, migration should not happen
mPreferenceController.setNotificationAssistantGranted(mNASComponent);
//Number of invocations should not increase
verify(mBackend, times(1))
.resetDefaultNotificationAssistant(eq(true));
}
@Test
@Config(shadows = ShadowSecureSettings.class)
public void testMigrationFromSetting_userDisable_multiProfile() throws Exception {
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 0);
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 10);
//Test user disable for the first time
mPreferenceController.setChecked(false);
assertEquals(1, Settings.Secure.getIntForUser(mContext.getContentResolver(),
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 0));
assertEquals(1, Settings.Secure.getIntForUser(mContext.getContentResolver(),
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 10));
verify(mBackend, times(1))
.resetDefaultNotificationAssistant(eq(false));