blob: dd4a9a3a4d697ec07214f5ae5079cba1bc5d3c52 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.BroadcastReceiver;
Bo Liu58a57662019-03-06 20:21:45 +000021import android.content.ContentResolver;
22import android.content.Context;
23import android.content.ContextWrapper;
24import android.content.Intent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.IntentFilter;
Bo Liu58a57662019-03-06 20:21:45 +000026import android.content.ServiceConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.pm.PackageManager;
28import android.net.Uri;
Paul Duffin20af1df2018-01-05 13:52:17 +000029import android.test.mock.MockAccountManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
Ken Shirriff3b95f532009-07-06 10:45:38 -070031import java.io.File;
Paul Duffin8c5a24d2017-05-10 13:30:16 +010032import java.util.ArrayList;
Paul Westbrook69120a72010-02-26 18:21:15 -080033import java.util.List;
Bo Liu58a57662019-03-06 20:21:45 +000034import java.util.concurrent.Executor;
Paul Westbrook69120a72010-02-26 18:21:15 -080035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37/**
Stephan Linznerb51617f2016-01-27 18:09:50 -080038 * 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 -080039 * stubbing enough methods to satify code that tries to talk to other packages.
Stephan Linznerb51617f2016-01-27 18:09:50 -080040 *
41 * @deprecated New tests should be written using the
42 * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 */
Stephan Linznerb51617f2016-01-27 18:09:50 -080044@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045public class IsolatedContext extends ContextWrapper {
46
47 private ContentResolver mResolver;
Paul Duffin20af1df2018-01-05 13:52:17 +000048 private final AccountManager mMockAccountManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
Paul Duffin8c5a24d2017-05-10 13:30:16 +010050 private List<Intent> mBroadcastIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
52 public IsolatedContext(
53 ContentResolver resolver, Context targetContext) {
54 super(targetContext);
55 mResolver = resolver;
Paul Duffin20af1df2018-01-05 13:52:17 +000056 mMockAccountManager = MockAccountManager.newMockAccountManager(IsolatedContext.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 }
58
59 /** Returns the list of intents that were broadcast since the last call to this method. */
60 public List<Intent> getAndClearBroadcastIntents() {
61 List<Intent> intents = mBroadcastIntents;
Paul Duffin8c5a24d2017-05-10 13:30:16 +010062 mBroadcastIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 return intents;
64 }
65
66 @Override
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
74 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
75 return false;
76 }
77
78 @Override
Bo Liu58a57662019-03-06 20:21:45 +000079 public boolean bindService(Intent service, int flags, Executor executor,
80 ServiceConnection conn) {
81 return false;
82 }
83
84 @Override
85 public boolean bindIsolatedService(Intent service, int flags, String instanceName,
86 Executor executor, ServiceConnection conn) {
Dianne Hackborn27b4d942018-11-12 15:01:40 -080087 return false;
88 }
89
90 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
92 return null;
93 }
94
95 @Override
Jonas Schwertfegerd6724752010-09-30 14:04:09 +020096 public void unregisterReceiver(BroadcastReceiver receiver) {
97 // Ignore
98 }
99
100 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 public void sendBroadcast(Intent intent) {
102 mBroadcastIntents.add(intent);
103 }
104
105 @Override
106 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
107 mBroadcastIntents.add(intent);
108 }
109
110 @Override
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
118 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
119 return PackageManager.PERMISSION_GRANTED;
120 }
121
122 @Override
123 public Object getSystemService(String name) {
Fred Quintana0eabf022009-04-27 15:08:17 -0700124 if (Context.ACCOUNT_SERVICE.equals(name)) {
125 return mMockAccountManager;
126 }
127 // No other services exist in this context.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 return null;
129 }
130
Ken Shirriff3b95f532009-07-06 10:45:38 -0700131 @Override
132 public File getFilesDir() {
133 return new File("/dev/null");
134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135}