简体   繁体   中英

Testing an outgoing intent

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 ?

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

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