DO Disclosure: add UI that lists apps that were managed by owner:

- had permissions granted by admin
- were installed by owner via policy

Bug: 32692748
Test: m RunSettingsRoboTests
Change-Id: I365e2f8f351671e68f83cceb7c0ca241d7a5a588
This commit is contained in:
Denis Kuznetsov
2017-04-12 15:15:53 +02:00
parent 10ad029318
commit 60b2960cbb
22 changed files with 1544 additions and 186 deletions

View File

@@ -17,8 +17,6 @@
package com.android.settings.enterprise;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.preference.Preference;
import com.android.settings.R;
@@ -28,24 +26,21 @@ import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Answers;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.anyObject;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Common base for testing subclasses of {@link AdminGrantedPermissionsPreferenceControllerBase}.
*/
public abstract class AdminGrantedPermissionsPreferenceControllerTestBase {
protected final String mKey;
protected final String[] mPermissions;
protected final String mPermissionGroup;
@@ -123,19 +118,8 @@ public abstract class AdminGrantedPermissionsPreferenceControllerTestBase {
@Test
public void testHandlePreferenceTreeClick() {
final Preference preference = new Preference(mContext, null, 0, 0);
preference.setKey(mKey);
assertThat(mController.handlePreferenceTreeClick(preference)).isTrue();
final ArgumentCaptor<Intent> argumentCaptor = ArgumentCaptor.forClass(Intent.class);
verify(mContext).startActivity(argumentCaptor.capture());
final Intent intent = argumentCaptor.getValue();
assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MANAGE_PERMISSION_APPS);
assertThat(intent.getStringExtra(Intent.EXTRA_PERMISSION_NAME)).
isEqualTo(mPermissionGroup);
assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
.isFalse();
}
@Test

View File

@@ -0,0 +1,131 @@
/*
* 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.enterprise;
import android.content.Context;
import android.content.pm.UserInfo;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.PreferenceScreen;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.ApplicationFeatureProvider;
import com.android.settings.applications.UserAppInfo;
import com.android.settings.core.PreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import java.util.ArrayList;
import java.util.List;
import static com.android.settings.testutils.ApplicationTestUtils.buildInfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class ApplicationListFragmentTest {
private static final int USER_ID = 0;
private static final int USER_APP_UID = 0;
private static final String APP = "APP";
@Mock(answer = RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen;
@Mock(answer = RETURNS_DEEP_STUBS)
private PreferenceManager mPreferenceManager;
private ApplicationListFragment mFragment;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ShadowApplication.getInstance().getApplicationContext();
when(mPreferenceManager.getContext()).thenReturn(mContext);
mFragment = new ApplicationListFragmentTestable(mPreferenceManager, mScreen);
}
@Test
public void getMetricsCategory() {
assertThat(mFragment.getMetricsCategory())
.isEqualTo(MetricsEvent.ENTERPRISE_PRIVACY_SETTINGS);
}
@Test
public void getLogTag() {
assertThat(mFragment.getLogTag())
.isEqualTo("EnterprisePrivacySettings");
}
@Test
public void getScreenResource() {
assertThat(mFragment.getPreferenceScreenResId())
.isEqualTo(R.xml.app_list_disclosure_settings);
}
@Test
public void getPreferenceControllers() {
final List<PreferenceController> controllers = mFragment.getPreferenceControllers(mContext);
assertThat(controllers).isNotNull();
assertThat(controllers.size()).isEqualTo(1);
int position = 0;
assertThat(controllers.get(position++)).isInstanceOf(
ApplicationListPreferenceController.class);
}
private static class ApplicationListFragmentTestable extends ApplicationListFragment {
private final PreferenceManager mPreferenceManager;
private final PreferenceScreen mPreferenceScreen;
public ApplicationListFragmentTestable(PreferenceManager preferenceManager,
PreferenceScreen screen) {
this.mPreferenceManager = preferenceManager;
this.mPreferenceScreen = screen;
}
@Override
public void buildApplicationList(Context context,
ApplicationFeatureProvider.ListOfAppsCallback callback) {
final List<UserAppInfo> apps = new ArrayList<>();
final UserInfo user = new UserInfo(USER_ID, "main", UserInfo.FLAG_ADMIN);
apps.add(new UserAppInfo(user, buildInfo(USER_APP_UID, APP, 0, 0)));
callback.onListOfAppsResult(apps);
}
@Override
public PreferenceManager getPreferenceManager() {
return mPreferenceManager;
}
@Override
public PreferenceScreen getPreferenceScreen() {
return mPreferenceScreen;
}
}
}

View File

@@ -0,0 +1,132 @@
/*
* 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.enterprise;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.ApplicationFeatureProvider;
import com.android.settings.applications.UserAppInfo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static com.android.settings.testutils.ApplicationTestUtils.buildInfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class ApplicationListPreferenceControllerTest {
private static final int MAIN_USER_ID = 0;
private static final int MANAGED_PROFILE_ID = 10;
private static final int PER_USER_UID_RANGE = 100000;
private static final int MAIN_USER_APP_UID = MAIN_USER_ID * PER_USER_UID_RANGE;
private static final int MANAGED_PROFILE_APP_UID = MANAGED_PROFILE_ID * PER_USER_UID_RANGE;
private static final String APP_1 = "APP_1";
private static final String APP_2 = "APP_2";
private static final String APP_3 = "APP_3";
@Mock(answer = RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen;
@Mock(answer = RETURNS_DEEP_STUBS)
private PackageManager mPackageManager;
@Mock(answer = RETURNS_DEEP_STUBS)
private SettingsPreferenceFragment mFragment;
private Context mContext;
private ApplicationListPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
final ShadowApplication shadowContext = ShadowApplication.getInstance();
mContext = shadowContext.getApplicationContext();
when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
when(mFragment.getPreferenceManager().getContext()).thenReturn(mContext);
when(mPackageManager.getText(eq(APP_1), anyInt(), any())).thenReturn(APP_1);
when(mPackageManager.getText(eq(APP_2), anyInt(), any())).thenReturn(APP_2);
when(mPackageManager.getText(eq(APP_3), anyInt(), any())).thenReturn(APP_3);
mController = new ApplicationListPreferenceController(mContext, new ThreeAppsBuilder(),
mPackageManager, mFragment);
}
@Test
public void checkNumberAndTitlesOfApps() {
ArgumentCaptor<Preference> apps = ArgumentCaptor.forClass(Preference.class);
verify(mScreen, times(3)).addPreference(apps.capture());
final Set<String> expectedPackages = new HashSet<>(Arrays.asList(APP_1, APP_2, APP_3));
final Set<String> packages = new HashSet<>();
for (Preference p : apps.getAllValues()) {
packages.add(p.getTitle().toString());
}
assertThat(packages).isEqualTo(expectedPackages);
}
@Test
public void isAvailable() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void getPreferenceKey() {
assertThat(mController.getPreferenceKey()).isNull();
}
private static class ThreeAppsBuilder
implements ApplicationListPreferenceController.ApplicationListBuilder {
@Override
public void buildApplicationList(Context context,
ApplicationFeatureProvider.ListOfAppsCallback callback) {
final List<UserAppInfo> apps = new ArrayList<>();
final UserInfo user = new UserInfo(MAIN_USER_ID, "main", UserInfo.FLAG_ADMIN);
apps.add(new UserAppInfo(user, buildInfo(MAIN_USER_APP_UID, APP_1, 0, 0)));
apps.add(new UserAppInfo(user, buildInfo(MAIN_USER_APP_UID, APP_2, 0, 0)));
apps.add(new UserAppInfo(user, buildInfo(MANAGED_PROFILE_APP_UID, APP_3, 0, 0)));
callback.onListOfAppsResult(apps);
}
}
}