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