Disambiguate default notes app user from stylus settings

Users can now select between work or personal notes app to be the app
that opens when the stylus tail button is pressed via a dialog.

Bug: 278555728
Test: StylusDevicesControllerTest
Change-Id: I9c63de6f11deb357b0497c7b972d4ac19b876e1f
This commit is contained in:
Vania Januar
2023-04-25 14:10:52 +01:00
parent f6b49a0204
commit 64d64fdd36
3 changed files with 154 additions and 11 deletions

View File

@@ -18,6 +18,10 @@ package com.android.settings.connecteddevice.stylus;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doNothing;
@@ -27,13 +31,17 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Dialog;
import android.app.role.RoleManager;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.os.Process;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.provider.Settings.Secure;
import android.view.InputDevice;
@@ -48,6 +56,7 @@ import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settings.dashboard.profileselector.UserAdapter;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.core.lifecycle.Lifecycle;
@@ -59,7 +68,9 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class StylusDevicesControllerTest {
@@ -79,6 +90,8 @@ public class StylusDevicesControllerTest {
@Mock
private PackageManager mPm;
@Mock
private UserManager mUserManager;
@Mock
private RoleManager mRm;
@Mock
private Lifecycle mLifecycle;
@@ -87,7 +100,6 @@ public class StylusDevicesControllerTest {
@Mock
private BluetoothDevice mBluetoothDevice;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
@@ -101,6 +113,7 @@ public class StylusDevicesControllerTest {
when(mContext.getSystemService(InputMethodManager.class)).thenReturn(mImm);
when(mContext.getSystemService(RoleManager.class)).thenReturn(mRm);
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
doNothing().when(mContext).startActivity(any());
when(mImm.getCurrentInputMethodInfo()).thenReturn(mInputMethodInfo);
@@ -115,6 +128,8 @@ public class StylusDevicesControllerTest {
when(mPm.getApplicationInfo(eq(NOTES_PACKAGE_NAME),
any(PackageManager.ApplicationInfoFlags.class))).thenReturn(new ApplicationInfo());
when(mPm.getApplicationLabel(any(ApplicationInfo.class))).thenReturn(NOTES_APP_LABEL);
when(mUserManager.getUsers()).thenReturn(Arrays.asList(new UserInfo(0, "default", 0)));
when(mUserManager.isManagedProfile(anyInt())).thenReturn(false);
when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
@@ -228,21 +243,35 @@ public class StylusDevicesControllerTest {
when(mInputMethodInfo.supportsStylusHandwriting()).thenReturn(false);
showScreen(mController);
Preference handwritingPref = mPreferenceContainer.getPreference(1);
Preference handwritingPref = mPreferenceContainer.getPreference(1);
assertThat(handwritingPref.isVisible()).isFalse();
}
@Test
public void defaultNotesPreference_showsNotesRoleApp() {
public void defaultNotesPreference_singleUser_showsNotesRoleApp() {
showScreen(mController);
Preference defaultNotesPref = mPreferenceContainer.getPreference(0);
Preference defaultNotesPref = mPreferenceContainer.getPreference(0);
assertThat(defaultNotesPref.getTitle().toString()).isEqualTo(
mContext.getString(R.string.stylus_default_notes_app));
assertThat(defaultNotesPref.getSummary().toString()).isEqualTo(NOTES_APP_LABEL.toString());
}
@Test
public void defaultNotesPreference_workProfileUser_showsWorkNotesRoleApp() {
when(mUserManager.isManagedProfile(0)).thenReturn(true);
showScreen(mController);
Preference defaultNotesPref = mPreferenceContainer.getPreference(0);
assertThat(defaultNotesPref.getTitle().toString()).isEqualTo(
mContext.getString(R.string.stylus_default_notes_app));
assertThat(defaultNotesPref.getSummary().toString()).isEqualTo(
mContext.getString(R.string.stylus_default_notes_summary_work,
NOTES_APP_LABEL.toString()));
}
@Test
public void defaultNotesPreference_roleHolderChanges_updatesPreference() {
showScreen(mController);
@@ -267,7 +296,7 @@ public class StylusDevicesControllerTest {
}
@Test
public void defaultNotesPreferenceClick_sendsManageDefaultRoleIntent() {
public void defaultNotesPreferenceClick_singleUser_sendsManageDefaultRoleIntent() {
final String permissionPackageName = "permissions.package";
when(mPm.getPermissionControllerPackageName()).thenReturn(permissionPackageName);
final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
@@ -282,6 +311,46 @@ public class StylusDevicesControllerTest {
assertThat(intent.getPackage()).isEqualTo(permissionPackageName);
assertThat(intent.getStringExtra(Intent.EXTRA_ROLE_NAME)).isEqualTo(
RoleManager.ROLE_NOTES);
assertNull(mController.mDialog);
}
@Test
public void defaultNotesPreferenceClick_multiUser_showsProfileSelectorDialog() {
mContext.setTheme(R.style.Theme_AppCompat);
final String permissionPackageName = "permissions.package";
final UserHandle currentUser = Process.myUserHandle();
List<UserInfo> userInfos = Arrays.asList(
new UserInfo(currentUser.getIdentifier(), "current", 0),
new UserInfo(1, "profile", UserInfo.FLAG_PROFILE)
);
when(mUserManager.getUsers()).thenReturn(userInfos);
when(mUserManager.isManagedProfile(1)).thenReturn(true);
when(mUserManager.getUserInfo(currentUser.getIdentifier())).thenReturn(userInfos.get(0));
when(mUserManager.getUserInfo(1)).thenReturn(userInfos.get(1));
when(mUserManager.getProfileParent(1)).thenReturn(userInfos.get(0));
when(mPm.getPermissionControllerPackageName()).thenReturn(permissionPackageName);
showScreen(mController);
Preference defaultNotesPref = mPreferenceContainer.getPreference(0);
mController.onPreferenceClick(defaultNotesPref);
assertTrue(mController.mDialog.isShowing());
}
@Test
public void profileSelectDialogClickCallback_onClick_sendsIntent() {
Intent intent = new Intent();
UserHandle user1 = mock(UserHandle.class);
UserHandle user2 = mock(UserHandle.class);
List<UserHandle> users = Arrays.asList(new UserHandle[] {user1, user2});
mController.mDialog = mock(Dialog.class);
UserAdapter.OnClickListener callback = mController
.createProfileDialogClickCallback(intent, users);
callback.onClick(1);
assertEquals(intent.getExtra(Intent.EXTRA_USER), user2);
verify(mContext).startActivity(intent);
}
@Test