简体   繁体   中英

ActivityUnitTestCase and Google Maps Android API v2

I have an Android project that uses external library project for Google Maps Android API v2 ( google-play-services_lib ).

I also have testing project, which tests my project form first point.

package third.task.android.test;

import third.task.android.CompassActivity;
import third.task.android.R;
import android.app.Activity;
import android.content.Intent;
import android.test.ActivityUnitTestCase;

public class CompassActivityTest extends ActivityUnitTestCase<CompassActivity> {

    public CompassActivityTest() {
        super(CompassActivity.class);
    }

    public void testTruth() {
        assertTrue(true);
    }

    public void testPreConditions() {
        startActivity(new Intent(getInstrumentation().getTargetContext(), CompassActivity.class), null, null);
        Activity activity = getActivity();
        assertNull(activity.findViewById(R.id.compasRadar));
    }
}

Here is my failure trace, which actually shows error about loading class="com.google.android.gms.maps.MapFragment"

android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
at android.app.Activity.setContentView(Activity.java:1924)
at third.task.android.CompassActivity.onCreate(CompassActivity.java:55)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:158)
at third.task.android.test.CompassActivityTest.testPreConditions(CompassActivityTest.java:19)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:177)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1608)
Caused by: java.lang.IllegalStateException: Not on the main thread
at maps.am.r.b(Unknown Source)
at maps.ar.d.b(Unknown Source)
at maps.y.ae.setCompassEnabled(Unknown Source)
at maps.y.ae.a(Unknown Source)
at maps.y.ae.a(Unknown Source)
at maps.y.bu.a(Unknown Source)
at maps.y.p.onCreateView(Unknown Source)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$Stub.onTransact(IMapFragmentDelegate.java:107)
at android.os.Binder.transact(Binder.java:326)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
at com.google.android.gms.maps.MapFragment$b.onCreateView(Unknown Source)
at com.google.android.gms.internal.d$4.a(Unknown Source)
at com.google.android.gms.internal.d.a(Unknown Source)
at com.google.android.gms.internal.d.onCreateView(Unknown Source)
at com.google.android.gms.maps.MapFragment.onCreateView(Unknown Source)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:807)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1013)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1112)
at android.app.Activity.onCreateView(Activity.java:4857)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
... 24 more

When I remove code responsible for displaying map in layout and also map-dependent code snippets, everything works fine, but I have to test my application with full functionality including maps. Is there any way to do this?

How about ActivityInstrumentationTestCase2 ? There is no need to start activity, you can simple use getActivity() . If you need to use custom start intent you can specify it using setActivityIntent(i) in setUp() . I have tested its work:

package com.grt_team.airplanemode.test;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;

import com.grt_team.airplanemode.MainActivity;
import com.grt_team.airplanemode.R;

public class Test extends ActivityInstrumentationTestCase2<MainActivity> {

    public Test() {
        super(MainActivity.class);
    }

    public void testTruth() {
        assertTrue(true);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        //setActivityIntent(i);
    }

    public void testPreConditions() {
        Activity activity = getActivity();

        assertNotNull(activity);
        assertNull(activity.findViewById(0));
        assertNotNull(activity.findViewById(R.id.map_fragment));
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM