blob: 9d712d319566d119529dbe4e07238b7ec5eb3c31 [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;
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;
Brad Ebinger9c5578a2020-09-23 17:03:48 -070035import android.util.SparseArray;
Brad Ebingerdc555b42019-12-09 16:12:57 -080036
37import org.mockito.Mock;
38import org.mockito.MockitoAnnotations;
Brad Ebinger9c5578a2020-09-23 17:03:48 -070039import org.mockito.stubbing.Answer;
Brad Ebingerdc555b42019-12-09 16:12:57 -080040
Brad Ebingera68a4972020-01-30 17:31:23 -080041import java.util.concurrent.Executor;
42
Brad Ebingerdc555b42019-12-09 16:12:57 -080043public class TestContext extends MockContext {
44
45 @Mock CarrierConfigManager mMockCarrierConfigManager;
46 @Mock TelecomManager mMockTelecomManager;
47 @Mock TelephonyManager mMockTelephonyManager;
48 @Mock SubscriptionManager mMockSubscriptionManager;
James.cf Lincdad3862020-02-25 15:55:03 +080049 @Mock ImsManager mMockImsManager;
Brad Ebingerdc555b42019-12-09 16:12:57 -080050
Brad Ebinger9c5578a2020-09-23 17:03:48 -070051 private SparseArray<PersistableBundle> mCarrierConfigs = new SparseArray<>();
Brad Ebingerdc555b42019-12-09 16:12:57 -080052
53 public TestContext() {
54 MockitoAnnotations.initMocks(this);
Brad Ebinger9c5578a2020-09-23 17:03:48 -070055 doAnswer((Answer<PersistableBundle>) invocation -> {
56 int subId = (int) invocation.getArguments()[0];
57 if (subId < 0) {
58 return new PersistableBundle();
59 }
60 PersistableBundle b = mCarrierConfigs.get(subId);
61
62 return (b != null ? b : new PersistableBundle());
63 }).when(mMockCarrierConfigManager).getConfigForSubId(anyInt());
Brad Ebingerdc555b42019-12-09 16:12:57 -080064 }
65
66 @Override
Brad Ebingera68a4972020-01-30 17:31:23 -080067 public Executor getMainExecutor() {
68 // Just run on current thread
69 return Runnable::run;
70 }
71
72 @Override
Brad Ebingerdc555b42019-12-09 16:12:57 -080073 public Context getApplicationContext() {
74 return this;
75 }
76
77 @Override
78 public String getPackageName() {
79 return "com.android.phone.tests";
80 }
81
82 @Override
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -080083 public String getAttributionTag() {
Brad Ebingerdc555b42019-12-09 16:12:57 -080084 return "";
85 }
86
87 @Override
88 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
89 return null;
90 }
91
92 @Override
93 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, int flags) {
94 return null;
95 }
96
97 @Override
98 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
99 String broadcastPermission, Handler scheduler) {
100 return null;
101 }
102
103 @Override
104 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
105 String broadcastPermission, Handler scheduler, int flags) {
106 return null;
107 }
108
109 @Override
James.cf Lincdad3862020-02-25 15:55:03 +0800110 public ContentResolver getContentResolver() {
111 return null;
112 }
113
114 @Override
Brad Ebingerdc555b42019-12-09 16:12:57 -0800115 public Object getSystemService(String name) {
116 switch (name) {
117 case (Context.CARRIER_CONFIG_SERVICE) : {
118 return mMockCarrierConfigManager;
119 }
120 case (Context.TELECOM_SERVICE) : {
121 return mMockTelecomManager;
122 }
123 case (Context.TELEPHONY_SERVICE) : {
124 return mMockTelephonyManager;
125 }
126 case (Context.TELEPHONY_SUBSCRIPTION_SERVICE) : {
127 return mMockSubscriptionManager;
128 }
James.cf Lincdad3862020-02-25 15:55:03 +0800129 case(Context.TELEPHONY_IMS_SERVICE) : {
130 return mMockImsManager;
131 }
Brad Ebingerdc555b42019-12-09 16:12:57 -0800132 }
133 return null;
134 }
135
136 @Override
137 public String getSystemServiceName(Class<?> serviceClass) {
138 if (serviceClass == CarrierConfigManager.class) {
139 return Context.CARRIER_CONFIG_SERVICE;
140 }
141 if (serviceClass == TelecomManager.class) {
142 return Context.TELECOM_SERVICE;
143 }
144 if (serviceClass == TelephonyManager.class) {
145 return Context.TELEPHONY_SERVICE;
146 }
147 if (serviceClass == SubscriptionManager.class) {
148 return Context.TELEPHONY_SUBSCRIPTION_SERVICE;
149 }
150 return null;
151 }
152
Brad Ebinger9c5578a2020-09-23 17:03:48 -0700153 /**
154 * @return CarrierConfig PersistableBundle for the subscription specified.
155 */
156 public PersistableBundle getCarrierConfig(int subId) {
157 PersistableBundle b = mCarrierConfigs.get(subId);
158 if (b == null) {
159 b = new PersistableBundle();
160 mCarrierConfigs.put(subId, b);
161 }
162 return b;
Brad Ebingerdc555b42019-12-09 16:12:57 -0800163 }
164}