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.internal.app;
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.graphics.Bitmap;
28 import android.os.UserHandle;
29 import android.util.Pair;
30 
31 import com.android.internal.app.AbstractMultiProfilePagerAdapter.CrossProfileIntentsChecker;
32 import com.android.internal.app.AbstractMultiProfilePagerAdapter.QuietModeManager;
33 import com.android.internal.app.chooser.TargetInfo;
34 import com.android.internal.logging.MetricsLogger;
35 
36 import java.util.function.BiFunction;
37 import java.util.function.Function;
38 
39 /**
40  * Singleton providing overrides to be applied by any {@code IChooserWrapper} used in testing.
41  * We cannot directly mock the activity created since instrumentation creates it, so instead we use
42  * this singleton to modify behavior.
43  */
44 public class ChooserActivityOverrideData {
45     private static ChooserActivityOverrideData sInstance = null;
46 
getInstance()47     public static ChooserActivityOverrideData getInstance() {
48         if (sInstance == null) {
49             sInstance = new ChooserActivityOverrideData();
50         }
51         return sInstance;
52     }
53 
54     @SuppressWarnings("Since15")
55     public Function<PackageManager, PackageManager> createPackageManager;
56     public Function<TargetInfo, Boolean> onSafelyStartInternalCallback;
57     public Function<ChooserListAdapter, Void> onQueryDirectShareTargets;
58     public BiFunction<
59             IChooserWrapper, ChooserListAdapter, Pair<Integer, ChooserActivity.ServiceResultInfo[]>>
60             directShareTargets;
61     public ResolverListController resolverListController;
62     public ResolverListController workResolverListController;
63     public Boolean isVoiceInteraction;
64     public boolean isImageType;
65     public Cursor resolverCursor;
66     public boolean resolverForceException;
67     public Bitmap previewThumbnail;
68     public MetricsLogger metricsLogger;
69     public ChooserActivityLogger chooserActivityLogger;
70     public int alternateProfileSetting;
71     public Resources resources;
72     public UserHandle workProfileUserHandle;
73     public UserHandle cloneProfileUserHandle;
74     public UserHandle tabOwnerUserHandleForLaunch;
75     public boolean hasCrossProfileIntents;
76     public boolean isQuietModeEnabled;
77     public boolean isWorkProfileUserRunning;
78     public boolean isWorkProfileUserUnlocked;
79     public Integer myUserId;
80     public QuietModeManager mQuietModeManager;
81     public CrossProfileIntentsChecker mCrossProfileIntentsChecker;
82     public PackageManager packageManager;
83 
reset()84     public void reset() {
85         onSafelyStartInternalCallback = null;
86         onQueryDirectShareTargets = null;
87         directShareTargets = null;
88         isVoiceInteraction = null;
89         createPackageManager = null;
90         previewThumbnail = null;
91         isImageType = false;
92         resolverCursor = null;
93         resolverForceException = false;
94         resolverListController = mock(ResolverListController.class);
95         workResolverListController = mock(ResolverListController.class);
96         metricsLogger = mock(MetricsLogger.class);
97         chooserActivityLogger = new ChooserActivityLoggerFake();
98         alternateProfileSetting = 0;
99         resources = null;
100         workProfileUserHandle = null;
101         cloneProfileUserHandle = null;
102         tabOwnerUserHandleForLaunch = null;
103         hasCrossProfileIntents = true;
104         isQuietModeEnabled = false;
105         isWorkProfileUserRunning = true;
106         isWorkProfileUserUnlocked = true;
107         myUserId = null;
108         packageManager = null;
109         mQuietModeManager = new QuietModeManager() {
110             @Override
111             public boolean isQuietModeEnabled(UserHandle workProfileUserHandle) {
112                 return isQuietModeEnabled;
113             }
114 
115             @Override
116             public void requestQuietModeEnabled(boolean enabled,
117                     UserHandle workProfileUserHandle) {
118                 isQuietModeEnabled = enabled;
119             }
120 
121             @Override
122             public void markWorkProfileEnabledBroadcastReceived() {
123             }
124 
125             @Override
126             public boolean isWaitingToEnableWorkProfile() {
127                 return false;
128             }
129         };
130 
131         mCrossProfileIntentsChecker = mock(CrossProfileIntentsChecker.class);
132         when(mCrossProfileIntentsChecker.hasCrossProfileIntents(any(), anyInt(), anyInt()))
133                 .thenAnswer(invocation -> hasCrossProfileIntents);
134     }
135 
ChooserActivityOverrideData()136     private ChooserActivityOverrideData() {}
137 }
138