我有一项活动让我们说A
它在给定的应用程序中定义,并且它是相应的清单。此活动加载一个contentView,它只是通过静态R索引加载。让我们说R.layout.foo。那个布局碰巧有一个组件在那里看起来不是基础android attrs的东西。我看到当测试应用程序运行此活动时,主题和主题中的样式没有填充任何内容。
示例清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.foo.bar"
android:versionCode="1"
android:versionName="Test"
android:installLocation="auto">
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:description="@string/description"
android:theme="@style/Theme.Custom"
android:name=".MyApplicationObject">
<activity android:name=".activity.A"/>
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true" />
</manifest>
活动A
public class A extends Activity {
public void onCreate(Bundle a) {
setContentView(R.layout.foo);
}
}
布局,foo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.foo.bar.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
CustomView
public class CustomView extends RelativeLayout {
public CustomView(Context context, AttributeSet set) {
this(context, set, R.attr.CustomViewStyle);
}
public CustomView(Context c, AttributeSet set, int defStyle) {
super(c, set, defStyle);
TypedArray array = c.obtainStyledAttributes(set, R.styleable.CustomViewAttrs, defStyle, defStyle);
final int layout = array.getResourceId(R.styleable.CustomViewAttrs_layout, 0);
final Drawable icon = array.getDrawable(R.styleable.CustomViewAttrs_icon);
array.recycle();
if (layout == null) {
throw new IllegalStateException("WTF");
}
if (icon == null) {
throw new IllegalStateException("For real, WTF");
}
}
}
值文件中的一些资源
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Used to define a namespace for our special types -->
<declare-styleable name="CustomTypes">
<attr name="CustomViewStyle" format="reference"/>
</declare-styleable>
<!-- Used to define Attributes specific to our new View, "CustomView" -->
<declare-styleable name="CustomViewAttrs">
<attr name="layout" format="reference"/>
<attr name="anotherOne" format="reference"/>
</declare-styleable>
<!-- A usable style that we can reference later to pass to an instance of CustumView -->
<style name="CustomView">
<item name="layout">@layout/foo</item>
<item name="AnotherOne">@drawable/icon</item>
</style>
<!-- A Style to act as our Theme, referenced in our Manifest as the Theme for all activities -->
<style name="Theme.Custom" parent="android:Theme">
<item name="CustomViewStyle">@style/CustomView</item>
</style>
<resources>
这很好用,但是当我使用ActivityUnitTest加载A的实例时,TypedArray中的值是空的。
某些测试类
public class ActivityTester extends ActivityUnitTestCase<A> {
public ActivityTester() {
super(A.class);
}
public void testOne() {
Intent intent = new Intent(getInstrumentation().getTargetContext(), A.class);
// This fails with my IllegalStateException
startActivity(intent, null, null);
}
}
知道如何/如果目标应用程序获得它的清单解析?似乎这个主题甚至没有被加载。 startActivity()的文档声明它将以与context.startActivity()相同的方式启动活动。我没有看到这种情况发生,因为它似乎不尊重活动的清单数据。
答案 0 :(得分:2)
因此,经过大量时间花在尝试这样做之后,我发现个人最好的方法就是模仿主题本身。
public void setUp() {
ContextThemeWrapper context = new ContextThemeWrapper(getInstrumentation().getTargetContext(), R.style.Theme_MyTheme);
setActivityContext(context);
}
public void testOne() {
Intent intent = new Intent(getInstrumentation().getTargetContext(), A.class);
startActivity(intent, null, null);
}
就我个人而言,我不喜欢这样,因为它将单元测试实现与我希望偏向另一个角色的风格细微差别(如视觉设计师或图形艺术家)相结合,并增加了测试的可能性与将要发布的实现完全不同步。即某人更改清单文件,以便与Theme
关联的Activity
资源与正在测试的资源不同。这个TestCase类型看起来真的很短视,因为它可能会导致一堆像这样的坏事。我的意思是你最终会得到一堆ActivityUnitTestCase
实例来测试基本功能,然后是一些针对相同Activity
的Instrumentation测试用例,它只是启动它或者通过可能调用a的转换布局通胀。不是那时正在测试的真实应用程序。
答案 1 :(得分:-3)
您可以使用ActivityInstrumentationTestCase2来测试使用系统基础结构创建的活动:
import android.test.ActivityInstrumentationTestCase2;
public class ActivityTester extends ActivityInstrumentationTestCase2<ActivityTester> {
public ActivityTester() {
super(ActivityTester.class);
}
public void testOne() {
//Intent intent = new Intent(getInstrumentation().getTargetContext(), ActivityTester.class);
// This fails with my IllegalStateException
//startActivity(intent, null, null);
getActivity();
}
}
ActivityUnitTestCase用于单独的单元测试活动。
您的代码中还有一些其他错误,但我认为它们是复制的结果,消除了某些部分。