1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.server.cts.device.batterystats; 17 18 import static org.junit.Assert.assertTrue; 19 20 import android.app.AlarmManager; 21 import android.app.PendingIntent; 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.os.SystemClock; 27 import android.support.test.InstrumentationRegistry; 28 import android.support.test.runner.AndroidJUnit4; 29 import android.util.Log; 30 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 import java.util.concurrent.CountDownLatch; 35 import java.util.concurrent.TimeUnit; 36 37 @RunWith(AndroidJUnit4.class) 38 public class BatteryStatsAlarmTest { 39 private static final String TAG = "BatteryStatsAlarmTest"; 40 41 /** 42 * Set and fire a wakeup alarm 3 times. 43 */ 44 @Test 45 public void testAlarms() throws Exception { 46 final int NUM_ALARMS = 3; 47 48 final Context context = InstrumentationRegistry.getContext(); 49 50 final Intent intent = new Intent("com.android.server.cts.device.batterystats.ALARM"); 51 final IntentFilter inf = new IntentFilter(intent.getAction()); 52 53 final CountDownLatch latch = new CountDownLatch(NUM_ALARMS); 54 55 context.registerReceiver(new BroadcastReceiver() { 56 @Override 57 public void onReceive(Context context, Intent intent) { 58 Log.i(TAG, "Received: " + intent); 59 latch.countDown(); 60 }}, inf); 61 62 final AlarmManager alm = context.getSystemService(AlarmManager.class); 63 for (int i = 0; i < NUM_ALARMS; i++) { 64 alm.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, 65 SystemClock.elapsedRealtime() + (i + 1) * 1000, 66 PendingIntent.getBroadcast(context, i, intent, 0)); 67 } 68 assertTrue("Didn't receive all broadcasts.", latch.await(60 * 1000, TimeUnit.SECONDS)); 69 } 70 } 71