blob: c190be960f3819679eeeb5e26acd89ce29b70074 [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;
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 Ebinger792729e2019-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 Ebinger792729e2019-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 Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
71 return null;
72 }
73
74 @Override
75 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, int flags) {
76 return null;
77 }
78
79 @Override
80 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
81 String broadcastPermission, Handler scheduler) {
82 return null;
83 }
84
85 @Override
86 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
87 String broadcastPermission, Handler scheduler, int flags) {
88 return null;
89 }
90
91 @Override
92 public Object getSystemService(String name) {
93 switch (name) {
94 case (Context.CARRIER_CONFIG_SERVICE) : {
95 return mMockCarrierConfigManager;
96 }
97 case (Context.TELECOM_SERVICE) : {
98 return mMockTelecomManager;
99 }
100 case (Context.TELEPHONY_SERVICE) : {
101 return mMockTelephonyManager;
102 }
103 case (Context.TELEPHONY_SUBSCRIPTION_SERVICE) : {
104 return mMockSubscriptionManager;
105 }
106 }
107 return null;
108 }
109
110 @Override
111 public String getSystemServiceName(Class<?> serviceClass) {
112 if (serviceClass == CarrierConfigManager.class) {
113 return Context.CARRIER_CONFIG_SERVICE;
114 }
115 if (serviceClass == TelecomManager.class) {
116 return Context.TELECOM_SERVICE;
117 }
118 if (serviceClass == TelephonyManager.class) {
119 return Context.TELEPHONY_SERVICE;
120 }
121 if (serviceClass == SubscriptionManager.class) {
122 return Context.TELEPHONY_SUBSCRIPTION_SERVICE;
123 }
124 return null;
125 }
126
127 public PersistableBundle getCarrierConfig() {
128 return mCarrierConfig;
129 }
130}