blob: ff85b6d9aa594a62625e70488b6e3c6b460084a3 [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;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
26import android.os.Handler;
27import android.os.PersistableBundle;
28import android.telecom.TelecomManager;
29import android.telephony.CarrierConfigManager;
30import android.telephony.SubscriptionManager;
31import android.telephony.TelephonyManager;
32import android.test.mock.MockContext;
33
34import org.mockito.Mock;
35import org.mockito.MockitoAnnotations;
36
Brad Ebingera68a4972020-01-30 17:31:23 -080037import java.util.concurrent.Executor;
38
Brad Ebingerdc555b42019-12-09 16:12:57 -080039public class TestContext extends MockContext {
40
41 @Mock CarrierConfigManager mMockCarrierConfigManager;
42 @Mock TelecomManager mMockTelecomManager;
43 @Mock TelephonyManager mMockTelephonyManager;
44 @Mock SubscriptionManager mMockSubscriptionManager;
45
46 private PersistableBundle mCarrierConfig = new PersistableBundle();
47
48 public TestContext() {
49 MockitoAnnotations.initMocks(this);
50 doReturn(mCarrierConfig).when(mMockCarrierConfigManager).getConfigForSubId(anyInt());
51 }
52
53 @Override
Brad Ebingera68a4972020-01-30 17:31:23 -080054 public Executor getMainExecutor() {
55 // Just run on current thread
56 return Runnable::run;
57 }
58
59 @Override
Brad Ebingerdc555b42019-12-09 16:12:57 -080060 public Context getApplicationContext() {
61 return this;
62 }
63
64 @Override
65 public String getPackageName() {
66 return "com.android.phone.tests";
67 }
68
69 @Override
70 public String getFeatureId() {
71 return "";
72 }
73
74 @Override
75 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
76 return null;
77 }
78
79 @Override
80 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, int flags) {
81 return null;
82 }
83
84 @Override
85 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
86 String broadcastPermission, Handler scheduler) {
87 return null;
88 }
89
90 @Override
91 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
92 String broadcastPermission, Handler scheduler, int flags) {
93 return null;
94 }
95
96 @Override
97 public Object getSystemService(String name) {
98 switch (name) {
99 case (Context.CARRIER_CONFIG_SERVICE) : {
100 return mMockCarrierConfigManager;
101 }
102 case (Context.TELECOM_SERVICE) : {
103 return mMockTelecomManager;
104 }
105 case (Context.TELEPHONY_SERVICE) : {
106 return mMockTelephonyManager;
107 }
108 case (Context.TELEPHONY_SUBSCRIPTION_SERVICE) : {
109 return mMockSubscriptionManager;
110 }
111 }
112 return null;
113 }
114
115 @Override
116 public String getSystemServiceName(Class<?> serviceClass) {
117 if (serviceClass == CarrierConfigManager.class) {
118 return Context.CARRIER_CONFIG_SERVICE;
119 }
120 if (serviceClass == TelecomManager.class) {
121 return Context.TELECOM_SERVICE;
122 }
123 if (serviceClass == TelephonyManager.class) {
124 return Context.TELEPHONY_SERVICE;
125 }
126 if (serviceClass == SubscriptionManager.class) {
127 return Context.TELEPHONY_SUBSCRIPTION_SERVICE;
128 }
129 return null;
130 }
131
132 public PersistableBundle getCarrierConfig() {
133 return mCarrierConfig;
134 }
135}