• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.intentresolver;
18 
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.ArgumentMatchers.anyInt;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.pm.PackageManager;
25 import android.content.res.Resources;
26 import android.database.Cursor;
27 import android.os.UserHandle;
28 
29 import com.android.intentresolver.AbstractMultiProfilePagerAdapter.CrossProfileIntentsChecker;
30 import com.android.intentresolver.chooser.TargetInfo;
31 import com.android.intentresolver.contentpreview.ImageLoader;
32 import com.android.intentresolver.flags.FeatureFlagRepository;
33 import com.android.intentresolver.logging.EventLog;
34 import com.android.intentresolver.shortcuts.ShortcutLoader;
35 
36 import java.util.function.Consumer;
37 import java.util.function.Function;
38 
39 import kotlin.jvm.functions.Function2;
40 
41 /**
42  * Singleton providing overrides to be applied by any {@code IChooserWrapper} used in testing.
43  * We cannot directly mock the activity created since instrumentation creates it, so instead we use
44  * this singleton to modify behavior.
45  */
46 public class ChooserActivityOverrideData {
47     private static ChooserActivityOverrideData sInstance = null;
48 
getInstance()49     public static ChooserActivityOverrideData getInstance() {
50         if (sInstance == null) {
51             sInstance = new ChooserActivityOverrideData();
52         }
53         return sInstance;
54     }
55 
56     @SuppressWarnings("Since15")
57     public Function<PackageManager, PackageManager> createPackageManager;
58     public Function<TargetInfo, Boolean> onSafelyStartInternalCallback;
59     public Function<TargetInfo, Boolean> onSafelyStartCallback;
60     public Function2<UserHandle, Consumer<ShortcutLoader.Result>, ShortcutLoader>
61             shortcutLoaderFactory = (userHandle, callback) -> null;
62     public ChooserActivity.ChooserListController resolverListController;
63     public ChooserActivity.ChooserListController workResolverListController;
64     public Boolean isVoiceInteraction;
65     public Cursor resolverCursor;
66     public boolean resolverForceException;
67     public ImageLoader imageLoader;
68     public EventLog mEventLog;
69     public int alternateProfileSetting;
70     public Resources resources;
71     public UserHandle workProfileUserHandle;
72     public UserHandle cloneProfileUserHandle;
73     public UserHandle tabOwnerUserHandleForLaunch;
74     public boolean hasCrossProfileIntents;
75     public boolean isQuietModeEnabled;
76     public Integer myUserId;
77     public WorkProfileAvailabilityManager mWorkProfileAvailability;
78     public CrossProfileIntentsChecker mCrossProfileIntentsChecker;
79     public PackageManager packageManager;
80     public FeatureFlagRepository featureFlagRepository;
81 
reset()82     public void reset() {
83         onSafelyStartInternalCallback = null;
84         isVoiceInteraction = null;
85         createPackageManager = null;
86         imageLoader = null;
87         resolverCursor = null;
88         resolverForceException = false;
89         resolverListController = mock(ChooserActivity.ChooserListController.class);
90         workResolverListController = mock(ChooserActivity.ChooserListController.class);
91         mEventLog = mock(EventLog.class);
92         alternateProfileSetting = 0;
93         resources = null;
94         workProfileUserHandle = null;
95         cloneProfileUserHandle = null;
96         tabOwnerUserHandleForLaunch = null;
97         hasCrossProfileIntents = true;
98         isQuietModeEnabled = false;
99         myUserId = null;
100         packageManager = null;
101         mWorkProfileAvailability = new WorkProfileAvailabilityManager(null, null, null) {
102             @Override
103             public boolean isQuietModeEnabled() {
104                 return isQuietModeEnabled;
105             }
106 
107             @Override
108             public boolean isWorkProfileUserUnlocked() {
109                 return true;
110             }
111 
112             @Override
113             public void requestQuietModeEnabled(boolean enabled) {
114                 isQuietModeEnabled = enabled;
115             }
116 
117             @Override
118             public void markWorkProfileEnabledBroadcastReceived() {}
119 
120             @Override
121             public boolean isWaitingToEnableWorkProfile() {
122                 return false;
123             }
124         };
125         shortcutLoaderFactory = ((userHandle, resultConsumer) -> null);
126 
127         mCrossProfileIntentsChecker = mock(CrossProfileIntentsChecker.class);
128         when(mCrossProfileIntentsChecker.hasCrossProfileIntents(any(), anyInt(), anyInt()))
129                 .thenAnswer(invocation -> hasCrossProfileIntents);
130         featureFlagRepository = null;
131     }
132 
ChooserActivityOverrideData()133     private ChooserActivityOverrideData() {}
134 }
135 
136