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.imsserviceentitlement;
18 
19 import static org.mockito.Mockito.never;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
22 
23 import android.content.Context;
24 
25 import androidx.test.runner.AndroidJUnit4;
26 
27 import com.android.imsserviceentitlement.WfcActivationController.EntitlementResultCallback;
28 import com.android.imsserviceentitlement.entitlement.EntitlementConfiguration;
29 import com.android.imsserviceentitlement.entitlement.EntitlementResult;
30 import com.android.imsserviceentitlement.utils.Executors;
31 import com.android.libraries.entitlement.ServiceEntitlement;
32 
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.junit.MockitoJUnit;
39 import org.mockito.junit.MockitoRule;
40 
41 import java.lang.reflect.Field;
42 import java.util.concurrent.CountDownLatch;
43 
44 @RunWith(AndroidJUnit4.class)
45 public class EntitlementUtilsTest {
46     @Rule public final MockitoRule rule = MockitoJUnit.rule();
47     @Mock private ImsEntitlementApi mMockImsEntitlementApi;
48     @Mock private EntitlementResultCallback mEntitlementResultCallback;
49     @Mock private EntitlementResult mEntitlementResult;
50     @Mock private Context mContext;
51     @Mock private ServiceEntitlement mServiceEntitlement;
52     @Mock private EntitlementConfiguration mEntitlementConfiguration;
53 
54     @Before
setUp()55     public void setUp() throws Exception {
56         useDirectExecutor(true);
57     }
58 
59     @Test
entitlementCheck_checkEntitlementStatusPass_onEntitlementResult()60     public void entitlementCheck_checkEntitlementStatusPass_onEntitlementResult() {
61         when(mMockImsEntitlementApi.checkEntitlementStatus()).thenReturn(mEntitlementResult);
62 
63         EntitlementUtils.entitlementCheck(mMockImsEntitlementApi, mEntitlementResultCallback);
64 
65         verify(mEntitlementResultCallback).onEntitlementResult(mEntitlementResult);
66     }
67 
68     @Test
entitlementCheck_checkEntitlementStatusWithRuntimeException_onFailure()69     public void entitlementCheck_checkEntitlementStatusWithRuntimeException_onFailure() {
70         when(mMockImsEntitlementApi.checkEntitlementStatus()).thenThrow(new RuntimeException());
71 
72         EntitlementUtils.entitlementCheck(mMockImsEntitlementApi, mEntitlementResultCallback);
73 
74         verify(mEntitlementResultCallback, never()).onEntitlementResult(mEntitlementResult);
75     }
76 
77     @Test
entitlementCheck_cancelEntitlementCheck_onFailure()78     public void entitlementCheck_cancelEntitlementCheck_onFailure() throws Exception {
79         useDirectExecutor(false);
80         CountDownLatch entitlementCheckLatch = new CountDownLatch(1);
81         ImsEntitlementApi mockImsEntitlementApi =
82                 new ImsEntitlementApi(
83                         mContext, 1, true, mServiceEntitlement, mEntitlementConfiguration) {
84                     @Override
85                     public EntitlementResult checkEntitlementStatus() {
86                         try {
87                             entitlementCheckLatch.await();
88                         } catch (InterruptedException e) {
89                         }
90                         return mEntitlementResult;
91                     }
92                 };
93 
94         EntitlementUtils.entitlementCheck(mockImsEntitlementApi, mEntitlementResultCallback);
95         EntitlementUtils.cancelEntitlementCheck();
96         entitlementCheckLatch.countDown();
97 
98         verify(mEntitlementResultCallback, never()).onEntitlementResult(mEntitlementResult);
99     }
100 
useDirectExecutor(boolean enable)101     private void useDirectExecutor(boolean enable) throws Exception {
102         Field field = Executors.class.getDeclaredField("sUseDirectExecutorForTest");
103         field.setAccessible(true);
104         field.set(null, enable);
105     }
106 }
107