Improve launch time for app & notification screen
- We won't query recent apps in getAvailabilityStatus(). And then we do that in background thread. So, we can launch app & notificatins screen as soon as possible. - Create a RecentAppStatsMixin class which is responsible for maintaining the recent apps data. - Controllers and fragment update their UI after they got onReloadDataCompleted callback from RecentAppStatsMixin. Test: manual, robotest Fixes: 128849426 Fixes: 126453868 Change-Id: I636d5878cb5d53496978fe613c625382d8d382bc
This commit is contained in:
@@ -17,16 +17,25 @@
|
||||
package com.android.settings.applications;
|
||||
|
||||
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
|
||||
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
|
||||
|
||||
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.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.usage.UsageStats;
|
||||
import android.content.Context;
|
||||
import android.os.UserManager;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
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;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@@ -36,29 +45,45 @@ import java.util.List;
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class AllAppsInfoPreferenceControllerTest {
|
||||
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
private AllAppsInfoPreferenceController mController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
final Context context = RuntimeEnvironment.application;
|
||||
MockitoAnnotations.initMocks(this);
|
||||
final Context context = spy(RuntimeEnvironment.application);
|
||||
final Preference preference = new Preference(context);
|
||||
doReturn(mUserManager).when(context).getSystemService(Context.USER_SERVICE);
|
||||
when(mUserManager.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[]{});
|
||||
mController = new AllAppsInfoPreferenceController(context, "test_key");
|
||||
mController.mPreference = preference;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_hasRecentApps_shouldReturnConditionallyUnavailable() {
|
||||
public void getAvailabilityStatus_shouldReturnAVAILABLE() {
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onReloadDataCompleted_recentAppsSet_hidePreference() {
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
final UsageStats stat1 = new UsageStats();
|
||||
stat1.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat1.mPackageName = "pkg.class";
|
||||
stats.add(stat1);
|
||||
mController.setRecentApps(stats);
|
||||
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
|
||||
mController.onReloadDataCompleted(stats);
|
||||
|
||||
assertThat(mController.mPreference.isVisible()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_noRecentApps_shouldReturnAvailable() {
|
||||
// No data
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
|
||||
public void onReloadDataCompleted_noRecentAppSet_showPreference() {
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
|
||||
mController.onReloadDataCompleted(stats);
|
||||
|
||||
assertThat(mController.mPreference.isVisible()).isTrue();
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,309 @@
|
||||
/*
|
||||
* Copyright (C) 2019 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 static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.usage.UsageStats;
|
||||
import android.app.usage.UsageStatsManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.ModuleInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.os.PowerManager;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
|
||||
import com.android.settingslib.applications.AppUtils;
|
||||
import com.android.settingslib.applications.ApplicationsState;
|
||||
import com.android.settingslib.applications.instantapps.InstantAppDataProvider;
|
||||
|
||||
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;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class RecentAppStatsMixinTest {
|
||||
|
||||
@Mock
|
||||
private UsageStatsManager mUsageStatsManager;
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
@Mock
|
||||
private ApplicationsState mAppState;
|
||||
@Mock
|
||||
private PackageManager mPackageManager;
|
||||
@Mock
|
||||
private ApplicationsState.AppEntry mAppEntry;
|
||||
@Mock
|
||||
private ApplicationInfo mApplicationInfo;
|
||||
@Mock
|
||||
private PowerManager mPowerManager;
|
||||
|
||||
private RecentAppStatsMixin mRecentAppStatsMixin;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
final Context context = spy(RuntimeEnvironment.application);
|
||||
when(context.getApplicationContext()).thenReturn(context);
|
||||
ReflectionHelpers.setStaticField(ApplicationsState.class, "sInstance", mAppState);
|
||||
doReturn(mUsageStatsManager).when(context).getSystemService(Context.USAGE_STATS_SERVICE);
|
||||
doReturn(mUserManager).when(context).getSystemService(Context.USER_SERVICE);
|
||||
doReturn(mPackageManager).when(context).getPackageManager();
|
||||
doReturn(mPowerManager).when(context).getSystemService(PowerManager.class);
|
||||
when(mUserManager.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[]{});
|
||||
|
||||
mRecentAppStatsMixin = new RecentAppStatsMixin(context, 3 /* maximumApps */);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadDisplayableRecentApps_oneValidRecentAppSet_shouldHaveOneRecentApp() {
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
final UsageStats stat1 = new UsageStats();
|
||||
stat1.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat1.mPackageName = "pkg.class";
|
||||
stats.add(stat1);
|
||||
// stat1 is valid app.
|
||||
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mAppEntry);
|
||||
when(mPackageManager.resolveActivity(any(Intent.class), anyInt()))
|
||||
.thenReturn(new ResolveInfo());
|
||||
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
|
||||
.thenReturn(stats);
|
||||
mAppEntry.info = mApplicationInfo;
|
||||
|
||||
mRecentAppStatsMixin.loadDisplayableRecentApps(3);
|
||||
|
||||
assertThat(mRecentAppStatsMixin.mRecentApps.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadDisplayableRecentApps_threeValidRecentAppsSet_shouldHaveThreeRecentApps() {
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
final UsageStats stat1 = new UsageStats();
|
||||
final UsageStats stat2 = new UsageStats();
|
||||
final UsageStats stat3 = new UsageStats();
|
||||
stat1.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat1.mPackageName = "pkg.class";
|
||||
stats.add(stat1);
|
||||
|
||||
stat2.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat2.mPackageName = "pkg.class2";
|
||||
stats.add(stat2);
|
||||
|
||||
stat3.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat3.mPackageName = "pkg.class3";
|
||||
stats.add(stat3);
|
||||
|
||||
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mAppEntry);
|
||||
when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mAppEntry);
|
||||
when(mAppState.getEntry(stat3.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mAppEntry);
|
||||
when(mPackageManager.resolveActivity(any(Intent.class), anyInt()))
|
||||
.thenReturn(new ResolveInfo());
|
||||
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
|
||||
.thenReturn(stats);
|
||||
mAppEntry.info = mApplicationInfo;
|
||||
|
||||
mRecentAppStatsMixin.loadDisplayableRecentApps(3);
|
||||
|
||||
assertThat(mRecentAppStatsMixin.mRecentApps.size()).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadDisplayableRecentApps_oneValidAndTwoInvalidSet_shouldHaveOneRecentApp() {
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
final UsageStats stat1 = new UsageStats();
|
||||
final UsageStats stat2 = new UsageStats();
|
||||
final UsageStats stat3 = new UsageStats();
|
||||
stat1.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat1.mPackageName = "pkg.class";
|
||||
stats.add(stat1);
|
||||
|
||||
stat2.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat2.mPackageName = "com.android.settings";
|
||||
stats.add(stat2);
|
||||
|
||||
stat3.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat3.mPackageName = "pkg.class3";
|
||||
stats.add(stat3);
|
||||
|
||||
// stat1, stat2 are valid apps. stat3 is invalid.
|
||||
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mAppEntry);
|
||||
when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mAppEntry);
|
||||
when(mAppState.getEntry(stat3.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(null);
|
||||
when(mPackageManager.resolveActivity(any(Intent.class), anyInt()))
|
||||
.thenReturn(new ResolveInfo());
|
||||
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
|
||||
.thenReturn(stats);
|
||||
mAppEntry.info = mApplicationInfo;
|
||||
|
||||
mRecentAppStatsMixin.loadDisplayableRecentApps(3);
|
||||
|
||||
// Only stat1. stat2 is skipped because of the package name, stat3 skipped because
|
||||
// it's invalid app.
|
||||
assertThat(mRecentAppStatsMixin.mRecentApps.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadDisplayableRecentApps_oneInstantAppSet_shouldHaveOneRecentApp() {
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
// Instant app.
|
||||
final UsageStats stat = new UsageStats();
|
||||
stat.mLastTimeUsed = System.currentTimeMillis() + 200;
|
||||
stat.mPackageName = "com.foo.barinstant";
|
||||
stats.add(stat);
|
||||
|
||||
ApplicationsState.AppEntry statEntry = mock(ApplicationsState.AppEntry.class);
|
||||
statEntry.info = mApplicationInfo;
|
||||
|
||||
when(mAppState.getEntry(stat.mPackageName, UserHandle.myUserId())).thenReturn(statEntry);
|
||||
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
|
||||
.thenReturn(stats);
|
||||
|
||||
// Make sure stat is considered an instant app.
|
||||
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
|
||||
(InstantAppDataProvider) (ApplicationInfo info) -> info == statEntry.info);
|
||||
|
||||
mRecentAppStatsMixin.loadDisplayableRecentApps(3);
|
||||
|
||||
assertThat(mRecentAppStatsMixin.mRecentApps.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadDisplayableRecentApps_withNullAppEntryOrInfo_shouldNotCrash() {
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
final UsageStats stat1 = new UsageStats();
|
||||
final UsageStats stat2 = new UsageStats();
|
||||
stat1.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat1.mPackageName = "pkg.class";
|
||||
stats.add(stat1);
|
||||
|
||||
stat2.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat2.mPackageName = "pkg.class2";
|
||||
stats.add(stat2);
|
||||
|
||||
// app1 has AppEntry with null info, app2 has null AppEntry.
|
||||
mAppEntry.info = null;
|
||||
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mAppEntry);
|
||||
when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(null);
|
||||
|
||||
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
|
||||
.thenReturn(stats);
|
||||
|
||||
// We should not crash here.
|
||||
mRecentAppStatsMixin.loadDisplayableRecentApps(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadDisplayableRecentApps_hiddenSystemModuleSet_shouldNotHaveHiddenSystemModule() {
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
// Regular app.
|
||||
final UsageStats stat1 = new UsageStats();
|
||||
stat1.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat1.mPackageName = "com.foo.bar";
|
||||
stats.add(stat1);
|
||||
|
||||
// Hidden system module.
|
||||
final UsageStats stat2 = new UsageStats();
|
||||
stat2.mLastTimeUsed = System.currentTimeMillis() + 200;
|
||||
stat2.mPackageName = "com.foo.hidden";
|
||||
stats.add(stat2);
|
||||
|
||||
ApplicationsState.AppEntry stat1Entry = mock(ApplicationsState.AppEntry.class);
|
||||
ApplicationsState.AppEntry stat2Entry = mock(ApplicationsState.AppEntry.class);
|
||||
stat1Entry.info = mApplicationInfo;
|
||||
stat2Entry.info = mApplicationInfo;
|
||||
|
||||
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId())).thenReturn(stat1Entry);
|
||||
when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId())).thenReturn(stat2Entry);
|
||||
|
||||
final ModuleInfo moduleInfo1 = new ModuleInfo();
|
||||
moduleInfo1.setPackageName(stat1.mPackageName);
|
||||
moduleInfo1.setHidden(false);
|
||||
|
||||
final ModuleInfo moduleInfo2 = new ModuleInfo();
|
||||
moduleInfo2.setPackageName(stat2.mPackageName);
|
||||
moduleInfo2.setHidden(true);
|
||||
|
||||
ReflectionHelpers.setStaticField(ApplicationsState.class, "sInstance", null);
|
||||
final List<ModuleInfo> modules = new ArrayList<>();
|
||||
modules.add(moduleInfo2);
|
||||
when(mPackageManager.getInstalledModules(anyInt() /* flags */))
|
||||
.thenReturn(modules);
|
||||
|
||||
when(mPackageManager.resolveActivity(any(Intent.class), anyInt()))
|
||||
.thenReturn(new ResolveInfo());
|
||||
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
|
||||
.thenReturn(stats);
|
||||
|
||||
mRecentAppStatsMixin.loadDisplayableRecentApps(3);
|
||||
|
||||
assertThat(mRecentAppStatsMixin.mRecentApps.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadDisplayableRecentApps_powerSaverModeOn_shouldHaveEmptyList() {
|
||||
when(mPowerManager.isPowerSaveMode()).thenReturn(true);
|
||||
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
final UsageStats stat1 = new UsageStats();
|
||||
|
||||
stat1.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat1.mPackageName = "pkg.class";
|
||||
stats.add(stat1);
|
||||
|
||||
// stat1, stat2 are valid apps. stat3 is invalid.
|
||||
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mAppEntry);
|
||||
when(mPackageManager.resolveActivity(any(Intent.class), anyInt()))
|
||||
.thenReturn(new ResolveInfo());
|
||||
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
|
||||
.thenReturn(stats);
|
||||
mAppEntry.info = mApplicationInfo;
|
||||
|
||||
mRecentAppStatsMixin.loadDisplayableRecentApps(3);
|
||||
|
||||
assertThat(mRecentAppStatsMixin.mRecentApps).isEmpty();
|
||||
}
|
||||
}
|
@@ -16,33 +16,23 @@
|
||||
|
||||
package com.android.settings.applications;
|
||||
|
||||
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
|
||||
import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE;
|
||||
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.usage.UsageStats;
|
||||
import android.app.usage.UsageStatsManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.ModuleInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.os.PowerManager;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.view.LayoutInflater;
|
||||
@@ -53,9 +43,7 @@ import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settingslib.applications.AppUtils;
|
||||
import com.android.settingslib.applications.ApplicationsState;
|
||||
import com.android.settingslib.applications.instantapps.InstantAppDataProvider;
|
||||
import com.android.settingslib.widget.AppEntitiesHeaderController;
|
||||
import com.android.settingslib.widget.AppEntityInfo;
|
||||
import com.android.settingslib.widget.LayoutPreference;
|
||||
@@ -63,7 +51,6 @@ import com.android.settingslib.widget.LayoutPreference;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentMatcher;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
@@ -79,8 +66,6 @@ public class RecentAppsPreferenceControllerTest {
|
||||
@Mock
|
||||
private PreferenceScreen mScreen;
|
||||
@Mock
|
||||
private UsageStatsManager mUsageStatsManager;
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
@Mock
|
||||
private ApplicationsState mAppState;
|
||||
@@ -91,11 +76,8 @@ public class RecentAppsPreferenceControllerTest {
|
||||
@Mock
|
||||
private ApplicationInfo mApplicationInfo;
|
||||
@Mock
|
||||
private PowerManager mPowerManager;
|
||||
@Mock
|
||||
private Fragment mFragment;
|
||||
|
||||
private LayoutPreference mRecentAppsPreference;
|
||||
private RecentAppsPreferenceController mController;
|
||||
|
||||
@Before
|
||||
@@ -104,92 +86,33 @@ public class RecentAppsPreferenceControllerTest {
|
||||
final Context context = spy(RuntimeEnvironment.application);
|
||||
when(context.getApplicationContext()).thenReturn(context);
|
||||
ReflectionHelpers.setStaticField(ApplicationsState.class, "sInstance", mAppState);
|
||||
doReturn(mUsageStatsManager).when(context).getSystemService(Context.USAGE_STATS_SERVICE);
|
||||
doReturn(mUserManager).when(context).getSystemService(Context.USER_SERVICE);
|
||||
doReturn(mPackageManager).when(context).getPackageManager();
|
||||
doReturn(mPowerManager).when(context).getSystemService(PowerManager.class);
|
||||
when(mUserManager.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[]{});
|
||||
|
||||
final View appEntitiesHeaderView = LayoutInflater.from(context).inflate(
|
||||
R.layout.app_entities_header, null /* root */);
|
||||
final Preference dividerPreference = new Preference(context);
|
||||
mRecentAppsPreference = spy(new LayoutPreference(context, appEntitiesHeaderView));
|
||||
final LayoutPreference recentAppsPreference =
|
||||
spy(new LayoutPreference(context, appEntitiesHeaderView));
|
||||
|
||||
mController = spy(new RecentAppsPreferenceController(context, "test_key"));
|
||||
mController.setFragment(mFragment);
|
||||
|
||||
mController.mAppEntitiesController = mock(AppEntitiesHeaderController.class);
|
||||
mController.mRecentAppsPreference = mRecentAppsPreference;
|
||||
mController.mRecentAppsPreference = recentAppsPreference;
|
||||
mController.mDivider = dividerPreference;
|
||||
|
||||
when(mScreen.findPreference(RecentAppsPreferenceController.KEY_DIVIDER))
|
||||
.thenReturn(dividerPreference);
|
||||
when(mScreen.findPreference("test_key")).thenReturn(mRecentAppsPreference);
|
||||
when(mRecentAppsPreference.findViewById(R.id.app_entities_header)).thenReturn(
|
||||
when(mScreen.findPreference("test_key")).thenReturn(recentAppsPreference);
|
||||
when(recentAppsPreference.findViewById(R.id.app_entities_header)).thenReturn(
|
||||
appEntitiesHeaderView);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_hasRecentApps_shouldReturnAvailable() {
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
final UsageStats stat1 = new UsageStats();
|
||||
stat1.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat1.mPackageName = "pkg.class";
|
||||
stats.add(stat1);
|
||||
// stat1 is valid app.
|
||||
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mAppEntry);
|
||||
when(mPackageManager.resolveActivity(any(Intent.class), anyInt()))
|
||||
.thenReturn(new ResolveInfo());
|
||||
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
|
||||
.thenReturn(stats);
|
||||
mAppEntry.info = mApplicationInfo;
|
||||
mController.reloadData();
|
||||
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_noRecentApps_shouldReturnConditionallyUnavailable() {
|
||||
// No data
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_powerSaverModeOn_shouldReturnConditionallyUnavailable() {
|
||||
when(mPowerManager.isPowerSaveMode()).thenReturn(true);
|
||||
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
final UsageStats stat1 = new UsageStats();
|
||||
|
||||
stat1.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat1.mPackageName = "pkg.class";
|
||||
stats.add(stat1);
|
||||
|
||||
// stat1, stat2 are valid apps. stat3 is invalid.
|
||||
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mAppEntry);
|
||||
when(mPackageManager.resolveActivity(any(Intent.class), anyInt()))
|
||||
.thenReturn(new ResolveInfo());
|
||||
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
|
||||
.thenReturn(stats);
|
||||
mAppEntry.info = mApplicationInfo;
|
||||
mController.reloadData();
|
||||
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference_shouldNotReloadData() {
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
verify(mController, never()).reloadData();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference_shouldRefreshUi() {
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
verify(mController).refreshUi();
|
||||
public void getAvailabilityStatus_shouldReturnAVAILABLE_UNSEARCHABLE() {
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE_UNSEARCHABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -200,7 +123,7 @@ public class RecentAppsPreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_threeValidRecentOpenAppsSet_setAppEntityThreeTime() {
|
||||
public void onReloadDataCompleted_threeValidRecentOpenAppsSet_setAppEntityThreeTime() {
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
final UsageStats stat1 = new UsageStats();
|
||||
final UsageStats stat2 = new UsageStats();
|
||||
@@ -216,22 +139,15 @@ public class RecentAppsPreferenceControllerTest {
|
||||
stat3.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat3.mPackageName = "pkg.class3";
|
||||
stats.add(stat3);
|
||||
|
||||
// stat1, stat2 are valid apps. stat3 is invalid.
|
||||
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mAppEntry);
|
||||
when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mAppEntry);
|
||||
when(mAppState.getEntry(stat3.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mAppEntry);
|
||||
when(mPackageManager.resolveActivity(any(Intent.class), anyInt()))
|
||||
.thenReturn(new ResolveInfo());
|
||||
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
|
||||
.thenReturn(stats);
|
||||
mAppEntry.info = mApplicationInfo;
|
||||
mController.reloadData();
|
||||
|
||||
mController.updateState(mRecentAppsPreference);
|
||||
mController.onReloadDataCompleted(stats);
|
||||
|
||||
verify(mController.mAppEntitiesController, times(3))
|
||||
.setAppEntity(anyInt(), any(AppEntityInfo.class));
|
||||
@@ -240,165 +156,12 @@ public class RecentAppsPreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_oneValidRecentOpenAppSet_setAppEntityOneTime() {
|
||||
public void onReloadDataCompleted_noRecentOpenAppsSet_shouldHideRecentAppPreference() {
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
final UsageStats stat1 = new UsageStats();
|
||||
final UsageStats stat2 = new UsageStats();
|
||||
final UsageStats stat3 = new UsageStats();
|
||||
stat1.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat1.mPackageName = "pkg.class";
|
||||
stats.add(stat1);
|
||||
|
||||
stat2.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat2.mPackageName = "com.android.settings";
|
||||
stats.add(stat2);
|
||||
mController.onReloadDataCompleted(stats);
|
||||
|
||||
stat3.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat3.mPackageName = "pkg.class3";
|
||||
stats.add(stat3);
|
||||
|
||||
// stat1, stat2 are valid apps. stat3 is invalid.
|
||||
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mAppEntry);
|
||||
when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mAppEntry);
|
||||
when(mAppState.getEntry(stat3.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(null);
|
||||
when(mPackageManager.resolveActivity(any(Intent.class), anyInt()))
|
||||
.thenReturn(new ResolveInfo());
|
||||
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
|
||||
.thenReturn(stats);
|
||||
mAppEntry.info = mApplicationInfo;
|
||||
mController.reloadData();
|
||||
|
||||
mController.updateState(mRecentAppsPreference);
|
||||
|
||||
// Only add stat1. stat2 is skipped because of the package name, stat3 skipped because
|
||||
// it's invalid app.
|
||||
verify(mController.mAppEntitiesController, times(1))
|
||||
.setAppEntity(anyInt(), any(AppEntityInfo.class));
|
||||
assertThat(mController.mRecentAppsPreference.isVisible()).isTrue();
|
||||
assertThat(mController.mDivider.isVisible()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_instantAppSet_shouldSetAppEntityForInstantApp() {
|
||||
// Regular app.
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
final UsageStats stat1 = new UsageStats();
|
||||
stat1.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat1.mPackageName = "com.foo.bar";
|
||||
stats.add(stat1);
|
||||
|
||||
// Instant app.
|
||||
final UsageStats stat2 = new UsageStats();
|
||||
stat2.mLastTimeUsed = System.currentTimeMillis() + 200;
|
||||
stat2.mPackageName = "com.foo.barinstant";
|
||||
stats.add(stat2);
|
||||
|
||||
ApplicationsState.AppEntry stat1Entry = mock(ApplicationsState.AppEntry.class);
|
||||
ApplicationsState.AppEntry stat2Entry = mock(ApplicationsState.AppEntry.class);
|
||||
stat1Entry.info = mApplicationInfo;
|
||||
stat2Entry.info = mApplicationInfo;
|
||||
|
||||
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId())).thenReturn(stat1Entry);
|
||||
when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId())).thenReturn(stat2Entry);
|
||||
|
||||
// Only the regular app stat1 should have its intent resolve.
|
||||
when(mPackageManager.resolveActivity(argThat(intentMatcher(stat1.mPackageName)), anyInt()))
|
||||
.thenReturn(new ResolveInfo());
|
||||
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
|
||||
.thenReturn(stats);
|
||||
|
||||
// Make sure stat2 is considered an instant app.
|
||||
ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider",
|
||||
(InstantAppDataProvider) (ApplicationInfo info) -> info == stat2Entry.info);
|
||||
mController.reloadData();
|
||||
|
||||
mController.updateState(mRecentAppsPreference);
|
||||
|
||||
verify(mController.mAppEntitiesController, times(2))
|
||||
.setAppEntity(anyInt(), any(AppEntityInfo.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_withNullAppEntryOrInfo_shouldNotCrash() {
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
final UsageStats stat1 = new UsageStats();
|
||||
final UsageStats stat2 = new UsageStats();
|
||||
stat1.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat1.mPackageName = "pkg.class";
|
||||
stats.add(stat1);
|
||||
|
||||
stat2.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat2.mPackageName = "pkg.class2";
|
||||
stats.add(stat2);
|
||||
|
||||
// app1 has AppEntry with null info, app2 has null AppEntry.
|
||||
mAppEntry.info = null;
|
||||
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(mAppEntry);
|
||||
when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId()))
|
||||
.thenReturn(null);
|
||||
|
||||
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
|
||||
.thenReturn(stats);
|
||||
|
||||
// We should not crash here.
|
||||
mController.updateState(mRecentAppsPreference);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_hiddenSystemModuleSet_shouldNotShowHiddenSystemModule() {
|
||||
final List<UsageStats> stats = new ArrayList<>();
|
||||
// Regular app.
|
||||
final UsageStats stat1 = new UsageStats();
|
||||
stat1.mLastTimeUsed = System.currentTimeMillis();
|
||||
stat1.mPackageName = "com.foo.bar";
|
||||
stats.add(stat1);
|
||||
|
||||
// Hidden system module.
|
||||
final UsageStats stat2 = new UsageStats();
|
||||
stat2.mLastTimeUsed = System.currentTimeMillis() + 200;
|
||||
stat2.mPackageName = "com.foo.hidden";
|
||||
stats.add(stat2);
|
||||
|
||||
ApplicationsState.AppEntry stat1Entry = mock(ApplicationsState.AppEntry.class);
|
||||
ApplicationsState.AppEntry stat2Entry = mock(ApplicationsState.AppEntry.class);
|
||||
stat1Entry.info = mApplicationInfo;
|
||||
stat2Entry.info = mApplicationInfo;
|
||||
|
||||
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId())).thenReturn(stat1Entry);
|
||||
when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId())).thenReturn(stat2Entry);
|
||||
|
||||
final ModuleInfo moduleInfo1 = new ModuleInfo();
|
||||
moduleInfo1.setPackageName(stat1.mPackageName);
|
||||
moduleInfo1.setHidden(false);
|
||||
|
||||
final ModuleInfo moduleInfo2 = new ModuleInfo();
|
||||
moduleInfo2.setPackageName(stat2.mPackageName);
|
||||
moduleInfo2.setHidden(true);
|
||||
|
||||
ReflectionHelpers.setStaticField(ApplicationsState.class, "sInstance", null);
|
||||
final List<ModuleInfo> modules = new ArrayList<>();
|
||||
modules.add(moduleInfo2);
|
||||
when(mPackageManager.getInstalledModules(anyInt() /* flags */))
|
||||
.thenReturn(modules);
|
||||
|
||||
when(mPackageManager.resolveActivity(any(Intent.class), anyInt()))
|
||||
.thenReturn(new ResolveInfo());
|
||||
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
|
||||
.thenReturn(stats);
|
||||
mController.reloadData();
|
||||
|
||||
mController.updateState(mRecentAppsPreference);
|
||||
|
||||
// Only add stat1. stat2 is skipped because it is hidden module.
|
||||
verify(mController.mAppEntitiesController).setAppEntity(anyInt(), any(AppEntityInfo.class));
|
||||
}
|
||||
|
||||
// Used for matching an intent with a specific package name.
|
||||
private static ArgumentMatcher<Intent> intentMatcher(String packageName) {
|
||||
return intent -> packageName.equals(intent.getPackage());
|
||||
assertThat(mController.mRecentAppsPreference.isVisible()).isFalse();
|
||||
assertThat(mController.mDivider.isVisible()).isFalse();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user