blob: 0b77c0062dfb481ada4398fd0b7673f4df5fe598 [file] [log] [blame]
Kenny Root10362ab2010-03-12 11:13:50 -08001/*
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.test;
18
Fred Quintana0eabf022009-04-27 15:08:17 -070019import android.accounts.AccountManager;
Paul Westbrook69120a72010-02-26 18:21:15 -080020import android.accounts.AccountManagerCallback;
21import android.accounts.AccountManagerFuture;
22import android.accounts.AuthenticatorException;
Fred Quintanaf7ae77c2009-10-02 17:19:31 -070023import android.accounts.OnAccountsUpdateListener;
Paul Westbrook69120a72010-02-26 18:21:15 -080024import android.accounts.OperationCanceledException;
Cynthia Wong904de612009-09-03 10:06:55 -070025import android.accounts.Account;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.ContextWrapper;
27import android.content.ContentResolver;
28import android.content.Intent;
29import android.content.Context;
30import android.content.ServiceConnection;
31import android.content.BroadcastReceiver;
32import android.content.IntentFilter;
33import android.content.pm.PackageManager;
34import android.net.Uri;
Fred Quintana0eabf022009-04-27 15:08:17 -070035import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
Ken Shirriff3b95f532009-07-06 10:45:38 -070037import java.io.File;
Paul Westbrook69120a72010-02-26 18:21:15 -080038import java.io.IOException;
Paul Duffin8c5a24d2017-05-10 13:30:16 +010039import java.util.ArrayList;
Paul Westbrook69120a72010-02-26 18:21:15 -080040import java.util.concurrent.TimeUnit;
Paul Westbrook69120a72010-02-26 18:21:15 -080041import java.util.List;
42
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44/**
Stephan Linznerb51617f2016-01-27 18:09:50 -080045 * A mock context which prevents its users from talking to the rest of the device while
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 * stubbing enough methods to satify code that tries to talk to other packages.
Stephan Linznerb51617f2016-01-27 18:09:50 -080047 *
48 * @deprecated New tests should be written using the
49 * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 */
Stephan Linznerb51617f2016-01-27 18:09:50 -080051@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052public class IsolatedContext extends ContextWrapper {
53
54 private ContentResolver mResolver;
Fred Quintana0eabf022009-04-27 15:08:17 -070055 private final MockAccountManager mMockAccountManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
Paul Duffin8c5a24d2017-05-10 13:30:16 +010057 private List<Intent> mBroadcastIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
59 public IsolatedContext(
60 ContentResolver resolver, Context targetContext) {
61 super(targetContext);
62 mResolver = resolver;
Fred Quintana0eabf022009-04-27 15:08:17 -070063 mMockAccountManager = new MockAccountManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 }
65
66 /** Returns the list of intents that were broadcast since the last call to this method. */
67 public List<Intent> getAndClearBroadcastIntents() {
68 List<Intent> intents = mBroadcastIntents;
Paul Duffin8c5a24d2017-05-10 13:30:16 +010069 mBroadcastIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 return intents;
71 }
72
73 @Override
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
81 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
82 return false;
83 }
84
85 @Override
86 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
87 return null;
88 }
89
90 @Override
Jonas Schwertfegerd6724752010-09-30 14:04:09 +020091 public void unregisterReceiver(BroadcastReceiver receiver) {
92 // Ignore
93 }
94
95 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 public void sendBroadcast(Intent intent) {
97 mBroadcastIntents.add(intent);
98 }
99
100 @Override
101 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
102 mBroadcastIntents.add(intent);
103 }
104
105 @Override
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
113 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
114 return PackageManager.PERMISSION_GRANTED;
115 }
116
117 @Override
118 public Object getSystemService(String name) {
Fred Quintana0eabf022009-04-27 15:08:17 -0700119 if (Context.ACCOUNT_SERVICE.equals(name)) {
120 return mMockAccountManager;
121 }
122 // No other services exist in this context.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 return null;
124 }
125
Fred Quintana0eabf022009-04-27 15:08:17 -0700126 private class MockAccountManager extends AccountManager {
127 public MockAccountManager() {
128 super(IsolatedContext.this, null /* IAccountManager */, null /* handler */);
129 }
130
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700131 public void addOnAccountsUpdatedListener(OnAccountsUpdateListener listener,
Fred Quintana0eabf022009-04-27 15:08:17 -0700132 Handler handler, boolean updateImmediately) {
133 // do nothing
134 }
Cynthia Wong904de612009-09-03 10:06:55 -0700135
136 public Account[] getAccounts() {
137 return new Account[]{};
138 }
Paul Westbrook69120a72010-02-26 18:21:15 -0800139
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
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
162 public MockAccountManagerFuture(T result) {
163 mResult = result;
164 }
165
166 public boolean cancel(boolean mayInterruptIfRunning) {
167 return false;
168 }
169
170 public boolean isCancelled() {
171 return false;
172 }
173
174 public boolean isDone() {
175 return true;
176 }
177
178 public T getResult()
179 throws OperationCanceledException, IOException, AuthenticatorException {
180 return mResult;
181 }
182
183 public T getResult(long timeout, TimeUnit unit)
184 throws OperationCanceledException, IOException, AuthenticatorException {
185 return getResult();
186 }
187 }
188
Fred Quintana0eabf022009-04-27 15:08:17 -0700189 }
Paul Westbrook69120a72010-02-26 18:21:15 -0800190
Ken Shirriff3b95f532009-07-06 10:45:38 -0700191 @Override
192 public File getFilesDir() {
193 return new File("/dev/null");
194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195}