0

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 ?

Traxex1909
  • 2,650
  • 4
  • 20
  • 25
  • What do you mean by "examining the contents", you have it in the code in the `putExtra()` method, you're passing the two arguments there, it's up to the `CalcActivity.class` to do whatever you want with them, based for example on the pressed button (like "+" or "-"). – g00dy Aug 08 '13 at 06:38
  • @g00dy I want to unit test the app so i need to check if the right data is being sent to `CalcActivity`. – Traxex1909 Aug 08 '13 at 06:41
  • well, why don't you just `LOG` this information, before calling `startActivity(in);` ? – g00dy Aug 08 '13 at 06:44

1 Answers1

2

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).

Traxex1909
  • 2,650
  • 4
  • 20
  • 25