Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 39f23515 authored by Sunny Goyal's avatar Sunny Goyal Committed by Android (Google) Code Review
Browse files

Merge "Fixing some launcher model tests" into main

parents e2966668 6449a210
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -191,7 +191,7 @@ public class AppEventProducer implements StatsLogConsumer {
    @Nullable
    AppTarget toAppTarget(LauncherAtom.ItemInfo info) {
        int iconInfoType = getIconInfoTypeFromItemInfo(info);
        UserCache userCache = UserCache.getInstance(mContext);
        UserCache userCache = UserCache.INSTANCE.get(mContext);
        UserHandle userHandle = userCache.getUserProfiles().stream()
                .filter(user -> userCache.getUserInfo(user).type == iconInfoType)
                .findFirst()
+10 −10
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ import static org.mockito.Mockito.when;

import android.app.prediction.AppTarget;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Process;
import android.os.UserHandle;
@@ -39,13 +38,12 @@ import androidx.test.filters.SmallTest;
import com.android.launcher3.logger.LauncherAtom;
import com.android.launcher3.model.data.AppInfo;
import com.android.launcher3.pm.UserCache;
import com.android.launcher3.util.ActivityContextWrapper;
import com.android.launcher3.util.MainThreadInitializedObject.SandboxContext;
import com.android.launcher3.util.UserIconInfo;
import com.android.launcher3.util.rule.StaticMockitoRule;
import com.android.systemui.shared.system.SysUiStatsLog;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -65,22 +63,24 @@ public class AppEventProducerTest {
    private static final UserIconInfo PRIVATE_ICON_INFO =
            new UserIconInfo(PRIVATE_HANDLE, UserIconInfo.TYPE_PRIVATE);

    private Context mContext;
    private SandboxContext mContext;
    private AppEventProducer mAppEventProducer;
    @Mock
    private UserCache mUserCache;

    @Rule
    public final StaticMockitoRule mStaticMockitoRule = new StaticMockitoRule(UserCache.class);

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mContext = new ActivityContextWrapper(getApplicationContext());
        when(UserCache.getInstance(any(Context.class))).thenReturn(mUserCache);
        mContext = new SandboxContext(getApplicationContext());
        mContext.putObject(UserCache.INSTANCE, mUserCache);
        mAppEventProducer = new AppEventProducer(mContext, null);
    }

    @After
    public void tearDown() {
        mContext.onDestroy();
    }

    @Test
    public void buildAppTarget_containsCorrectUser() {
        when(mUserCache.getUserProfiles())
+4 −2
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ import com.android.launcher3.icons.IconCache;
import com.android.launcher3.icons.IconProvider;
import com.android.launcher3.icons.LauncherIconProvider;
import com.android.launcher3.icons.LauncherIcons;
import com.android.launcher3.model.ModelLauncherCallbacks;
import com.android.launcher3.notification.NotificationListener;
import com.android.launcher3.pm.InstallSessionHelper;
import com.android.launcher3.pm.InstallSessionTracker;
@@ -101,9 +102,10 @@ public class LauncherAppState implements SafeCloseable {
            }
        });

        mContext.getSystemService(LauncherApps.class).registerCallback(mModel);
        ModelLauncherCallbacks callbacks = mModel.newModelCallbacks();
        mContext.getSystemService(LauncherApps.class).registerCallback(callbacks);
        mOnTerminateCallback.add(() ->
                mContext.getSystemService(LauncherApps.class).unregisterCallback(mModel));
                mContext.getSystemService(LauncherApps.class).unregisterCallback(callbacks));

        SimpleBroadcastReceiver modelChangeReceiver =
                new SimpleBroadcastReceiver(mModel::onBroadcastIntent);
+8 −76
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static android.app.admin.DevicePolicyManager.ACTION_DEVICE_POLICY_RESOURC

import static com.android.launcher3.LauncherAppState.ACTION_FORCE_ROLOAD;
import static com.android.launcher3.config.FeatureFlags.IS_STUDIO_BUILD;
import static com.android.launcher3.model.PackageUpdatedTask.OP_UPDATE;
import static com.android.launcher3.pm.UserCache.ACTION_PROFILE_AVAILABLE;
import static com.android.launcher3.pm.UserCache.ACTION_PROFILE_UNAVAILABLE;
import static com.android.launcher3.testing.shared.TestProtocol.sDebugTracing;
@@ -28,7 +29,6 @@ import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;

import android.content.Context;
import android.content.Intent;
import android.content.pm.LauncherApps;
import android.content.pm.PackageInstaller;
import android.content.pm.ShortcutInfo;
import android.os.UserHandle;
@@ -43,7 +43,6 @@ import androidx.annotation.WorkerThread;
import com.android.launcher3.celllayout.CellPosMapper;
import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.icons.IconCache;
import com.android.launcher3.logging.FileLog;
import com.android.launcher3.model.AddWorkspaceItemsTask;
import com.android.launcher3.model.AllAppsList;
import com.android.launcher3.model.BaseModelUpdateTask;
@@ -55,8 +54,8 @@ import com.android.launcher3.model.LauncherBinder;
import com.android.launcher3.model.LoaderTask;
import com.android.launcher3.model.ModelDbController;
import com.android.launcher3.model.ModelDelegate;
import com.android.launcher3.model.ModelLauncherCallbacks;
import com.android.launcher3.model.ModelWriter;
import com.android.launcher3.model.PackageIncrementalDownloadUpdatedTask;
import com.android.launcher3.model.PackageInstallStateChangedTask;
import com.android.launcher3.model.PackageUpdatedTask;
import com.android.launcher3.model.ReloadStringCacheTask;
@@ -89,7 +88,7 @@ import java.util.function.Supplier;
 * LauncherModel object held in a static. Also provide APIs for updating the database state
 * for the Launcher.
 */
public class LauncherModel extends LauncherApps.Callback implements InstallSessionTracker.Callback {
public class LauncherModel implements InstallSessionTracker.Callback {
    private static final boolean DEBUG_RECEIVER = false;

    static final String TAG = "Launcher.Model";
@@ -168,6 +167,10 @@ public class LauncherModel extends LauncherApps.Callback implements InstallSessi
        return mModelDbController;
    }

    public ModelLauncherCallbacks newModelCallbacks() {
        return new ModelLauncherCallbacks(this::enqueueModelUpdateTask);
    }

    /**
     * Adds the provided items to the workspace.
     */
@@ -186,77 +189,6 @@ public class LauncherModel extends LauncherApps.Callback implements InstallSessi
                owner);
    }

    @Override
    public void onPackageChanged(
            @NonNull final String packageName, @NonNull final UserHandle user) {
        int op = PackageUpdatedTask.OP_UPDATE;
        enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
    }

    @Override
    public void onPackageRemoved(
            @NonNull final String packageName, @NonNull final UserHandle user) {
        onPackagesRemoved(user, packageName);
    }

    public void onPackagesRemoved(
            @NonNull final UserHandle user, @NonNull final String... packages) {
        int op = PackageUpdatedTask.OP_REMOVE;
        FileLog.d(TAG, "package removed received " + TextUtils.join(",", packages));
        enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
    }

    @Override
    public void onPackageAdded(@NonNull final String packageName, @NonNull final UserHandle user) {
        int op = PackageUpdatedTask.OP_ADD;
        enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
    }

    @Override
    public void onPackagesAvailable(@NonNull final String[] packageNames,
            @NonNull final UserHandle user, final boolean replacing) {
        enqueueModelUpdateTask(
                new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
    }

    @Override
    public void onPackagesUnavailable(@NonNull final String[] packageNames,
            @NonNull final UserHandle user, final boolean replacing) {
        if (!replacing) {
            enqueueModelUpdateTask(new PackageUpdatedTask(
                    PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
        }
    }

    @Override
    public void onPackagesSuspended(
            @NonNull final String[] packageNames, @NonNull final UserHandle user) {
        enqueueModelUpdateTask(new PackageUpdatedTask(
                PackageUpdatedTask.OP_SUSPEND, user, packageNames));
    }

    @Override
    public void onPackagesUnsuspended(
            @NonNull final String[] packageNames, @NonNull final UserHandle user) {
        enqueueModelUpdateTask(new PackageUpdatedTask(
                PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
    }

    @Override
    public void onPackageLoadingProgressChanged(@NonNull final String packageName,
            @NonNull final UserHandle user, final float progress) {
        if (Utilities.ATLEAST_S) {
            enqueueModelUpdateTask(new PackageIncrementalDownloadUpdatedTask(
                    packageName, user, progress));
        }
    }

    @Override
    public void onShortcutsChanged(@NonNull final String packageName,
            @NonNull final List<ShortcutInfo> shortcuts, @NonNull final UserHandle user) {
        enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
    }

    /**
     * Called when the icon for an app changes, outside of package event
     */
@@ -265,7 +197,7 @@ public class LauncherModel extends LauncherApps.Callback implements InstallSessi
            @NonNull final UserHandle user) {
        // Update the icon for the calendar package
        Context context = mApp.getContext();
        onPackageChanged(packageName, user);
        enqueueModelUpdateTask(new PackageUpdatedTask(OP_UPDATE, user, packageName));

        List<ShortcutInfo> pinnedShortcuts = new ShortcutRequest(context, user)
                .forPackage(packageName).query(ShortcutRequest.PINNED);
+3 −2
Original line number Diff line number Diff line
@@ -123,6 +123,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CancellationException;

@@ -185,7 +186,7 @@ public class LoaderTask implements Runnable {
        mLauncherBinder = launcherBinder;
        mLauncherApps = mApp.getContext().getSystemService(LauncherApps.class);
        mUserManager = mApp.getContext().getSystemService(UserManager.class);
        mUserCache = UserCache.getInstance(mApp.getContext());
        mUserCache = UserCache.INSTANCE.get(mApp.getContext());
        mSessionHelper = InstallSessionHelper.INSTANCE.get(mApp.getContext());
        mIconCache = mApp.getIconCache();
        mUserManagerState = userManagerState;
@@ -247,7 +248,7 @@ public class LoaderTask implements Runnable {
            // sanitizeData should not be invoked if the workspace is loaded from a db different
            // from the main db as defined in the invariant device profile.
            // (e.g. both grid preview and minimal device mode uses a different db)
            if (mApp.getInvariantDeviceProfile().dbFile.equals(mDbName)) {
            if (Objects.equals(mApp.getInvariantDeviceProfile().dbFile, mDbName)) {
                verifyNotStopped();
                sanitizeFolders(mItemsDeleted);
                sanitizeWidgetsShortcutsAndPackages();
Loading