Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Finishing up offline game #310

Merged
merged 15 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,17 @@ class DeleteSongsActivityTest {
val context = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext
var records = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt")
records.createNewFile()
records.appendText("Testing 1\n")
records.appendText("Testing 2\n")
records.appendText("Song 1 - Artist 1\n")
records.appendText("Song 2 - Artist 2\n")

var testing1 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Testing 1")
var properties = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "properties.txt")
properties.createNewFile()
properties.appendText("Song 1 - Artist 1 - Artwork 1 - Preview 1\n")
properties.appendText("Song 2 - Artist 2 - Artwork 2 - Preview 2\n")

var testing1 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Song 1 - Artist 1")
testing1.createNewFile()
var testing2 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Testing 2")
var testing2 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Song 2 - Artist 2")
testing2.createNewFile()

val intent = Intent(ApplicationProvider.getApplicationContext(), DeleteSongsActivity::class.java)
Expand All @@ -89,9 +94,10 @@ class DeleteSongsActivityTest {
}


onView(withId(layout.getChildAt(0).id)).check(matches(withText("TESTING 1")))
onView(withId(layout.getChildAt(1).id)).check(matches(withText("TESTING 2")))
onView(withId(layout.getChildAt(0).id)).check(matches(withText("SONG 1 - ARTIST 1")))
onView(withId(layout.getChildAt(1).id)).check(matches(withText("SONG 2 - ARTIST 2")))
records.delete()
properties.delete()
testing1.delete()
testing2.delete()
}
Expand All @@ -101,12 +107,17 @@ class DeleteSongsActivityTest {
val context = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext
var records = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt")
records.createNewFile()
records.appendText("Testing 1\n")
records.appendText("Testing 2\n")
records.appendText("Song 1 - Artist 1\n")
records.appendText("Song 2 - Artist 2\n")

var properties = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "properties.txt")
properties.createNewFile()
properties.appendText("Song 1 - Artist 1 - Artwork 1 - Preview 1\n")
properties.appendText("Song 2 - Artist 2 - Artwork 2 - Preview 2\n")

var testing1 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Testing 1")
var testing1 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Song 1 - Artist 1")
testing1.createNewFile()
var testing2 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Testing 2")
var testing2 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Song 2 - Artist 2")
testing2.createNewFile()

val intent = Intent(ApplicationProvider.getApplicationContext(), DeleteSongsActivity::class.java)
Expand All @@ -121,9 +132,10 @@ class DeleteSongsActivityTest {

assert(!testing1.exists())
assert(testing2.exists())
onView(withId(layout.getChildAt(0).id)).check(matches(withText("TESTING 2")))
onView(withId(layout.getChildAt(0).id)).check(matches(withText("SONG 2 - ARTIST 2")))

records.delete()
properties.delete()
testing1.delete()
testing2.delete()
}
Expand All @@ -133,12 +145,17 @@ class DeleteSongsActivityTest {
val context = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext
var records = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt")
records.createNewFile()
records.appendText("Testing 1\n")
records.appendText("Testing 2\n")
records.appendText("Song 1 - Artist 1\n")
records.appendText("Song 2 - Artist 2\n")

var testing1 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Testing 1")
var properties = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "properties.txt")
properties.createNewFile()
properties.appendText("Song 1 - Artist 1 - Artwork 1 - Preview 1\n")
properties.appendText("Song 2 - Artist 2 - Artwork 2 - Preview 2\n")

var testing1 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Song 1 - Artist 1")
testing1.createNewFile()
var testing2 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Testing 2")
var testing2 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Song 2 - Artist 2")
testing2.createNewFile()

val intent = Intent(ApplicationProvider.getApplicationContext(), DeleteSongsActivity::class.java)
Expand All @@ -153,9 +170,10 @@ class DeleteSongsActivityTest {

assert(testing1.exists())
assert(!testing2.exists())
onView(withId(layout.getChildAt(0).id)).check(matches(withText("TESTING 1")))
onView(withId(layout.getChildAt(0).id)).check(matches(withText("SONG 1 - ARTIST 1")))

records.delete()
properties.delete()
testing1.delete()
testing2.delete()
}
Expand All @@ -165,12 +183,17 @@ class DeleteSongsActivityTest {
val context = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext
var records = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt")
records.createNewFile()
records.appendText("Testing 1\n")
records.appendText("Testing 2\n")
records.appendText("Song 1 - Artist 1\n")
records.appendText("Song 2 - Artist 2\n")

var properties = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "properties.txt")
properties.createNewFile()
properties.appendText("Song 1 - Artist 1 - Artwork 1 - Preview 1\n")
properties.appendText("Song 2 - Artist 2 - Artwork 2 - Preview 2\n")

var testing1 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Testing 1")
var testing1 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Song 1 - Artist 1")
testing1.createNewFile()
var testing2 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Testing 2")
var testing2 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Song 2 - Artist 2")
testing2.createNewFile()

val intent = Intent(ApplicationProvider.getApplicationContext(), DeleteSongsActivity::class.java)
Expand All @@ -189,6 +212,7 @@ class DeleteSongsActivityTest {
onView(withId(layout.getChildAt(0).id)).check(matches(withText(R.string.delete_nothing_to_delete)))

records.delete()
properties.delete()
testing1.delete()
testing2.delete()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ class DownloadActivityTest {
if(records.exists()) {
records.delete()
}

val properties = File(activity.applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "properties.txt")
assert(!properties.exists())
if(properties.exists()) {
properties.delete()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,5 +297,13 @@ class GameSetupFragmentTest {
onView(withId(R.id.validateSearch)).perform(scrollTo(), click())
}

@Test
fun internetButtonClick() {
onView(withId(R.id.game_setup_has_internet)).check(matches(withText("Internet is on")))
onView(withId(R.id.game_setup_has_internet)).perform(click())
onView(withId(R.id.game_setup_has_internet)).check(matches(withText("Internet is off")))
onView(withId(R.id.game_setup_has_internet)).perform(click())
onView(withId(R.id.game_setup_has_internet)).check(matches(withText("Internet is on")))
}

}
135 changes: 135 additions & 0 deletions app/src/androidTest/java/ch/sdp/vibester/helper/GameManagerTest.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package ch.sdp.vibester.helper

import android.os.Environment
import androidx.test.platform.app.InstrumentationRegistry
import ch.sdp.vibester.api.LastfmMethod
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import java.io.File

class GameManagerTest {
private fun setGameManager(valid:Boolean = true): GameManager {
Expand Down Expand Up @@ -85,4 +89,135 @@ class GameManagerTest {
assertEquals(gameManager.getCurrentSong().getArtistName().lowercase(), "imagine dragons")
assertEquals(gameManager.getCurrentSong().getTrackName().lowercase(), "monday")
}

//OFFLINE TESTS

private fun offlineTestSetup(): GameManager {
val context = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext
var records = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt")
records.createNewFile()
records.appendText("Song 1 - Artist 1\n")
records.appendText("Song 2 - Artist 2\n")

var properties = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "properties.txt")
properties.createNewFile()
properties.appendText("Song 1 - Artist 1 - Artwork 1 - Preview 1\n")
properties.appendText("Song 2 - Artist 2 - Artwork 2 - Preview 2\n")

var testing1 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Song 1 - Artist 1")
testing1.createNewFile()
var testing2 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Song 2 - Artist 2")
testing2.createNewFile()

lateinit var manager: GameManager
val path = context?.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
if(path != null) {
manager = createOfflineGameManager(path)
}
return manager
}

private fun createOfflineGameManager(path: File): GameManager {
val gameManager = GameManager()
if(path != null) {
gameManager.setOffline(path, false)
}
gameManager.setGameSongList("", LastfmMethod.BY_TAG.method)
return gameManager
}

@Test
fun checkExistsError() {
val context = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext
val gameManager = offlineTestSetup()

var properties = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "properties.txt")
if(properties.exists()){
properties.delete()
}

assert(!gameManager.setNextSong())

offlineTestCleanup()
}

@Test
fun checkEmptyLineError() {
val context = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext
val gameManager = offlineTestSetup()

var properties = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "properties.txt")
if(properties.exists()) {
properties.delete()
}
properties.createNewFile()
properties.appendText("\n")

assert(!gameManager.setNextSong())

offlineTestCleanup()
}

@Test
fun checkIncorrectInformationInPropertiesShort() {
val context = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext
val gameManager = offlineTestSetup()

var properties = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "properties.txt")
if(properties.exists()){
properties.delete()
}
properties.createNewFile()
properties.appendText("Song 1 - Artist 1 - Preview 1\n")

assert(!gameManager.setNextSong())

offlineTestCleanup()
}

@Test
fun checkIncorrectInformationInPropertiesLong() {
val context = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext
val gameManager = offlineTestSetup()

var properties = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "properties.txt")
if(properties.exists()){
properties.delete()
}
properties.createNewFile()
properties.appendText("Song 1 - Artist 1 - Preview 1 - Artwork 1 - Extra 1\n")

assert(!gameManager.setNextSong())

offlineTestCleanup()
}

@Test
fun checkCurrentSong() {
val gameManager = offlineTestSetup()

assert(gameManager.setNextSong())

val curSong = gameManager.currentSong
assertEquals(curSong.getTrackName(), "Song 2")
assertEquals(curSong.getArtistName(), "Artist 2")
assertEquals(curSong.getPreviewUrl(), "Preview 2")
assertEquals(curSong.getArtworkUrl(), "Artwork 2")


offlineTestCleanup()
}

private fun offlineTestCleanup() {
val context = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext
var records = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt")
var properties = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "properties.txt")
var testing1 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Song 1 - Artist 1")
var testing2 = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_Song 2 - Artist 2")

records.delete()
properties.delete()
testing1.delete()
testing2.delete()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ class OfflineSongListTest {
@Test
fun noSongsAvailableInitialization() {
val context = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext
val mySongsList = OfflineSongList(context)
val path = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
lateinit var mySongsList: OfflineSongList
if (path != null) {
mySongsList = OfflineSongList(path)
}

val inputSongsList = mutableListOf<Pair<String, String>>()
val page = "1"
Expand All @@ -35,7 +39,11 @@ class OfflineSongListTest {
records.createNewFile()
records.appendText("bones - imagine dragons\n")

val mySongsList = OfflineSongList(context)
val path = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
lateinit var mySongsList: OfflineSongList
if (path != null) {
mySongsList = OfflineSongList(path)
}

val songName = "bones"
val artistName = "imagine dragons"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ class BuzzerScreenActivity : GameActivity() {
val title = gameManager.getCurrentSong().getTrackName()
val artist = gameManager.getCurrentSong().getArtistName()
findViewById<TextView>(R.id.songTitle).text= "$title - $artist"
Glide.with(ctx).load(gameManager.getCurrentSong().getArtworkUrl()).override(artworkDim, artworkDim).into(findViewById(R.id.songArtwork))
//Checks if internet is available. If not, skip the loading of the artwork from url.
if(gameManager.getInternet()) {
Glide.with(ctx).load(gameManager.getCurrentSong().getArtworkUrl()).override(artworkDim, artworkDim).into(findViewById(R.id.songArtwork))
}
gameManager.playSong()
checkRunnable()
super.barTimer(findViewById(R.id.progressBarBuzzer), ctx, gameManager, ::timeoutAnswer)
Expand Down
Loading