• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.accounts.AccountManagerCallback;
21 import android.accounts.AccountManagerFuture;
22 import android.accounts.AuthenticatorException;
23 import android.accounts.OnAccountsUpdateListener;
24 import android.accounts.OperationCanceledException;
25 import android.accounts.Account;
26 import android.content.ContextWrapper;
27 import android.content.ContentResolver;
28 import android.content.Intent;
29 import android.content.Context;
30 import android.content.ServiceConnection;
31 import android.content.BroadcastReceiver;
32 import android.content.IntentFilter;
33 import android.content.pm.PackageManager;
34 import android.net.Uri;
35 import android.os.Handler;
36 
37 import java.io.File;
38 import java.io.IOException;
39 import java.util.ArrayList;
40 import java.util.concurrent.TimeUnit;
41 import java.util.List;
42 
43 
44 /**
45  * A mock context which prevents its users from talking to the rest of the device while
46  * stubbing enough methods to satify code that tries to talk to other packages.
47  *
48  * @deprecated New tests should be written using the
49  * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>.
50  */
51 @Deprecated
52 public class IsolatedContext extends ContextWrapper {
53 
54     private ContentResolver mResolver;
55     private final MockAccountManager mMockAccountManager;
56 
57     private List<Intent> mBroadcastIntents = new ArrayList<>();
58 
IsolatedContext( ContentResolver resolver, Context targetContext)59     public IsolatedContext(
60             ContentResolver resolver, Context targetContext) {
61         super(targetContext);
62         mResolver = resolver;
63         mMockAccountManager = new MockAccountManager();
64     }
65 
66     /** Returns the list of intents that were broadcast since the last call to this method. */
getAndClearBroadcastIntents()67     public List<Intent> getAndClearBroadcastIntents() {
68         List<Intent> intents = mBroadcastIntents;
69         mBroadcastIntents = new ArrayList<>();
70         return intents;
71     }
72 
73     @Override
getContentResolver()74     public ContentResolver getContentResolver() {
75         // We need to return the real resolver so that MailEngine.makeRight can get to the
76         // subscribed feeds provider. TODO: mock out subscribed feeds too.
77         return mResolver;
78     }
79 
80     @Override
bindService(Intent service, ServiceConnection conn, int flags)81     public boolean bindService(Intent service, ServiceConnection conn, int flags) {
82         return false;
83     }
84 
85     @Override
registerReceiver(BroadcastReceiver receiver, IntentFilter filter)86     public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
87         return null;
88     }
89 
90     @Override
unregisterReceiver(BroadcastReceiver receiver)91     public void unregisterReceiver(BroadcastReceiver receiver) {
92         // Ignore
93     }
94 
95     @Override
sendBroadcast(Intent intent)96     public void sendBroadcast(Intent intent) {
97         mBroadcastIntents.add(intent);
98     }
99 
100     @Override
sendOrderedBroadcast(Intent intent, String receiverPermission)101     public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
102         mBroadcastIntents.add(intent);
103     }
104 
105     @Override
checkUriPermission( Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags)106     public int checkUriPermission(
107             Uri uri, String readPermission, String writePermission, int pid,
108             int uid, int modeFlags) {
109         return PackageManager.PERMISSION_GRANTED;
110     }
111 
112     @Override
checkUriPermission(Uri uri, int pid, int uid, int modeFlags)113     public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
114         return PackageManager.PERMISSION_GRANTED;
115     }
116 
117     @Override
getSystemService(String name)118     public Object getSystemService(String name) {
119         if (Context.ACCOUNT_SERVICE.equals(name)) {
120             return mMockAccountManager;
121         }
122         // No other services exist in this context.
123         return null;
124     }
125 
126     private class MockAccountManager extends AccountManager {
MockAccountManager()127         public MockAccountManager() {
128             super(IsolatedContext.this, null /* IAccountManager */, null /* handler */);
129         }
130 
addOnAccountsUpdatedListener(OnAccountsUpdateListener listener, Handler handler, boolean updateImmediately)131         public void addOnAccountsUpdatedListener(OnAccountsUpdateListener listener,
132                 Handler handler, boolean updateImmediately) {
133             // do nothing
134         }
135 
getAccounts()136         public Account[] getAccounts() {
137             return new Account[]{};
138         }
139 
getAccountsByTypeAndFeatures( final String type, final String[] features, AccountManagerCallback<Account[]> callback, Handler handler)140         public AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures(
141                 final String type, final String[] features,
142                 AccountManagerCallback<Account[]> callback, Handler handler) {
143             return new MockAccountManagerFuture<Account[]>(new Account[0]);
144         }
145 
blockingGetAuthToken(Account account, String authTokenType, boolean notifyAuthFailure)146         public String blockingGetAuthToken(Account account, String authTokenType,
147                 boolean notifyAuthFailure)
148                 throws OperationCanceledException, IOException, AuthenticatorException {
149             return null;
150         }
151 
152 
153         /**
154          * A very simple AccountManagerFuture class
155          * that returns what ever was passed in
156          */
157         private class MockAccountManagerFuture<T>
158                 implements AccountManagerFuture<T> {
159 
160             T mResult;
161 
MockAccountManagerFuture(T result)162             public MockAccountManagerFuture(T result) {
163                 mResult = result;
164             }
165 
cancel(boolean mayInterruptIfRunning)166             public boolean cancel(boolean mayInterruptIfRunning) {
167                 return false;
168             }
169 
isCancelled()170             public boolean isCancelled() {
171                 return false;
172             }
173 
isDone()174             public boolean isDone() {
175                 return true;
176             }
177 
getResult()178             public T getResult()
179                     throws OperationCanceledException, IOException, AuthenticatorException {
180                 return mResult;
181             }
182 
getResult(long timeout, TimeUnit unit)183             public T getResult(long timeout, TimeUnit unit)
184                     throws OperationCanceledException, IOException, AuthenticatorException {
185                 return getResult();
186             }
187         }
188 
189     }
190 
191     @Override
getFilesDir()192     public File getFilesDir() {
193         return new File("/dev/null");
194     }
195 }
196