Skip to content Skip to sidebar Skip to footer

How Can I Test The Result Of A Button Click That Changes The Activity's View Asynchronously?

I am trying to write some Activity tests for an app, and one particular scenario that I want to test is that when I click a certain button, the Activity view updates accordingly. H

Solution 1:

The most common and simplest solution is to use Thread.sleep():

public void testFoo() {
  TextView textView = (TextView) myActivity.findViewById(com.company.app.R.id.text);
  assertEquals("text should be empty", "", textView.getText());

  // simulate a button click, which start an AsyncTask and update TextView when done.
  final Button button = (Button) myActivity.findViewById(com.company.app.R.id.refresh);
  myActivity.runOnUiThread(new Runnable() {
    public void run() {
      button.performClick();
    }
  });

  // assume AsyncTask will be finished in 6 seconds.
  try {
    Thread.sleep(6000);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }

  assertEquals("text should be refreshed", "refreshed", textView.getText());
}

Hope this helps.


Solution 2:

If you're using Eclipse, you could use the debugger by setting a breakpoint in the code that updates the view. You could also set some breakpoints in the long running task to watch and ensure that all your code is executing.

An alternative, write some log or console outputs in your long-running task and the view updater code, so you can see the progress without interrupting the thread by a debugger.

As a piece of advise, if its a long-running process, you should be showing a progress bar of some description to the user, so they aren't stuck there thinking "Is something happening?". If you use a progress bar with a maximum value, you can update it in your long-running task as it is running, so the user can see the activity going from 10% to 20%... etc.

Sorry if you were expecting some kind of jUnit-specific answer.


Solution 3:

I ended up solving this by using the Robotium library's Solo.waitForText method that takes a string and timeout period and blocks until either the expected text appears or the timeout occurs. Great UI testing library.


Post a Comment for "How Can I Test The Result Of A Button Click That Changes The Activity's View Asynchronously?"