• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.server.devicelock;
18 
19 import static android.devicelock.DeviceId.DEVICE_ID_TYPE_IMEI;
20 import static android.devicelock.DeviceId.DEVICE_ID_TYPE_MEID;
21 
22 import static com.android.dx.mockito.inline.extended.ExtendedMockito.anyString;
23 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing;
24 import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
25 import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;
26 
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.spy;
30 import static org.mockito.Mockito.timeout;
31 
32 import android.content.Context;
33 import android.content.pm.PackageManager;
34 import android.devicelock.IGetDeviceIdCallback;
35 import android.telephony.TelephonyManager;
36 
37 import androidx.test.platform.app.InstrumentationRegistry;
38 
39 import com.android.dx.mockito.inline.extended.ExtendedMockito;
40 
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.mockito.Mock;
45 import org.mockito.MockitoSession;
46 import org.mockito.quality.Strictness;
47 
48 /**
49  * Tests for {@link com.android.server.devicelock.DeviceLockService}.
50  */
51 public class DeviceLockUnitTest {
52 
53     private Context mMockContext;
54 
55     @Mock
56     private PackageManager mPackageManager;
57 
58     @Mock
59     private TelephonyManager mTelephonyManager;
60 
61     private MockitoSession mSession;
62 
63     private DeviceLockServiceImpl mService;
64 
65     private static final long ONE_SEC_MILLIS = 1000;
66 
67     @Before
setup()68     public void setup() {
69         mSession = ExtendedMockito.mockitoSession().initMocks(this)
70                 .strictness(Strictness.LENIENT)
71                 .startMocking();
72         mMockContext = spy(InstrumentationRegistry.getInstrumentation().getContext());
73 
74         doNothing().when(mMockContext).enforceCallingPermission(anyString(), anyString());
75 
76         when(mMockContext.getPackageManager()).thenReturn(mPackageManager);
77         when(mMockContext.getSystemService(TelephonyManager.class))
78                 .thenReturn(mTelephonyManager);
79     }
80 
81     @After
teardown()82     public void teardown() {
83         mSession.finishMocking();
84     }
85 
86     /**
87      * Test IMEI for {@link com.android.server.devicelock.DeviceLockServiceImpl#getDeviceId}
88      */
89     @Test
getDeviceIdShouldReturnIMEI()90     public void getDeviceIdShouldReturnIMEI() {
91         final String testImei = "983402979622353";
92 
93         mService = new DeviceLockServiceImpl(mMockContext);
94 
95         when(mTelephonyManager.getImei()).thenReturn(testImei);
96 
97         IGetDeviceIdCallback mockCallback = mock(IGetDeviceIdCallback.class);
98 
99         mService.getDeviceId(mockCallback, 1 << DEVICE_ID_TYPE_IMEI);
100 
101         try {
102             verify(mockCallback, timeout(ONE_SEC_MILLIS))
103                     .onDeviceIdReceived(eq(DEVICE_ID_TYPE_IMEI), eq(testImei));
104         } catch (Exception ex) {
105             // Should not happen.
106         }
107     }
108 
109     /**
110      * Test MEID for {@link com.android.server.devicelock.DeviceLockServiceImpl#getDeviceId}
111      */
112     @Test
getDeviceIdShouldReturnMEID()113     public void getDeviceIdShouldReturnMEID() {
114         final String testMeid = "354403064522046";
115 
116         mService = new DeviceLockServiceImpl(mMockContext);
117 
118         when(mTelephonyManager.getMeid()).thenReturn(testMeid);
119 
120         IGetDeviceIdCallback mockCallback = mock(IGetDeviceIdCallback.class);
121 
122         mService.getDeviceId(mockCallback, 1 << DEVICE_ID_TYPE_MEID);
123 
124         try {
125             verify(mockCallback, timeout(1000))
126                     .onDeviceIdReceived(eq(DEVICE_ID_TYPE_MEID), eq(testMeid));
127         } catch (Exception ex) {
128             // Should not happen.
129         }
130     }
131 }
132