18

私は次の活動をしています:

package codeguru.startactivityforresult;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class StartActivityForResult extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        this.startButton = (Button) this.findViewById(R.id.start_button);
        this.startButton.setOnClickListener(onStart);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        int result = data.getIntExtra(StartActivityForResult.this.getString(R.string.result), -1);
        String msg = "requestCode=" + requestCode + ", resultCode=" + resultCode + ", result=" + result;
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

    private View.OnClickListener onStart = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(StartActivityForResult.this, ChildActivity.class);

            StartActivityForResult.this.startActivityForResult(intent, R.id.child_request);
        }
    };
    private Button startButton = null;
}

そして、次のJUnitテスト:

package codeguru.startactivityforresult;

import android.app.Activity;
import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.widget.Button;
import junit.framework.Assert;

public class StartActivityForResultTest extends ActivityInstrumentationTestCase2<StartActivityForResult> {

    public StartActivityForResultTest() {
        super(StartActivityForResult.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();

        this.setActivityInitialTouchMode(false);

        this.activity = this.getActivity();
        this.startButton = (Button) this.activity.findViewById(R.id.start_button);
    }

    @Override
    public void tearDown() throws Exception {
        this.activity.finish();

        super.tearDown();
    }

    @UiThreadTest
    public void testStartButtonOnClick() {
        Assert.assertTrue(this.startButton.performClick());

        Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, null);
        Assert.assertNotNull(result);

        Instrumentation.ActivityMonitor am = new Instrumentation.ActivityMonitor(ChildActivity.class.getName(), result, true);
        Assert.assertNotNull(am);

        Activity childActivity = this.getInstrumentation().waitForMonitorWithTimeout(am, TIME_OUT);
        Assert.assertNotNull(childActivity);

        Assert.fail("How do I check that StartActivityForResult correctly handles the returned result?");
    }
    private Activity activity = null;
    private Button startButton = null;
    private static final int TIME_OUT = 5 * 1000; // 5 seconds
}

ご覧のとおり、とを使用して結果をモックアップする方法を理解しましInstrumentation.ActivityResultInstrumentation.ActivityMonitorStartActivityForResult.onActivityResult()この結果が適切に処理されることを確認するにはどうすればよいですか?

4

3 に答える 3

13

インテントフレームワークを使用して、アクティビティの結果をモックします

intending(hasComponent(DummyActivity.class.getName())).respondWith(new ActivityResult(resultCode, dataIntent));
rule.getActivity().startActivityForResult(new Intent(context,DummyActivity.class));

アクティビティ結果ロジックを確認する

于 2017-09-20T20:07:34.013 に答える
12

テストクラスでonActivityResult()をテストするには、次のことを行う必要があります。

  1. ChildActivityの作成をキャッチし、模擬ActivityResultを再調整するActivityMonitorを作成します。
  2. 結果のChildActivityを開始するボタンクリックをシミュレートします。
  3. ステータスとモックActivityResultに対してアサーションを実行します。

StartActivityForResultのサンプル:

public class StartActivityForResult extends Activity {
  private boolean activityResultIsReturned = false;
  private String activityResult = null;

  ... ...

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    activityResultIsReturned = true;
    activityResult = data.getStringExtra("result");

    ... ...
  }

  ... ...
}

StartActivityForResultTestのサンプル:

public class StartActivityForResultTest extends ActivityInstrumentationTestCase2<StartActivityForResult> {
  ... ...

  public void testOnActivityResult() {
    // Get current Activity and check initial status:
    StartActivityForResult myActivity = getActivity();
    assertFalse(myActivity.getActivityResultIsReturned());
    assertNull(myActivity.getActiityResult());

    // Mock up an ActivityResult:
    Intent returnIntent = new Intent();
    returnIntent.putExtra("result", "This is the result");
    Instrumentation.ActivityResult activityResult = new Instrumentation.ActivityResult(Activity.RESULT_OK, returnIntent);

    // Create an ActivityMonitor that catch ChildActivity and return mock ActivityResult:
    Instrumentation.ActivityMonitor activityMonitor = getInstrumentation().addMonitor(ChildActivity.class.getName(), activityResult , true);

    // Simulate a button click that start ChildActivity for result:
    final Button button = (Button) myActivity.findViewById(com.company.R.id.open_next_activity);
    myActivity.runOnUiThread(new Runnable() {
      @Override
      public void run() {
        // click button and open next activity.
        button.performClick();
      }
    });

    // Wait for the ActivityMonitor to be hit, Instrumentation will then return the mock ActivityResult:
    ChildActivity childActivity = getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 5);

    // How do I check that StartActivityForResult correctly handles the returned result?
    assertTrue(myActivity.getActivityResultIsReturned());
    assertEqual(myActivity.getActiityResult(), "This is the result");
  }

  ... ...
}
于 2012-10-25T02:06:42.480 に答える
-4

以下のサイトを参照してください。それはあなたがあなたの問題を解決するのを助けるでしょう。 http://saigeethamn.blogspot.in/2009/08/android-developer-tutorial-for_31.html

http://android-er.blogspot.in/2011/08/return-result-to-onactivityresult.html

http://www.mubasheralam.com/tutorials/android/how-start-new-activity-and-getting-results

于 2012-10-24T03:27:46.223 に答える