This is my Asnyc task class
class EndpointsAsyncTask extends AsyncTask<Pair<Context, String>, Void, String> {
private static MyApi myApiService = null;
private Context context;
private static final String TAG = "EndpointsAsyncTask";
private MainActivity activity;
private ProgressBar mProgressBar;
private Exception mError = null;
private JsonGetTaskListener mListener = null;
InterstitialAd mInterstitialAd;
public EndpointsAsyncTask setListener(JsonGetTaskListener listener) {
this.mListener = listener;
return this;
}
public static interface JsonGetTaskListener {
public void onComplete(String jsonString, Exception e);
}
public EndpointsAsyncTask(MainActivity activity,ProgressBar mProgressBar){
this.activity = activity;
this.mProgressBar= mProgressBar;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// mProgressBar = new ProgressBar(this.activity);
mInterstitialAd = new InterstitialAd(this.activity);
mProgressBar.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(Pair<Context, String>... params) {
if(myApiService == null) { // Only do this once
// end options for devappserver
MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null)
.setRootUrl("https://backendversionone.appspot.com/_ah/api/");
// https://endpoint-backend-1056.appspot.com/_ah/api/
myApiService = builder.build();
}
context = params[0].first;
String name = params[0].second;
try {
return myApiService.sayHi(name+"check").execute().getData();
} catch (IOException e) {
return e.getMessage();
}
}
@Override
protected void onPostExecute(String result) {
// Toast.makeText(context, result, Toast.LENGTH_LONG).show();
// mProgressBar.setVisibility(View.GONE);
if (this.mListener != null)
this.mListener.onComplete(result, mError);
mProgressBar.setVisibility(View.GONE);
Intent myIntent = new Intent(context, LibraryMainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
myIntent.putExtra("joke", result);
context.startActivity(myIntent);
}
}
Im trying to test my Async task from test class
Find my class below. public class MainActivityTest extends ActivityInstrumentationTestCase2 {
private MainActivity mMainActivity;
private TextView mFirstTestText;
ProgressBar pbar;
String mJsonString = null;
Exception mError = null;
CountDownLatch signal = null;
public MainActivityTest() {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mMainActivity = getActivity();
signal = new CountDownLatch(1);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
signal.countDown();
}
@MediumTest
public void testcheck(){
Log.d("Testing baby", "Testcheck");
Log.d("Testing baby","Testcheck");
Log.d("Testing baby", "Testcheck");
final Button sendToReceiverButton = (Button)
mMainActivity.findViewById(R.id.buttontelljoke);
assertNotNull(sendToReceiverButton);
}
@SmallTest
public void testchecks(){
Log.d("Testing baby", "Testcheck");
Log.d("Testing baby","Testcheck");
Log.d("Testing baby", "Testcheck");
final Button sendToReceiverButton = (Button)
mMainActivity.findViewById(R.id.buttontelljoke);
assertNotNull(sendToReceiverButton);
}
@MediumTest
public void testasyncTaskTest(){
pbar = (ProgressBar)mMainActivity.findViewById(progressBar1);
try {
EndpointsAsyncTask jokeTask = new EndpointsAsyncTask(mMainActivity,pbar);
jokeTask.setListener(new EndpointsAsyncTask.JsonGetTaskListener() {
@Override
public void onComplete(String jsonString, Exception e) {
mJsonString = jsonString;
mError = e;
signal.countDown();
}
}).execute((new Pair<Context, String>(getActivity(), "")));
signal.await();
assertNotNull(mJsonString);
} catch (Exception e){
fail("Timed out");
}
}
protected Fragment waitForFragment(String tag, int timeout) {
long endTime = SystemClock.uptimeMillis() + timeout;
while (SystemClock.uptimeMillis() <= endTime) {
Fragment fragment = getActivity().getSupportFragmentManager().findFragmentByTag(tag);
if (fragment != null) {
return fragment;
}
}
return null;
}
When running the test case , This line throws exception - Only the created thread can update the Async task
jokeTask.setListener(new EndpointsAsyncTask.JsonGetTaskListener() {
@Override
public void onComplete(String jsonString, Exception e) {
mJsonString = jsonString;
mError = e;
signal.countDown();
}
}).execute((new Pair<Context, String>(getActivity(), "")));
signal.await();
But running the test when my phone is locked, I dont get exception and works fine.
How can i fix this issue.