blob: 6e4c41eef81c4065c80234312789600363951a2d [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.ContextWrapper;
21import android.content.ContentResolver;
22import android.content.Intent;
23import android.content.Context;
24import android.content.ServiceConnection;
25import android.content.BroadcastReceiver;
26import android.content.IntentFilter;
27import 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;
34
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36/**
Stephan Linznerb51617f2016-01-27 18:09:50 -080037 * 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 -080038 * stubbing enough methods to satify code that tries to talk to other packages.
Stephan Linznerb51617f2016-01-27 18:09:50 -080039 *
40 * @deprecated New tests should be written using the
41 * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 */
Stephan Linznerb51617f2016-01-27 18:09:50 -080043@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044public class IsolatedContext extends ContextWrapper {
45
46 private ContentResolver mResolver;
Paul Duffin20af1df2018-01-05 13:52:17 +000047 private final AccountManager mMockAccountManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
Paul Duffin8c5a24d2017-05-10 13:30:16 +010049 private List<Intent> mBroadcastIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
51 public IsolatedContext(
52 ContentResolver resolver, Context targetContext) {
53 super(targetContext);
54 mResolver = resolver;
Paul Duffin20af1df2018-01-05 13:52:17 +000055 mMockAccountManager = MockAccountManager.newMockAccountManager(IsolatedContext.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 }
57
58 /** Returns the list of intents that were broadcast since the last call to this method. */
59 public List<Intent> getAndClearBroadcastIntents() {
60 List<Intent> intents = mBroadcastIntents;
Paul Duffin8c5a24d2017-05-10 13:30:16 +010061 mBroadcastIntents = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 return intents;
63 }
64
65 @Override
66 public ContentResolver getContentResolver() {
67 // We need to return the real resolver so that MailEngine.makeRight can get to the
68 // subscribed feeds provider. TODO: mock out subscribed feeds too.
69 return mResolver;
70 }
71
72 @Override
73 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
74 return false;
75 }
76
77 @Override
78 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
79 return null;
80 }
81
82 @Override
Jonas Schwertfegerd6724752010-09-30 14:04:09 +020083 public void unregisterReceiver(BroadcastReceiver receiver) {
84 // Ignore
85 }
86
87 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 public void sendBroadcast(Intent intent) {
89 mBroadcastIntents.add(intent);
90 }
91
92 @Override
93 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
94 mBroadcastIntents.add(intent);
95 }
96
97 @Override
98 public int checkUriPermission(
99 Uri uri, String readPermission, String writePermission, int pid,
100 int uid, int modeFlags) {
101 return PackageManager.PERMISSION_GRANTED;
102 }
103
104 @Override
105 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
106 return PackageManager.PERMISSION_GRANTED;
107 }
108
109 @Override
110 public Object getSystemService(String name) {
Fred Quintana0eabf022009-04-27 15:08:17 -0700111 if (Context.ACCOUNT_SERVICE.equals(name)) {
112 return mMockAccountManager;
113 }
114 // No other services exist in this context.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 return null;
116 }
117
Ken Shirriff3b95f532009-07-06 10:45:38 -0700118 @Override
119 public File getFilesDir() {
120 return new File("/dev/null");
121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122}