• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.content.ContextWrapper;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 import androidx.test.platform.app.InstrumentationRegistry;
26 
27 import com.android.launcher3.allapps.ActivityAllAppsContainerView;
28 import com.android.launcher3.allapps.AllAppsStore;
29 import com.android.launcher3.allapps.AlphabeticalAppsList;
30 import com.android.launcher3.model.BgDataModel;
31 import com.android.launcher3.model.WidgetsFilterDataProvider;
32 import com.android.launcher3.model.data.AppInfo;
33 import com.android.launcher3.pm.UserCache;
34 import com.android.launcher3.popup.PopupDataProvider;
35 import com.android.launcher3.widget.picker.model.WidgetPickerDataProvider;
36 
37 import java.util.Map;
38 import java.util.concurrent.CountDownLatch;
39 
40 /**
41  * {@link ContextWrapper} around {@link ActivityContextWrapper} with internal Launcher interface for
42  * testing.
43  *
44  * There are 2 constructors in this class. The base context can be {@link SandboxContext} or
45  * Instrumentation target context.
46  * Using {@link SandboxContext} as base context allows custom implementations for
47  * providing objects in Dagger components.
48  */
49 
50 public class TestSandboxModelContextWrapper extends ActivityContextWrapper implements
51         BgDataModel.Callbacks {
52 
53     protected AllAppsStore<ActivityContextWrapper> mAllAppsStore;
54     protected AlphabeticalAppsList<ActivityContextWrapper> mAppsList;
55 
56     public final CountDownLatch mBindCompleted = new CountDownLatch(1);
57 
58     protected ActivityAllAppsContainerView<ActivityContextWrapper> mAppsView;
59 
60     private final PopupDataProvider mPopupDataProvider = new PopupDataProvider(this);
61     private final WidgetPickerDataProvider mWidgetPickerDataProvider =
62             new WidgetPickerDataProvider(new WidgetsFilterDataProvider());
63     protected final UserCache mUserCache;
64 
TestSandboxModelContextWrapper(SandboxContext base)65     public TestSandboxModelContextWrapper(SandboxContext base) {
66         super(base);
67         mUserCache = UserCache.getInstance(base);
68         InstrumentationRegistry.getInstrumentation().runOnMainSync(() ->
69                 mAppsView = new ActivityAllAppsContainerView<>(this));
70         mAppsList = mAppsView.getPersonalAppList();
71         mAllAppsStore = mAppsView.getAppsStore();
72     }
73 
TestSandboxModelContextWrapper()74     public TestSandboxModelContextWrapper() {
75         super(getInstrumentation().getTargetContext());
76         InstrumentationRegistry.getInstrumentation().runOnMainSync(() ->
77                 mAppsView = new ActivityAllAppsContainerView<>(this));
78         mUserCache = UserCache.getInstance(this);
79         mAppsList = mAppsView.getPersonalAppList();
80         mAllAppsStore = mAppsView.getAppsStore();
81     }
82 
83     @NonNull
84     @Override
getPopupDataProvider()85     public PopupDataProvider getPopupDataProvider() {
86         return mPopupDataProvider;
87     }
88 
89     @Nullable
90     @Override
getWidgetPickerDataProvider()91     public WidgetPickerDataProvider getWidgetPickerDataProvider() {
92         return mWidgetPickerDataProvider;
93     }
94 
95     @Override
getAppsView()96     public ActivityAllAppsContainerView<ActivityContextWrapper> getAppsView() {
97         return mAppsView;
98     }
99 
100     @Override
bindAllApplications(AppInfo[] apps, int flags, Map<PackageUserKey, Integer> packageUserKeytoUidMap)101     public void bindAllApplications(AppInfo[] apps, int flags,
102             Map<PackageUserKey, Integer> packageUserKeytoUidMap) {
103         mAllAppsStore.setApps(apps, flags, packageUserKeytoUidMap);
104         mBindCompleted.countDown();
105     }
106 }
107