summaryrefslogtreecommitdiffstats
path: root/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/HeadsUpNotificationTest.java
blob: e8a80d9f587d2b519ad35be81f1425b2adb80e07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.systemui.statusbar.policy;

import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;

import android.app.Notification;
import android.os.*;
import android.service.notification.StatusBarNotification;
import com.android.systemui.SwipeHelper;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.statusbar.ExpandableNotificationRow;
import com.android.systemui.statusbar.NotificationData;
import com.android.systemui.statusbar.phone.PhoneStatusBar;

import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

/**
 * Test the Heads Up Notification.
 *
 * Specifically the policy that a notificaiton must remain visibile for a minimum period of time.
 */
public class HeadsUpNotificationTest extends SysuiTestCase {
    private static final String TAG = "HeadsUpNotificationTest";

    private static int TOUCH_SENSITIVITY = 100;
    private static int NOTIFICATION_DECAY = 10000;
    private static int MINIMUM_DISPLAY_TIME = 3000;
    private static int SNOOZE_TIME = 60000;
    private static long TOO_SOON = 1000L;  // less than MINIMUM_DISPLAY_TIME
    private static long LATER = 5000L;  // more than MINIMUM_DISPLAY_TIME
    private static long REMAINING_VISIBILITY = MINIMUM_DISPLAY_TIME - TOO_SOON;

    protected HeadsUpNotificationView mHeadsUp;

    @Mock protected PhoneStatusBar mMockStatusBar;
    @Mock private HeadsUpNotificationView.Clock mClock;
    @Mock private SwipeHelper mMockSwipeHelper;
    @Mock private HeadsUpNotificationView.EdgeSwipeHelper mMockEdgeSwipeHelper;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        MockitoAnnotations.initMocks(this);

        mHeadsUp = new HeadsUpNotificationView(mContext,
                mClock, mMockSwipeHelper, mMockEdgeSwipeHelper,
                NOTIFICATION_DECAY, MINIMUM_DISPLAY_TIME, TOUCH_SENSITIVITY, SNOOZE_TIME);
        mHeadsUp.setBar(mMockStatusBar);
    }

    private NotificationData.Entry makeNotification(String key) {
        StatusBarNotification sbn = mock(StatusBarNotification.class);
        when(sbn.getKey()).thenReturn(key);
        return new NotificationData.Entry(sbn, null);
    }

    public void testPostAndDecay() {
        NotificationData.Entry a = makeNotification("a");
        mHeadsUp.showNotification(a);
        Mockito.verify(mMockStatusBar, never()).scheduleHeadsUpClose();
        Mockito.verify(mMockStatusBar, times(1)).scheduleHeadsUpOpen();
        ArgumentCaptor<Long> decayArg = ArgumentCaptor.forClass(Long.class);
        Mockito.verify(mMockStatusBar).scheduleHeadsUpDecay(decayArg.capture());
        // New notification gets a full decay time.
        assertEquals(NOTIFICATION_DECAY, (long) decayArg.getValue());
    }

    public void testPostAndDeleteTooSoon() {
        when(mClock.currentTimeMillis()).thenReturn(0L);
        NotificationData.Entry a = makeNotification("a");
        mHeadsUp.showNotification(a);
        reset(mMockStatusBar);

        when(mClock.currentTimeMillis()).thenReturn(TOO_SOON);
        mHeadsUp.removeNotification(a.key);
        ArgumentCaptor<Long> decayArg = ArgumentCaptor.forClass(Long.class);
        Mockito.verify(mMockStatusBar, never()).scheduleHeadsUpClose();
        Mockito.verify(mMockStatusBar).scheduleHeadsUpDecay(decayArg.capture());
        // Leave the window up for the balance of the minumum time.
        assertEquals(REMAINING_VISIBILITY, (long) decayArg.getValue());
    }

    public void testPostAndDeleteLater() {
        when(mClock.currentTimeMillis()).thenReturn(0L);
        NotificationData.Entry a = makeNotification("a");
        mHeadsUp.showNotification(a);
        reset(mMockStatusBar);

        when(mClock.currentTimeMillis()).thenReturn(LATER);
        mHeadsUp.removeNotification(a.key);
        // Delete closes immediately if the minimum time window is satisfied.
        Mockito.verify(mMockStatusBar, times(1)).scheduleHeadsUpClose();
        Mockito.verify(mMockStatusBar, never()).scheduleHeadsUpDecay(anyInt());
    }

    // This is a bad test.  It should not care that there is a call to scheduleHeadsUpClose(),
    // but it happens that there will be one, so it is important that it happen before the
    // call to scheduleHeadsUpOpen(), so that the final state is open.
    // Maybe mMockStatusBar should instead be a fake that tracks the open/closed state.
    public void testPostAndReplaceTooSoon() {
        InOrder callOrder = inOrder(mMockStatusBar);
        when(mClock.currentTimeMillis()).thenReturn(0L);
        NotificationData.Entry a = makeNotification("a");
        mHeadsUp.showNotification(a);
        reset(mMockStatusBar);

        when(mClock.currentTimeMillis()).thenReturn(TOO_SOON);
        NotificationData.Entry b = makeNotification("b");
        mHeadsUp.showNotification(b);
        Mockito.verify(mMockStatusBar, times(1)).scheduleHeadsUpClose();
        ArgumentCaptor<Long> decayArg = ArgumentCaptor.forClass(Long.class);
        Mockito.verify(mMockStatusBar, times(1)).scheduleHeadsUpDecay(decayArg.capture());
        // New notification gets a full decay time.
        assertEquals(NOTIFICATION_DECAY, (long) decayArg.getValue());

        // Make sure close was called before open, so that the heads up stays open.
        callOrder.verify(mMockStatusBar).scheduleHeadsUpClose();
        callOrder.verify(mMockStatusBar).scheduleHeadsUpOpen();
    }

    public void testPostAndUpdateAlertAgain() {
        when(mClock.currentTimeMillis()).thenReturn(0L);
        NotificationData.Entry a = makeNotification("a");
        mHeadsUp.showNotification(a);
        reset(mMockStatusBar);

        when(mClock.currentTimeMillis()).thenReturn(TOO_SOON);
        mHeadsUp.updateNotification(a, true);
        Mockito.verify(mMockStatusBar, never()).scheduleHeadsUpClose();
        ArgumentCaptor<Long> decayArg = ArgumentCaptor.forClass(Long.class);
        Mockito.verify(mMockStatusBar, times(1)).scheduleHeadsUpDecay(decayArg.capture());
        // Alert again gets a full decay time.
        assertEquals(NOTIFICATION_DECAY, (long) decayArg.getValue());
    }

    public void testPostAndUpdateAlertAgainFastFail() {
        when(mClock.currentTimeMillis()).thenReturn(0L);
        NotificationData.Entry a = makeNotification("a");
        mHeadsUp.showNotification(a);
        reset(mMockStatusBar);

        when(mClock.currentTimeMillis()).thenReturn(TOO_SOON);
        NotificationData.Entry a_prime = makeNotification("a");
        mHeadsUp.updateNotification(a_prime, true);
        Mockito.verify(mMockStatusBar, never()).scheduleHeadsUpClose();
        ArgumentCaptor<Long> decayArg = ArgumentCaptor.forClass(Long.class);
        Mockito.verify(mMockStatusBar, times(1)).scheduleHeadsUpDecay(decayArg.capture());
        // Alert again gets a full decay time.
        assertEquals(NOTIFICATION_DECAY, (long) decayArg.getValue());
    }

    public void testPostAndUpdateNoAlertAgain() {
        when(mClock.currentTimeMillis()).thenReturn(0L);
        NotificationData.Entry a = makeNotification("a");
        mHeadsUp.showNotification(a);
        reset(mMockStatusBar);

        when(mClock.currentTimeMillis()).thenReturn(TOO_SOON);
        mHeadsUp.updateNotification(a, false);
        Mockito.verify(mMockStatusBar, never()).scheduleHeadsUpClose();
        Mockito.verify(mMockStatusBar, never()).scheduleHeadsUpDecay(anyInt());
    }

    public void testPostAndUpdateNoAlertAgainFastFail() {
        when(mClock.currentTimeMillis()).thenReturn(0L);
        NotificationData.Entry a = makeNotification("a");
        mHeadsUp.showNotification(a);
        reset(mMockStatusBar);

        when(mClock.currentTimeMillis()).thenReturn(TOO_SOON);
        NotificationData.Entry a_prime = makeNotification("a");
        mHeadsUp.updateNotification(a_prime, false);
        Mockito.verify(mMockStatusBar, never()).scheduleHeadsUpClose();
        Mockito.verify(mMockStatusBar, never()).scheduleHeadsUpDecay(anyInt());
    }

    public void testPostAndUpdateLowPriorityTooSoon() {
        when(mClock.currentTimeMillis()).thenReturn(0L);
        NotificationData.Entry a = makeNotification("a");
        mHeadsUp.showNotification(a);
        reset(mMockStatusBar);

        when(mClock.currentTimeMillis()).thenReturn(TOO_SOON);
        mHeadsUp.release();
        Mockito.verify(mMockStatusBar, never()).scheduleHeadsUpClose();
        ArgumentCaptor<Long> decayArg = ArgumentCaptor.forClass(Long.class);
        Mockito.verify(mMockStatusBar, times(1)).scheduleHeadsUpDecay(decayArg.capture());
        // Down grade on update leaves the window up for the balance of the minumum time.
        assertEquals(REMAINING_VISIBILITY, (long) decayArg.getValue());
    }

    public void testPostAndUpdateLowPriorityTooSoonFastFail() {
        when(mClock.currentTimeMillis()).thenReturn(0L);
        NotificationData.Entry a = makeNotification("a");
        mHeadsUp.showNotification(a);
        reset(mMockStatusBar);

        when(mClock.currentTimeMillis()).thenReturn(TOO_SOON);
        NotificationData.Entry a_prime = makeNotification("a");
        mHeadsUp.updateNotification(a_prime, false);
        mHeadsUp.release();
        Mockito.verify(mMockStatusBar, never()).scheduleHeadsUpClose();
        ArgumentCaptor<Long> decayArg = ArgumentCaptor.forClass(Long.class);
        Mockito.verify(mMockStatusBar, times(1)).scheduleHeadsUpDecay(decayArg.capture());
        // Down grade on update leaves the window up for the balance of the minumum time.
        assertEquals(REMAINING_VISIBILITY, (long) decayArg.getValue());
    }

    public void testPostAndUpdateLowPriorityLater() {
        when(mClock.currentTimeMillis()).thenReturn(0L);
        NotificationData.Entry a = makeNotification("a");
        mHeadsUp.showNotification(a);
        reset(mMockStatusBar);

        when(mClock.currentTimeMillis()).thenReturn(LATER);
        mHeadsUp.release();
        // Down grade on update closes immediately if the minimum time window is satisfied.
        Mockito.verify(mMockStatusBar, times(1)).scheduleHeadsUpClose();
        Mockito.verify(mMockStatusBar, never()).scheduleHeadsUpDecay(anyInt());
    }

    public void testPostAndUpdateLowPriorityLaterFastFail() {
        when(mClock.currentTimeMillis()).thenReturn(0L);
        NotificationData.Entry a = makeNotification("a");
        mHeadsUp.showNotification(a);
        reset(mMockStatusBar);

        when(mClock.currentTimeMillis()).thenReturn(LATER);
        NotificationData.Entry a_prime = makeNotification("a");
        mHeadsUp.updateNotification(a_prime, false);
        mHeadsUp.release();
        // Down grade on update closes immediately if the minimum time window is satisfied.
        Mockito.verify(mMockStatusBar, times(1)).scheduleHeadsUpClose();
        Mockito.verify(mMockStatusBar, never()).scheduleHeadsUpDecay(anyInt());
    }
}