blob: d5f92a3181b93a4ec9aa1215ea062dcec23782af [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;
Rafael Higuera Silva7974e192022-02-28 19:16:34 +000020import android.content.AttributionSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.BroadcastReceiver;
Bo Liu58a57662019-03-06 20:21:45 +000022import android.content.ContentResolver;
23import android.content.Context;
24import android.content.ContextWrapper;
25import android.content.Intent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.IntentFilter;
Bo Liu58a57662019-03-06 20:21:45 +000027import android.content.ServiceConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.pm.PackageManager;
29import android.net.Uri;
Rafael Higuera Silva7974e192022-02-28 19:16:34 +000030import android.os.Process;
Paul Duffin20af1df2018-01-05 13:52:17 +000031import android.test.mock.MockAccountManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
Ken Shirriff3b95f532009-07-06 10:45:38 -070033import java.io.File;
Paul Duffin8c5a24d2017-05-10 13:30:16 +010034import java.util.ArrayList;
Paul Westbrook69120a72010-02-26 18:21:15 -080035import java.util.List;
Bo Liu58a57662019-03-06 20:21:45 +000036import java.util.concurrent.Executor;
Paul Westbrook69120a72010-02-26 18:21:15 -080037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39/**
Stephan Linznerb51617f2016-01-27 18:09:50 -080040 * 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 -080041 * stubbing enough methods to satify code that tries to talk to other packages.
Stephan Linznerb51617f2016-01-27 18:09:50 -080042 *
43 * @deprecated New tests should be written using the
44 * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 */
Stephan Linznerb51617f2016-01-27 18:09:50 -080046@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047public class IsolatedContext extends ContextWrapper {
48
49 private ContentResolver mResolver;
Paul Duffin20af1df2018-01-05 13:52:17 +000050 private final AccountManager mMockAccountManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Paul Duffin8c5a24d2017-05-10 13:30:16 +010052 private List<Intent> mBroadcastIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54 public IsolatedContext(
55 ContentResolver resolver, Context targetContext) {
56 super(targetContext);
57 mResolver = resolver;
Paul Duffin20af1df2018-01-05 13:52:17 +000058 mMockAccountManager = MockAccountManager.newMockAccountManager(IsolatedContext.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 }
60
61 /** Returns the list of intents that were broadcast since the last call to this method. */
62 public List<Intent> getAndClearBroadcastIntents() {
63 List<Intent> intents = mBroadcastIntents;
Paul Duffin8c5a24d2017-05-10 13:30:16 +010064 mBroadcastIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 return intents;
66 }
67
68 @Override
Rafael Higuera Silva7974e192022-02-28 19:16:34 +000069 public AttributionSource getAttributionSource() {
70 AttributionSource attributionSource = super.getAttributionSource();
71 if (attributionSource == null) {
72 return new AttributionSource.Builder(Process.myUid()).build();
73 }
74 return attributionSource;
75 }
76
77 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 public ContentResolver getContentResolver() {
79 // We need to return the real resolver so that MailEngine.makeRight can get to the
80 // subscribed feeds provider. TODO: mock out subscribed feeds too.
81 return mResolver;
82 }
83
84 @Override
85 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
86 return false;
87 }
88
89 @Override
Bo Liu58a57662019-03-06 20:21:45 +000090 public boolean bindService(Intent service, int flags, Executor executor,
91 ServiceConnection conn) {
92 return false;
93 }
94
95 @Override
96 public boolean bindIsolatedService(Intent service, int flags, String instanceName,
97 Executor executor, ServiceConnection conn) {
Dianne Hackborn27b4d942018-11-12 15:01:40 -080098 return false;
99 }
100
101 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
103 return null;
104 }
105
106 @Override
Jonas Schwertfegerd6724752010-09-30 14:04:09 +0200107 public void unregisterReceiver(BroadcastReceiver receiver) {
108 // Ignore
109 }
110
111 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 public void sendBroadcast(Intent intent) {
113 mBroadcastIntents.add(intent);
114 }
115
116 @Override
117 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
118 mBroadcastIntents.add(intent);
119 }
120
121 @Override
122 public int checkUriPermission(
123 Uri uri, String readPermission, String writePermission, int pid,
124 int uid, int modeFlags) {
125 return PackageManager.PERMISSION_GRANTED;
126 }
127
128 @Override
129 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
130 return PackageManager.PERMISSION_GRANTED;
131 }
132
133 @Override
134 public Object getSystemService(String name) {
Fred Quintana0eabf022009-04-27 15:08:17 -0700135 if (Context.ACCOUNT_SERVICE.equals(name)) {
136 return mMockAccountManager;
137 }
138 // No other services exist in this context.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 return null;
140 }
141
Ken Shirriff3b95f532009-07-06 10:45:38 -0700142 @Override
143 public File getFilesDir() {
144 return new File("/dev/null");
145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146}