Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent resolved to different process when running Unit Test in Android

I have a small application that uses two activities. Both the activities inherit from MapActivity and display a map (com.google.android.maps).

Since the Android Google Map documentation says

Only one MapActivity is supported per process. Multiple MapActivities running simultaneously are likely to interfere in unexpected and undesired ways.

I modified my manifest to run the two activities in two different processes (I have removed some lines to make it short):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Light">

    <uses-library android:name="com.google.android.maps" />

    <activity 
        android:name=".Activity1"
        android:process=".Activity1">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>Unit
    </activity>

    <activity
        android:name=".Activity2"
        android:process=".Activity2">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

</application>

<uses-sdk android:minSdkVersion="8" />
</manifest> 

Now the application runs fine but I have problems when I what to run Unit Tests on both the Activities. For example:

package com.example.myapp;
public class Activity1Test extends ActivityInstrumentationTestCase2<Activity1> {

    Activity1 mActivity;

    public Activity1Test() {
        super("com.example.myapp.Activity1", Activity1.class);
    }

    @Override 
    protected void setUp() throws Exception {
        super.setUp();
        setActivityInitialTouchMode(false);
        setActivityIntent(new Intent());
        mActivity = getActivity();  //An exception is thrown at this line
    }
}

When I call the getActivity() method an exception is thrown:

java.lang.RuntimeException: Intent in process com.example.myapp resolved to different process .Activity1: Intent { flg=0x10000000 cmp=com.example.myapp/.Activity1 }
at android.app.Instrumentation.startActivitySync(Instrumentation.java:377)
at android.test.InstrumentationTestCase.launchActivityWithIntent(InstrumentationTestCase.java:119)
at android.test.ActivityInstrumentationTestCase2.getActivity(ActivityInstrumentationTestCase2.java:100)
at com.example.myapp.Activity1Test.setUp(Activity1Test.java:28)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)

Is there a way to make the Unit Test to "resolve" the correct process?

like image 240
Michele Avatar asked Nov 13 '22 21:11

Michele


1 Answers

Instrumentation runs all of your application components in the same process.

enter image description here

like image 164
Diego Torres Milano Avatar answered Dec 17 '22 14:12

Diego Torres Milano