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