Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.internal.app; |
| 18 | |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 19 | import static org.mockito.Mockito.mock; |
| 20 | |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 21 | import android.app.usage.UsageStatsManager; |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 25 | import android.content.pm.ResolveInfo; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 26 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 27 | import com.android.internal.app.chooser.TargetInfo; |
Zhen Zhang | cb55c8a | 2019-10-22 14:49:09 -0700 | [diff] [blame] | 28 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 29 | import java.util.List; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 30 | import java.util.function.Function; |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * Simple wrapper around chooser activity to be able to initiate it under test |
| 34 | */ |
| 35 | public class ResolverWrapperActivity extends ResolverActivity { |
| 36 | static final OverrideData sOverrides = new OverrideData(); |
| 37 | private UsageStatsManager mUsm; |
Zhen Zhang | cb55c8a | 2019-10-22 14:49:09 -0700 | [diff] [blame] | 38 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 39 | @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 Zhang | cb55c8a | 2019-10-22 14:49:09 -0700 | [diff] [blame] | 45 | } |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 46 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 47 | ResolverWrapperAdapter getAdapter() { |
| 48 | return (ResolverWrapperAdapter) mAdapter; |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 49 | } |
| 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 Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 100 | } |