From 81216848eb400067fbabe27d7e215bfcc36635ff Mon Sep 17 00:00:00 2001 From: Thomas Durieux Date: Mon, 20 Oct 2014 14:47:43 +0200 Subject: Create Unit tests Activity tests: - tests the layout of the page Domain tests: - tests genre comparator - tests bookmark set date DownloadService Test: --- AndroidManifest.xml | 6 + .../dsub/activity/DownloadActivityTest.java | 32 +++ .../activity/SubsonicFragmentActivityTest.java | 34 +++ .../daneren2005/dsub/domain/BookmarkTest.java | 40 +++ .../dsub/domain/GenreComparatorTest.java | 68 +++++ .../dsub/service/DownloadServiceTest.java | 296 +++++++++++++++++++++ 6 files changed, 476 insertions(+) create mode 100644 test/github/daneren2005/dsub/activity/DownloadActivityTest.java create mode 100644 test/github/daneren2005/dsub/activity/SubsonicFragmentActivityTest.java create mode 100644 test/github/daneren2005/dsub/domain/BookmarkTest.java create mode 100644 test/github/daneren2005/dsub/domain/GenreComparatorTest.java create mode 100644 test/github/daneren2005/dsub/service/DownloadServiceTest.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index aad37057..081c00d3 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -4,6 +4,10 @@ android:installLocation="internalOnly" android:versionCode="128" android:versionName="4.8.1"> + + @@ -33,6 +37,8 @@ android:backupAgent="github.daneren2005.dsub.util.SettingsBackupAgent" android:icon="@drawable/launch" android:theme="@style/Theme.DSub.Light"> + + { + + private DownloadActivity activity; + + public DownloadActivityTest() { + super(DownloadActivity.class); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + activity = getActivity(); + } + + /** + * Test the main layout. + */ + public void testLayout() { + View view = activity.findViewById(R.layout.download_activity); + assertNotNull(view); + assertNotNull(view.findViewById(R.layout.download_activity)); + assertNotNull(activity.findViewById(R.id.fragment_container)); + } + +} diff --git a/test/github/daneren2005/dsub/activity/SubsonicFragmentActivityTest.java b/test/github/daneren2005/dsub/activity/SubsonicFragmentActivityTest.java new file mode 100644 index 00000000..553938c8 --- /dev/null +++ b/test/github/daneren2005/dsub/activity/SubsonicFragmentActivityTest.java @@ -0,0 +1,34 @@ +package github.daneren2005.dsub.activity; + +import github.daneren2005.dsub.R; +import android.test.ActivityInstrumentationTestCase2; + +public class SubsonicFragmentActivityTest extends + ActivityInstrumentationTestCase2 { + + private SubsonicFragmentActivity activity; + + public SubsonicFragmentActivityTest() { + super(SubsonicFragmentActivity.class); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + activity = getActivity(); + } + + /** + * Test the main layout. + */ + public void testLayout() { + assertNotNull(activity.findViewById(R.id.content_frame)); + } + + /** + * Test the bottom bar. + */ + public void testBottomBar() { + assertNotNull(activity.findViewById(R.id.bottom_bar)); + } +} diff --git a/test/github/daneren2005/dsub/domain/BookmarkTest.java b/test/github/daneren2005/dsub/domain/BookmarkTest.java new file mode 100644 index 00000000..814f658a --- /dev/null +++ b/test/github/daneren2005/dsub/domain/BookmarkTest.java @@ -0,0 +1,40 @@ +package github.daneren2005.dsub.domain; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +import junit.framework.TestCase; + +public class BookmarkTest extends TestCase { + + /** + * tests the set created date + * @throws ParseException + */ + public void testSetCreated() throws ParseException { + Bookmark bookmark = new Bookmark(); + bookmark.setCreated(null); + assertEquals(null, bookmark.getCreated()); + + bookmark.setCreated(""); + assertEquals(null, bookmark.getCreated()); + + bookmark.setCreated("2014-04-04"); + assertEquals(null, bookmark.getCreated()); + + bookmark.setCreated("2014/04/04"); + assertEquals(null, bookmark.getCreated()); + + bookmark.setCreated("18/03/1988"); + assertEquals(null, bookmark.getCreated()); + + bookmark.setCreated("18/03/88"); + assertEquals(null, bookmark.getCreated()); + + Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH).parse("2013-10-20T00:00:00"); + bookmark.setCreated("2013-10-20T00:00:00"); + assertEquals(date, bookmark.getCreated()); + } +} \ No newline at end of file diff --git a/test/github/daneren2005/dsub/domain/GenreComparatorTest.java b/test/github/daneren2005/dsub/domain/GenreComparatorTest.java new file mode 100644 index 00000000..9ffa518e --- /dev/null +++ b/test/github/daneren2005/dsub/domain/GenreComparatorTest.java @@ -0,0 +1,68 @@ +package github.daneren2005.dsub.domain; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; + +public class GenreComparatorTest extends TestCase { + + /** + * Sort genres which doesn't have name + */ + public void testSortGenreWithoutNameComparator() { + Genre g1 = new Genre(); + g1.setName("Genre"); + + Genre g2 = new Genre(); + + List genres = new ArrayList(); + genres.add(g1); + genres.add(g2); + + List sortedGenre = Genre.GenreComparator.sort(genres); + assertEquals(sortedGenre.get(0), g2); + } + + /** + * Sort genre with same name + */ + public void testSortGenreWithSameName() { + Genre g1 = new Genre(); + g1.setName("Genre"); + + Genre g2 = new Genre(); + g2.setName("genre"); + + List genres = new ArrayList(); + genres.add(g1); + genres.add(g2); + + List sortedGenre = Genre.GenreComparator.sort(genres); + assertEquals(sortedGenre.get(0), g1); + } + + /** + * test nominal genre sort + */ + public void testSortGenre() { + Genre g1 = new Genre(); + g1.setName("Rock"); + + Genre g2 = new Genre(); + g2.setName("Pop"); + + Genre g3 = new Genre(); + g2.setName("Rap"); + + List genres = new ArrayList(); + genres.add(g1); + genres.add(g2); + genres.add(g3); + + List sortedGenre = Genre.GenreComparator.sort(genres); + assertEquals(sortedGenre.get(0), g2); + assertEquals(sortedGenre.get(1), g3); + assertEquals(sortedGenre.get(2), g1); + } +} \ No newline at end of file diff --git a/test/github/daneren2005/dsub/service/DownloadServiceTest.java b/test/github/daneren2005/dsub/service/DownloadServiceTest.java new file mode 100644 index 00000000..44b77b84 --- /dev/null +++ b/test/github/daneren2005/dsub/service/DownloadServiceTest.java @@ -0,0 +1,296 @@ +package github.daneren2005.dsub.service; + +import static github.daneren2005.dsub.domain.PlayerState.COMPLETED; +import static github.daneren2005.dsub.domain.PlayerState.IDLE; +import static github.daneren2005.dsub.domain.PlayerState.PAUSED; +import static github.daneren2005.dsub.domain.PlayerState.STARTED; +import static github.daneren2005.dsub.domain.PlayerState.STOPPED; +import java.util.List; + +import github.daneren2005.dsub.activity.SubsonicFragmentActivity; +import github.daneren2005.dsub.domain.MusicDirectory; +import github.daneren2005.dsub.domain.PlayerState; + +import java.util.LinkedList; +import android.test.ActivityInstrumentationTestCase2; +import android.util.Log; + +public class DownloadServiceTest extends + ActivityInstrumentationTestCase2 { + + private SubsonicFragmentActivity activity; + private DownloadService downloadService; + + public DownloadServiceTest() { + super(SubsonicFragmentActivity.class); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + activity = getActivity(); + downloadService = activity.getDownloadService(); + downloadService.clear(); + } + + /** + * Test the get player duration without playlist. + */ + public void testGetPlayerDurationWithoutPlayList() { + int duration = downloadService.getPlayerDuration(); + assertEquals(0, duration); + } + + /** + * Test the get player position without playlist. + */ + public void testGetPlayerPositionWithoutPlayList() { + int position = downloadService.getPlayerPosition(); + assertEquals(0, position); + } + + public void testGetCurrentPlayingIndexWithoutPlayList() { + int currentPlayingIndex = activity.getDownloadService() + .getCurrentPlayingIndex(); + assertEquals(currentPlayingIndex, -1); + } + + /** + * Test next action without playlist. + */ + public void testNextWithoutPlayList() { + int oldCurrentPlayingIndex = downloadService.getCurrentPlayingIndex(); + downloadService.next(); + int newCurrentPlayingIndex = downloadService.getCurrentPlayingIndex(); + assertTrue(oldCurrentPlayingIndex == newCurrentPlayingIndex); + } + + /** + * Test previous action without playlist. + */ + public void testPreviousWithoutPlayList() { + int oldCurrentPlayingIndex = downloadService.getCurrentPlayingIndex(); + downloadService.previous(); + int newCurrentPlayingIndex = downloadService.getCurrentPlayingIndex(); + assertTrue(oldCurrentPlayingIndex == newCurrentPlayingIndex); + } + + /** + * Test next action with playlist. + */ + public void testNextWithPlayList() throws InterruptedException { + // Download two songs + downloadService.getDownloads().clear(); + downloadService.download(this.createMusicSongs(2), false, false, false, + false, 0, 0); + + Log.w("testPreviousWithPlayList", "Start waiting to downloads"); + Thread.sleep(5000); + Log.w("testPreviousWithPlayList", "Stop waiting downloads"); + + // Get the current index + int oldCurrentPlayingIndex = downloadService.getCurrentPlayingIndex(); + + // Do the next + downloadService.next(); + + // Check that the new current index is incremented + int newCurrentPlayingIndex = downloadService.getCurrentPlayingIndex(); + assertEquals(oldCurrentPlayingIndex + 1, newCurrentPlayingIndex); + } + + /** + * Test previous action with playlist. + */ + public void testPreviousWithPlayList() throws InterruptedException { + // Download two songs + downloadService.getDownloads().clear(); + downloadService.download(this.createMusicSongs(2), false, false, false, + false, 0, 0); + + Log.w("testPreviousWithPlayList", "Start waiting downloads"); + Thread.sleep(5000); + Log.w("testPreviousWithPlayList", "Stop waiting downloads"); + + // Get the current index + int oldCurrentPlayingIndex = downloadService.getCurrentPlayingIndex(); + + // Do a next before the previous + downloadService.next(); + + // Do the previous + downloadService.previous(); + + // Check that the new current index is incremented + int newCurrentPlayingIndex = downloadService.getCurrentPlayingIndex(); + assertEquals(oldCurrentPlayingIndex, newCurrentPlayingIndex); + } + + /** + * Test seek feature. + */ + public void testSeekTo() { + // seek with negative + downloadService.seekTo(Integer.MIN_VALUE); + + // seek with null + downloadService.seekTo(0); + + // seek with big value + downloadService.seekTo(Integer.MAX_VALUE); + } + + /** + * Test toggle play pause. + */ + public void testTogglePlayPause() { + PlayerState oldPlayState = downloadService.getPlayerState(); + downloadService.togglePlayPause(); + PlayerState newPlayState = downloadService.getPlayerState(); + if (oldPlayState == PAUSED || oldPlayState == COMPLETED + || oldPlayState == STOPPED) { + assertEquals(STARTED, newPlayState); + } else if (oldPlayState == STOPPED || oldPlayState == IDLE) { + if (downloadService.size() == 0) { + assertEquals(IDLE, newPlayState); + } else { + assertEquals(STARTED, newPlayState); + } + } else if (oldPlayState == STARTED) { + assertEquals(PAUSED, newPlayState); + } + downloadService.togglePlayPause(); + newPlayState = downloadService.getPlayerState(); + assertEquals(oldPlayState, newPlayState); + } + + /** + * Test toggle play pause without playlist. + */ + public void testTogglePlayPauseWithoutPlayList() { + PlayerState oldPlayState = downloadService.getPlayerState(); + downloadService.togglePlayPause(); + PlayerState newPlayState = downloadService.getPlayerState(); + + assertEquals(IDLE, oldPlayState); + assertEquals(IDLE, newPlayState); + } + + /** + * Test toggle play pause without playlist. + * + * @throws InterruptedException + */ + public void testTogglePlayPauseWithPlayList() throws InterruptedException { + // Download two songs + downloadService.getDownloads().clear(); + downloadService.download(this.createMusicSongs(2), false, false, false, + false, 0, 0); + + Log.w("testPreviousWithPlayList", "Start waiting downloads"); + Thread.sleep(5000); + Log.w("testPreviousWithPlayList", "Stop waiting downloads"); + + PlayerState oldPlayState = downloadService.getPlayerState(); + downloadService.togglePlayPause(); + Thread.sleep(500); + assertEquals(STARTED, downloadService.getPlayerState()); + downloadService.togglePlayPause(); + PlayerState newPlayState = downloadService.getPlayerState(); + assertEquals(PAUSED, newPlayState); + } + + /** + * Test the autoplay. + * + * @throws InterruptedException + */ + public void testAutoplay() throws InterruptedException { + // Download one songs + downloadService.getDownloads().clear(); + downloadService.download(this.createMusicSongs(1), false, true, false, + false, 0, 0); + + Log.w("testPreviousWithPlayList", "Start waiting downloads"); + Thread.sleep(5000); + Log.w("testPreviousWithPlayList", "Stop waiting downloads"); + + PlayerState playerState = downloadService.getPlayerState(); + assertEquals(STARTED, playerState); + } + + /** + * Test if the download list is empty. + */ + public void testGetDownloadsEmptyList() { + List list = downloadService.getDownloads(); + assertEquals(0, list.size()); + } + + /** + * Test if the download service add the given song to its queue. + */ + public void testAddMusicToDownload() { + assertNotNull(downloadService); + + // Download list before + List downloadList = downloadService.getDownloads(); + int beforeDownloadAction = 0; + if (downloadList != null) { + beforeDownloadAction = downloadList.size(); + } + + // Launch download + downloadService.download(this.createMusicSongs(1), false, false, false, + false, 0, 0); + + // Check number of download after + int afterDownloadAction = 0; + downloadList = downloadService.getDownloads(); + if (downloadList != null && !downloadList.isEmpty()) { + afterDownloadAction = downloadList.size(); + } + assertEquals(beforeDownloadAction + 1, afterDownloadAction); + } + + /** + * Generate a list containing some music directory entries. + * + * @return list containing some music directory entries. + */ + private List createMusicSongs(int size) { + MusicDirectory.Entry musicEntry = new MusicDirectory.Entry(); + musicEntry.setAlbum("Itchy Hitchhiker"); + musicEntry.setBitRate(198); + musicEntry.setAlbumId("49"); + musicEntry.setDuration(247); + musicEntry.setSize(Long.valueOf(6162717)); + musicEntry.setArtistId("23"); + musicEntry.setArtist("The Dada Weatherman"); + musicEntry.setCloseness(0); + musicEntry.setContentType("audio/mpeg"); + musicEntry.setCoverArt("433"); + musicEntry.setDirectory(false); + musicEntry.setGenre("Easy Listening/New Age"); + musicEntry.setGrandParent("306"); + musicEntry.setId("466"); + musicEntry.setParent("433"); + musicEntry + .setPath("The Dada Weatherman/Itchy Hitchhiker/08 - The Dada Weatherman - Harmonies.mp3"); + musicEntry.setStarred(true); + musicEntry.setSuffix("mp3"); + musicEntry.setTitle("Harmonies"); + musicEntry.setType(0); + musicEntry.setVideo(false); + + List musicEntries = new LinkedList(); + + for (int i = 0; i < size; i++) { + musicEntries.add(musicEntry); + } + + return musicEntries; + + } + +} -- cgit v1.2.3