Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

test if a button starts a new activity in android junit (pref without robotium)?

I cant find any good help on this. I have a simple activity with just a few buttons on and I need to test if they re-direct to the correct new page (activity).

public void testButton() {
         button.requestFocus();
         button.performClick();

      }

I really have no idea beyond that. The tutorials are all very unhelpful in doing this :/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need ActivityMonitor, it helps you moniotor newly opened activity during instrumentation, check out the pseudo code below:

public void testOpenNextActivity() {
  // register next activity that need to be monitored.
  ActivityMonitor activityMonitor = getInstrumentation().addMonitor(NextActivity.class.getName(), null, false);

  // open current activity.
  MyActivity myActivity = getActivity();
  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();
    }
  });

  //Watch for the timeout
  //example values 5000 if in ms, or 5 if it's in seconds.
  NextActivity nextActivity = getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 5000);
  // next activity is opened and captured.
  assertNotNull(nextActivity);
  nextActivity .finish();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...