diff --git a/app/build.gradle b/app/build.gradle index 843ec6c3..36d452b6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,6 +37,7 @@ dependencies { // Kotlin implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${versions.kotlin}" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:${versions.coroutines}" + implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:${versions.coroutines}" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:${versions.coroutines}" // AndroidX @@ -106,6 +107,7 @@ dependencies { testImplementation "com.google.truth:truth:${versions.truth}" testImplementation "androidx.arch.core:core-testing:${versions.archTesting}" testImplementation "org.robolectric:robolectric:${versions.robolectric}" + testImplementation "io.mockk:mockk:${versions.mockk}" // Instrumentation Testing androidTestImplementation "androidx.test:runner:${versions.androidxTestRunner}" diff --git a/app/src/main/java/com/naman14/timberx/playback/players/MediaSessionCallback.kt b/app/src/main/java/com/naman14/timberx/playback/players/MediaSessionCallback.kt index 393c8057..bef3214d 100644 --- a/app/src/main/java/com/naman14/timberx/playback/players/MediaSessionCallback.kt +++ b/app/src/main/java/com/naman14/timberx/playback/players/MediaSessionCallback.kt @@ -50,7 +50,7 @@ class MediaSessionCallback( override fun onPlayFromSearch(query: String?, extras: Bundle?) { query?.let { - val song = songsRepository.searchSongs(query, 1) + val song = songsRepository.getSongs(query, 1) if (song.isNotEmpty()) { songPlayer.playSong(song.first()) } diff --git a/app/src/main/java/com/naman14/timberx/repository/SongsRepository.kt b/app/src/main/java/com/naman14/timberx/repository/SongsRepository.kt index 5ecadfef..8fdfb46c 100644 --- a/app/src/main/java/com/naman14/timberx/repository/SongsRepository.kt +++ b/app/src/main/java/com/naman14/timberx/repository/SongsRepository.kt @@ -39,7 +39,7 @@ interface SongsRepository { fun getSongFromPath(songPath: String): Song - fun searchSongs(searchString: String, limit: Int): List + fun getSongs(paramString: String, limit: Int): List fun deleteTracks(ids: LongArray): Int } @@ -91,11 +91,11 @@ class RealSongsRepository( } ?: throw IllegalStateException("Unable to query $uri, system returned null.") } - override fun searchSongs(searchString: String, limit: Int): List { - val result = makeSongCursor("title LIKE ?", arrayOf("$searchString%")) + override fun getSongs(paramString: String, limit: Int): List { + val result = makeSongCursor("title LIKE ?", arrayOf("$paramString%")) .mapList(true) { Song.fromCursor(this) } if (result.size < limit) { - val moreSongs = makeSongCursor("title LIKE ?", arrayOf("%_$searchString%")) + val moreSongs = makeSongCursor("title LIKE ?", arrayOf("%_$paramString%")) .mapList(true) { Song.fromCursor(this) } result += moreSongs } diff --git a/app/src/main/java/com/naman14/timberx/ui/viewmodels/SearchViewModel.kt b/app/src/main/java/com/naman14/timberx/ui/viewmodels/SearchViewModel.kt index 15d0f612..ff2e52dd 100644 --- a/app/src/main/java/com/naman14/timberx/ui/viewmodels/SearchViewModel.kt +++ b/app/src/main/java/com/naman14/timberx/ui/viewmodels/SearchViewModel.kt @@ -14,6 +14,7 @@ */ package com.naman14.timberx.ui.viewmodels +import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import com.naman14.timberx.models.Album import com.naman14.timberx.models.Artist @@ -35,13 +36,13 @@ class SearchViewModel( private val searchData = SearchData() private val _searchLiveData = MutableLiveData() - val searchLiveData = _searchLiveData + val searchLiveData: LiveData = _searchLiveData fun search(query: String) { if (query.length >= 3) { launch { val songs = withContext(IO) { - songsRepository.searchSongs(query, 10) + songsRepository.getSongs(query, 10) } if (songs.isNotEmpty()) { searchData.songs = songs.toMutableList() diff --git a/app/src/test/java/com/naman14/timberx/ui/viewmodels/SearchViewModelTest.kt b/app/src/test/java/com/naman14/timberx/ui/viewmodels/SearchViewModelTest.kt new file mode 100644 index 00000000..837e63be --- /dev/null +++ b/app/src/test/java/com/naman14/timberx/ui/viewmodels/SearchViewModelTest.kt @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2019 Naman Dwivedi. + * + * Licensed under the GNU General Public License v3 + * + * This is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + */ +package com.naman14.timberx.ui.viewmodels + +import com.naman14.timberx.repository.AlbumRepository +import com.naman14.timberx.repository.ArtistRepository +import com.naman14.timberx.repository.SongsRepository +import io.mockk.mockk +import io.mockk.verify +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.test.setMain +import org.junit.Before +import org.junit.Test + +class SearchViewModelTest { + val songsRepo = mockk() + val albumRepo = mockk() + val artistRepo = mockk() + val tested = SearchViewModel(songsRepo, albumRepo, artistRepo) + + @Before + fun setup() { + Dispatchers.setMain(Dispatchers.IO) + } + + @Test + fun `given query when search called then searchSongs called with query and limit 10`() { + // given + val query = "song" + // when + tested.search(query) + // then + verify { songsRepo.getSongs(query, 10) } + } + + @Test + fun `given query when search called then searchAlbums called with query and limit 7`() { + // given + val query = "song" + // when + tested.search(query) + // then + verify { albumRepo.getAlbums(query, 7) } + } + + @Test + fun `given query when search called then searchArtists called with query and limit 7`() { + // given + val query = "song" + // when + tested.search(query) + // then + verify { artistRepo.getArtists(query, 7) } + } +} diff --git a/dependencies.gradle b/dependencies.gradle index e22716d6..b8a6af71 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -62,6 +62,7 @@ ext.versions = [ mockitoKotlin : "2.1.0", truth : "0.42", robolectric : "4.1", + mockk : "1.9.3", // Instrumentation Testing androidxTestRunner: "1.1.1",