android - Accessing asynchronous activity state in Espresso -
i want know how many list items used in recyclerview can iterate on list items , click on them espresso. problem i'm using volley asynchronously json data need underlying data set of adapter. i've used idling resource counter in ui test defer execution of test until counter zero. however, calling getitemcount on adapter still results in zero, despite fact networking operation should resolve before test executes.
ui test code
@test public void foo(){ // register idling resource counter volley json data asynchronously espresso.registeridlingresources(mactivitytestrule.getactivity().idlingcounter); recyclerview v = (recyclerview) mactivitytestrule.getactivity().findviewbyid(r.id.rv_recycler_view); int count = v.getadapter().getitemcount(); // prints zero; why? system.out.println("adapter count = "+string.valueof(count)); } here oncreate method. adapter list initialized empty, populated in onreponse when volley finishes networking. however, espresso doesn't seem wait onreponse called. not sure why, believe i'm using idling resource counter correctly.
recyclerview mrecyclerview; public adapter madapter; public countingidlingresource idlingcounter = new countingidlingresource("data_loader"); @override public void oncreate(bundle savedinstacestate){ // ... code // init empty array list arraylist<myobject> list = new arraylist<>(); // init adapter list, , set adapter view mrecycleradapter = new adapter(list, mainactivity.this); mrecyclerview.setadapter(mrecycleradapter); mrecyclerview.setlayoutmanager(new linearlayoutmanager(mainactivity.this)); // add json data list; increment idlingcounter resource counter. // decrement idlingcounter in onresponse idlingcounter.increment(); fetchjson(); } edit
mainactivity class
public class mainactivity extends appcompatactivity { // debugging private static final string tag = mainactivity.class.getsimplename(); // testing public countingidlingresource idlingcounter = new countingidlingresource("data_loader"); // networking private requestqueue requestqueue; // data private arraylist<recipe> mrecipes = new arraylist<>(); // ui public adapter mrecycleradapter; @bindview(r.id.rv_recycler_view) recyclerview mrecyclerview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); butterknife.bind(this); getsupportactionbar().settitle("recipes"); // perform networking if there no saved instance state if(null == savedinstancestate){ requestqueue = volley.newrequestqueue(this); // todo calling idlingcounter.increment() idlingcounter.increment(); fetch(requestqueue); // initializes recycler view adapter when done fetching } else { log.v(tag,"null != savedinstancestate"); mrecipes = savedinstancestate.getparcelablearraylist(constants.key_recipes); mrecycleradapter = new adapter(mrecipes, this); mrecyclerview.setadapter(mrecycleradapter); mrecyclerview.setlayoutmanager(new linearlayoutmanager(this)); } } @override public void onsaveinstancestate(bundle outstate) { outstate.putparcelablearraylist(constants.key_recipes,mrecipes); super.onsaveinstancestate(outstate); } // fetch json , build recipe-object array-list private void fetch(requestqueue requestqueue) { // define request jsonarrayrequest request = new jsonarrayrequest(constants.jsonurl, new response.listener<jsonarray>() { // handles json response data @override public void onresponse(jsonarray jsonarray) { log.v(tag,"onresponse"); (int = 0; < jsonarray.length(); i++) { try { jsonobject jsonobject = jsonarray.getjsonobject(i); recipe recipe = new recipe(jsonobject); //recipe.msteps.remove(0); mrecipes.add(recipe); log.v(tag,jsonobject.tostring()); } catch (jsonexception e) { e.printstacktrace(); } } // initialize adapter when json data ready // todo initializing adapter withe data set mrecycleradapter = new adapter(mrecipes, mainactivity.this); mrecyclerview.setadapter(mrecycleradapter); mrecyclerview.setlayoutmanager(new linearlayoutmanager(mainactivity.this)); // test if activity opened widget using intent intent intent = getintent(); if(intent.getextras() != null){ // start twopaneactivity passing intent int position = intent.getintextra(widgetprovider.intent_key_recipe_idx,-1); intent = new intent(mainactivity.this,twopaneactivity.class); recipe recipe = mrecipes.get(position); intent.putextra(constants.key_single_recipe,recipe); startactivity(intent); } // todo calling idlingcounter.decrement() idlingcounter.decrement(); } }, // error listener object new response.errorlistener() { @override public void onerrorresponse(volleyerror volleyerror) { toast.maketext(mainactivity.this, "unable fetch data: " + volleyerror.getmessage(), toast.length_short).show(); } }); // queue request requestqueue.add(request); } public void foo(){ } }
Comments
Post a Comment