How does Android ActivityUnitTestCase enable the target application AndroidManifest.xml?

I have activity. Let them say A

it is defined in this appendix and corresponds to the relevant manifest. This activity loads content that it simply loads through the static index R. Let's say R.layout.foo. There is a component in this layout that looks at what is not a basic android. I see that when the Test application launches this action, the theme and styles within the theme are not filled with anything.

Manifest example

   <?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>

Activity A

public class A extends Activity {
   public void onCreate(Bundle a) {
     setContentView(R.layout.foo);
   }
}

Layout, 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");
    }
  }
}

Some resources in the value file

<?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>

This works fine, but when I use ActivityUnitTest to load an instance of A, the values ​​inside TypedArray are empty.

Some test classes

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(). , , , , .

+3
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);
   }

, unit test , (, ) , . - , Theme, Activity, , . TestCase , , . , ActivityUnitTestCase, , Instrumentation Activity, , . .

+2

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 unit test .

, , , .

-3

Source: https://habr.com/ru/post/1785096/


All Articles