+++ /dev/null
-package com.farmerbb.taskbar.shadow;
-
-import android.content.Context;
-import android.content.pm.ActivityInfo;
-import android.content.pm.LauncherActivityInfo;
-import android.content.pm.LauncherApps;
-import android.os.UserHandle;
-
-import org.robolectric.annotation.Implementation;
-import org.robolectric.annotation.Implements;
-import org.robolectric.shadows.ShadowLauncherApps;
-import org.robolectric.util.ReflectionHelpers;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.function.Predicate;
-import java.util.stream.Collectors;
-
-import static android.os.Build.VERSION_CODES.LOLLIPOP;
-import static org.robolectric.util.ReflectionHelpers.ClassParameter.from;
-
-@Implements(value = LauncherApps.class, minSdk = LOLLIPOP)
-public class TaskbarShadowLauncherApps extends ShadowLauncherApps {
- private Map<UserHandle, List<String>> enabledPackages = new HashMap<>();
- private Map<UserHandle, List<LauncherActivityInfo>> activityList = new HashMap<>();
-
- public static LauncherActivityInfo generateTestLauncherActivityInfo(Context context,
- ActivityInfo activityInfo,
- int userHandleId) {
- return
- ReflectionHelpers.callConstructor(
- LauncherActivityInfo.class,
- from(Context.class, context),
- from(ActivityInfo.class, activityInfo),
- from(UserHandle.class, UserHandle.getUserHandleForUid(userHandleId))
- );
- }
-
- public void reset() {
- enabledPackages.clear();
- }
-
- /**
- * Adds an enabled package to be checked by {@link #isPackageEnabled(String, UserHandle)}.
- *
- * @param userHandle the user handle to be added.
- * @param packageName the package name to be added.
- */
- public void addEnabledPackage(UserHandle userHandle, String packageName) {
- List<String> packages = enabledPackages.get(userHandle);
- if (packages == null) {
- packages = new ArrayList<>();
- }
- if (!packages.contains(packageName)) {
- packages.add(packageName);
- }
- enabledPackages.put(userHandle, packages);
- }
-
- /**
- * Add an {@link LauncherActivityInfo} to be got by {@link #getActivityList(String, UserHandle)}.
- *
- * @param userHandle the user handle to be added.
- * @param activityInfo the {@link LauncherActivityInfo} to be added.
- */
- public void addActivity(UserHandle userHandle, LauncherActivityInfo activityInfo) {
- List<LauncherActivityInfo> activityInfoList = activityList.get(userHandle);
- if (activityInfoList == null) {
- activityInfoList = new ArrayList<>();
- }
- activityInfoList.remove(activityInfo);
- activityInfoList.add(activityInfo);
- activityList.put(userHandle, activityInfoList);
- }
-
- @Implementation
- protected boolean isPackageEnabled(String packageName, UserHandle user) {
- List<String> packages = enabledPackages.get(user);
- return packages != null && packages.contains(packageName);
- }
-
- @Implementation
- protected List<LauncherActivityInfo> getActivityList(String packageName, UserHandle user) {
- List<LauncherActivityInfo> activityInfoList = activityList.get(user);
- if (activityInfoList == null || packageName == null) {
- return Collections.emptyList();
- }
- final Predicate<LauncherActivityInfo> predicatePackage =
- info ->
- info.getComponentName() != null
- && packageName.equals(info.getComponentName().getPackageName());
- return activityInfoList.stream().filter(predicatePackage).collect(Collectors.toList());
- }
-}
import androidx.test.core.app.ApplicationProvider;
import com.farmerbb.taskbar.Constants;
+import com.farmerbb.taskbar.LauncherAppsHelper;
import com.farmerbb.taskbar.R;
import com.farmerbb.taskbar.activity.HomeActivity;
import com.farmerbb.taskbar.activity.HomeActivityDelegate;
import com.farmerbb.taskbar.activity.SecondaryHomeActivity;
import com.farmerbb.taskbar.mockito.BooleanAnswer;
import com.farmerbb.taskbar.mockito.StringAnswer;
-import com.farmerbb.taskbar.shadow.TaskbarShadowLauncherApps;
import com.farmerbb.taskbar.shadow.TaskbarShadowScrollView;
import com.farmerbb.taskbar.util.AppEntry;
import com.farmerbb.taskbar.util.DisplayInfo;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
+import org.robolectric.shadows.ShadowLauncherApps;
import org.robolectric.shadows.ShadowUsageStatsManager.EventBuilder;
import org.robolectric.util.ReflectionHelpers;
import static org.robolectric.Shadows.shadowOf;
@RunWith(RobolectricTestRunner.class)
-@PowerMockIgnore({"org.mockito.*", "org.robolectric.*",
- "android.*", "androidx.*", "com.farmerbb.taskbar.shadow.*"})
+@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*", "androidx.*"})
@PrepareForTest(value = {U.class, TaskbarPosition.class})
public class TaskbarControllerTest {
@Rule
}
@Test
- @Config(shadows = TaskbarShadowLauncherApps.class)
public void testFilterRealPinnedApps() {
List<AppEntry> pinnedApps = new ArrayList<>();
List<AppEntry> entries = new ArrayList<>();
LauncherApps launcherApps =
(LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
- TaskbarShadowLauncherApps taskbarShadowLauncherApps =
- (TaskbarShadowLauncherApps) shadowOf(launcherApps);
+ ShadowLauncherApps shadowLauncherApps = shadowOf(launcherApps);
AppEntry appEntry = generateTestAppEntry(1);
pinnedApps.add(appEntry);
- taskbarShadowLauncherApps
+ shadowLauncherApps
.addEnabledPackage(
UserHandle.getUserHandleForUid(Constants.DEFAULT_TEST_USER_ID),
appEntry.getPackageName()
assertEquals(1, realNumOfPinnedApps);
assertEquals(2, applicationIdsToRemove.size());
assertEquals(1, entries.size());
-
- taskbarShadowLauncherApps.reset();
}
@Test
- @Config(shadows = TaskbarShadowLauncherApps.class)
public void testGenerateAppEntries() {
List<AppEntry> usageStatsList = new ArrayList<>();
List<AppEntry> entries = new ArrayList<>();
);
LauncherApps launcherApps =
(LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
- TaskbarShadowLauncherApps taskbarShadowLauncherApps =
- (TaskbarShadowLauncherApps) Shadows.shadowOf(launcherApps);
+ ShadowLauncherApps shadowLauncherApps = Shadows.shadowOf(launcherApps);
AppEntry appEntry = generateTestAppEntry(0);
usageStatsList.add(appEntry);
info.name = appEntry.getLabel();
info.nonLocalizedLabel = appEntry.getLabel();
LauncherActivityInfo launcherActivityInfo =
- TaskbarShadowLauncherApps
+ LauncherAppsHelper
.generateTestLauncherActivityInfo(
context, info, Constants.DEFAULT_TEST_USER_ID
);
- taskbarShadowLauncherApps.addActivity(launcherActivityInfo.getUser(), launcherActivityInfo);
+ shadowLauncherApps.addActivity(launcherActivityInfo.getUser(), launcherActivityInfo);
uiController.generateAppEntries(context, 1, usageStatsList, entries, launcherAppCache);
assertEquals(1, entries.size());
entries.clear();
launcherAppCache.clear();
- ActivityInfo secondInfo = new ActivityInfo();
- secondInfo.packageName = appEntry.getPackageName();
- secondInfo.name = appEntry.getLabel() + "-second";
- secondInfo.nonLocalizedLabel = appEntry.getLabel();
- LauncherActivityInfo secondLauncherActivityInfo =
- TaskbarShadowLauncherApps
- .generateTestLauncherActivityInfo(
- context, secondInfo, Constants.DEFAULT_TEST_USER_ID
- );
- taskbarShadowLauncherApps
- .addActivity(secondLauncherActivityInfo.getUser(), secondLauncherActivityInfo);
- uiController.generateAppEntries(context, 1, usageStatsList, entries, launcherAppCache);
- assertEquals(1, entries.size());
- assertEquals(1, launcherAppCache.size());
- assertEquals(appEntry.getPackageName(), entries.get(0).getPackageName());
- assertSame(launcherActivityInfo, launcherAppCache.get(0));
- entries.clear();
- launcherAppCache.clear();
-
LauncherActivityInfo launcherActivityInfoForProfile =
- TaskbarShadowLauncherApps
+ LauncherAppsHelper
.generateTestLauncherActivityInfo(
context, info, Constants.DEFAULT_TEST_USER_PROFILE_ID
);
- taskbarShadowLauncherApps.addActivity(
+ shadowLauncherApps.addActivity(
UserHandle.getUserHandleForUid(Constants.DEFAULT_TEST_USER_PROFILE_ID),
launcherActivityInfoForProfile
);
thirdInfo.name = appEntry.getLabel();
thirdInfo.nonLocalizedLabel = appEntry.getLabel();
LauncherActivityInfo thirdLauncherActivityInfo =
- TaskbarShadowLauncherApps
+ LauncherAppsHelper
.generateTestLauncherActivityInfo(
context, thirdInfo, Constants.DEFAULT_TEST_USER_ID
);
- taskbarShadowLauncherApps
+ shadowLauncherApps
.addActivity(thirdLauncherActivityInfo.getUser(), thirdLauncherActivityInfo);
uiController.generateAppEntries(context, 1, usageStatsList, entries, launcherAppCache);
assertSame(thirdLauncherActivityInfo, launcherAppCache.get(0));
ActivityInfo forthInfo = new ActivityInfo(thirdInfo);
forthInfo.name = "com.google.android.googlequicksearchbox.SearchActivity";
LauncherActivityInfo forthLauncherActivityInfo =
- TaskbarShadowLauncherApps
+ LauncherAppsHelper
.generateTestLauncherActivityInfo(
context, forthInfo, Constants.DEFAULT_TEST_USER_ID
);
- taskbarShadowLauncherApps
+ shadowLauncherApps
.addActivity(forthLauncherActivityInfo.getUser(), forthLauncherActivityInfo);
uiController.generateAppEntries(context, 1, usageStatsList, entries, launcherAppCache);
assertSame(forthLauncherActivityInfo, launcherAppCache.get(0));
info.name = Constants.TEST_NAME;
info.nonLocalizedLabel = Constants.TEST_LABEL;
LauncherActivityInfo launcherActivityInfo =
- TaskbarShadowLauncherApps
+ LauncherAppsHelper
.generateTestLauncherActivityInfo(
context, info, Constants.DEFAULT_TEST_USER_ID
);