aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/xtremelabs/robolectric/res/RobolectricPackageManager.java
blob: 730dc3dbed84fd04d5c3a20a8a019ff8ea75e4aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.xtremelabs.robolectric.res;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ComponentName;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;

import com.xtremelabs.robolectric.RobolectricConfig;
import com.xtremelabs.robolectric.tester.android.content.pm.StubPackageManager;

public class RobolectricPackageManager extends StubPackageManager {

    private Map<String, PackageInfo> packageList;
    private Map<Intent, List<ResolveInfo>> resolveList = new HashMap<Intent, List<ResolveInfo>>();
    private Map<ComponentName, ComponentState> componentList = new HashMap<ComponentName,ComponentState>();
    private Map<ComponentName, Drawable> drawableList = new HashMap<ComponentName, Drawable>();
    private Map<String, Boolean> systemFeatureList = new HashMap<String, Boolean>();

    private ContextWrapper contextWrapper;
    private RobolectricConfig config;
    private ApplicationInfo applicationInfo;

    public RobolectricPackageManager(ContextWrapper contextWrapper, RobolectricConfig config) {
        this.contextWrapper = contextWrapper;
        this.config = config;
        initializePackageInfo();
    }

    @Override
    public PackageInfo getPackageInfo(String packageName, int flags) throws NameNotFoundException {
        if (packageList.containsKey(packageName)) {
        	return packageList.get(packageName);
        }

        throw new NameNotFoundException();
    }

    @Override
    public ApplicationInfo getApplicationInfo(String packageName, int flags) throws NameNotFoundException {

        if (config.getPackageName().equals(packageName)) {
            if (applicationInfo == null) {
                applicationInfo = new ApplicationInfo();
                applicationInfo.flags = config.getApplicationFlags();
                applicationInfo.targetSdkVersion = config.getSdkVersion();
                applicationInfo.packageName = config.getPackageName();
                applicationInfo.processName = config.getProcessName();
                applicationInfo.name = config.getApplicationName();
            }
            return applicationInfo;
        }

        PackageInfo info;
        if ((info = packageList.get(packageName)) != null) {
        	return info.applicationInfo;
        }

        throw new NameNotFoundException();
    }

    @Override
    public List<PackageInfo> getInstalledPackages(int flags) {
        return new ArrayList<PackageInfo>(packageList.values());
    }

    @Override
    public List<ResolveInfo> queryIntentActivities( Intent intent, int flags ) {
        return queryIntent(intent, flags);
    }

    @Override
    public List<ResolveInfo> queryIntentServices( Intent intent, int flags ) {
        return queryIntent(intent, flags);
    }

    @Override
    public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
        return queryIntent(intent, flags);
    }

    @Override
    public ResolveInfo resolveActivity(Intent intent, int flags) {
    	List<ResolveInfo> candidates = queryIntentActivities(intent, flags);
    	return candidates.isEmpty() ? null : candidates.get(0);
    }

    @Override
    public ResolveInfo resolveService(Intent intent, int flags) {
        return resolveActivity(intent, flags);
    }

    public void addResolveInfoForIntent( Intent intent, List<ResolveInfo> info ) {
        resolveList.put(intent, info);
    }

    public void addResolveInfoForIntent(Intent intent, ResolveInfo info) {
        List<ResolveInfo> l = resolveList.get(intent);
        if (l == null) {
            l = new ArrayList<ResolveInfo>();
            resolveList.put(intent, l);
        }
        l.add(info);
    }

    @Override
    public Drawable getActivityIcon(Intent intent) {
    	return drawableList.get(intent.getComponent());
    }

    @Override
    public Drawable getActivityIcon(ComponentName componentName) {
    	return drawableList.get(componentName);
    }

    public void addActivityIcon( ComponentName component, Drawable d ) {
    	drawableList.put( component, d);
    }

    public void addActivityIcon( Intent intent, Drawable d ) {
    	drawableList.put( intent.getComponent(), d);
    }

	@Override
	public Intent getLaunchIntentForPackage(String packageName) {
		Intent i = new Intent();
		i.setComponent( new ComponentName(packageName, "") );
		return i;
	}

	@Override
	public CharSequence getApplicationLabel(ApplicationInfo info) {
		return info.name;
	}

	@Override
	public void setComponentEnabledSetting(ComponentName componentName, int newState, int flags) {
		componentList.put(componentName, new ComponentState(newState, flags));
	}

	/**
	 * Non-Android accessor.  Use to make assertions on values passed to
	 * setComponentEnabledSetting.
	 *
	 * @param componentName
	 * @return
	 */
	public RobolectricPackageManager.ComponentState getComponentState(ComponentName componentName) {
		return componentList.get(componentName);
	}

    /**
     * Non-Android accessor.  Used to add a package to the list of those
     * already 'installed' on system.
     *
     * @param packageInfo
     */
    public void addPackage( PackageInfo packageInfo ) {
    	 packageList.put(packageInfo.packageName, packageInfo);
    }

    public void addPackage( String packageName ) {
    	PackageInfo info = new PackageInfo();
    	info.packageName = packageName;
    	addPackage( info );
    }

    @Override
    public boolean hasSystemFeature(String name) {
        return systemFeatureList.containsKey(name) ? systemFeatureList.get(name) : false;
    }

    /**
     * Non-Android accessor.  Used to declare a system feature is
     * or is not supported.
     *
     * @param name
     * @param supported
     */
    public void setSystemFeature(String name, boolean supported) {
    	systemFeatureList.put(name, supported);
    }

    private List<ResolveInfo> queryIntent(Intent intent, int flags) {
        List<ResolveInfo> result = resolveList.get(intent);
        if (result == null) {
            return Collections.emptyList();
        } else {
            return result;
        }
    }

    private void initializePackageInfo() {
    	if (packageList != null) { return; }

        PackageInfo packageInfo = new PackageInfo();
        packageInfo.packageName = contextWrapper.getPackageName();
        packageInfo.versionName = "1.0";

        packageList = new HashMap<String, PackageInfo>();
        addPackage( packageInfo );
    }

    public class ComponentState {
    	public int newState;
    	public int flags;

		public ComponentState(int newState, int flags) {
			this.newState = newState;
			this.flags = flags;
		}
    }
}