• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.documentsui.testing;
18 
19 import android.content.Intent;
20 import android.content.pm.ActivityInfo;
21 import android.content.pm.ApplicationInfo;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ProviderInfo;
24 
25 import android.content.pm.ResolveInfo;
26 import com.android.documentsui.base.RootInfo;
27 
28 import org.mockito.Mockito;
29 
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 
35 /**
36  * Abstract to avoid having to implement unnecessary Activity stuff.
37  * Instances are created using {@link #create()}.
38  */
39 public abstract class TestPackageManager extends PackageManager {
40 
41     public Map<String, ResolveInfo> contentProviders;
42     public List<ResolveInfo> queryIntentProvidersResults = new ArrayList<>();
43 
addStubContentProviderForRoot(RootInfo... roots)44     public void addStubContentProviderForRoot(RootInfo... roots) {
45         for (RootInfo root : roots) {
46             // only one entry per authority is required.
47             if (!contentProviders.containsKey(root.authority)) {
48                 ResolveInfo info = new ResolveInfo();
49                 contentProviders.put(root.authority, info);
50                 info.providerInfo = new ProviderInfo();
51                 info.providerInfo.authority = root.authority;
52             }
53         }
54     }
55 
create()56     public static TestPackageManager create() {
57         TestPackageManager pm = Mockito.mock(
58                 TestPackageManager.class, Mockito.CALLS_REAL_METHODS);
59         pm.contentProviders = new HashMap<>();
60         return pm;
61     }
62 
63     @Override
queryIntentContentProviders(Intent intent, int flags)64     public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
65         List<ResolveInfo> result = new ArrayList<>();
66         result.addAll(contentProviders.values());
67         return result;
68     }
69 
70     /**
71      * Query's a list of fake apps that can open an application.
72      */
73     @Override
queryIntentActivities(Intent intent, int flags)74     public List<ResolveInfo> queryIntentActivities(Intent intent, int flags) {
75         if (queryIntentProvidersResults == null) {
76             return new ArrayList<>();
77         } else {
78             return queryIntentProvidersResults;
79         }
80     }
81 
82     @Override
resolveActivity(Intent intent, int flags)83     public ResolveInfo resolveActivity(Intent intent, int flags) {
84         ResolveInfo info = new TestResolveInfo();
85         info.activityInfo = new ActivityInfo();
86         info.activityInfo.packageName =
87                 intent.getPackage() != null ? intent.getPackage() : "TestPackage";
88         info.activityInfo.applicationInfo = new ApplicationInfo();
89         info.activityInfo.applicationInfo.packageName = intent.getPackage();
90         info.activityInfo.name = "Fake Quick Viewer";
91         return info;
92     }
93 
resolveActivityAsUser( Intent intent, int flags, int userId)94     public final ResolveInfo resolveActivityAsUser(
95             Intent intent, int flags, int userId) {
96         return resolveActivity(intent, flags);
97     }
98 
99     /**
100      * Hacky way to use resolve info in test. resolve info return null when new'ing up a instance
101      * because of an exception thrown in toString.
102      */
103     public static class TestResolveInfo extends ResolveInfo {
104 
105         @Override
toString()106         public String toString() {
107             return "";
108         }
109     }
110 }
111