summaryrefslogtreecommitdiff
path: root/services/QualifiedNetworksService/tests/src/com/android/telephony/qns/QnsTimerTest.java
blob: 50a36b57274cd4814f1e543581edbcf9a4eae8ef (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * Copyright (C) 2023 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.telephony.qns;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.os.PowerManager;
import android.os.SystemClock;
import android.os.test.TestLooper;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.concurrent.CountDownLatch;

@RunWith(AndroidJUnit4.class)
public class QnsTimerTest extends QnsTest {

    private static final int EVENT_QNS_TIMER_EXPIRED = 1;
    static final String ACTION_ALARM_TIMER_EXPIRED =
            "com.android.telephony.qns.action.ALARM_TIMER_EXPIRED";
    static final String KEY_TIMER_ID = "key_timer_id";
    @Mock private Context mContext;
    @Mock private AlarmManager mAlarmManager;
    @Mock private PowerManager mPowerManager;
    @Mock Message mMessage;
    private QnsTimer mQnsTimer;
    private BroadcastReceiver mBroadcastReceiver;
    int mTimerId;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        mContext = spy(ApplicationProvider.getApplicationContext());
        when(mContext.getSystemService(AlarmManager.class)).thenReturn(mAlarmManager);
        when(mContext.getSystemService(PowerManager.class)).thenReturn(mPowerManager);
        mQnsTimer = new QnsTimer(mContext);
        ArgumentCaptor<BroadcastReceiver> args = ArgumentCaptor.forClass(BroadcastReceiver.class);
        verify(mContext).registerReceiver(args.capture(), isA(IntentFilter.class), anyInt());
        mBroadcastReceiver = args.getValue();
    }

    @After
    public void tearDown() {
        mQnsTimer.close();
    }

    @Test
    public void testRegisterTimerForScreenOff() {
        mBroadcastReceiver.onReceive(mContext, new Intent(Intent.ACTION_SCREEN_OFF));
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);

        mTimerId = mQnsTimer.registerTimer(mMessage, 30000);
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);

        assertTrue(mQnsTimer.getTimersInfo().contains(new QnsTimer.TimerInfo(mTimerId)));
        assertTrue(mQnsTimer.mHandler.hasMessages(EVENT_QNS_TIMER_EXPIRED));
        verify(mAlarmManager)
                .setExactAndAllowWhileIdle(anyInt(), anyLong(), isA(PendingIntent.class));
    }

    @Test
    public void testRegisterTimerForScreenOn() {
        mBroadcastReceiver.onReceive(mContext, new Intent(Intent.ACTION_SCREEN_ON));
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);

        mTimerId = mQnsTimer.registerTimer(mMessage, 80000);
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);

        assertTrue(mQnsTimer.getTimersInfo().contains(new QnsTimer.TimerInfo(mTimerId)));
        assertTrue(mQnsTimer.mHandler.hasMessages(EVENT_QNS_TIMER_EXPIRED));
        verify(mAlarmManager, never())
                .setExactAndAllowWhileIdle(anyInt(), anyLong(), isA(PendingIntent.class));
    }

    @Test
    public void testUnregisterForInvalidId() {
        testRegisterTimerForScreenOn();
        int timerInfoSize = mQnsTimer.getTimersInfo().size();
        mQnsTimer.unregisterTimer(QnsConstants.INVALID_ID);
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);
        assertEquals(timerInfoSize, mQnsTimer.getTimersInfo().size());
    }

    @Test
    public void testUnregisterTimerForScreenOff() {
        testRegisterTimerForScreenOff();
        mQnsTimer.unregisterTimer(mTimerId);
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);
        assertFalse(mQnsTimer.getTimersInfo().contains(new QnsTimer.TimerInfo(mTimerId)));
        assertFalse(mQnsTimer.mHandler.hasMessages(EVENT_QNS_TIMER_EXPIRED));
        verify(mAlarmManager).cancel(isA(PendingIntent.class));
    }

    @Test
    public void testUnregisterTimerForScreenOn() {
        testRegisterTimerForScreenOn();
        mQnsTimer.unregisterTimer(mTimerId);
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);
        assertFalse(mQnsTimer.getTimersInfo().contains(new QnsTimer.TimerInfo(mTimerId)));
        assertFalse(mQnsTimer.mHandler.hasMessages(EVENT_QNS_TIMER_EXPIRED));
        verify(mAlarmManager, never()).cancel(isA(PendingIntent.class));
    }

    @Test
    public void testUpdateTimerTypeToAlarm() {
        testRegisterTimerForScreenOn();

        mBroadcastReceiver.onReceive(mContext, new Intent(Intent.ACTION_SCREEN_OFF));
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);

        verify(mAlarmManager)
                .setExactAndAllowWhileIdle(anyInt(), anyLong(), isA(PendingIntent.class));
    }

    @Test
    public void testTimerExpired() {
        mBroadcastReceiver.onReceive(mContext, new Intent(Intent.ACTION_SCREEN_OFF));
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);

        mTimerId = mQnsTimer.registerTimer(mMessage, 50);
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);

        assertTrue(mQnsTimer.getTimersInfo().contains(new QnsTimer.TimerInfo(mTimerId)));
        assertTrue(mQnsTimer.mHandler.hasMessages(EVENT_QNS_TIMER_EXPIRED));
        verify(mAlarmManager)
                .setExactAndAllowWhileIdle(anyInt(), anyLong(), isA(PendingIntent.class));

        waitForDelayedHandlerAction(mQnsTimer.mHandler, 40, 200);
        mBroadcastReceiver.onReceive(mContext, new Intent(ACTION_ALARM_TIMER_EXPIRED));
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);

        verify(mMessage).sendToTarget();
        assertFalse(mQnsTimer.mHandler.hasMessages(EVENT_QNS_TIMER_EXPIRED));
        verify(mAlarmManager).cancel(isA(PendingIntent.class));
    }

    @Test
    public void testMultipleTimerRegistered() {
        mBroadcastReceiver.onReceive(mContext, new Intent(Intent.ACTION_SCREEN_OFF));
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);
        TestLooper testLooper = new TestLooper();
        Handler h = new Handler(testLooper.getLooper());

        mQnsTimer.registerTimer(Message.obtain(h, 4), 300);
        mQnsTimer.registerTimer(Message.obtain(h, 3), 200);
        mQnsTimer.registerTimer(Message.obtain(h, 1), 50);
        mQnsTimer.registerTimer(Message.obtain(h, 1), 50);
        mQnsTimer.registerTimer(Message.obtain(h, 2), 100);
        mQnsTimer.registerTimer(Message.obtain(h, 2), 100);
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 320, 100);

        // alarm timer should update for shortest delay and since the minimum timer value is 10
        // secs for screen off condition, the alarm timer will not replace be replaced until new
        // timer requested for less than 10 secs.
        verify(mAlarmManager)
                .setExactAndAllowWhileIdle(anyInt(), anyLong(), isA(PendingIntent.class));

        // verify order of message received:
        Message msg = testLooper.nextMessage();
        assertEquals(1, msg.what);
        msg = testLooper.nextMessage();
        assertEquals(1, msg.what);
        msg = testLooper.nextMessage();
        assertEquals(2, msg.what);
        msg = testLooper.nextMessage();
        assertEquals(2, msg.what);
        msg = testLooper.nextMessage();
        assertEquals(3, msg.what);
        msg = testLooper.nextMessage();
        assertEquals(4, msg.what);
    }

    @Test
    public void testCancelOngoingAlarm() {
        mBroadcastReceiver.onReceive(mContext, new Intent(Intent.ACTION_SCREEN_OFF));
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);
        TestLooper testLooper = new TestLooper();
        Handler h = new Handler(testLooper.getLooper());

        int timerId1 = mQnsTimer.registerTimer(Message.obtain(h, 1), 61 * 1000);
        int timerId2 = mQnsTimer.registerTimer(Message.obtain(h, 2), 1000);
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);

        assertEquals(2, mQnsTimer.getTimersInfo().size());
        assertEquals(timerId2, mQnsTimer.getTimersInfo().peek().getTimerId());
        assertTrue(mQnsTimer.mHandler.hasMessages(EVENT_QNS_TIMER_EXPIRED));
        verify(mAlarmManager, times(2))
                .setExactAndAllowWhileIdle(anyInt(), anyLong(), isA(PendingIntent.class));

        mQnsTimer.unregisterTimer(timerId2);
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);
        assertEquals(1, mQnsTimer.getTimersInfo().size());
        assertEquals(timerId1, mQnsTimer.getTimersInfo().peek().getTimerId());
        assertTrue(mQnsTimer.mHandler.hasMessages(EVENT_QNS_TIMER_EXPIRED));

        verify(mAlarmManager, times(3))
                .setExactAndAllowWhileIdle(anyInt(), anyLong(), isA(PendingIntent.class));
    }

    @Test
    public void testAlarmOnLiteIdleModeMinDelay() {
        int setDelay = 20000;
        when(mPowerManager.isDeviceLightIdleMode()).thenReturn(true);
        mBroadcastReceiver.onReceive(
                sMockContext, new Intent(PowerManager.ACTION_DEVICE_LIGHT_IDLE_MODE_CHANGED));
        long delay = setupAlarmForDelay(setDelay);

        // assume 100ms as max delay in execution
        assertTrue(delay < 30000 && delay > 30000 - 100);
    }

    @Test
    public void testAlarmOnLiteIdleMode() {
        int setDelay = 40000;
        when(mPowerManager.isDeviceLightIdleMode()).thenReturn(true);
        mBroadcastReceiver.onReceive(
                sMockContext, new Intent(PowerManager.ACTION_DEVICE_LIGHT_IDLE_MODE_CHANGED));
        long delay = setupAlarmForDelay(setDelay);

        // assume 100ms as max delay in execution
        assertTrue(delay < setDelay && delay > setDelay - 100);
    }

    @Test
    public void testAlarmOnIdleModeMinDelay() {
        int setDelay = 50000;
        when(mPowerManager.isDeviceIdleMode()).thenReturn(true);
        mBroadcastReceiver.onReceive(
                sMockContext, new Intent(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED));
        long delay = setupAlarmForDelay(setDelay);

        // assume 100ms as max delay in execution
        assertTrue(delay < 60000 && delay > 60000 - 100);
    }

    @Test
    public void testAlarmOnIdleMode() {
        int setDelay = 70000;
        when(mPowerManager.isDeviceIdleMode()).thenReturn(true);
        mBroadcastReceiver.onReceive(
                sMockContext, new Intent(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED));
        long delay = setupAlarmForDelay(setDelay);

        // assume 100ms as max delay in execution
        assertTrue(delay < setDelay && delay > setDelay - 100);
    }

    private long setupAlarmForDelay(int setDelay) {
        mQnsTimer.registerTimer(mMessage, setDelay);

        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);
        ArgumentCaptor<Long> capture = ArgumentCaptor.forClass(Long.class);
        verify(mAlarmManager)
                .setExactAndAllowWhileIdle(anyInt(), capture.capture(), isA(PendingIntent.class));
        return capture.getValue() - SystemClock.elapsedRealtime();
    }

    @Test
    public void testAlarmInCallActiveState() {
        mQnsTimer.updateCallState(QnsConstants.CALL_TYPE_VOICE);
        int setDelay = 4000;
        when(mPowerManager.isDeviceIdleMode()).thenReturn(true);
        mBroadcastReceiver.onReceive(
                sMockContext, new Intent(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED));
        long delay = setupAlarmForDelay(setDelay);

        // assume 100ms as max delay in execution
        assertTrue(delay < setDelay && delay > setDelay - 100);
    }

    @Test
    public void testDeviceMovesToActiveState() {
        int setDelay = 30000;
        CountDownLatch latch = new CountDownLatch(2);
        HandlerThread ht = new HandlerThread("");
        ht.start();
        Handler tempHandler = spy(new Handler(ht.getLooper()));
        when(mPowerManager.isDeviceLightIdleMode()).thenReturn(true, false);
        mBroadcastReceiver.onReceive(sMockContext, new Intent(Intent.ACTION_SCREEN_OFF));
        mBroadcastReceiver.onReceive(
                sMockContext, new Intent(PowerManager.ACTION_DEVICE_LIGHT_IDLE_MODE_CHANGED));
        mQnsTimer.registerTimer(mMessage, setDelay);
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);
        verify(mAlarmManager)
                .setExactAndAllowWhileIdle(anyInt(), anyLong(), isA(PendingIntent.class));
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 5000);

        mQnsTimer.mHandler = tempHandler;
        mBroadcastReceiver.onReceive(
                sMockContext, new Intent(PowerManager.ACTION_DEVICE_LIGHT_IDLE_MODE_CHANGED));
        mBroadcastReceiver.onReceive(sMockContext, new Intent(Intent.ACTION_SCREEN_ON));
        waitForDelayedHandlerAction(mQnsTimer.mHandler, 10, 200);
        verify(mAlarmManager).cancel(isA(PendingIntent.class));

        // Handler should reset for the updated delay
        verify(tempHandler).removeMessages(EVENT_QNS_TIMER_EXPIRED);
        verify(tempHandler).sendEmptyMessageDelayed(anyInt(), anyLong());
        assertTrue(mQnsTimer.mHandler.hasMessages(EVENT_QNS_TIMER_EXPIRED));
        ht.quit();
    }
}