1 /* 2 * Copyright (C) 2016 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.cellbroadcastreceiver; 18 19 import static org.mockito.Mockito.doReturn; 20 import static org.mockito.Mockito.times; 21 import static org.mockito.Mockito.verify; 22 23 import android.app.Notification; 24 import android.app.NotificationManager; 25 import android.content.Intent; 26 import android.os.Bundle; 27 import android.os.IPowerManager; 28 import android.os.PowerManager; 29 import android.telephony.CellBroadcastMessage; 30 import android.widget.TextView; 31 32 import org.junit.After; 33 import org.junit.Before; 34 import org.mockito.ArgumentCaptor; 35 import org.mockito.Captor; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 39 import java.util.ArrayList; 40 41 public class CellBroadcastAlertDialogTest extends 42 CellBroadcastActivityTestCase<CellBroadcastAlertDialog> { 43 44 @Mock 45 private NotificationManager mMockedNotificationManager; 46 47 @Mock 48 private IPowerManager.Stub mMockedPowerManagerService; 49 50 @Captor 51 private ArgumentCaptor<Integer> mInt; 52 53 @Captor 54 private ArgumentCaptor<Notification> mNotification; 55 56 private PowerManager mPowerManager; 57 CellBroadcastAlertDialogTest()58 public CellBroadcastAlertDialogTest() { 59 super(CellBroadcastAlertDialog.class); 60 } 61 62 @Override createActivityIntent()63 protected Intent createActivityIntent() { 64 ArrayList<CellBroadcastMessage> messageList = new ArrayList<>(1); 65 messageList.add(new CellBroadcastMessage( 66 CellBroadcastAlertServiceTest.createMessage(12412))); 67 68 Intent intent = new Intent(getInstrumentation().getTargetContext(), 69 CellBroadcastAlertDialog.class); 70 intent.putParcelableArrayListExtra(CellBroadcastMessage.SMS_CB_MESSAGE_EXTRA, 71 messageList); 72 return intent; 73 } 74 75 @Before setUp()76 public void setUp() throws Exception { 77 super.setUp(); 78 MockitoAnnotations.initMocks(this); 79 injectSystemService(NotificationManager.class, mMockedNotificationManager); 80 // PowerManager is a final class so we can't use Mockito to mock it, but we can mock 81 // its underlying service. 82 doReturn(true).when(mMockedPowerManagerService).isInteractive(); 83 mPowerManager = new PowerManager(mContext, mMockedPowerManagerService, null); 84 injectSystemService(PowerManager.class, mPowerManager); 85 } 86 87 @After tearDown()88 public void tearDown() throws Exception { 89 super.tearDown(); 90 } 91 testTitleAndMessageText()92 public void testTitleAndMessageText() throws Throwable { 93 startActivity(); 94 waitForMs(100); 95 96 CharSequence etremeAlertString = 97 getActivity().getResources().getText(R.string.cmas_presidential_level_alert); 98 assertEquals(etremeAlertString, getActivity().getTitle()); 99 assertEquals(etremeAlertString, 100 ((TextView) getActivity().findViewById(R.id.alertTitle)).getText()); 101 102 assertEquals(CellBroadcastAlertServiceTest.createMessage(34596).getMessageBody(), 103 (String) ((TextView) getActivity().findViewById(R.id.message)).getText()); 104 105 stopActivity(); 106 } 107 testAddToNotification()108 public void testAddToNotification() throws Throwable { 109 startActivity(); 110 waitForMs(100); 111 stopActivity(); 112 waitForMs(100); 113 verify(mMockedNotificationManager, times(1)).notify(mInt.capture(), 114 mNotification.capture()); 115 Bundle b = mNotification.getValue().extras; 116 117 assertEquals(1, (int) mInt.getValue()); 118 119 assertEquals(getActivity().getTitle(), b.getCharSequence(Notification.EXTRA_TITLE)); 120 assertEquals(CellBroadcastAlertServiceTest.createMessage(98235).getMessageBody(), 121 b.getCharSequence(Notification.EXTRA_TEXT)); 122 } 123 }