android - Unit Test Interaction Between Activity and Service -
how can test interaction between activity , service?
for example, have service manages current music player play queue , activity displays play queue user. want test activity correctly displays play queue.
public class viewavailablesongsactivitytests extends activityinstrumentationtestcase2<viewavailablesongsactivity> { public viewavailablesongsactivitytests() { super(viewavailablesongsactivity.class); } public void testoneavailablesong() { songparcel song = new songparcel("test title", "test artist", "content://testuri"); addsongtoavailablelist(song); activity activity = this.getactivity(); listview lstavailablesongs = (listview) activity.findviewbyid(r.id.lstavailablesongs); assertequals(1, lstavailablesongs.getcount()); assertlistviewitemiscorrect(lstavailablesongs, 0, song); stopmyservice(); } public void test3availablesongs() { songparcel songlast = new songparcel("test title", "test artist1", "content://testuri"); addsongtoavailablelist(songlast); songparcel songfirst = new songparcel("a song", "test artist2", "content://testuri"); addsongtoavailablelist(songfirst); songparcel songmiddle = new songparcel("bravo", "david", "content://tests"); addsongtoavailablelist(songmiddle); activity activity = this.getactivity(); listview lstavailablesongs = (listview) activity.findviewbyid(r.id.lstavailablesongs); assertequals(3, lstavailablesongs.getcount()); assertlistviewitemiscorrect(lstavailablesongs, 0, songfirst); assertlistviewitemiscorrect(lstavailablesongs, 1, songmiddle); assertlistviewitemiscorrect(lstavailablesongs, 2, songlast); stopmyservice(); } private void addsongtoavailablelist(songparcel song) { context context = this.getinstrumentation().gettargetcontext(); intent addsongintent = new intent(context, myservice.class); addsongintent.setaction(myservice.add_song_to_available_list); addsongintent.putextra(myservice.extra_song, song); context.startservice(addsongintent); } private void assertlistviewitemiscorrect(listview lstview, int index, songparcel song) { adapter adapter = lstview.getadapter(); view row = adapter.getview(0, null, lstview); textview txttitle = (textview) row.findviewbyid(r.id.txttitle); assertequals(song.gettitle(), txttitle.gettext()); textview txtartist = (textview) row.findviewbyid(r.id.txtartist); assertequals(song.getartist(), txtartist.gettext()); } private void stopmyservice() { this.getactivity().stopservice(new intent(this.getinstrumentation().gettargetcontext(), myservice.class)); } }
i tried using activityinstrumentationtestcase2
, had problems using service. appeared service persisting test test because activity display 3 songs.
am using activityinstrumentationtestcase2
incorrectly or should using different test class?
it turns out had bug, stopservice()
never being called =p. in assertlistviewitemiscorrect()
, adapter.getview(0, null, lstview)
should have been adapter.getview(index, null, lstview)
. appears working fine now.
Comments
Post a Comment