Java 从InstrumentationTestCase创建通知
我想通过单元测试来测试通知是否能够播放来自资产的自定义声音。 这个测试并不是为了验证任何东西,我写它是为了在不干扰主要应用程序代码的情况下快速演示一个功能 所以在测试项目中,我在Java 从InstrumentationTestCase创建通知,java,android,unit-testing,notifications,android-instrumentation,Java,Android,Unit Testing,Notifications,Android Instrumentation,我想通过单元测试来测试通知是否能够播放来自资产的自定义声音。 这个测试并不是为了验证任何东西,我写它是为了在不干扰主要应用程序代码的情况下快速演示一个功能 所以在测试项目中,我在/res/raw中添加了一个wav文件。我将在通知生成器中使用此URL: Uri path = Uri.parse("android.resource://<main app package name>/testsound.wav"); notify调用引发以下异常: java.lang.Secur
/res/raw
中添加了一个wav文件。我将在通知生成器中使用此URL:
Uri path = Uri.parse("android.resource://<main app package name>/testsound.wav");
notify
调用引发以下异常:
java.lang.SecurityException: Calling uid 10198 gave package <main app package name> which is owned by uid 10199
at android.os.Parcel.readException(Parcel.java:1540)
at android.os.Parcel.readException(Parcel.java:1493)
at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:611)
at android.app.NotificationManager.notify(NotificationManager.java:187)
at android.app.NotificationManager.notify(NotificationManager.java:140)
...
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1873)
显然,异常与定制声音无关,但与我们正在从InstrumentationTestCase
创建通知这一事实有关
有没有办法测试这个?我记得过去曾从
AndroidTestCase
创建过通知,但如果我这样做,我将无法访问测试wav文件。我可以用wav创建一个jar,并将其放入测试项目的lib文件夹中,但这会隐藏文件,其他程序员可能很难找到它,如果他们将来需要替换它的话。事实上,我对这个问题有点困惑。所以我写了一个小型仪器测试
为了断言,我将测试标记为仅在API23上运行(getActiveNotifications
以前似乎不可用),但它在较旧的API上也可以正常工作
诀窍是使用getTargetContext()
而不是getContext()
:
public final class MainActivityTest extends ActivityUnitTestCase<MainActivity> {
public MainActivityTest() {
super(MainActivity.class);
}
@TargetApi(Build.VERSION_CODES.M)
public void testSendNotification() {
final NotificationManager manager = (NotificationManager) getInstrumentation().getTargetContext().getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(42);
assertEquals(0, manager.getActiveNotifications().length);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(getInstrumentation().getTargetContext());
builder.setContentTitle("Notification test")
.setAutoCancel(true)
.setContentText("Hello, Mister Smith")
.setSmallIcon(R.drawable.ic_launcher_notification);
manager.notify(42, builder.build());
assertEquals(1, manager.getActiveNotifications().length);
}
}
public final类MainActivityTest扩展了ActivityUnitTestCase{
公共维护活动测试(){
超级(MainActivity.class);
}
@TargetApi(Build.VERSION\u CODES.M)
公共无效testSendNotification(){
final NotificationManager manager=(NotificationManager)getInstrumentation().getTargetContext().getSystemService(Context.NOTIFICATION_服务);
经理:取消(42);
assertEquals(0,manager.getActiveNotifications().length);
final NotificationCompat.Builder=new NotificationCompat.Builder(getInstrumentation().getTargetContext());
builder.setContentTitle(“通知测试”)
.setAutoCancel(真)
.setContentText(“你好,史密斯先生”)
.setSmallIcon(R.drawable.ic_启动器_通知);
manager.notify(42,builder.build());
assertEquals(1,manager.getActiveNotifications().length);
}
}
它就像一个符咒:
希望有帮助。我想到了如何从
AndroidTestCase
中让声音正常工作。
wav文件已添加到测试项目的原始文件夹中,未按预期包含在主项目中
通知的URL如下所示:
Uri path = Uri.parse("android.resource://<test project package name>/" + R.raw.air_horn);
是的,这很有效。wav文件按预期播放。唯一的缺点是您需要一个来自主项目的活动。@MisterSmith,但您一直在询问的是仪表测试,而不是单元测试。你的回答没有反映你所问的问题。是的,很抱歉。我假设为了访问测试项目中的资源,我必须使用仪器,如中所述。但事实证明,它是通过常规的单元测试实现的。无论如何,谢谢你。
public final class MainActivityTest extends ActivityUnitTestCase<MainActivity> {
public MainActivityTest() {
super(MainActivity.class);
}
@TargetApi(Build.VERSION_CODES.M)
public void testSendNotification() {
final NotificationManager manager = (NotificationManager) getInstrumentation().getTargetContext().getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(42);
assertEquals(0, manager.getActiveNotifications().length);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(getInstrumentation().getTargetContext());
builder.setContentTitle("Notification test")
.setAutoCancel(true)
.setContentText("Hello, Mister Smith")
.setSmallIcon(R.drawable.ic_launcher_notification);
manager.notify(42, builder.build());
assertEquals(1, manager.getActiveNotifications().length);
}
}
Uri path = Uri.parse("android.resource://<test project package name>/" + R.raw.air_horn);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext());