Home | History | Annotate | Download | only in statusbar
      1 package com.android.systemui.statusbar;
      2 
      3 import static junit.framework.Assert.assertFalse;
      4 import static junit.framework.Assert.assertTrue;
      5 
      6 import static org.mockito.Mockito.verify;
      7 import static org.mockito.Mockito.when;
      8 
      9 import android.app.Notification;
     10 import android.content.Context;
     11 import android.os.Handler;
     12 import android.os.Looper;
     13 import android.os.UserHandle;
     14 import android.service.notification.NotificationListenerService;
     15 import android.service.notification.StatusBarNotification;
     16 import android.support.test.filters.SmallTest;
     17 import android.testing.AndroidTestingRunner;
     18 import android.testing.TestableLooper;
     19 
     20 import com.android.systemui.SysuiTestCase;
     21 
     22 import com.google.android.collect.Sets;
     23 
     24 import org.junit.Before;
     25 import org.junit.Test;
     26 import org.junit.runner.RunWith;
     27 import org.mockito.Mock;
     28 import org.mockito.MockitoAnnotations;
     29 
     30 @SmallTest
     31 @RunWith(AndroidTestingRunner.class)
     32 @TestableLooper.RunWithLooper
     33 public class NotificationRemoteInputManagerTest extends SysuiTestCase {
     34     private static final String TEST_PACKAGE_NAME = "test";
     35     private static final int TEST_UID = 0;
     36 
     37     @Mock private NotificationPresenter mPresenter;
     38     @Mock private RemoteInputController.Delegate mDelegate;
     39     @Mock private NotificationRemoteInputManager.Callback mCallback;
     40     @Mock private RemoteInputController mController;
     41     @Mock private NotificationListenerService.RankingMap mRanking;
     42     @Mock private ExpandableNotificationRow mRow;
     43 
     44     // Dependency mocks:
     45     @Mock private NotificationEntryManager mEntryManager;
     46     @Mock private NotificationLockscreenUserManager mLockscreenUserManager;
     47 
     48     private TestableNotificationRemoteInputManager mRemoteInputManager;
     49     private StatusBarNotification mSbn;
     50     private NotificationData.Entry mEntry;
     51 
     52     @Before
     53     public void setUp() {
     54         MockitoAnnotations.initMocks(this);
     55         mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
     56         mDependency.injectTestDependency(NotificationLockscreenUserManager.class,
     57                 mLockscreenUserManager);
     58 
     59         when(mPresenter.getHandler()).thenReturn(Handler.createAsync(Looper.myLooper()));
     60         when(mEntryManager.getLatestRankingMap()).thenReturn(mRanking);
     61 
     62         mRemoteInputManager = new TestableNotificationRemoteInputManager(mContext);
     63         mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID,
     64                 0, new Notification(), UserHandle.CURRENT, null, 0);
     65         mEntry = new NotificationData.Entry(mSbn);
     66         mEntry.row = mRow;
     67 
     68         mRemoteInputManager.setUpWithPresenterForTest(mPresenter, mEntryManager, mCallback,
     69                 mDelegate, mController);
     70     }
     71 
     72     @Test
     73     public void testOnRemoveNotificationNotKept() {
     74         assertFalse(mRemoteInputManager.onRemoveNotification(mEntry));
     75         assertTrue(mRemoteInputManager.getRemoteInputEntriesToRemoveOnCollapse().isEmpty());
     76     }
     77 
     78     @Test
     79     public void testOnRemoveNotificationKept() {
     80         when(mController.isRemoteInputActive(mEntry)).thenReturn(true);
     81         assertTrue(mRemoteInputManager.onRemoveNotification(mEntry));
     82         assertTrue(mRemoteInputManager.getRemoteInputEntriesToRemoveOnCollapse().equals(
     83                 Sets.newArraySet(mEntry)));
     84     }
     85 
     86     @Test
     87     public void testPerformOnRemoveNotification() {
     88         when(mController.isRemoteInputActive(mEntry)).thenReturn(true);
     89         mRemoteInputManager.onPerformRemoveNotification(mSbn, mEntry);
     90 
     91         verify(mController).removeRemoteInput(mEntry, null);
     92     }
     93 
     94     @Test
     95     public void testRemoveRemoteInputEntriesKeptUntilCollapsed() {
     96         mRemoteInputManager.getRemoteInputEntriesToRemoveOnCollapse().add(mEntry);
     97         mRemoteInputManager.removeRemoteInputEntriesKeptUntilCollapsed();
     98 
     99         assertTrue(mRemoteInputManager.getRemoteInputEntriesToRemoveOnCollapse().isEmpty());
    100         verify(mController).removeRemoteInput(mEntry, null);
    101         verify(mEntryManager).removeNotification(mEntry.key, mRanking);
    102     }
    103 
    104     private class TestableNotificationRemoteInputManager extends NotificationRemoteInputManager {
    105 
    106         public TestableNotificationRemoteInputManager(Context context) {
    107             super(context);
    108         }
    109 
    110         public void setUpWithPresenterForTest(NotificationPresenter presenter,
    111                 NotificationEntryManager entryManager,
    112                 Callback callback,
    113                 RemoteInputController.Delegate delegate,
    114                 RemoteInputController controller) {
    115             super.setUpWithPresenter(presenter, entryManager, callback, delegate);
    116             mRemoteInputController = controller;
    117         }
    118     }
    119 }
    120