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 static org.mockito.Mockito.mock; 20 21 import android.app.usage.UsageStatsManager; 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.PackageManager; 26 import android.content.pm.ResolveInfo; 27 import android.database.Cursor; 28 import android.graphics.Bitmap; 29 import android.net.Uri; 30 import android.util.Size; 31 32 import com.android.internal.logging.MetricsLogger; 33 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 34 35 import java.util.function.Function; 36 37 public class ChooserWrapperActivity extends ChooserActivity { 38 /* 39 * Simple wrapper around chooser activity to be able to initiate it under test 40 */ 41 static final OverrideData sOverrides = new OverrideData(); 42 private UsageStatsManager mUsm; 43 getAdapter()44 ChooserListAdapter getAdapter() { 45 return (ChooserListAdapter) mAdapter; 46 } 47 getIsSelected()48 boolean getIsSelected() { return mIsSuccessfullySelected; } 49 getUsageStatsManager()50 UsageStatsManager getUsageStatsManager() { 51 if (mUsm == null) { 52 mUsm = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE); 53 } 54 return mUsm; 55 } 56 57 @Override isVoiceInteraction()58 public boolean isVoiceInteraction() { 59 if (sOverrides.isVoiceInteraction != null) { 60 return sOverrides.isVoiceInteraction; 61 } 62 return super.isVoiceInteraction(); 63 } 64 65 @Override safelyStartActivity(TargetInfo cti)66 public void safelyStartActivity(TargetInfo cti) { 67 if (sOverrides.onSafelyStartCallback != null && 68 sOverrides.onSafelyStartCallback.apply(cti)) { 69 return; 70 } 71 super.safelyStartActivity(cti); 72 } 73 74 @Override createListController()75 protected ResolverListController createListController() { 76 return sOverrides.resolverListController; 77 } 78 79 @Override getPackageManager()80 public PackageManager getPackageManager() { 81 if (sOverrides.createPackageManager != null) { 82 return sOverrides.createPackageManager.apply(super.getPackageManager()); 83 } 84 return super.getPackageManager(); 85 } 86 87 @Override loadThumbnail(Uri uri, Size size)88 protected Bitmap loadThumbnail(Uri uri, Size size) { 89 if (sOverrides.previewThumbnail != null) { 90 return sOverrides.previewThumbnail; 91 } 92 return super.loadThumbnail(uri, size); 93 } 94 95 @Override isImageType(String mimeType)96 protected boolean isImageType(String mimeType) { 97 return sOverrides.isImageType; 98 } 99 100 @Override getMetricsLogger()101 protected MetricsLogger getMetricsLogger() { 102 return sOverrides.metricsLogger; 103 } 104 105 @Override queryResolver(ContentResolver resolver, Uri uri)106 public Cursor queryResolver(ContentResolver resolver, Uri uri) { 107 if (sOverrides.resolverCursor != null) { 108 return sOverrides.resolverCursor; 109 } 110 111 if (sOverrides.resolverForceException) { 112 throw new SecurityException("Test exception handling"); 113 } 114 115 return super.queryResolver(resolver, uri); 116 } 117 118 @Override isWorkProfile()119 protected boolean isWorkProfile() { 120 if (sOverrides.alternateProfileSetting != 0) { 121 return sOverrides.alternateProfileSetting == MetricsEvent.MANAGED_PROFILE; 122 } 123 return super.isWorkProfile(); 124 } 125 createTestDisplayResolveInfo(Intent originalIntent, ResolveInfo pri, CharSequence pLabel, CharSequence pInfo, Intent pOrigIntent)126 public DisplayResolveInfo createTestDisplayResolveInfo(Intent originalIntent, ResolveInfo pri, 127 CharSequence pLabel, CharSequence pInfo, Intent pOrigIntent) { 128 return new DisplayResolveInfo(originalIntent, pri, pLabel, pInfo, pOrigIntent); 129 } 130 131 /** 132 * We cannot directly mock the activity created since instrumentation creates it. 133 * <p> 134 * Instead, we use static instances of this object to modify behavior. 135 */ 136 static class OverrideData { 137 @SuppressWarnings("Since15") 138 public Function<PackageManager, PackageManager> createPackageManager; 139 public Function<TargetInfo, Boolean> onSafelyStartCallback; 140 public ResolverListController resolverListController; 141 public Boolean isVoiceInteraction; 142 public boolean isImageType; 143 public Cursor resolverCursor; 144 public boolean resolverForceException; 145 public Bitmap previewThumbnail; 146 public MetricsLogger metricsLogger; 147 public int alternateProfileSetting; 148 reset()149 public void reset() { 150 onSafelyStartCallback = null; 151 isVoiceInteraction = null; 152 createPackageManager = null; 153 previewThumbnail = null; 154 isImageType = false; 155 resolverCursor = null; 156 resolverForceException = false; 157 resolverListController = mock(ResolverListController.class); 158 metricsLogger = mock(MetricsLogger.class); 159 alternateProfileSetting = 0; 160 } 161 } 162 } 163