1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.systemui.statusbar; 16 17 import static junit.framework.Assert.assertFalse; 18 import static junit.framework.Assert.assertTrue; 19 import static org.mockito.ArgumentMatchers.argThat; 20 import static org.mockito.ArgumentMatchers.eq; 21 import static org.mockito.ArgumentMatchers.isNull; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 26 import android.app.Notification; 27 import android.os.RemoteException; 28 import android.service.notification.StatusBarNotification; 29 import android.support.test.filters.SmallTest; 30 import android.testing.AndroidTestingRunner; 31 import android.testing.TestableLooper; 32 33 import com.android.internal.statusbar.IStatusBarService; 34 import com.android.systemui.R; 35 import com.android.systemui.SysuiTestCase; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 43 @RunWith(AndroidTestingRunner.class) 44 @TestableLooper.RunWithLooper 45 @SmallTest 46 public class SmartReplyControllerTest extends SysuiTestCase { 47 private static final String TEST_NOTIFICATION_KEY = "akey"; 48 private static final String TEST_CHOICE_TEXT = "A Reply"; 49 private static final int TEST_CHOICE_INDEX = 2; 50 private static final int TEST_CHOICE_COUNT = 4; 51 52 private Notification mNotification; 53 private NotificationData.Entry mEntry; 54 55 @Mock 56 private NotificationEntryManager mNotificationEntryManager; 57 @Mock 58 private IStatusBarService mIStatusBarService; 59 60 @Before 61 public void setUp() { 62 MockitoAnnotations.initMocks(this); 63 64 mDependency.injectTestDependency(NotificationEntryManager.class, 65 mNotificationEntryManager); 66 mDependency.injectTestDependency(IStatusBarService.class, mIStatusBarService); 67 68 mNotification = new Notification.Builder(mContext, "") 69 .setSmallIcon(R.drawable.ic_person) 70 .setContentTitle("Title") 71 .setContentText("Text").build(); 72 StatusBarNotification sbn = mock(StatusBarNotification.class); 73 when(sbn.getNotification()).thenReturn(mNotification); 74 when(sbn.getKey()).thenReturn(TEST_NOTIFICATION_KEY); 75 mEntry = new NotificationData.Entry(sbn); 76 } 77 78 @Test 79 public void testSendSmartReply_updatesRemoteInput() { 80 StatusBarNotification sbn = mock(StatusBarNotification.class); 81 when(sbn.getKey()).thenReturn(TEST_NOTIFICATION_KEY); 82 when(mNotificationEntryManager.rebuildNotificationWithRemoteInput( 83 argThat(entry -> entry.notification.getKey().equals(TEST_NOTIFICATION_KEY)), 84 eq(TEST_CHOICE_TEXT), eq(true))).thenReturn(sbn); 85 86 SmartReplyController controller = new SmartReplyController(); 87 controller.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT); 88 89 // Sending smart reply should make calls to NotificationEntryManager 90 // to update the notification with reply and spinner. 91 verify(mNotificationEntryManager).rebuildNotificationWithRemoteInput( 92 argThat(entry -> entry.notification.getKey().equals(TEST_NOTIFICATION_KEY)), 93 eq(TEST_CHOICE_TEXT), eq(true)); 94 verify(mNotificationEntryManager).updateNotification( 95 argThat(sbn2 -> sbn2.getKey().equals(TEST_NOTIFICATION_KEY)), isNull()); 96 } 97 98 @Test 99 public void testSendSmartReply_logsToStatusBar() throws RemoteException { 100 StatusBarNotification sbn = mock(StatusBarNotification.class); 101 when(sbn.getKey()).thenReturn(TEST_NOTIFICATION_KEY); 102 when(mNotificationEntryManager.rebuildNotificationWithRemoteInput( 103 argThat(entry -> entry.notification.getKey().equals(TEST_NOTIFICATION_KEY)), 104 eq(TEST_CHOICE_TEXT), eq(true))).thenReturn(sbn); 105 106 SmartReplyController controller = new SmartReplyController(); 107 controller.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT); 108 109 // Check we log the result to the status bar service. 110 verify(mIStatusBarService).onNotificationSmartReplySent(TEST_NOTIFICATION_KEY, 111 TEST_CHOICE_INDEX); 112 } 113 114 @Test 115 public void testShowSmartReply_logsToStatusBar() throws RemoteException { 116 SmartReplyController controller = new SmartReplyController(); 117 controller.smartRepliesAdded(mEntry, TEST_CHOICE_COUNT); 118 119 // Check we log the result to the status bar service. 120 verify(mIStatusBarService).onNotificationSmartRepliesAdded(TEST_NOTIFICATION_KEY, 121 TEST_CHOICE_COUNT); 122 } 123 124 @Test 125 public void testSendSmartReply_reportsSending() { 126 SmartReplyController controller = new SmartReplyController(); 127 controller.smartReplySent(mEntry, TEST_CHOICE_INDEX, TEST_CHOICE_TEXT); 128 129 assertTrue(controller.isSendingSmartReply(TEST_NOTIFICATION_KEY)); 130 } 131 132 @Test 133 public void testSendingSmartReply_afterRemove_shouldReturnFalse() { 134 SmartReplyController controller = new SmartReplyController(); 135 controller.isSendingSmartReply(TEST_NOTIFICATION_KEY); 136 controller.stopSending(mEntry); 137 138 assertFalse(controller.isSendingSmartReply(TEST_NOTIFICATION_KEY)); 139 } 140 } 141