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