Question

I've been trying to unit test a simple calculator app with ActivityUnitTestCase. The code for my calculator app

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_page);

    disp = (TextView) findViewById(R.id.disp);

    n1 = (EditText) findViewById(R.id.n1);
    n2 = (EditText) findViewById(R.id.n2);

    calc = (Button) findViewById(R.id.calc);


    calc.setOnClickListener(this);

}
public void onClick(View v) {
    double num1 = Double.valueOf(n1.getText().toString());
    double num2 = Double.valueOf(n2.getText().toString());

    Intent in = new Intent(this,CalcActivity.class);
    in.putExtra("num1",num1);
    in.putExtra("num2", num2);
    startActivity(in);
}

I want to be able to perform some operations on the two numbers and then send it through the intent. My question is, how do you examine the contents of the outgoing intent during the unit test ?

Was it helpful?

Solution

Found it. There's a function in ActivityUnitTestCase that does the trick.

Intent in = getStartedActivityIntent();

which will return the launch intent if your Activity under test calls startActivity(Intent) or startActivityForResult(Intent, int).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top