blob: 26dff9a5f212bd45494afa09a4220fd04beef605 [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;
Brad Ebinger9c5578a2020-09-23 17:03:48 -070020import static org.mockito.Mockito.doAnswer;
Brad Ebingerdc555b42019-12-09 16:12:57 -080021
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;
Hall Liu12feea22021-03-22 15:37:41 -070028import android.os.Looper;
Brad Ebingerdc555b42019-12-09 16:12:57 -080029import android.os.PersistableBundle;
30import android.telecom.TelecomManager;
31import android.telephony.CarrierConfigManager;
32import android.telephony.SubscriptionManager;
33import android.telephony.TelephonyManager;
James.cf Lincdad3862020-02-25 15:55:03 +080034import android.telephony.ims.ImsManager;
Brad Ebingerdc555b42019-12-09 16:12:57 -080035import android.test.mock.MockContext;
Brad Ebinger9c5578a2020-09-23 17:03:48 -070036import android.util.SparseArray;
Brad Ebingerdc555b42019-12-09 16:12:57 -080037
38import org.mockito.Mock;
39import org.mockito.MockitoAnnotations;
Brad Ebinger9c5578a2020-09-23 17:03:48 -070040import org.mockito.stubbing.Answer;
Brad Ebingerdc555b42019-12-09 16:12:57 -080041
Brad Ebingera68a4972020-01-30 17:31:23 -080042import java.util.concurrent.Executor;
43
Brad Ebingerdc555b42019-12-09 16:12:57 -080044public class TestContext extends MockContext {
45
46 @Mock CarrierConfigManager mMockCarrierConfigManager;
47 @Mock TelecomManager mMockTelecomManager;
48 @Mock TelephonyManager mMockTelephonyManager;
49 @Mock SubscriptionManager mMockSubscriptionManager;
James.cf Lincdad3862020-02-25 15:55:03 +080050 @Mock ImsManager mMockImsManager;
Brad Ebingerdc555b42019-12-09 16:12:57 -080051
Brad Ebinger9c5578a2020-09-23 17:03:48 -070052 private SparseArray<PersistableBundle> mCarrierConfigs = new SparseArray<>();
Brad Ebingerdc555b42019-12-09 16:12:57 -080053
54 public TestContext() {
55 MockitoAnnotations.initMocks(this);
Brad Ebinger9c5578a2020-09-23 17:03:48 -070056 doAnswer((Answer<PersistableBundle>) invocation -> {
57 int subId = (int) invocation.getArguments()[0];
58 if (subId < 0) {
59 return new PersistableBundle();
60 }
61 PersistableBundle b = mCarrierConfigs.get(subId);
62
63 return (b != null ? b : new PersistableBundle());
64 }).when(mMockCarrierConfigManager).getConfigForSubId(anyInt());
Brad Ebingerdc555b42019-12-09 16:12:57 -080065 }
66
67 @Override
Brad Ebingera68a4972020-01-30 17:31:23 -080068 public Executor getMainExecutor() {
69 // Just run on current thread
70 return Runnable::run;
71 }
72
73 @Override
Brad Ebingerdc555b42019-12-09 16:12:57 -080074 public Context getApplicationContext() {
75 return this;
76 }
77
78 @Override
79 public String getPackageName() {
80 return "com.android.phone.tests";
81 }
82
83 @Override
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -080084 public String getAttributionTag() {
Brad Ebingerdc555b42019-12-09 16:12:57 -080085 return "";
86 }
87
88 @Override
89 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
90 return null;
91 }
92
93 @Override
94 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, int flags) {
95 return null;
96 }
97
98 @Override
99 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
100 String broadcastPermission, Handler scheduler) {
101 return null;
102 }
103
104 @Override
105 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
106 String broadcastPermission, Handler scheduler, int flags) {
107 return null;
108 }
109
110 @Override
James.cf Lincdad3862020-02-25 15:55:03 +0800111 public ContentResolver getContentResolver() {
112 return null;
113 }
114
115 @Override
Brad Ebingerdc555b42019-12-09 16:12:57 -0800116 public Object getSystemService(String name) {
117 switch (name) {
118 case (Context.CARRIER_CONFIG_SERVICE) : {
119 return mMockCarrierConfigManager;
120 }
121 case (Context.TELECOM_SERVICE) : {
122 return mMockTelecomManager;
123 }
124 case (Context.TELEPHONY_SERVICE) : {
125 return mMockTelephonyManager;
126 }
127 case (Context.TELEPHONY_SUBSCRIPTION_SERVICE) : {
128 return mMockSubscriptionManager;
129 }
James.cf Lincdad3862020-02-25 15:55:03 +0800130 case(Context.TELEPHONY_IMS_SERVICE) : {
131 return mMockImsManager;
132 }
Brad Ebingerdc555b42019-12-09 16:12:57 -0800133 }
134 return null;
135 }
136
137 @Override
138 public String getSystemServiceName(Class<?> serviceClass) {
139 if (serviceClass == CarrierConfigManager.class) {
140 return Context.CARRIER_CONFIG_SERVICE;
141 }
142 if (serviceClass == TelecomManager.class) {
143 return Context.TELECOM_SERVICE;
144 }
145 if (serviceClass == TelephonyManager.class) {
146 return Context.TELEPHONY_SERVICE;
147 }
148 if (serviceClass == SubscriptionManager.class) {
149 return Context.TELEPHONY_SUBSCRIPTION_SERVICE;
150 }
151 return null;
152 }
153
Hall Liu12feea22021-03-22 15:37:41 -0700154 @Override
155 public Handler getMainThreadHandler() {
156 return new Handler(Looper.getMainLooper());
157 }
158
Brad Ebinger9c5578a2020-09-23 17:03:48 -0700159 /**
160 * @return CarrierConfig PersistableBundle for the subscription specified.
161 */
162 public PersistableBundle getCarrierConfig(int subId) {
163 PersistableBundle b = mCarrierConfigs.get(subId);
164 if (b == null) {
165 b = new PersistableBundle();
166 mCarrierConfigs.put(subId, b);
167 }
168 return b;
Brad Ebingerdc555b42019-12-09 16:12:57 -0800169 }
170}