blob: 39cc83c3bc433205ec8a9048e20e4583f1b6b3fd [file] [log] [blame]
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -08001/*
2 * Copyright (C) 2017 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
17package com.android.internal.app;
18
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -080019import static org.mockito.Mockito.mock;
20
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090021import android.app.usage.UsageStatsManager;
arangelovb0802dc2019-10-18 18:03:44 +010022import android.content.Context;
23import android.content.Intent;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090024import android.content.pm.PackageManager;
arangelovb0802dc2019-10-18 18:03:44 +010025import android.content.pm.ResolveInfo;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090026
arangelovb0802dc2019-10-18 18:03:44 +010027import com.android.internal.app.chooser.TargetInfo;
Zhen Zhangcb55c8a2019-10-22 14:49:09 -070028
arangelovb0802dc2019-10-18 18:03:44 +010029import java.util.List;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090030import java.util.function.Function;
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -080031
32/*
33 * Simple wrapper around chooser activity to be able to initiate it under test
34 */
35public class ResolverWrapperActivity extends ResolverActivity {
36 static final OverrideData sOverrides = new OverrideData();
37 private UsageStatsManager mUsm;
Zhen Zhangcb55c8a2019-10-22 14:49:09 -070038
arangelovb0802dc2019-10-18 18:03:44 +010039 @Override
40 public ResolverListAdapter createAdapter(Context context, List<Intent> payloadIntents,
41 Intent[] initialIntents, List<ResolveInfo> rList, boolean filterLastUsed,
42 boolean useLayoutForBrowsables) {
43 return new ResolverWrapperAdapter(context, payloadIntents, initialIntents, rList,
44 filterLastUsed, createListController(), useLayoutForBrowsables, this);
Zhen Zhangcb55c8a2019-10-22 14:49:09 -070045 }
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -080046
arangelovb0802dc2019-10-18 18:03:44 +010047 ResolverWrapperAdapter getAdapter() {
48 return (ResolverWrapperAdapter) mAdapter;
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -080049 }
50
51 @Override
52 public boolean isVoiceInteraction() {
53 if (sOverrides.isVoiceInteraction != null) {
54 return sOverrides.isVoiceInteraction;
55 }
56 return super.isVoiceInteraction();
57 }
58
59 @Override
60 public void safelyStartActivity(TargetInfo cti) {
61 if (sOverrides.onSafelyStartCallback != null &&
62 sOverrides.onSafelyStartCallback.apply(cti)) {
63 return;
64 }
65 super.safelyStartActivity(cti);
66 }
67
68 @Override
69 protected ResolverListController createListController() {
70 return sOverrides.resolverListController;
71 }
72
73 @Override
74 public PackageManager getPackageManager() {
75 if (sOverrides.createPackageManager != null) {
76 return sOverrides.createPackageManager.apply(super.getPackageManager());
77 }
78 return super.getPackageManager();
79 }
80
81 /**
82 * We cannot directly mock the activity created since instrumentation creates it.
83 * <p>
84 * Instead, we use static instances of this object to modify behavior.
85 */
86 static class OverrideData {
87 @SuppressWarnings("Since15")
88 public Function<PackageManager, PackageManager> createPackageManager;
89 public Function<TargetInfo, Boolean> onSafelyStartCallback;
90 public ResolverListController resolverListController;
91 public Boolean isVoiceInteraction;
92
93 public void reset() {
94 onSafelyStartCallback = null;
95 isVoiceInteraction = null;
96 createPackageManager = null;
97 resolverListController = mock(ResolverListController.class);
98 }
99 }
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -0800100}