blob: bc00f68e8322ce6d3003f0b3749c34eb3f097663 [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
19import com.google.android.collect.Lists;
20
Fred Quintana0eabf022009-04-27 15:08:17 -070021import android.accounts.AccountManager;
Paul Westbrook69120a72010-02-26 18:21:15 -080022import android.accounts.AccountManagerCallback;
23import android.accounts.AccountManagerFuture;
24import android.accounts.AuthenticatorException;
Fred Quintanaf7ae77c2009-10-02 17:19:31 -070025import android.accounts.OnAccountsUpdateListener;
Paul Westbrook69120a72010-02-26 18:21:15 -080026import android.accounts.OperationCanceledException;
Cynthia Wong904de612009-09-03 10:06:55 -070027import android.accounts.Account;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.ContextWrapper;
29import android.content.ContentResolver;
30import android.content.Intent;
31import android.content.Context;
32import android.content.ServiceConnection;
33import android.content.BroadcastReceiver;
34import android.content.IntentFilter;
35import android.content.pm.PackageManager;
36import android.net.Uri;
Fred Quintana0eabf022009-04-27 15:08:17 -070037import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
Ken Shirriff3b95f532009-07-06 10:45:38 -070039import java.io.File;
Paul Westbrook69120a72010-02-26 18:21:15 -080040import java.io.IOException;
41import java.util.concurrent.TimeUnit;
42import java.util.concurrent.ExecutionException;
43import java.util.concurrent.TimeoutException;
44import java.util.List;
45
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
47/**
48 * A mock context which prevents its users from talking to the rest of the device while
49 * stubbing enough methods to satify code that tries to talk to other packages.
50 */
51public class IsolatedContext extends ContextWrapper {
52
53 private ContentResolver mResolver;
Fred Quintana0eabf022009-04-27 15:08:17 -070054 private final MockAccountManager mMockAccountManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
56 private List<Intent> mBroadcastIntents = Lists.newArrayList();
57
58 public IsolatedContext(
59 ContentResolver resolver, Context targetContext) {
60 super(targetContext);
61 mResolver = resolver;
Fred Quintana0eabf022009-04-27 15:08:17 -070062 mMockAccountManager = new MockAccountManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 }
64
65 /** Returns the list of intents that were broadcast since the last call to this method. */
66 public List<Intent> getAndClearBroadcastIntents() {
67 List<Intent> intents = mBroadcastIntents;
68 mBroadcastIntents = Lists.newArrayList();
69 return intents;
70 }
71
72 @Override
73 public ContentResolver getContentResolver() {
74 // We need to return the real resolver so that MailEngine.makeRight can get to the
75 // subscribed feeds provider. TODO: mock out subscribed feeds too.
76 return mResolver;
77 }
78
79 @Override
80 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
81 return false;
82 }
83
84 @Override
85 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
86 return null;
87 }
88
89 @Override
Jonas Schwertfegerd6724752010-09-30 14:04:09 +020090 public void unregisterReceiver(BroadcastReceiver receiver) {
91 // Ignore
92 }
93
94 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 public void sendBroadcast(Intent intent) {
96 mBroadcastIntents.add(intent);
97 }
98
99 @Override
100 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
101 mBroadcastIntents.add(intent);
102 }
103
104 @Override
105 public int checkUriPermission(
106 Uri uri, String readPermission, String writePermission, int pid,
107 int uid, int modeFlags) {
108 return PackageManager.PERMISSION_GRANTED;
109 }
110
111 @Override
112 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
113 return PackageManager.PERMISSION_GRANTED;
114 }
115
116 @Override
117 public Object getSystemService(String name) {
Fred Quintana0eabf022009-04-27 15:08:17 -0700118 if (Context.ACCOUNT_SERVICE.equals(name)) {
119 return mMockAccountManager;
120 }
121 // No other services exist in this context.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 return null;
123 }
124
Fred Quintana0eabf022009-04-27 15:08:17 -0700125 private class MockAccountManager extends AccountManager {
126 public MockAccountManager() {
127 super(IsolatedContext.this, null /* IAccountManager */, null /* handler */);
128 }
129
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700130 public void addOnAccountsUpdatedListener(OnAccountsUpdateListener listener,
Fred Quintana0eabf022009-04-27 15:08:17 -0700131 Handler handler, boolean updateImmediately) {
132 // do nothing
133 }
Cynthia Wong904de612009-09-03 10:06:55 -0700134
135 public Account[] getAccounts() {
136 return new Account[]{};
137 }
Paul Westbrook69120a72010-02-26 18:21:15 -0800138
139 public AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures(
140 final String type, final String[] features,
141 AccountManagerCallback<Account[]> callback, Handler handler) {
142 return new MockAccountManagerFuture<Account[]>(new Account[0]);
143 }
144
145 public String blockingGetAuthToken(Account account, String authTokenType,
146 boolean notifyAuthFailure)
147 throws OperationCanceledException, IOException, AuthenticatorException {
148 return null;
149 }
150
151
152 /**
153 * A very simple AccountManagerFuture class
154 * that returns what ever was passed in
155 */
156 private class MockAccountManagerFuture<T>
157 implements AccountManagerFuture<T> {
158
159 T mResult;
160
161 public MockAccountManagerFuture(T result) {
162 mResult = result;
163 }
164
165 public boolean cancel(boolean mayInterruptIfRunning) {
166 return false;
167 }
168
169 public boolean isCancelled() {
170 return false;
171 }
172
173 public boolean isDone() {
174 return true;
175 }
176
177 public T getResult()
178 throws OperationCanceledException, IOException, AuthenticatorException {
179 return mResult;
180 }
181
182 public T getResult(long timeout, TimeUnit unit)
183 throws OperationCanceledException, IOException, AuthenticatorException {
184 return getResult();
185 }
186 }
187
Fred Quintana0eabf022009-04-27 15:08:17 -0700188 }
Paul Westbrook69120a72010-02-26 18:21:15 -0800189
Ken Shirriff3b95f532009-07-06 10:45:38 -0700190 @Override
191 public File getFilesDir() {
192 return new File("/dev/null");
193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194}