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 19 import android.app.usage.UsageStatsManager; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.content.pm.PackageManager; 24 import android.os.RemoteException; 25 26 import java.util.function.Function; 27 28 import static org.mockito.Mockito.mock; 29 30 31 /* 32 * Simple wrapper around chooser activity to be able to initiate it under test 33 */ 34 public class ResolverWrapperActivity extends ResolverActivity { 35 static final OverrideData sOverrides = new OverrideData(); 36 private UsageStatsManager mUsm; 37 getAdapter()38 ResolveListAdapter getAdapter() { 39 return mAdapter; 40 } 41 42 @Override isVoiceInteraction()43 public boolean isVoiceInteraction() { 44 if (sOverrides.isVoiceInteraction != null) { 45 return sOverrides.isVoiceInteraction; 46 } 47 return super.isVoiceInteraction(); 48 } 49 50 @Override safelyStartActivity(TargetInfo cti)51 public void safelyStartActivity(TargetInfo cti) { 52 if (sOverrides.onSafelyStartCallback != null && 53 sOverrides.onSafelyStartCallback.apply(cti)) { 54 return; 55 } 56 super.safelyStartActivity(cti); 57 } 58 59 @Override createListController()60 protected ResolverListController createListController() { 61 return sOverrides.resolverListController; 62 } 63 64 @Override getPackageManager()65 public PackageManager getPackageManager() { 66 if (sOverrides.createPackageManager != null) { 67 return sOverrides.createPackageManager.apply(super.getPackageManager()); 68 } 69 return super.getPackageManager(); 70 } 71 72 /** 73 * We cannot directly mock the activity created since instrumentation creates it. 74 * <p> 75 * Instead, we use static instances of this object to modify behavior. 76 */ 77 static class OverrideData { 78 @SuppressWarnings("Since15") 79 public Function<PackageManager, PackageManager> createPackageManager; 80 public Function<TargetInfo, Boolean> onSafelyStartCallback; 81 public ResolverListController resolverListController; 82 public Boolean isVoiceInteraction; 83 reset()84 public void reset() { 85 onSafelyStartCallback = null; 86 isVoiceInteraction = null; 87 createPackageManager = null; 88 resolverListController = mock(ResolverListController.class); 89 } 90 } 91 }