微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将Robolectric与android.support.v7.app.MediaRouteButton一起使用

我有一个View,其中包含 android.support.v7.app.MediaRouteButton的布局实例.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="mykwillis.com.mediaroutebutton.MainActivity">

    <android.support.v7.app.MediaRouteButton
        android:id="@+id/video_layer_chromecast_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"/>

</android.support.constraint.ConstraintLayout>

我已经创建了一个基本的Robolectric测试的开始,它只是构建和创建MainActivity:

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class MainActivityTest {
    @Test
    public void loadActivity() {
        MainActivity activity = Robolectric.buildActivity(MainActivity.class).create().get();
    }
}

当我尝试运行此测试时,收到以下错误

android.view.InflateException: XML file build/intermediates/res/merged/debug/layout/activity_main.xml line #-1 (sorry,not yet implemented): XML file build/intermediates/res/merged/debug/layout/activity_main.xml line #-1 (sorry,not yet implemented): Error inflating class android.support.v7.app.MediaRouteButton

Caused by: android.view.InflateException: XML file build/intermediates/res/merged/debug/layout/activity_main.xml line #-1 (sorry,not yet implemented): Error inflating class android.support.v7.app.MediaRouteButton

深入研究,当Robolectric尝试加载MediaRouteButton的样式信息时,它似乎失败了.相关的堆栈跟踪似乎是这样的:

Caused by: java.lang.classCastException: org.robolectric.res.AttrData cannot be cast to org.robolectric.res.StyleData at
org.robolectric.shadows.ShadowAssetManager.resolveStyle(ShadowAssetManager.java:617) at 
org.robolectric.shadows.ShadowAssetManager.buildTypedValue(ShadowAssetManager.java:742) at 
org.robolectric.shadows.ShadowAssetManager.attrsToTypedArray(ShadowAssetManager.java:793) at 
org.robolectric.shadows.ShadowResourcesImpl$ShadowThemeImpl.obtainStyledAttributes(ShadowResourcesImpl.java:177) at android.content.res.ResourcesImpl$ThemeImpl.obtainStyledAttributes(ResourcesImpl.java) at 
android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java:1440) at android.content.Context.obtainStyledAttributes(Context.java:587) at 
android.support.v7.app.MediaRouterThemeHelper.getThemeColor(MediaRouterThemeHelper.java:169) at 
android.support.v7.app.MediaRouterThemeHelper.getControllerColor(MediaRouterThemeHelper.java:94) at 
android.support.v7.app.MediaRouterThemeHelper.createThemedContext(MediaRouterThemeHelper.java:62) at android.support.v7.app.MediaRouteButton.(MediaRouteButton.java) at 
android.view.LayoutInflater.createView(LayoutInflater.java:645)

我使用以下依赖项:

dependencies {
    compile filetree(dir: 'libs',include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.3.0'
    compile 'com.android.support:mediarouter-v7:25.3.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    testCompile "org.robolectric:robolectric:3.3.1"
    testCompile 'org.khronos:opengl-api:gl1.1-android-2.1_r1'   // https://github.com/robolectric/robolectric/issues/1932
}

我认为这意味着我对Robolectric提供了一个Shadow for MediaRouteButton(由com.android.support:mediarouter-v7:25.3.0提供)的运气不好,而且我需要自己为这个类创建一个Shadow.我在http://robolectric.org/extending/跟随指南试图创建一个简单的阴影,至少可以让我的视图加载,但无济于事.这是我创造的:

@Implements(android.support.v7.app.MediaRouteButton.class)
public class MediaRouteButtonShadow extends ShadowView {
    public void __constructor__(Context context,AttributeSet attributeSet,int defStyle) {
    }
}

然后我将@Config语句更新为:

@Config(constants = BuildConfig.class,shadows = MediaRouteButtonShadow.class)

不幸的是,我在这个配置中得到了同样的错误.

有关如何创建允许视图加载的最小阴影的任何指针?

更新:演示此问题的示例应用程序位于:https://github.com/mykwillis/android-mediaroutebutton-test

解决方法

此问题已跟踪 here,已使用 Robolectric version 4.0中的新功能解决.

更新依赖项并将testOptions部分添加到build.gradle:

dependencies {
    // ...
    testCompile "org.robolectric:robolectric:4.0"
}
testOptions {
    unitTests {
        includeAndroidResources = true
    }
}

并将此行添加到gradle.properties:

enableunitTestBinaryResources=true

还要确保您已更新到最新的gradle插件.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐