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 android.test;
18 
19 import android.accounts.AccountManager;
20 import android.content.AttributionSource;
21 import android.content.BroadcastReceiver;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.content.ContextWrapper;
25 import android.content.Intent;
26 import android.content.IntentFilter;
27 import android.content.ServiceConnection;
28 import android.content.pm.PackageManager;
29 import android.net.Uri;
30 import android.os.Process;
31 import android.test.mock.MockAccountManager;
32 
33 import java.io.File;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.concurrent.Executor;
37 
38 
39 /**
40  * A mock context which prevents its users from talking to the rest of the device while
41  * stubbing enough methods to satify code that tries to talk to other packages.
42  *
43  * @deprecated New tests should be written using the
44  * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>.
45  */
46 @Deprecated
47 public class IsolatedContext extends ContextWrapper {
48 
49     private ContentResolver mResolver;
50     private final AccountManager mMockAccountManager;
51 
52     private List<Intent> mBroadcastIntents = new ArrayList<>();
53 
IsolatedContext( ContentResolver resolver, Context targetContext)54     public IsolatedContext(
55             ContentResolver resolver, Context targetContext) {
56         super(targetContext);
57         mResolver = resolver;
58         mMockAccountManager = MockAccountManager.newMockAccountManager(IsolatedContext.this);
59     }
60 
61     /** Returns the list of intents that were broadcast since the last call to this method. */
getAndClearBroadcastIntents()62     public List<Intent> getAndClearBroadcastIntents() {
63         List<Intent> intents = mBroadcastIntents;
64         mBroadcastIntents = new ArrayList<>();
65         return intents;
66     }
67 
68     @Override
getAttributionSource()69     public AttributionSource getAttributionSource() {
70         AttributionSource attributionSource = super.getAttributionSource();
71         if (attributionSource == null) {
72             return new AttributionSource.Builder(Process.myUid()).build();
73         }
74         return attributionSource;
75     }
76 
77     @Override
getContentResolver()78     public ContentResolver getContentResolver() {
79         // We need to return the real resolver so that MailEngine.makeRight can get to the
80         // subscribed feeds provider. TODO: mock out subscribed feeds too.
81         return mResolver;
82     }
83 
84     @Override
bindService(Intent service, ServiceConnection conn, int flags)85     public boolean bindService(Intent service, ServiceConnection conn, int flags) {
86         return false;
87     }
88 
89     @Override
bindService(Intent service, int flags, Executor executor, ServiceConnection conn)90     public boolean bindService(Intent service, int flags, Executor executor,
91             ServiceConnection conn) {
92         return false;
93     }
94 
95     @Override
bindIsolatedService(Intent service, int flags, String instanceName, Executor executor, ServiceConnection conn)96     public boolean bindIsolatedService(Intent service, int flags, String instanceName,
97             Executor executor, ServiceConnection conn) {
98         return false;
99     }
100 
101     @Override
registerReceiver(BroadcastReceiver receiver, IntentFilter filter)102     public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
103         return null;
104     }
105 
106     @Override
unregisterReceiver(BroadcastReceiver receiver)107     public void unregisterReceiver(BroadcastReceiver receiver) {
108         // Ignore
109     }
110 
111     @Override
sendBroadcast(Intent intent)112     public void sendBroadcast(Intent intent) {
113         mBroadcastIntents.add(intent);
114     }
115 
116     @Override
sendOrderedBroadcast(Intent intent, String receiverPermission)117     public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
118         mBroadcastIntents.add(intent);
119     }
120 
121     @Override
checkUriPermission( Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags)122     public int checkUriPermission(
123             Uri uri, String readPermission, String writePermission, int pid,
124             int uid, int modeFlags) {
125         return PackageManager.PERMISSION_GRANTED;
126     }
127 
128     @Override
checkUriPermission(Uri uri, int pid, int uid, int modeFlags)129     public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
130         return PackageManager.PERMISSION_GRANTED;
131     }
132 
133     @Override
getSystemService(String name)134     public Object getSystemService(String name) {
135         if (Context.ACCOUNT_SERVICE.equals(name)) {
136             return mMockAccountManager;
137         }
138         // No other services exist in this context.
139         return null;
140     }
141 
142     @Override
getFilesDir()143     public File getFilesDir() {
144         return new File("/dev/null");
145     }
146 }
147