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 com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.when; 22 23 import android.app.job.JobParameters; 24 import android.app.job.JobScheduler; 25 import android.content.Context; 26 import android.os.PersistableBundle; 27 import android.telephony.CarrierConfigManager; 28 import android.telephony.SubscriptionInfo; 29 import android.telephony.SubscriptionManager; 30 31 import androidx.test.core.app.ApplicationProvider; 32 import androidx.test.runner.AndroidJUnit4; 33 34 import com.android.imsserviceentitlement.job.JobManager; 35 36 import com.google.firebase.iid.FirebaseInstanceId; 37 import com.google.firebase.messaging.FirebaseMessaging; 38 39 import org.junit.Before; 40 import org.junit.Rule; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Mock; 44 import org.mockito.Spy; 45 import org.mockito.junit.MockitoJUnit; 46 import org.mockito.junit.MockitoRule; 47 48 import java.util.ArrayList; 49 import java.util.List; 50 51 @RunWith(AndroidJUnit4.class) 52 public class FcmRegistrationServiceTest { 53 @Rule public final MockitoRule rule = MockitoJUnit.rule(); 54 55 @Spy private Context mContext = ApplicationProvider.getApplicationContext(); 56 57 @Mock private JobParameters mJobParameters; 58 @Mock private FirebaseInstanceId mInstanceID; 59 @Mock private SubscriptionManager mSubscriptionManager; 60 @Mock private SubscriptionInfo mSubscriptionInfo; 61 @Mock private CarrierConfigManager mCarrierConfigManager; 62 63 private FcmRegistrationService mService; 64 private JobScheduler mScheduler; 65 66 private static final int SUB_ID = 1; 67 private static final String TOKEN = "TEST_TOKEN"; 68 private static final String SENDER_ID = "SENDER_ID"; 69 70 @Before setup()71 public void setup() throws Exception { 72 setActiveSubscriptionInfoList(); 73 setFcmSenderIdString(SENDER_ID); 74 mService = new FcmRegistrationService(); 75 mService.attachBaseContext(mContext); 76 mService.onCreate(); 77 mService.onBind(null); 78 mScheduler = mContext.getSystemService(JobScheduler.class); 79 FcmTokenStore.setToken(mContext, SUB_ID, ""); 80 } 81 82 @Test enqueueJob_getPendingJob_registerFcmOnceNetworkReady()83 public void enqueueJob_getPendingJob_registerFcmOnceNetworkReady() { 84 mService.enqueueJob(mContext); 85 86 assertThat(mScheduler.getPendingJob(JobManager.REGISTER_FCM_JOB_ID)).isNotNull(); 87 } 88 89 @Test onStartJob_setToken_getToken()90 public void onStartJob_setToken_getToken() throws Exception { 91 when(mInstanceID.getToken(SENDER_ID, FirebaseMessaging.INSTANCE_ID_SCOPE)) 92 .thenReturn(TOKEN); 93 mService.setFakeInstanceID(mInstanceID); 94 95 mService.onStartJob(mJobParameters); 96 mService.mOngoingTask.get(); // wait for job finish. 97 98 assertThat(FcmTokenStore.getToken(mContext, SUB_ID)).isEqualTo(TOKEN); 99 } 100 101 @Test onStopJob_alwaysRetunedTrue()102 public void onStopJob_alwaysRetunedTrue() { 103 assertThat(mService.onStopJob(mJobParameters)).isTrue(); 104 } 105 setActiveSubscriptionInfoList()106 private void setActiveSubscriptionInfoList() { 107 when(mSubscriptionInfo.getSimSlotIndex()).thenReturn(0); 108 when(mSubscriptionManager.getActiveSubscriptionInfo(SUB_ID)).thenReturn(mSubscriptionInfo); 109 when(mSubscriptionInfo.getSubscriptionId()).thenReturn(SUB_ID); 110 List<SubscriptionInfo> mSubscriptionInfoList = new ArrayList<>(); 111 mSubscriptionInfoList.add(mSubscriptionInfo); 112 when(mSubscriptionManager.getActiveSubscriptionInfoList()) 113 .thenReturn(mSubscriptionInfoList); 114 when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager); 115 } 116 setFcmSenderIdString(String senderId)117 private void setFcmSenderIdString(String senderId) { 118 PersistableBundle carrierConfig = new PersistableBundle(); 119 carrierConfig.putString( 120 CarrierConfigManager.ImsServiceEntitlement.KEY_FCM_SENDER_ID_STRING, 121 senderId 122 ); 123 when(mCarrierConfigManager.getConfigForSubId(SUB_ID)).thenReturn(carrierConfig); 124 when(mContext.getSystemService(CarrierConfigManager.class)) 125 .thenReturn(mCarrierConfigManager); 126 } 127 } 128