我想从单元测试中测试通知是否能够从资产中播放自定义声音.测试并不是为了验证任何东西,我把它写成一种快速的方式来演示一个功能而不会混淆主应用程序代码.
所以在测试项目中,我在里面添加了一个wav文件/res/raw
.我将在通知构建器中使用此URL:
Uri path = Uri.parse("android.resource:///testsound.wav");
该URL应该根据我在SO中阅读的问题工作.我们假设它有效.
现在因为我不想将测试wav文件包含在主项目的/res/raw
文件夹中但是在测试项目中,我不得不进行单元测试扩展,InstrumentationTestCase
以便我可以访问测试项目中的资源.
这是代码:
NotificationCompat.Builder builder = new NotificationCompat.Builder(getInstrumentation().getContext()); ... builder.setSound(path, AudioManager.STREAM_NOTIFICATION); ... NotificationManager notificationManager = (NotificationManager) getInstrumentation().getContext().getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID, builder.build());
该notify
调用抛出以下异常:
java.lang.SecurityException: Calling uid 10198 gave packagewhich 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)
我已将此异常跟踪到NotificationManagerService
课程:
void checkCallerIsSystemOrSameApp(String pkg) { int uid = Binder.getCallingUid(); if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) { return; } try { ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo( pkg, 0, UserHandle.getCallingUserId()); if (!UserHandle.isSameApp(ai.uid, uid)) { throw new SecurityException("Calling uid " + uid + " gave package" + pkg + " which is owned by uid " + ai.uid); } } catch (RemoteException re) { throw new SecurityException("Unknown package " + pkg + "\n" + re); } }
显然,异常与自定义声音没有任何关系,但我们正在创建一个通知InstrumentationTestCase
.
有没有办法测试这个?我记得曾经创建AndroidTestCase
了过去的通知,但如果我这样做,那么我将无法访问测试wav文件.我可以使用wav创建一个jar并将jar放在测试项目的lib文件夹中,但是这将隐藏文件,如果将来需要替换它,其他程序员可能很难找到它.