1a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek/*
2a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * Copyright (C) 2017 The Android Open Source Project
3a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek *
4a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * Licensed under the Apache License, Version 2.0 (the "License");
5a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * you may not use this file except in compliance with the License.
6a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * You may obtain a copy of the License at
7a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek *
8a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek *      http://www.apache.org/licenses/LICENSE-2.0
9a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek *
10a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * Unless required by applicable law or agreed to in writing, software
11a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * distributed under the License is distributed on an "AS IS" BASIS,
12a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * See the License for the specific language governing permissions and
14a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinek * limitations under the License
15a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek */
16a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
17a7679b6f3f0fc3e7c40102b732cfa51a9830a891Selim Cinekpackage com.android.systemui.statusbar.notification;
18a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
19a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinekimport android.service.notification.StatusBarNotification;
20a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinekimport android.support.test.runner.AndroidJUnit4;
21a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinekimport android.test.suitebuilder.annotation.SmallTest;
22a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
2325a52b65b2cac1f49f37f4532cfa62282432957aJason Monkimport com.android.systemui.SysuiTestCase;
24a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinekimport com.android.systemui.statusbar.ExpandableNotificationRow;
25a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinekimport com.android.systemui.statusbar.NotificationData;
26a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinekimport com.android.systemui.statusbar.notification.VisibilityLocationProvider;
27a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinekimport com.android.systemui.statusbar.notification.VisualStabilityManager;
28a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
29a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinekimport org.junit.Before;
30a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinekimport org.junit.Test;
31a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinekimport org.junit.runner.RunWith;
32a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
33a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinekimport static junit.framework.Assert.assertEquals;
34a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinekimport static org.mockito.Matchers.anyObject;
35a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinekimport static org.mockito.Mockito.mock;
36a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinekimport static org.mockito.Mockito.verify;
37a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinekimport static org.mockito.Mockito.when;
38a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
39a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek@SmallTest
40a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek@RunWith(AndroidJUnit4.class)
4125a52b65b2cac1f49f37f4532cfa62282432957aJason Monkpublic class VisualStabilityManagerTest extends SysuiTestCase {
42a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
43a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    private VisualStabilityManager mVisualStabilityManager = new VisualStabilityManager();
44a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    private VisualStabilityManager.Callback mCallback = mock(VisualStabilityManager.Callback.class);
45a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    private VisibilityLocationProvider mLocationProvider = mock(VisibilityLocationProvider.class);
46a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    private ExpandableNotificationRow mRow = mock(ExpandableNotificationRow.class);
47a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    private NotificationData.Entry mEntry;
48a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
49a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    @Before
50a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    public void setUp() {
51a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setVisibilityLocationProvider(mLocationProvider);
52a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mEntry = new NotificationData.Entry(mock(StatusBarNotification.class));
53a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mEntry.row = mRow;
54a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    }
55a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
56a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    @Test
57a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    public void testPanelExpansion() {
58a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setPanelExpanded(true);
59a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setScreenOn(true);
60a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        assertEquals(mVisualStabilityManager.canReorderNotification(mRow), false);
61a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setPanelExpanded(false);
62a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        assertEquals(mVisualStabilityManager.canReorderNotification(mRow), true);
63a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    }
64a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
65a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    @Test
66a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    public void testScreenOn() {
67a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setPanelExpanded(true);
68a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setScreenOn(true);
69a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        assertEquals(mVisualStabilityManager.canReorderNotification(mRow), false);
70a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setScreenOn(false);
71a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        assertEquals(mVisualStabilityManager.canReorderNotification(mRow), true);
72a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    }
73a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
74a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    @Test
75a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    public void testReorderingAllowedChangesScreenOn() {
76a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setPanelExpanded(true);
77a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setScreenOn(true);
78a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        assertEquals(mVisualStabilityManager.isReorderingAllowed(), false);
79a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setScreenOn(false);
80a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        assertEquals(mVisualStabilityManager.isReorderingAllowed(), true);
81a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    }
82a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
83a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    @Test
84a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    public void testReorderingAllowedChangesPanel() {
85a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setPanelExpanded(true);
86a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setScreenOn(true);
87a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        assertEquals(mVisualStabilityManager.isReorderingAllowed(), false);
88a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setPanelExpanded(false);
89a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        assertEquals(mVisualStabilityManager.isReorderingAllowed(), true);
90a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    }
91a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
92a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    @Test
93a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    public void testCallBackCalledScreenOn() {
94a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setPanelExpanded(true);
95a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setScreenOn(true);
96a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.addReorderingAllowedCallback(mCallback);
97a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setScreenOn(false);
98a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        verify(mCallback).onReorderingAllowed();
99a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    }
100a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
101a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    @Test
102a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    public void testCallBackCalledPanelExpanded() {
103a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setPanelExpanded(true);
104a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setScreenOn(true);
105a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.addReorderingAllowedCallback(mCallback);
106a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setPanelExpanded(false);
107a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        verify(mCallback).onReorderingAllowed();
108a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    }
109a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
110a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    @Test
111a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    public void testCallBackExactlyOnce() {
112a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setPanelExpanded(true);
113a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setScreenOn(true);
114a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.addReorderingAllowedCallback(mCallback);
115a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setScreenOn(false);
116a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setScreenOn(true);
117a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setScreenOn(false);
118a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        verify(mCallback).onReorderingAllowed();
119a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    }
120a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
121a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    @Test
122a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    public void testAddedCanReorder() {
123a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setPanelExpanded(true);
124a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setScreenOn(true);
125a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.notifyViewAddition(mRow);
126a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        assertEquals(mVisualStabilityManager.canReorderNotification(mRow), true);
127a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    }
128a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
129a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    @Test
130a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    public void testReorderingVisibleHeadsUpNotAllowed() {
131a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setPanelExpanded(true);
132a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setScreenOn(true);
133a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        when(mLocationProvider.isInVisibleLocation(anyObject())).thenReturn(true);
134a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.onHeadsUpStateChanged(mEntry, true);
135a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        assertEquals(mVisualStabilityManager.canReorderNotification(mRow), false);
136a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    }
137a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
138a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    @Test
139a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    public void testReorderingVisibleHeadsUpAllowed() {
140a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setPanelExpanded(true);
141a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setScreenOn(true);
142a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        when(mLocationProvider.isInVisibleLocation(anyObject())).thenReturn(false);
143a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.onHeadsUpStateChanged(mEntry, true);
144a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        assertEquals(mVisualStabilityManager.canReorderNotification(mRow), true);
145a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    }
146a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek
147a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    @Test
148a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    public void testReorderingVisibleHeadsUpAllowedOnce() {
149a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setPanelExpanded(true);
150a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.setScreenOn(true);
151a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        when(mLocationProvider.isInVisibleLocation(anyObject())).thenReturn(false);
152a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.onHeadsUpStateChanged(mEntry, true);
153a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        mVisualStabilityManager.onReorderingFinished();
154a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek        assertEquals(mVisualStabilityManager.canReorderNotification(mRow), false);
155a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek    }
1564b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos
1574b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos    @Test
1584b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos    public void testPulsing() {
1594b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos        mVisualStabilityManager.setPulsing(true);
1604b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos        assertEquals(mVisualStabilityManager.canReorderNotification(mRow), false);
1614b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos        mVisualStabilityManager.setPulsing(false);
1624b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos        assertEquals(mVisualStabilityManager.canReorderNotification(mRow), true);
1634b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos    }
1644b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos
1654b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos    @Test
1664b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos    public void testReorderingAllowedChanges_Pulsing() {
1674b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos        mVisualStabilityManager.setPulsing(true);
1684b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos        assertEquals(mVisualStabilityManager.isReorderingAllowed(), false);
1694b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos        mVisualStabilityManager.setPulsing(false);
1704b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos        assertEquals(mVisualStabilityManager.isReorderingAllowed(), true);
1714b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos    }
1724b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos
1734b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos    @Test
1744b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos    public void testCallBackCalled_Pulsing() {
1754b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos        mVisualStabilityManager.setPulsing(true);
1764b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos        mVisualStabilityManager.addReorderingAllowedCallback(mCallback);
1774b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos        mVisualStabilityManager.setPulsing(false);
1784b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos        verify(mCallback).onReorderingAllowed();
1794b820e4e5e0404adbf9f9678b79fa7e988e3e597Adrian Roos    }
180a7d4f82c05f1c6ea5dd92a4871711d380b01100aSelim Cinek}
181