CarrierServiceStateTrackerTest.java revision bdb9b8f05fdc4c8a87229e76dbe47bee9a784b31
1/* 2 * Copyright (C) 2017 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 17package com.android.internal.telephony; 18 19import android.app.Notification; 20import android.app.NotificationManager; 21import android.content.Context; 22import android.content.pm.ApplicationInfo; 23import android.content.res.Resources; 24import android.os.HandlerThread; 25import android.os.Message; 26import android.test.suitebuilder.annotation.SmallTest; 27 28import org.junit.After; 29import org.junit.Before; 30import org.junit.Test; 31import org.mockito.Mock; 32import org.mockito.MockitoAnnotations; 33 34import static org.mockito.Mockito.any; 35import static org.mockito.Mockito.doReturn; 36import static org.mockito.Mockito.eq; 37import static org.mockito.Mockito.isA; 38import static org.mockito.Mockito.spy; 39import static org.mockito.Mockito.verify; 40import static org.mockito.Mockito.when; 41 42/** 43 * Unit tests for {@link com.android.internal.telephony.CarrierServiceStateTracker}. 44 */ 45@SmallTest 46public class CarrierServiceStateTrackerTest extends TelephonyTest { 47 public static final String LOG_TAG = "CSST"; 48 public static final int TEST_TIMEOUT = 5000; 49 50 private CarrierServiceStateTracker mCarrierSST; 51 private CarrierServiceStateTrackerTestHandler mCarrierServiceStateTrackerTestHandler; 52 private CarrierServiceStateTracker.PrefNetworkNotification mPrefNetworkNotification; 53 private CarrierServiceStateTracker.EmergencyNetworkNotification mEmergencyNetworkNotification; 54 55 @Mock Context mContext; 56 @Mock ServiceStateTracker mServiceStateTracker; 57 @Mock NotificationManager mNotificationManager; 58 @Mock Resources mResources; 59 60 private class CarrierServiceStateTrackerTestHandler extends HandlerThread { 61 62 private CarrierServiceStateTrackerTestHandler(String name) { 63 super(name); 64 } 65 66 @Override 67 public void onLooperPrepared() { 68 mCarrierSST = spy(new CarrierServiceStateTracker(mPhone, mServiceStateTracker)); 69 setReady(true); 70 } 71 } 72 73 @Before 74 public void setUp() throws Exception { 75 MockitoAnnotations.initMocks(this); 76 logd(LOG_TAG + "Setup!"); 77 super.setUp(getClass().getSimpleName()); 78 mCarrierServiceStateTrackerTestHandler = 79 new CarrierServiceStateTrackerTestHandler(getClass().getSimpleName()); 80 mCarrierServiceStateTrackerTestHandler.start(); 81 when(mContext.getResources()).thenReturn(mResources); 82 when(mContext.getPackageManager()).thenReturn(mPackageManager); 83 when(mContext.getApplicationInfo()).thenReturn(new ApplicationInfo()); 84 waitUntilReady(); 85 } 86 87 @After 88 public void tearDown() throws Exception { 89 mCarrierServiceStateTrackerTestHandler.quit(); 90 super.tearDown(); 91 } 92 93 @Test 94 @SmallTest 95 public void testCancelBothNotifications() { 96 logd(LOG_TAG + ":testCancelBothNotifications()"); 97 Message notificationMsg = mCarrierSST.obtainMessage( 98 CarrierServiceStateTracker.CARRIER_EVENT_DATA_REGISTRATION, null); 99 doReturn(false).when(mCarrierSST).evaluateSendingMessage(any()); 100 doReturn(mNotificationManager).when(mCarrierSST).getNotificationManager(any()); 101 mCarrierSST.handleMessage(notificationMsg); 102 waitForHandlerAction(mCarrierSST, TEST_TIMEOUT); 103 verify(mNotificationManager).cancel( 104 CarrierServiceStateTracker.NOTIFICATION_EMERGENCY_NETWORK); 105 verify(mNotificationManager).cancel( 106 CarrierServiceStateTracker.NOTIFICATION_PREF_NETWORK); 107 } 108 109 @Test 110 @SmallTest 111 public void testSendBothNotifications() { 112 logd(LOG_TAG + ":testSendBothNotifications()"); 113 Notification.Builder mNotificationBuilder = new Notification.Builder(mContext); 114 Message notificationMsg = mCarrierSST.obtainMessage( 115 CarrierServiceStateTracker.CARRIER_EVENT_DATA_DEREGISTRATION, null); 116 doReturn(true).when(mCarrierSST).evaluateSendingMessage(any()); 117 doReturn(false).when(mCarrierSST).isRadioOffOrAirplaneMode(); 118 doReturn(0).when(mCarrierSST).getDelay(any()); 119 doReturn(mNotificationBuilder).when(mCarrierSST).getNotificationBuilder(any()); 120 doReturn(mNotificationManager).when(mCarrierSST).getNotificationManager(any()); 121 mCarrierSST.handleMessage(notificationMsg); 122 waitForHandlerAction(mCarrierSST, TEST_TIMEOUT); 123 verify(mNotificationManager).notify( 124 eq(CarrierServiceStateTracker.NOTIFICATION_PREF_NETWORK), isA(Notification.class)); 125 verify(mNotificationManager).notify( 126 eq(CarrierServiceStateTracker.NOTIFICATION_EMERGENCY_NETWORK), any()); 127 } 128} 129