Make Linux terminal option profile aware

Bug: 374034911
Test: atest, plus following manual test \
  - Test tabbed UI with/without work profile \
  - Test that disabled by work profile launches alert dialog \
  - Test whether toggling an app only toggle the app for the user.
Flag: Build.RELEASE_AVF_SUPPORT_CUSTOM_VM_WITH_PARAVIRTUALIZED_DEVICES
Change-Id: I4bf0a2d521cf3e632f6c0320e0b5cc0154d5b68f
This commit is contained in:
Jaewan Kim
2024-11-04 15:25:53 +09:00
parent 15f88ddc70
commit ed3abffcfc
13 changed files with 684 additions and 274 deletions

View File

@@ -1,131 +0,0 @@
/*
* Copyright 2024 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.development;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
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 LinuxTerminalPreferenceControllerTest {
@Mock
private Context mContext;
@Mock
private SwitchPreference mPreference;
@Mock
private PreferenceScreen mPreferenceScreen;
@Mock
private PackageManager mPackageManager;
@Mock
private ApplicationInfo mApplicationInfo;
private String mTerminalPackageName = "com.android.virtualization.terminal";
private LinuxTerminalPreferenceController mController;
@Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
doReturn(mPackageManager).when(mContext).getPackageManager();
doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo(
eq(mTerminalPackageName), any());
mController = spy(new LinuxTerminalPreferenceController(mContext));
doReturn(true).when(mController).isAvailable();
doReturn(mTerminalPackageName).when(mController).getTerminalPackageName();
when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mPreference);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void isAvailable_whenPackageNameIsNull_returnsFalse() throws Exception {
mController = spy(new LinuxTerminalPreferenceController(mContext));
doReturn(null).when(mController).getTerminalPackageName();
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_whenAppDoesNotExist_returnsFalse() throws Exception {
doThrow(new NameNotFoundException()).when(mPackageManager).getApplicationInfo(
eq(mTerminalPackageName), any());
mController = spy(new LinuxTerminalPreferenceController(mContext));
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void onPreferenceChanged_turnOnTerminal() {
mController.onPreferenceChange(null, true);
verify(mPackageManager).setApplicationEnabledSetting(
mTerminalPackageName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
/* flags= */ 0);
}
@Test
public void onPreferenceChanged_turnOffTerminal() {
mController.onPreferenceChange(null, false);
verify(mPackageManager).setApplicationEnabledSetting(
mTerminalPackageName,
PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
/* flags= */ 0);
}
@Test
public void updateState_preferenceShouldBeChecked() {
when(mPackageManager.getApplicationEnabledSetting(mTerminalPackageName))
.thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
mController.updateState(mPreference);
verify(mPreference).setChecked(true);
}
@Test
public void updateState_preferenceShouldNotBeChecked() {
when(mPackageManager.getApplicationEnabledSetting(mTerminalPackageName))
.thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
mController.updateState(mPreference);
verify(mPreference).setChecked(false);
}
}

View File

@@ -0,0 +1,178 @@
/*
* Copyright 2024 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.development.linuxterminal;
import static com.android.settings.development.linuxterminal.EnableLinuxTerminalPreferenceController.TERMINAL_PACKAGE_NAME_RESID;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import androidx.preference.PreferenceScreen;
import com.android.settings.widget.SettingsMainSwitchPreference;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.ParameterizedRobolectricTestRunner;
import java.util.Arrays;
import java.util.List;
/** Tests {@link EnableLinuxTerminalPreferenceController} */
@RunWith(ParameterizedRobolectricTestRunner.class)
public class EnableLinuxTerminalPreferenceControllerTest {
/** Defines parameters for parameterized test */
@ParameterizedRobolectricTestRunner.Parameters(
name = "isPrimaryUser={0}, installed={1}, enabled={2}")
public static List<Object[]> params() {
return Arrays.asList(
new Object[] {true, true, false},
new Object[] {true, true, true},
new Object[] {false, false, false},
new Object[] {false, true, false},
new Object[] {false, true, true});
}
@ParameterizedRobolectricTestRunner.Parameter(0)
public boolean mIsPrimaryUser;
@ParameterizedRobolectricTestRunner.Parameter(1)
public boolean mInstalled;
@ParameterizedRobolectricTestRunner.Parameter(2)
public boolean mEnabled;
@Mock private Context mContext;
@Mock private Context mUserContext;
@Mock private SettingsMainSwitchPreference mPreference;
@Mock private PreferenceScreen mPreferenceScreen;
@Mock private PackageManager mPackageManager;
@Mock private PackageInfo mPackageInfo;
private String mTerminalPackageName = "com.android.virtualization.terminal";
private EnableLinuxTerminalPreferenceController mController;
@Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
doReturn(mTerminalPackageName)
.when(mUserContext)
.getString(eq(TERMINAL_PACKAGE_NAME_RESID));
doReturn(mPackageManager).when(mUserContext).getPackageManager();
doReturn(mInstalled ? mPackageInfo : null)
.when(mPackageManager)
.getPackageInfo(eq(mTerminalPackageName), anyInt());
doReturn(
mEnabled
? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
: PackageManager.COMPONENT_ENABLED_STATE_DEFAULT)
.when(mPackageManager)
.getApplicationEnabledSetting(eq(mTerminalPackageName));
mController =
new EnableLinuxTerminalPreferenceController(mContext, mUserContext, mIsPrimaryUser);
doReturn(mPreference)
.when(mPreferenceScreen)
.findPreference(eq(mController.getPreferenceKey()));
mController.displayPreference(mPreferenceScreen);
mController.updateState(mPreference);
}
@Test
public void isAvailable_returnTrue() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void onCheckedChanged_whenChecked_turnOnTerminal() {
assumeTrue(mInstalled);
mController.onCheckedChanged(/* buttonView= */ null, /* isChecked= */ true);
verify(mPackageManager)
.setApplicationEnabledSetting(
mTerminalPackageName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
/* flags= */ 0);
}
@Test
public void onCheckedChanged_whenUnchecked_turnOffTerminal() {
assumeTrue(mInstalled);
mController.onCheckedChanged(/* buttonView= */ null, /* isChecked= */ false);
verify(mPackageManager)
.setApplicationEnabledSetting(
mTerminalPackageName,
PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
/* flags= */ 0);
}
@Test
public void updateState_enabled() {
verify(mPreference).setEnabled(/* enabled= */ true);
}
@Test
public void updateState_whenEnabled_checked() {
assumeTrue(mEnabled);
verify(mPreference).setChecked(/* checked= */ true);
}
@Test
public void updateState_whenDisabled_unchecked() {
assumeFalse(mEnabled);
verify(mPreference).setChecked(/* checked= */ false);
}
@Test
public void updateState_withProfileWhenAllowed_enabledByAdmin() {
assumeFalse(mIsPrimaryUser);
assumeTrue(mInstalled);
verify(mPreference).setDisabledByAdmin(eq(null));
}
@Test
public void updateState_withProfileWhenNotAllowed_disabledByAdmin() {
assumeFalse(mIsPrimaryUser);
assumeFalse(mInstalled);
verify(mPreference).setDisabledByAdmin(any(EnforcedAdmin.class));
}
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright 2024 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.development.linuxterminal;
import static com.android.settings.development.linuxterminal.LinuxTerminalPreferenceController.TERMINAL_PACKAGE_NAME_RESID;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.eq;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
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;
/** Tests {@link LinuxTerminalPreferenceController} */
@RunWith(RobolectricTestRunner.class)
public class LinuxTerminalPreferenceControllerTest {
@Mock private Context mContext;
@Mock private PackageManager mPackageManager;
@Mock private PackageInfo mPackageInfo;
private String mTerminalPackageName = "com.android.virtualization.terminal";
private LinuxTerminalPreferenceController mController;
@Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
doReturn(mTerminalPackageName).when(mContext).getString(TERMINAL_PACKAGE_NAME_RESID);
doReturn(mPackageManager).when(mContext).getPackageManager();
doReturn(mPackageInfo)
.when(mPackageManager)
.getPackageInfo(eq(mTerminalPackageName), anyInt());
}
@Test
public void isAvailable_whenPackageExists_returnsTrue() throws NameNotFoundException {
mController = new LinuxTerminalPreferenceController(mContext);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_whenPackageNameIsNull_returnsFalse() {
doReturn(null).when(mContext).getString(TERMINAL_PACKAGE_NAME_RESID);
mController = new LinuxTerminalPreferenceController(mContext);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_whenAppDoesNotExist_returnsFalse() throws Exception {
doThrow(new NameNotFoundException())
.when(mPackageManager)
.getPackageInfo(eq(mTerminalPackageName), anyInt());
mController = new LinuxTerminalPreferenceController(mContext);
assertThat(mController.isAvailable()).isFalse();
}
}