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.fcm;
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 import android.os.PersistableBundle;
25 import android.telephony.CarrierConfigManager;
26 import android.telephony.SubscriptionInfo;
27 import android.telephony.SubscriptionManager;
28 
29 import androidx.test.core.app.ApplicationProvider;
30 import androidx.test.runner.AndroidJUnit4;
31 
32 import com.android.imsserviceentitlement.job.JobManager;
33 import com.android.libraries.entitlement.ServiceEntitlement;
34 
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.Spy;
41 import org.mockito.junit.MockitoJUnit;
42 import org.mockito.junit.MockitoRule;
43 
44 import java.util.ArrayList;
45 import java.util.List;
46 import java.util.Map;
47 
48 @RunWith(AndroidJUnit4.class)
49 public class FcmServiceTest {
50     @Rule public final MockitoRule rule = MockitoJUnit.rule();
51 
52     @Spy private Context mContext = ApplicationProvider.getApplicationContext();
53 
54     @Mock private SubscriptionManager mSubscriptionManager;
55     @Mock private SubscriptionInfo mSubscriptionInfo;
56     @Mock private CarrierConfigManager mCarrierConfigManager;
57     @Mock private JobManager mJobManager;
58 
59     private FcmService mService;
60 
61     private static final String DATA_APP_KEY = "app";
62     private static final String ERROR_DATA_APP_KEY = "error_app";
63     private static final String DATA_TIMESTAMP_KEY = "timestamp";
64     private static final String TIME_STAMP = "2019-01-29T13:15:31-08:00";
65     private static final String SENDER_ID = "SENDER_ID";
66     private static final int SUB_ID = 1;
67 
68     @Before
setup()69     public void setup() throws Exception {
70         setActiveSubscriptionInfoList();
71         setFcmSenderIdString(SENDER_ID);
72         mService = new FcmService();
73         mService.attachBaseContext(mContext);
74         mService.onCreate();
75         mService.setMockJobManager(mJobManager);
76     }
77 
78     @Test
onMessageReceived_isFcmSupported_queryEntitlementStatusOnceNetworkReady()79     public void onMessageReceived_isFcmSupported_queryEntitlementStatusOnceNetworkReady() {
80         Map<String, String> dataMap = setFcmData(DATA_APP_KEY, ServiceEntitlement.APP_VOWIFI);
81 
82         mService.onMessageReceived(SENDER_ID, dataMap);
83 
84         verify(mJobManager).queryEntitlementStatusOnceNetworkReady();
85     }
86 
87     @Test
onMessageReceived_isNotTs43EntitlementsChangeEvent_noJobs()88     public void onMessageReceived_isNotTs43EntitlementsChangeEvent_noJobs() {
89         Map<String, String> dataMap = setFcmData(ERROR_DATA_APP_KEY, ServiceEntitlement.APP_VOWIFI);
90 
91         mService.onMessageReceived(SENDER_ID, dataMap);
92 
93         verify(mJobManager, never()).queryEntitlementStatusOnceNetworkReady();
94     }
95 
96     @Test
onMessageReceived_emptySenderId_isNotFcmSupported()97     public void onMessageReceived_emptySenderId_isNotFcmSupported() {
98         setFcmSenderIdString("");
99         Map<String, String> dataMap = setFcmData(DATA_APP_KEY, ServiceEntitlement.APP_VOWIFI);
100 
101         mService.onMessageReceived(SENDER_ID, dataMap);
102 
103         verify(mJobManager, never()).queryEntitlementStatusOnceNetworkReady();
104     }
105 
setFcmData(String dataAppKey, String dataAppValue)106     private Map<String, String> setFcmData(String dataAppKey, String dataAppValue) {
107         Map<String, String> dataMap = Map.of(
108                 dataAppKey, dataAppValue,
109                 DATA_TIMESTAMP_KEY, TIME_STAMP);
110         return dataMap;
111     }
112 
setActiveSubscriptionInfoList()113     private void setActiveSubscriptionInfoList() {
114         when(mSubscriptionInfo.getSubscriptionId()).thenReturn(SUB_ID);
115         List<SubscriptionInfo> mSubscriptionInfoList = new ArrayList<>();
116         mSubscriptionInfoList.add(mSubscriptionInfo);
117         when(mSubscriptionManager.getActiveSubscriptionInfoList())
118                 .thenReturn(mSubscriptionInfoList);
119         when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
120     }
121 
setFcmSenderIdString(String senderId)122     private void setFcmSenderIdString(String senderId) {
123         PersistableBundle carrierConfig = new PersistableBundle();
124         carrierConfig.putString(
125                 CarrierConfigManager.ImsServiceEntitlement.KEY_FCM_SENDER_ID_STRING,
126                 senderId
127         );
128         when(mCarrierConfigManager.getConfigForSubId(SUB_ID)).thenReturn(carrierConfig);
129         when(mContext.getSystemService(CarrierConfigManager.class))
130                 .thenReturn(mCarrierConfigManager);
131     }
132 }
133