我有一个视图,在它的布局中包含一个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不能提供
MediaRouteButton
的影子(由com.android.support:mediarouter-v7:25.3.0提供),我需要自己为这个类创建一个影子。我按照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)
很不幸,我用这个配置也遇到了相同的错误。
请问如何创建一个最小的Shadow,使得视图能够加载?
更新:这里有一个演示问题的样例应用程序:https://github.com/mykwillis/android-mediaroutebutton-test