blob: b1c69230eaa40571e396cded144ca500b063f2d5 [file] [log] [blame]
Brad Ebinger792729e2019-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 Ebinger792729e2019-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 Ebinger792729e2019-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 Ebinger792729e2019-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 Ebinger792729e2019-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 Ebinger792729e2019-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
73 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
74 return null;
75 }
76
77 @Override
78 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, int flags) {
79 return null;
80 }
81
82 @Override
83 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
84 String broadcastPermission, Handler scheduler) {
85 return null;
86 }
87
88 @Override
89 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
90 String broadcastPermission, Handler scheduler, int flags) {
91 return null;
92 }
93
94 @Override
James.cf Lincdad3862020-02-25 15:55:03 +080095 public ContentResolver getContentResolver() {
96 return null;
97 }
98
99 @Override
Brad Ebinger792729e2019-12-09 16:12:57 -0800100 public Object getSystemService(String name) {
101 switch (name) {
102 case (Context.CARRIER_CONFIG_SERVICE) : {
103 return mMockCarrierConfigManager;
104 }
105 case (Context.TELECOM_SERVICE) : {
106 return mMockTelecomManager;
107 }
108 case (Context.TELEPHONY_SERVICE) : {
109 return mMockTelephonyManager;
110 }
111 case (Context.TELEPHONY_SUBSCRIPTION_SERVICE) : {
112 return mMockSubscriptionManager;
113 }
James.cf Lincdad3862020-02-25 15:55:03 +0800114 case(Context.TELEPHONY_IMS_SERVICE) : {
115 return mMockImsManager;
116 }
Brad Ebinger792729e2019-12-09 16:12:57 -0800117 }
118 return null;
119 }
120
121 @Override
122 public String getSystemServiceName(Class<?> serviceClass) {
123 if (serviceClass == CarrierConfigManager.class) {
124 return Context.CARRIER_CONFIG_SERVICE;
125 }
126 if (serviceClass == TelecomManager.class) {
127 return Context.TELECOM_SERVICE;
128 }
129 if (serviceClass == TelephonyManager.class) {
130 return Context.TELEPHONY_SERVICE;
131 }
132 if (serviceClass == SubscriptionManager.class) {
133 return Context.TELEPHONY_SUBSCRIPTION_SERVICE;
134 }
135 return null;
136 }
137
138 public PersistableBundle getCarrierConfig() {
139 return mCarrierConfig;
140 }
141}