Issue
How can you test the interaction between an activity and a service?
For example, I have a service that manages the current music player play queue and an activity that displays the play queue to the user. I want to test that the activity correctly displays the 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 Good 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, but I had problems using the service. It appeared that the service was persisting from test to test because the activity would always display 3 songs.
Am I using ActivityInstrumentationTestCase2 incorrectly or should I be using a different test class?
Solution
It turns out that I had a bug, so stopService() was never being called =P. In assertListViewItemIsCorrect(), adapter.getView(0, null, lstView) should have been adapter.getView(index, null, lstView). Everything appears to be working fine now.
Answered By - MageWind
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.