Files
app_Settings/tests/robotests/src/com/android/settings/applications/InstalledAppDetailsTest.java
Daniel Nishi 0d5d180d7d Lower case the first letter of storage type.
In the app details, the first letter of the storage type was
capitalized, causing an odd string of "X used in Internal storage."

Change-Id: I2b4f23ea1799dc1730fdab596e4a87a0cb673c68
Fixes: 36001265
Test: Settings robotest
2017-03-28 17:07:30 -07:00

272 lines
11 KiB
Java

/*
* 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.applications;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.os.UserManager;
import android.support.v7.preference.Preference;
import android.view.View;
import android.widget.Button;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.instantapps.InstantAppButtonsController;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.applications.AppUtils;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
import com.android.settingslib.applications.instantapps.InstantAppDataProvider;
import com.android.settingslib.applications.StorageStatsSource.AppStorageStats;
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.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
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 final class InstalledAppDetailsTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock
ApplicationFeatureProvider mApplicationFeatureProvider;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private UserManager mUserManager;
@Mock
private Activity mActivity;
@Mock
private DevicePolicyManager mDevicePolicyManager;
private InstalledAppDetails mAppDetail;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mAppDetail = new InstalledAppDetails();
// Default to not considering any apps to be instant (individual tests can override this).
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
(InstantAppDataProvider) (i -> false));
}
@Test
public void shouldShowUninstallForAll_installForOneOtherUserOnly_shouldReturnTrue() {
when(mDevicePolicyManager.packageHasActiveAdmins(anyString())).thenReturn(false);
when(mUserManager.getUsers().size()).thenReturn(2);
ReflectionHelpers.setField(mAppDetail, "mDpm", mDevicePolicyManager);
ReflectionHelpers.setField(mAppDetail, "mUserManager", mUserManager);
final ApplicationInfo info = new ApplicationInfo();
info.enabled = true;
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = info;
final PackageInfo packageInfo = mock(PackageInfo.class);
ReflectionHelpers.setField(mAppDetail, "mPackageInfo", packageInfo);
assertThat(mAppDetail.shouldShowUninstallForAll(appEntry)).isTrue();
}
@Test
public void shouldShowUninstallForAll_installForSelfOnly_shouldReturnFalse() {
when(mDevicePolicyManager.packageHasActiveAdmins(anyString())).thenReturn(false);
when(mUserManager.getUsers().size()).thenReturn(2);
ReflectionHelpers.setField(mAppDetail, "mDpm", mDevicePolicyManager);
ReflectionHelpers.setField(mAppDetail, "mUserManager", mUserManager);
final ApplicationInfo info = new ApplicationInfo();
info.flags = ApplicationInfo.FLAG_INSTALLED;
info.enabled = true;
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = info;
final PackageInfo packageInfo = mock(PackageInfo.class);
ReflectionHelpers.setField(mAppDetail, "mPackageInfo", packageInfo);
assertThat(mAppDetail.shouldShowUninstallForAll(appEntry)).isFalse();
}
@Test
public void getStorageSummary_shouldWorkForExternal() {
Context context = RuntimeEnvironment.application.getApplicationContext();
AppStorageStats stats = mock(AppStorageStats.class);
when(stats.getTotalBytes()).thenReturn(1L);
assertThat(InstalledAppDetails.getStorageSummary(context, stats, true))
.isEqualTo("1.00B used in external storage");
}
@Test
public void getStorageSummary_shouldWorkForInternal() {
Context context = RuntimeEnvironment.application.getApplicationContext();
AppStorageStats stats = mock(AppStorageStats.class);
when(stats.getTotalBytes()).thenReturn(1L);
assertThat(InstalledAppDetails.getStorageSummary(context, stats, false))
.isEqualTo("1.00B used in internal storage");
}
@Test
public void launchFragment_hasNoPackageInfo_shouldFinish() {
ReflectionHelpers.setField(mAppDetail, "mPackageInfo", null);
assertThat(mAppDetail.ensurePackageInfoAvailable(mActivity)).isFalse();
verify(mActivity).finishAndRemoveTask();
}
@Test
public void launchFragment_hasPackageInfo_shouldReturnTrue() {
final PackageInfo packageInfo = mock(PackageInfo.class);
ReflectionHelpers.setField(mAppDetail, "mPackageInfo", packageInfo);
assertThat(mAppDetail.ensurePackageInfoAvailable(mActivity)).isTrue();
verify(mActivity, never()).finishAndRemoveTask();
}
// Tests that we don't show the "uninstall for all users" button for instant apps.
@Test
public void instantApps_noUninstallForAllButton() {
// Make this app appear to be instant.
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
(InstantAppDataProvider) (i -> true));
when(mDevicePolicyManager.packageHasActiveAdmins(anyString())).thenReturn(false);
when(mUserManager.getUsers().size()).thenReturn(2);
final ApplicationInfo info = new ApplicationInfo();
info.enabled = true;
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = info;
final PackageInfo packageInfo = mock(PackageInfo.class);
ReflectionHelpers.setField(mAppDetail, "mDpm", mDevicePolicyManager);
ReflectionHelpers.setField(mAppDetail, "mUserManager", mUserManager);
ReflectionHelpers.setField(mAppDetail, "mPackageInfo", packageInfo);
assertThat(mAppDetail.shouldShowUninstallForAll(appEntry)).isFalse();
}
// Tests that we don't show the uninstall button for instant apps"
@Test
public void instantApps_noUninstallButton() {
// Make this app appear to be instant.
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
(InstantAppDataProvider) (i -> true));
final ApplicationInfo info = new ApplicationInfo();
info.flags = ApplicationInfo.FLAG_INSTALLED;
info.enabled = true;
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = info;
final PackageInfo packageInfo = mock(PackageInfo.class);
packageInfo.applicationInfo = info;
final Button uninstallButton = mock(Button.class);
ReflectionHelpers.setField(mAppDetail, "mUserManager", mUserManager);
ReflectionHelpers.setField(mAppDetail, "mAppEntry", appEntry);
ReflectionHelpers.setField(mAppDetail, "mPackageInfo", packageInfo);
ReflectionHelpers.setField(mAppDetail, "mUninstallButton", uninstallButton);
mAppDetail.initUnintsallButtonForUserApp();
verify(uninstallButton).setVisibility(View.GONE);
}
// Tests that we don't show the force stop button for instant apps (they aren't allowed to run
// when they aren't in the foreground).
@Test
public void instantApps_noForceStop() {
// Make this app appear to be instant.
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
(InstantAppDataProvider) (i -> true));
final PackageInfo packageInfo = mock(PackageInfo.class);
final AppEntry appEntry = mock(AppEntry.class);
final ApplicationInfo info = new ApplicationInfo();
appEntry.info = info;
final Button forceStopButton = mock(Button.class);
ReflectionHelpers.setField(mAppDetail, "mDpm", mDevicePolicyManager);
ReflectionHelpers.setField(mAppDetail, "mPackageInfo", packageInfo);
ReflectionHelpers.setField(mAppDetail, "mAppEntry", appEntry);
ReflectionHelpers.setField(mAppDetail, "mForceStopButton", forceStopButton);
mAppDetail.checkForceStop();
verify(forceStopButton).setVisibility(View.GONE);
}
// A helper class for testing the InstantAppButtonsController - it lets us look up the
// preference associated with a key for instant app buttons and get back a mock
// LayoutPreference (to avoid a null pointer exception).
public static class InstalledAppDetailsWithMockInstantButtons extends InstalledAppDetails {
@Mock
private LayoutPreference mInstantButtons;
public InstalledAppDetailsWithMockInstantButtons() {
super();
MockitoAnnotations.initMocks(this);
}
@Override
public Preference findPreference(CharSequence key) {
if (key == "instant_app_buttons") {
return mInstantButtons;
}
return super.findPreference(key);
}
}
@Test
public void instantApps_instantSpecificButtons() {
// Make this app appear to be instant.
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
(InstantAppDataProvider) (i -> true));
final PackageInfo packageInfo = mock(PackageInfo.class);
final InstalledAppDetailsWithMockInstantButtons
fragment = new InstalledAppDetailsWithMockInstantButtons();
ReflectionHelpers.setField(fragment, "mPackageInfo", packageInfo);
final InstantAppButtonsController buttonsController =
mock(InstantAppButtonsController.class);
when(buttonsController.setPackageName(anyString())).thenReturn(buttonsController);
FakeFeatureFactory.setupForTest(mContext);
FakeFeatureFactory factory =
(FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
when(factory.applicationFeatureProvider.newInstantAppButtonsController(any(),
any())).thenReturn(buttonsController);
fragment.maybeAddInstantAppButtons();
verify(buttonsController).setPackageName(anyString());
verify(buttonsController).show();
}
}