从InstrumentationTestCase创建通知

Mis*_*ith 11 java notifications android unit-testing android-instrumentation

我想从单元测试中测试通知是否能够从资产中播放自定义声音.测试并不是为了验证任何东西,我把它写成一种快速的方式来演示一个功能而不会混淆主应用程序代码.

所以在测试项目中,我在里面添加了一个wav文件/res/raw.我将在通知构建器中使用此URL:

Uri path = Uri.parse("android.resource://<main app package name>/testsound.wav");
Run Code Online (Sandbox Code Playgroud)

该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());
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

我已将此异常跟踪到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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

显然,异常与自定义声音没有任何关系,但我们正在创建一个通知InstrumentationTestCase.

有没有办法测试这个?我记得曾经创建AndroidTestCase了过去的通知,但如果我这样做,那么我将无法访问测试wav文件.我可以使用wav创建一个jar并将jar放在测试项目的lib文件夹中,但是这将隐藏文件,如果将来需要替换它,其他程序员可能很难找到它.

Mis*_*ith 0

我想出了如何让声音从AndroidTestCase. wav 文件已添加到测试项目的原始文件夹中,但未按预期包含在主项目中。

通知的 URL 是这样的:

Uri path = Uri.parse("android.resource://<test project package name>/" + R.raw.air_horn);
Run Code Online (Sandbox Code Playgroud)

生成器的获取方式如下:

NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext());
Run Code Online (Sandbox Code Playgroud)