\frameworks\base\services\tests\servicestests目录下有一个apk其中包括了很多对service模块的单元测试:
但是没有对AlarmManager进行单元测试,因此准备将cts的AlarmManager的测试,移植到这里,下面是具体的步骤:
将cts/tests/tests/app/src/android/app/cts/AlarmManagerTest.java文件移到frameworks\base\services\tests\servicestests\src\com\android\server目录下并且有两个连带的类
PollingCheck.java和MockAlarmReceiver.java也移植过来,在\frameworks\base\services\tests\servicestests目录下mm编译,会变出来一个FrameworksServicesTests.apk。
接下来将这个apk,adb install 到手机上。
然后使用命令可以单独跑AlarmManagerTest里面的测试用例。
adb shell am instrument -w -e class com.android.server.AlarmManagerTest com.android.frameworks.servicestests/android.test.InstrumentationTestRunner
如果跑所有用例是:
adb shell am instrument -w com.android.frameworks.servicestests/android.test.InstrumentationTestRunner
其中com.android.frameworks.servicestests代表的是包名
结果可以使用logcat查看。如果出现failed,自己看看是什么原因。
下面看看AlarmManagerTest 这个类的代码:下面省略了很多代码
<span style="background-color: rgb(255, 255, 51);">package com.android.server;//包名因为从cts移植过来的需要修改</span>
import android.app.AlarmManager;
import android.app.AlarmManager.AlarmClockInfo;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.SystemClock;
import android.test.AndroidTestCase;
import android.util.Log;
import android.test.MoreAsserts;
public class AlarmManagerTest extends AndroidTestCase {
public static final String MOCKACTION = "android.app.AlarmManagerTest.TEST_ALARMRECEIVER";
public static final String MOCKACTION2 = "android.app.AlarmManagerTest.TEST_ALARMRECEIVER2";
private AlarmManager mAm;
private Intent mIntent;
private PendingIntent mSender;
private Intent mIntent2;
private PendingIntent mSender2;
/*
* The default snooze delay: 5 seconds
*/
private static final long SNOOZE_DELAY = 5 * 1000L;
private long mWakeupTime;
private MockAlarmReceiver mMockAlarmReceiver;
private MockAlarmReceiver mMockAlarmReceiver2;
private static final int TIME_DELTA = 1000;
private static final int TIME_DELAY = 10000;
private static final int REPEAT_PERIOD = 60000;
// Receiver registration/unregistration between tests races with the system process, so
// we add a little buffer time here to allow the system to process before we proceed.
// This value is in milliseconds.
private static final long REGISTER_PAUSE = 250;
private static final long TEST_WINDOW_LENGTH = 5 * 1000L;
private static final long TEST_ALARM_FUTURITY = 6 * 1000L;
private static final long FAIL_DELTA = 50;
private static final long NUM_TRIALS = 5;
private static final long MAX_NEAR_DELIVERIES = 2;
@Override
protected void setUp() throws Exception {
super.setUp();
mAm = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
mIntent = new Intent(MOCKACTION)
.addFlags(Intent.FLAG_RECEIVER_FOREGROUND | Intent.FLAG_RECEIVER_REGISTERED_ONLY);
mSender = PendingIntent.getBroadcast(mContext, 0, mIntent, 0);
mMockAlarmReceiver = new MockAlarmReceiver(mIntent.getAction());
mIntent2 = new Intent(MOCKACTION2)
.addFlags(Intent.FLAG_RECEIVER_FOREGROUND | Intent.FLAG_RECEIVER_REGISTERED_ONLY);
mSender2 = PendingIntent.getBroadcast(mContext, 0, mIntent2, 0);
mMockAlarmReceiver2 = new MockAlarmReceiver(mIntent2.getAction());
IntentFilter filter = new IntentFilter(mIntent.getAction());
mContext.registerReceiver(mMockAlarmReceiver, filter);
IntentFilter filter2 = new IntentFilter(mIntent2.getAction());
mContext.registerReceiver(mMockAlarmReceiver2, filter2);
Thread.sleep(REGISTER_PAUSE);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
mContext.unregisterReceiver(mMockAlarmReceiver);
mContext.unregisterReceiver(mMockAlarmReceiver2);
Thread.sleep(REGISTER_PAUSE);
}
public void testSetTypes() throws Exception {
// TODO: try to find a way to make device sleep then test whether
// AlarmManager perform the expected way
// test parameter type is RTC_WAKEUP
mMockAlarmReceiver.setAlarmedFalse();
mWakeupTime = System.currentTimeMillis() + SNOOZE_DELAY;
mAm.setExact(AlarmManager.RTC_WAKEUP, mWakeupTime, mSender);
new PollingCheck(SNOOZE_DELAY + TIME_DELAY) {
@Override
protected boolean check() {
return mMockAlarmReceiver.alarmed;
}
}.run();
assertEquals(mMockAlarmReceiver.rtcTime, mWakeupTime, TIME_DELTA);
// test parameter type is RTC
mMockAlarmReceiver.setAlarmedFalse();
mWakeupTime = System.currentTimeMillis() + SNOOZE_DELAY;
mAm.setExact(AlarmManager.RTC, mWakeupTime, mSender);
new PollingCheck(SNOOZE_DELAY + TIME_DELAY) {
@Override
protected boolean check() {
return mMockAlarmReceiver.alarmed;
}
}.run();
assertEquals(mMockAlarmReceiver.rtcTime, mWakeupTime, TIME_DELTA);
// test parameter type is ELAPSED_REALTIME
mMockAlarmReceiver.setAlarmedFalse();
mWakeupTime = SystemClock.elapsedRealtime() + SNOOZE_DELAY;
mAm.setExact(AlarmManager.ELAPSED_REALTIME, mWakeupTime, mSender);
new PollingCheck(SNOOZE_DELAY + TIME_DELAY) {
@Override
protected boolean check() {
return mMockAlarmReceiver.alarmed;
}
}.run();
assertEquals(mMockAlarmReceiver.elapsedTime, mWakeupTime, TIME_DELTA);
// test parameter type is ELAPSED_REALTIME_WAKEUP
mMockAlarmReceiver.setAlarmedFalse();
mWakeupTime = SystemClock.elapsedRealtime() + SNOOZE_DELAY;
mAm.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, mWakeupTime, mSender);
new PollingCheck(SNOOZE_DELAY + TIME_DELAY) {
@Override
protected boolean check() {
return mMockAlarmReceiver.alarmed;
}
}.run();
assertEquals(mMockAlarmReceiver.elapsedTime, mWakeupTime, TIME_DELTA);
}
。。。。。。
这样讲cts中AlarmManager的测试代码移植到service中来了。