1 /* 2 * Copyright (C) 2020 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.settings.testutils; 18 19 import android.content.pm.ActivityInfo; 20 import android.content.pm.ApplicationInfo; 21 import android.content.pm.PackageInfo; 22 import android.content.pm.ProviderInfo; 23 import android.content.pm.ResolveInfo; 24 25 import com.google.common.base.Preconditions; 26 27 /** 28 * Helper for building {@link ResolveInfo}s to be used in Robolectric tests. 29 * 30 * <p>The resulting {@link PackageInfo}s should typically be added to {@link 31 * org.robolectric.shadows.ShadowPackageManager#addResolveInfoForIntent(Intent, ResolveInfo)}. 32 */ 33 public final class ResolveInfoBuilder { 34 35 private final String mPackageName; 36 private ActivityInfo mActivityInfo; 37 private ProviderInfo mProviderInfo; 38 ResolveInfoBuilder(String packageName)39 public ResolveInfoBuilder(String packageName) { 40 this.mPackageName = Preconditions.checkNotNull(packageName); 41 } 42 setActivity(String packageName, String className)43 public ResolveInfoBuilder setActivity(String packageName, String className) { 44 mActivityInfo = new ActivityInfo(); 45 mActivityInfo.packageName = packageName; 46 mActivityInfo.name = className; 47 return this; 48 } 49 setProvider( String packageName, String className, String authority, boolean isSystemApp)50 public ResolveInfoBuilder setProvider( 51 String packageName, String className, String authority, boolean isSystemApp) { 52 mProviderInfo = new ProviderInfo(); 53 mProviderInfo.authority = authority; 54 mProviderInfo.applicationInfo = new ApplicationInfo(); 55 if (isSystemApp) { 56 mProviderInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; 57 } 58 mProviderInfo.packageName = mPackageName; 59 mProviderInfo.applicationInfo.packageName = mPackageName; 60 mProviderInfo.name = className; 61 return this; 62 } 63 build()64 public ResolveInfo build() { 65 ResolveInfo info = new ResolveInfo(); 66 info.activityInfo = mActivityInfo; 67 info.resolvePackageName = mPackageName; 68 info.providerInfo = mProviderInfo; 69 return info; 70 } 71 } 72