121bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney/*
221bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney * Copyright (C) 2017 The Android Open Source Project
321bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney *
421bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney * Licensed under the Apache License, Version 2.0 (the "License");
521bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney * you may not use this file except in compliance with the License.
621bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney * You may obtain a copy of the License at
721bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney *
821bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney *      http://www.apache.org/licenses/LICENSE-2.0
921bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney *
1021bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney * Unless required by applicable law or agreed to in writing, software
1121bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney * distributed under the License is distributed on an "AS IS" BASIS,
1221bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1321bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney * See the License for the specific language governing permissions and
1421bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney * limitations under the License.
1521bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney */
1621bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney
1721bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneypackage com.android.systemui.statusbar;
1821bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney
1921bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport static junit.framework.Assert.assertTrue;
2021bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney
2121bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport static org.mockito.ArgumentMatchers.any;
2221bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport static org.mockito.Mockito.verify;
2321bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport static org.mockito.Mockito.when;
2421bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney
2521bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport android.app.Notification;
2621bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport android.os.Handler;
2721bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport android.os.Looper;
2821bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport android.os.UserHandle;
2921bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport android.service.notification.NotificationListenerService;
3021bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport android.service.notification.StatusBarNotification;
3121bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport android.support.test.filters.SmallTest;
3221bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport android.testing.AndroidTestingRunner;
3321bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport android.testing.TestableLooper;
3421bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney
3521bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport com.android.systemui.SysuiTestCase;
3621bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney
3721bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport org.junit.Before;
3821bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport org.junit.Test;
3921bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport org.junit.runner.RunWith;
408f56b0e3452dcc940319708061a3a22a4d994027Eliot Courtneyimport org.mockito.Mock;
418f56b0e3452dcc940319708061a3a22a4d994027Eliot Courtneyimport org.mockito.MockitoAnnotations;
4221bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney
4321bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport java.util.HashSet;
4421bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneyimport java.util.Set;
4521bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney
4621bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney@SmallTest
4721bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney@RunWith(AndroidTestingRunner.class)
4821bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney@TestableLooper.RunWithLooper
4921bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtneypublic class NotificationListenerTest extends SysuiTestCase {
5021bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    private static final String TEST_PACKAGE_NAME = "test";
5121bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    private static final int TEST_UID = 0;
5221bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney
538f56b0e3452dcc940319708061a3a22a4d994027Eliot Courtney    @Mock private NotificationPresenter mPresenter;
548f56b0e3452dcc940319708061a3a22a4d994027Eliot Courtney    @Mock private NotificationListenerService.RankingMap mRanking;
558f56b0e3452dcc940319708061a3a22a4d994027Eliot Courtney    @Mock private NotificationData mNotificationData;
568f56b0e3452dcc940319708061a3a22a4d994027Eliot Courtney
578f56b0e3452dcc940319708061a3a22a4d994027Eliot Courtney    // Dependency mocks:
588f56b0e3452dcc940319708061a3a22a4d994027Eliot Courtney    @Mock private NotificationEntryManager mEntryManager;
598f56b0e3452dcc940319708061a3a22a4d994027Eliot Courtney    @Mock private NotificationRemoteInputManager mRemoteInputManager;
608f56b0e3452dcc940319708061a3a22a4d994027Eliot Courtney
6121bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    private NotificationListener mListener;
6221bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    private StatusBarNotification mSbn;
6321bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney
6421bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    @Before
6521bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    public void setUp() {
668f56b0e3452dcc940319708061a3a22a4d994027Eliot Courtney        MockitoAnnotations.initMocks(this);
678f56b0e3452dcc940319708061a3a22a4d994027Eliot Courtney        mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
688f56b0e3452dcc940319708061a3a22a4d994027Eliot Courtney        mDependency.injectTestDependency(NotificationRemoteInputManager.class,
698f56b0e3452dcc940319708061a3a22a4d994027Eliot Courtney                mRemoteInputManager);
708f56b0e3452dcc940319708061a3a22a4d994027Eliot Courtney
716dceace0eddd08156e6b71c17e3de4ed5f4f2f41Jason Monk        when(mPresenter.getHandler()).thenReturn(Handler.createAsync(Looper.myLooper()));
72a6d8cf294dfb587f130fdecc5e6897e75de7bf45Eliot Courtney        when(mEntryManager.getNotificationData()).thenReturn(mNotificationData);
7321bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney
746c313d3224c878d832db3ed833f4a3dd3786fb1fEliot Courtney        mListener = new NotificationListener(mContext);
7521bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney        mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID, 0,
7621bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney                new Notification(), UserHandle.CURRENT, null, 0);
773985ad5773cd4573525cbfb00e132b960a83ef48Eliot Courtney
784a96b36fd9857cd3d3534ed0396ec3d7155a324cEliot Courtney        mListener.setUpWithPresenter(mPresenter, mEntryManager);
7921bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    }
8021bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney
8121bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    @Test
8221bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    public void testNotificationAddCallsAddNotification() {
8321bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney        mListener.onNotificationPosted(mSbn, mRanking);
846dceace0eddd08156e6b71c17e3de4ed5f4f2f41Jason Monk        TestableLooper.get(this).processAllMessages();
85a6d8cf294dfb587f130fdecc5e6897e75de7bf45Eliot Courtney        verify(mEntryManager).addNotification(mSbn, mRanking);
8621bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    }
8721bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney
8821bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    @Test
8921bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    public void testPostNotificationRemovesKeyKeptForRemoteInput() {
9021bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney        mListener.onNotificationPosted(mSbn, mRanking);
916dceace0eddd08156e6b71c17e3de4ed5f4f2f41Jason Monk        TestableLooper.get(this).processAllMessages();
928cc15d2ebf3cb2e5caaefb984e547b4b3c1249d0Kenny Guy        verify(mEntryManager).removeKeyKeptForRemoteInput(mSbn.getKey());
9321bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    }
9421bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney
9521bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    @Test
9621bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    public void testNotificationUpdateCallsUpdateNotification() {
9721bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney        when(mNotificationData.get(mSbn.getKey())).thenReturn(new NotificationData.Entry(mSbn));
9821bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney        mListener.onNotificationPosted(mSbn, mRanking);
996dceace0eddd08156e6b71c17e3de4ed5f4f2f41Jason Monk        TestableLooper.get(this).processAllMessages();
100a6d8cf294dfb587f130fdecc5e6897e75de7bf45Eliot Courtney        verify(mEntryManager).updateNotification(mSbn, mRanking);
10121bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    }
10221bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney
10321bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    @Test
10421bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    public void testNotificationRemovalCallsRemoveNotification() {
10521bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney        mListener.onNotificationRemoved(mSbn, mRanking);
1066dceace0eddd08156e6b71c17e3de4ed5f4f2f41Jason Monk        TestableLooper.get(this).processAllMessages();
107a6d8cf294dfb587f130fdecc5e6897e75de7bf45Eliot Courtney        verify(mEntryManager).removeNotification(mSbn.getKey(), mRanking);
10821bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    }
10921bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney
11021bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    @Test
11121bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    public void testRankingUpdateCallsNotificationRankingUpdate() {
11221bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney        mListener.onNotificationRankingUpdate(mRanking);
1136dceace0eddd08156e6b71c17e3de4ed5f4f2f41Jason Monk        TestableLooper.get(this).processAllMessages();
11421bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney        // RankingMap may be modified by plugins.
115a6d8cf294dfb587f130fdecc5e6897e75de7bf45Eliot Courtney        verify(mEntryManager).updateNotificationRanking(any());
11621bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney    }
11721bc05f78359fe75531c010da9e669212f3c9c51Eliot Courtney}
118