blob: 13bfe3b078e25ee2dd6a5e399099505d78d5a006 [file] [log] [blame]
Brad Ebingerdc555b42019-12-09 16:12:57 -08001/*
2 * Copyright (C) 2019 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
17package com.android;
18
19import static org.mockito.ArgumentMatchers.anyInt;
20import static org.mockito.Mockito.doReturn;
21
22import android.content.BroadcastReceiver;
James.cf Lincdad3862020-02-25 15:55:03 +080023import android.content.ContentResolver;
Brad Ebingerdc555b42019-12-09 16:12:57 -080024import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
27import android.os.Handler;
28import android.os.PersistableBundle;
29import android.telecom.TelecomManager;
30import android.telephony.CarrierConfigManager;
31import android.telephony.SubscriptionManager;
32import android.telephony.TelephonyManager;
James.cf Lincdad3862020-02-25 15:55:03 +080033import android.telephony.ims.ImsManager;
Brad Ebingerdc555b42019-12-09 16:12:57 -080034import android.test.mock.MockContext;
35
36import org.mockito.Mock;
37import org.mockito.MockitoAnnotations;
38
Brad Ebingera68a4972020-01-30 17:31:23 -080039import java.util.concurrent.Executor;
40
Brad Ebingerdc555b42019-12-09 16:12:57 -080041public class TestContext extends MockContext {
42
43 @Mock CarrierConfigManager mMockCarrierConfigManager;
44 @Mock TelecomManager mMockTelecomManager;
45 @Mock TelephonyManager mMockTelephonyManager;
46 @Mock SubscriptionManager mMockSubscriptionManager;
James.cf Lincdad3862020-02-25 15:55:03 +080047 @Mock ImsManager mMockImsManager;
Brad Ebingerdc555b42019-12-09 16:12:57 -080048
49 private PersistableBundle mCarrierConfig = new PersistableBundle();
50
51 public TestContext() {
52 MockitoAnnotations.initMocks(this);
53 doReturn(mCarrierConfig).when(mMockCarrierConfigManager).getConfigForSubId(anyInt());
54 }
55
56 @Override
Brad Ebingera68a4972020-01-30 17:31:23 -080057 public Executor getMainExecutor() {
58 // Just run on current thread
59 return Runnable::run;
60 }
61
62 @Override
Brad Ebingerdc555b42019-12-09 16:12:57 -080063 public Context getApplicationContext() {
64 return this;
65 }
66
67 @Override
68 public String getPackageName() {
69 return "com.android.phone.tests";
70 }
71
72 @Override
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -080073 public String getAttributionTag() {
Brad Ebingerdc555b42019-12-09 16:12:57 -080074 return "";
75 }
76
77 @Override
78 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
79 return null;
80 }
81
82 @Override
83 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, int flags) {
84 return null;
85 }
86
87 @Override
88 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
89 String broadcastPermission, Handler scheduler) {
90 return null;
91 }
92
93 @Override
94 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
95 String broadcastPermission, Handler scheduler, int flags) {
96 return null;
97 }
98
99 @Override
James.cf Lincdad3862020-02-25 15:55:03 +0800100 public ContentResolver getContentResolver() {
101 return null;
102 }
103
104 @Override
Brad Ebingerdc555b42019-12-09 16:12:57 -0800105 public Object getSystemService(String name) {
106 switch (name) {
107 case (Context.CARRIER_CONFIG_SERVICE) : {
108 return mMockCarrierConfigManager;
109 }
110 case (Context.TELECOM_SERVICE) : {
111 return mMockTelecomManager;
112 }
113 case (Context.TELEPHONY_SERVICE) : {
114 return mMockTelephonyManager;
115 }
116 case (Context.TELEPHONY_SUBSCRIPTION_SERVICE) : {
117 return mMockSubscriptionManager;
118 }
James.cf Lincdad3862020-02-25 15:55:03 +0800119 case(Context.TELEPHONY_IMS_SERVICE) : {
120 return mMockImsManager;
121 }
Brad Ebingerdc555b42019-12-09 16:12:57 -0800122 }
123 return null;
124 }
125
126 @Override
127 public String getSystemServiceName(Class<?> serviceClass) {
128 if (serviceClass == CarrierConfigManager.class) {
129 return Context.CARRIER_CONFIG_SERVICE;
130 }
131 if (serviceClass == TelecomManager.class) {
132 return Context.TELECOM_SERVICE;
133 }
134 if (serviceClass == TelephonyManager.class) {
135 return Context.TELEPHONY_SERVICE;
136 }
137 if (serviceClass == SubscriptionManager.class) {
138 return Context.TELEPHONY_SUBSCRIPTION_SERVICE;
139 }
140 return null;
141 }
142
143 public PersistableBundle getCarrierConfig() {
144 return mCarrierConfig;
145 }
146}