blob: b483b825fdef654a24972bad1c16b7458c6e6c9f [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
90 public void sendBroadcast(Intent intent) {
91 mBroadcastIntents.add(intent);
92 }
93
94 @Override
95 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
96 mBroadcastIntents.add(intent);
97 }
98
99 @Override
100 public int checkUriPermission(
101 Uri uri, String readPermission, String writePermission, int pid,
102 int uid, int modeFlags) {
103 return PackageManager.PERMISSION_GRANTED;
104 }
105
106 @Override
107 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
108 return PackageManager.PERMISSION_GRANTED;
109 }
110
111 @Override
112 public Object getSystemService(String name) {
Fred Quintana0eabf022009-04-27 15:08:17 -0700113 if (Context.ACCOUNT_SERVICE.equals(name)) {
114 return mMockAccountManager;
115 }
116 // No other services exist in this context.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 return null;
118 }
119
Fred Quintana0eabf022009-04-27 15:08:17 -0700120 private class MockAccountManager extends AccountManager {
121 public MockAccountManager() {
122 super(IsolatedContext.this, null /* IAccountManager */, null /* handler */);
123 }
124
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700125 public void addOnAccountsUpdatedListener(OnAccountsUpdateListener listener,
Fred Quintana0eabf022009-04-27 15:08:17 -0700126 Handler handler, boolean updateImmediately) {
127 // do nothing
128 }
Cynthia Wong904de612009-09-03 10:06:55 -0700129
130 public Account[] getAccounts() {
131 return new Account[]{};
132 }
Paul Westbrook69120a72010-02-26 18:21:15 -0800133
134 public AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures(
135 final String type, final String[] features,
136 AccountManagerCallback<Account[]> callback, Handler handler) {
137 return new MockAccountManagerFuture<Account[]>(new Account[0]);
138 }
139
140 public String blockingGetAuthToken(Account account, String authTokenType,
141 boolean notifyAuthFailure)
142 throws OperationCanceledException, IOException, AuthenticatorException {
143 return null;
144 }
145
146
147 /**
148 * A very simple AccountManagerFuture class
149 * that returns what ever was passed in
150 */
151 private class MockAccountManagerFuture<T>
152 implements AccountManagerFuture<T> {
153
154 T mResult;
155
156 public MockAccountManagerFuture(T result) {
157 mResult = result;
158 }
159
160 public boolean cancel(boolean mayInterruptIfRunning) {
161 return false;
162 }
163
164 public boolean isCancelled() {
165 return false;
166 }
167
168 public boolean isDone() {
169 return true;
170 }
171
172 public T getResult()
173 throws OperationCanceledException, IOException, AuthenticatorException {
174 return mResult;
175 }
176
177 public T getResult(long timeout, TimeUnit unit)
178 throws OperationCanceledException, IOException, AuthenticatorException {
179 return getResult();
180 }
181 }
182
Fred Quintana0eabf022009-04-27 15:08:17 -0700183 }
Paul Westbrook69120a72010-02-26 18:21:15 -0800184
Ken Shirriff3b95f532009-07-06 10:45:38 -0700185 @Override
186 public File getFilesDir() {
187 return new File("/dev/null");
188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189}