如何加载资源来测试使用robotium的项目?

时间:2014-05-31 10:17:28

标签: android robotium mapfragment android-instrumentation

我的Android应用程序中有一个布局文件 map_layout.xml 。其中包含 MapFragment 。我想在使用Robotium的测试项目中使用这个片段。目标是单击标记以进行测试。

我正在获取ID但无法获取片段实例。有谁可以提供帮助?

int id = activity.getResources().getIdentifier("googleMap","id",solo.getCurrentActivity().getPackageName());
Log.d(TAG, "My ID is..." + id);

Log.d(TAG, "Frag is..." + activity.getFragmentManager().findFragmentById(id));

1 个答案:

答案 0 :(得分:0)

尝试使用:

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import android.content.Context;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.MediumTest;

import com.robotium.solo.Solo;

public class FragmentTest extends ActivityInstrumentationTestCase2<YourActivity> {

    protected final CountDownLatch signal = new CountDownLatch (1);
    protected Context context;
    protected YourActivity activity;
    protected YourFragment fragment;
    protected Solo solo;

    public FragmentTest () {

        super(YourActivity.class);
    }

    @MediumTest
    public void testFragment () {

        int fragmentId = yourfragmentId;

        solo.waitForFragmentById (fragmentId);

        waitForTime (1); //wait till your fragment is loaded

        fragment = (YourFragment) activity.getFragmentManager ().findFragmentById (fragmentId);

        waitForTime (100);
    }

    @Override
    protected void setUp () throws Exception {

        super.setUp ();

        this.context = getInstrumentation ().getTargetContext ();

        prepareTestData ();

        activity = getActivity ();

        solo = new Solo (getInstrumentation (), activity);
    }

    protected void waitForTime (long seconds) {

        try {
            signal.await (seconds, TimeUnit.SECONDS);
        }
        catch (InterruptedException e) {
            e.printStackTrace ();
        }
    }

    protected void prepareTestData () {

    }
}