3

here is some sample code, it seems that it doesn't work for me.

public class CropImageTest extends ActivityInstrumentationTestCase2<CropImage> {

    private Instrumentation mInstrumentation;
    private CropImage mActivity;
    private String filename = "/mnt/sdcard/DCIM/Camera/2011-05-12 09.22.56.jpg";
    private int aspectX = 1;
    private int aspectY = 1;
    private boolean scale = true;

    public CropImageTest() {
        super("hk.com.novare.android.cropimage", CropImage.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        this.mInstrumentation = getInstrumentation();
        Intent i = new Intent(mInstrumentation.getContext(), CropImage.class);

        i.putExtra("image-path", filename);
        i.putExtra("aspectY", aspectY);
        i.putExtra("aspectX", aspectX);
        i.putExtra("scale", scale);
        setActivityIntent(i);
        mActivity = this.getActivity();  
    }

    public void testExtras() {
        String str = "";
        str = mActivity.getIntent().getStringExtra("image-path");
        assertEquals(filename, str);
    }
}

error encountered:

Unable to resolve activity for: Intent ( has Extras )

my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          android:versionCode="1"
          android:versionName="1.0"
          package="hk.com.novare.android.cropimage.tests">

    <uses-sdk android:minSdkVersion="8"/>
    <instrumentation
            android:targetPackage="hk.com.novare.android.cropimage"
            android:name="android.test.InstrumentationTestRunner"/>
    <application
            android:icon="@drawable/icon"
            android:label="@string/app_name">
        <uses-library android:name="android.test.runner"/>
    </application>
</manifest>

I encountered the error above even if I set the constructor -> super's string(a package ) same as the one that I've indicated within this test project's manifest.xml. please help me.

JJD
  • 50,076
  • 60
  • 203
  • 339

3 Answers3

2

I think all you need to do is use

Intent i = new Intent();

See, for instance, the answers on this thread.

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

While creating the intent you are passing getInstrumentation().getContext() which is the test app's context.

what you want is getInstrumentation().getTargetContext() which is the target application's context.

Matt Accola
  • 4,090
  • 4
  • 28
  • 37
0

When constructing the Intent, I think instead of mInstrumentation.getContext() you want to call getActivity(). I'm assuming that CropImage class is actually in the package under test, not the test package.

cyngus
  • 1,763
  • 13
  • 14
  • yep, but it is the one that should get and process the extras, so it doesn't make sense. and as the documentation indicates, you must not call getActivity() method before setActivityIntent()! :(( – Mark Joseph Del Rosario Jun 27 '11 at 00:20