1 /*
2  * Copyright (C) 2021 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 
17 package com.android.car.admin.ui;
18 
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.spy;
21 
22 import android.app.Activity;
23 import android.app.admin.DevicePolicyManager;
24 import android.content.Context;
25 import android.content.pm.PackageManager;
26 import android.os.Bundle;
27 
28 import androidx.annotation.NonNull;
29 import androidx.annotation.Nullable;
30 
31 import com.android.car.carservice_unittest.R;
32 
33 
34 /**
35  * A helper activity to facilitate testing of {@link UserAvatarView} and {@link
36  * ManagedDeviceTextView}.
37  */
38 public final class CarAdminUiTestActivity extends Activity {
39 
40     public UserAvatarView mUserAvatarView;
41     public PackageManager mSpyPackageManager;
42     public DevicePolicyManager mMockDevicePolicyManager;
43 
44     @Override
onCreate(@ullable Bundle savedInstanceState)45     protected void onCreate(@Nullable Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47         mSpyPackageManager = spy(getPackageManager());
48         mMockDevicePolicyManager = mock(DevicePolicyManager.class);
49         setContentView(R.layout.car_admin_ui_test_activity);
50         mUserAvatarView = findViewById(R.id.view_user_avatar);
51     }
52 
53     @Override
getPackageManager()54     public PackageManager getPackageManager() {
55         if (mSpyPackageManager != null) {
56             return mSpyPackageManager;
57         }
58         return super.getPackageManager();
59     }
60 
61     @Override
getSystemService(@onNull String name)62     public Object getSystemService(@NonNull String name) {
63         if (Context.DEVICE_POLICY_SERVICE.equals(name)) {
64             if (mMockDevicePolicyManager != null) {
65                 return mMockDevicePolicyManager;
66             }
67         }
68         return super.getSystemService(name);
69     }
70 }
71