1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.launcher3.util; 18 19 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; 20 21 import static com.android.launcher3.util.MainThreadInitializedObject.SandboxContext; 22 23 import android.content.ContextWrapper; 24 25 import androidx.annotation.Nullable; 26 import androidx.test.platform.app.InstrumentationRegistry; 27 28 import com.android.launcher3.allapps.ActivityAllAppsContainerView; 29 import com.android.launcher3.allapps.AllAppsStore; 30 import com.android.launcher3.allapps.AlphabeticalAppsList; 31 import com.android.launcher3.model.BgDataModel; 32 import com.android.launcher3.model.data.AppInfo; 33 import com.android.launcher3.pm.UserCache; 34 import com.android.launcher3.popup.PopupDataProvider; 35 36 import java.util.Map; 37 import java.util.concurrent.CountDownLatch; 38 39 /** 40 * {@link ContextWrapper} around {@link ActivityContextWrapper} with internal Launcher interface for 41 * testing. 42 * 43 * There are 2 constructors in this class. The base context can be {@link SandboxContext} or 44 * Instrumentation target context. 45 * Using {@link SandboxContext} as base context allows custom implementations for 46 * MainThreadInitializedObject providers. 47 */ 48 49 public class TestSandboxModelContextWrapper extends ActivityContextWrapper implements 50 BgDataModel.Callbacks { 51 52 protected AllAppsStore<ActivityContextWrapper> mAllAppsStore; 53 protected AlphabeticalAppsList<ActivityContextWrapper> mAppsList; 54 55 public final CountDownLatch mBindCompleted = new CountDownLatch(1); 56 57 protected ActivityAllAppsContainerView<ActivityContextWrapper> mAppsView; 58 59 private final PopupDataProvider mPopupDataProvider = new PopupDataProvider(i -> {}); 60 protected final UserCache mUserCache; 61 TestSandboxModelContextWrapper(SandboxContext base)62 public TestSandboxModelContextWrapper(SandboxContext base) { 63 super(base); 64 mUserCache = base.getObject(UserCache.INSTANCE); 65 InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> 66 mAppsView = new ActivityAllAppsContainerView<>(this)); 67 mAppsList = mAppsView.getPersonalAppList(); 68 mAllAppsStore = mAppsView.getAppsStore(); 69 } 70 TestSandboxModelContextWrapper()71 public TestSandboxModelContextWrapper() { 72 super(getInstrumentation().getTargetContext()); 73 InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> 74 mAppsView = new ActivityAllAppsContainerView<>(this)); 75 mUserCache = UserCache.getInstance(this); 76 mAppsList = mAppsView.getPersonalAppList(); 77 mAllAppsStore = mAppsView.getAppsStore(); 78 } 79 @Nullable 80 @Override getPopupDataProvider()81 public PopupDataProvider getPopupDataProvider() { 82 return mPopupDataProvider; 83 } 84 85 @Override getAppsView()86 public ActivityAllAppsContainerView<ActivityContextWrapper> getAppsView() { 87 return mAppsView; 88 } 89 90 @Override bindAllApplications(AppInfo[] apps, int flags, Map<PackageUserKey, Integer> packageUserKeytoUidMap)91 public void bindAllApplications(AppInfo[] apps, int flags, 92 Map<PackageUserKey, Integer> packageUserKeytoUidMap) { 93 mAllAppsStore.setApps(apps, flags, packageUserKeytoUidMap); 94 mBindCompleted.countDown(); 95 } 96 } 97