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

Jwen/enhance lyric game #187

Merged
merged 40 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7143c02
gradle update
jiabaow Apr 7, 2022
66b7e5e
Merge branch 'main' of github.com:MaximeZmt/SDP_2022-Vibester into main
jiabaow Apr 8, 2022
e1f7f7e
toggle next button
jiabaow Apr 11, 2022
1434c7c
delete activity_lyric_temporary.xml
jiabaow Apr 11, 2022
72722b9
decrease time bar
jiabaow Apr 11, 2022
ef90a3c
handle no speech input
jiabaow Apr 11, 2022
6722a4c
question remark
jiabaow Apr 11, 2022
8566f86
refactor
jiabaow Apr 11, 2022
0ff3a88
skip lyricAPI call when no speech input when time's up
jiabaow Apr 11, 2022
ed9fb91
remove duplicate call to next round
jiabaow Apr 11, 2022
4c097ba
refactor check btn
jiabaow Apr 11, 2022
1795247
add doc
jiabaow Apr 11, 2022
9314f5e
use toast
jiabaow Apr 12, 2022
b64cb76
refactor timebar test && update lyricsNoFound test
jiabaow Apr 12, 2022
9bf3ae1
test next button
jiabaow Apr 12, 2022
35598bb
update tests
jiabaow Apr 12, 2022
e756b93
remove hard-coded first song
jiabaow Apr 12, 2022
f136aae
clean up and add doc
jiabaow Apr 12, 2022
992ccff
refactor image and text generator
jiabaow Apr 18, 2022
8e5b931
add image of the song in question for LyricsBelongGameActivity.kt
jiabaow Apr 18, 2022
ff528ff
update TypingGameActivityTest.kt
jiabaow Apr 18, 2022
237b6e3
extract showSongAndImage()
jiabaow Apr 18, 2022
2cbeb68
Merge branch 'main' of github.com:MaximeZmt/SDP_2022-Vibester into main
jiabaow Apr 18, 2022
382077b
remove hint
jiabaow Apr 18, 2022
878b3b0
resolve conflict with main
jiabaow Apr 18, 2022
65ccce3
update test
jiabaow Apr 18, 2022
0ad3926
comment handleLyricsNoFoundCorrectly
jiabaow Apr 18, 2022
31ebef2
comment the whole handleLyricsNoFoundCorrectly
jiabaow Apr 18, 2022
46300e0
modify time bar
jiabaow Apr 18, 2022
d48afc9
clean up
jiabaow Apr 18, 2022
b736bc7
add initialization check for gameManager
jiabaow Apr 18, 2022
552829e
update initialization check for gameManager
jiabaow Apr 18, 2022
fee51c2
update initialization check for gameManager
jiabaow Apr 18, 2022
de7441e
use string.xml
jiabaow Apr 18, 2022
4d422e0
Merge branch 'main' into jwen/enhance-lyric-game
MaximeZmt Apr 18, 2022
14d2163
extract toast msg
jiabaow Apr 18, 2022
6895045
Merge remote-tracking branch 'origin/jwen/enhance-lyric-game' into jw…
jiabaow Apr 18, 2022
92be18b
Merge branch 'main' of github.com:MaximeZmt/SDP_2022-Vibester into main
jiabaow Apr 18, 2022
92ae97b
Merge branch 'main' into jwen/enhance-lyric-game
jiabaow Apr 18, 2022
e98abab
extract toast msg
jiabaow Apr 18, 2022
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 @@ -5,6 +5,7 @@ import android.content.Intent
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.matcher.IntentMatchers
Expand All @@ -13,11 +14,8 @@ import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.GrantPermissionRule
import ch.sdp.vibester.R
import ch.sdp.vibester.api.ItunesMusicApi
import ch.sdp.vibester.api.LastfmMethod
import ch.sdp.vibester.helper.GameManager
import ch.sdp.vibester.model.Song
import okhttp3.OkHttpClient
import ch.sdp.vibester.helper.TypingGameManager
import org.hamcrest.CoreMatchers.not
import org.junit.*
import org.junit.Assert.assertEquals
Expand Down Expand Up @@ -91,30 +89,23 @@ class LyricsBelongGameActivityTest {
"Thunder, thun-, thunder\n" +
"Thun-thun-thunder, thunder"

private fun setGameManager() : GameManager {
val managerTxt = """
{"tracks":
{"track":[{"name":"Monday","duration":"259","mbid":"31623cce-9717-4513-9d83-1b5d04e44f9b",
"url":"https://www.last.fm/music/Oasis/_/Wonderwall",
"streamable":{"#text":"0","fulltrack":"0"},
"artist":{"name":"Imagine Dragons","mbid":"ecf9f3a3-35e9-4c58-acaa-e707fba45060","url":"https://www.last.fm/music/Oasis"},
"image":[{"#text":"https://lastfm.freetls.fastly.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png","size":"small"},
{"#text":"https://lastfm.freetls.fastly.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png","size":"medium"},
{"#text":"https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png","size":"large"},
{"#text":"https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png","size":"extralarge"}],
"@attr":{"rank":"1"}}],"@attr":{"tag":"british","page":"1","perPage":"1","totalPages":"66649","total":"66649"}}}
"""
val gameManager = GameManager()
gameManager.setGameSongList(managerTxt, LastfmMethod.BY_TAG.method)

gameManager.currentSong = getFirstSong() // hard-coded
return gameManager
}
private fun setGameManager(numSongs:Int = 1, valid: Boolean = true): TypingGameManager {
val epilogue = "{\"tracks\":{\"track\":["
val prologue =
"], \"@attr\":{\"tag\":\"british\",\"page\":\"1\",\"perPage\":\"1\",\"totalPages\":\"66649\",\"total\":\"66649\"}}}"
var middle = "{\"name\":\"Monday\",\"artist\":{\"name\":\"Imagine Dragons\"}}"
if(!valid) middle = "{\"name\":\"TEST_SONG_TEST\",\"artist\":{\"name\":\"TEST_ARTIST_TEST\"}}"
val gameManager = TypingGameManager()

var i = 0
var completeMiddle = middle
while(i < numSongs-1){
completeMiddle += ",$middle"
i++
}
gameManager.setGameSongList(epilogue + completeMiddle + prologue, LastfmMethod.BY_TAG.method)

private fun getFirstSong() :Song {
return Song.singleSong(
ItunesMusicApi.querySong(songName + " " + artistName, OkHttpClient(), 1).get()
)
return gameManager
}

@get: Rule
Expand All @@ -135,10 +126,8 @@ class LyricsBelongGameActivityTest {
}

@Test
fun buttonsShouldBeDisplayedOnCreate() {
fun elementsShouldBeDisplayedOnCreate() {
onView(withId(R.id.btnSpeak)).check(matches(isDisplayed()))
onView(withId(R.id.lyricMatchButton)).check(matches(not(isDisplayed())))
onView(withId(R.id.nextSongButton)).check(matches(isDisplayed()))
onView(withId(R.id.progressBarLyrics)).check(matches(isDisplayed()))
}

Expand All @@ -150,13 +139,17 @@ class LyricsBelongGameActivityTest {
LyricsBelongGameActivity::class.java
)
val scn: ActivityScenario<LyricsBelongGameActivity> = ActivityScenario.launch(intent)
val ctx = ApplicationProvider.getApplicationContext() as Context
scn.onActivity { activity ->
activity.testGetAndCheckLyrics("the best song in the world", "Mr.Mystery", "", gameManager)
activity.testGetAndCheckLyrics(ctx, "the best song in the world", "Mr.Mystery", "", gameManager)
}
//FIXME
// API takes a lot of time to process this request, thus resulting in wrong test output
// Thread.sleep(10000)
// onView(withId(R.id.lyricMatchResult)).check(matches(withText("No lyrics found, try another song")))
/** FIXME: API takes a lot of time to process this request
comment the following lines if this test fail */
Thread.sleep(sleepTime)
onView(withId(R.id.nextSongButton)).check(matches(isDisplayed()))
//song skipped, not consider as wrong
assertEquals(true, gameManager.getScore() == 0)
assertEquals(true, gameManager.getWrongSongs().size == 0)
}

@Test
Expand All @@ -173,67 +166,62 @@ class LyricsBelongGameActivityTest {
}

@Test
fun btnCheckVisibleAfterSpeak() {
val intent = Intent(
ApplicationProvider.getApplicationContext(),
LyricsBelongGameActivity::class.java
)
fun nextButtonOnClick() {
val gameManager = setGameManager(2)
val intent = Intent(ApplicationProvider.getApplicationContext(), LyricsBelongGameActivity::class.java)
intent.putExtra("gameManager", gameManager)
val scn: ActivityScenario<LyricsBelongGameActivity> = ActivityScenario.launch(intent)
scn.onActivity { activity ->
activity.testUpdateSpeechResult("hey")
}
onView(withId(R.id.lyricMatchResult)).check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
}
scn.onActivity { activityRule -> activityRule.testProgressBar() }
Thread.sleep(1000)

@Test
fun checkLyricsCorrectTest() {
val gameManager = setGameManager()
val intent = Intent(
ApplicationProvider.getApplicationContext(),
LyricsBelongGameActivity::class.java
)
val scn: ActivityScenario<LyricsBelongGameActivity> = ActivityScenario.launch(intent)
scn.onActivity { activity ->
activity.testCheckLyrics(speechInputCorrect, lyrics, gameManager)
}
Thread.sleep(sleepTime)
onView(withId(R.id.lyricMatchResult)).check(matches(withText("res: correct")))
assertEquals(1, gameManager.getCorrectSongs().size)
assertEquals(1, gameManager.getScore())
onView(withId(R.id.nextSongButton)).check(matches(isDisplayed())).perform(click())
scn.onActivity { activityRule -> activityRule.testProgressBar() }
Thread.sleep(1000)

val statNames: ArrayList<String> = arrayListOf()
val statName = "Total Score"
statNames.addAll(arrayOf(statName, statName, statName, statName, statName))

val statVal: ArrayList<String> = arrayListOf()
val score = "0"
statVal.addAll(arrayOf(score, score, score, score, score))
Intents.intended(IntentMatchers.hasComponent(GameEndingActivity::class.java.name))
Intents.intended(IntentMatchers.hasExtra("nbIncorrectSong", 2))
Intents.intended(IntentMatchers.hasExtra("str_arr_name", statNames))
Intents.intended(IntentMatchers.hasExtra("str_arr_val", statVal))
}

@Test
fun checkLyricsWrongTest() {
val gameManager = setGameManager()
fun btnCheckVisibilityAfterSpeak() {
val intent = Intent(
ApplicationProvider.getApplicationContext(),
LyricsBelongGameActivity::class.java
)
val scn: ActivityScenario<LyricsBelongGameActivity> = ActivityScenario.launch(intent)
onView(withId(R.id.lyricMatchButton)).check(matches(not(isDisplayed())))
scn.onActivity { activity ->
activity.testCheckLyrics(speechInputWrong, lyrics, gameManager)
activity.testUpdateSpeechResult("hey")
}
Thread.sleep(sleepTime)
onView(withId(R.id.lyricMatchResult)).check(matches(withText("res: too bad")))
assertEquals(true, gameManager.getScore() == 0)
assertEquals(1, gameManager.getWrongSongs().size)
onView(withId(R.id.lyricMatchButton)).check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
}

@Test
fun getAndCheckLyricsGivesCorrectAnswerWhenMatch() {
val gameManager = setGameManager()
gameManager.setNextSong()
val intent = Intent(
ApplicationProvider.getApplicationContext(),
LyricsBelongGameActivity::class.java
)
val scn: ActivityScenario<LyricsBelongGameActivity> = ActivityScenario.launch(intent)
val ctx = ApplicationProvider.getApplicationContext() as Context
scn.onActivity { activity ->
activity.testGetAndCheckLyrics(songName, artistName, speechInputCorrect, gameManager)
activity.testGetAndCheckLyrics(ctx, songName, artistName, speechInputCorrect, gameManager)
}
//FIXME
// API takes a lot of time to process this request, thus resulting in wrong test output
// Thread.sleep(10000)
// onView(withId(R.id.lyricMatchResult)).check(matches(withText("res: correct")))
//FIXME: API takes a lot of time to process this request
//comment the following lines if this test fail
Thread.sleep(sleepTime)
assertEquals(true, gameManager.getScore() == 1)
}

@Test
Expand All @@ -247,8 +235,7 @@ class LyricsBelongGameActivityTest {
val scn: ActivityScenario<LyricsBelongGameActivity> = ActivityScenario.launch(intent)
val ctx = ApplicationProvider.getApplicationContext() as Context
scn.onActivity { activity ->
activity.testCheckLyrics(speechInputWrong, lyrics, gameManager)
activity.playRound(gameManager)
activity.testCheckLyrics(ctx, speechInputWrong, lyrics, gameManager)
}
val incArray: ArrayList<String> = ArrayList(
gameManager.getWrongSongs().map { it.getTrackName() + " - " + it.getArtistName() })
Expand All @@ -272,8 +259,8 @@ class LyricsBelongGameActivityTest {

@Test
fun checkIntentOnNextRoundForCorrectSong() {
val gameManager = setGameManager()
gameManager.gameSize = 1
val gameManager = setGameManager(2)
gameManager.setNextSong()

var currentArtist = ""
var currentSong = ""
Expand All @@ -282,46 +269,17 @@ class LyricsBelongGameActivityTest {
val scn: ActivityScenario<LyricsBelongGameActivity> = ActivityScenario.launch(intent)
val ctx = ApplicationProvider.getApplicationContext() as Context
scn.onActivity { activity ->
activity.playRound(gameManager)
activity.testStartRound(ctx, gameManager)
currentArtist = activity.getArtistName()
currentSong = activity.getSongName()
}
onView(withId(R.id.lyricMatchResult)).check(matches(withText("result will show here")))
onView(withId(R.id.lyricMatchButton)).check(matches(not(isDisplayed())))
assertEquals(artistName, currentArtist)
assertEquals("Monday", currentSong)
onView(withId(R.id.lyricResult)).check(matches(withText("Say something from Monday - $artistName")))
onView(withId(R.id.progressBarLyrics)).check(matches(isDisplayed()))
assertEquals(1, gameManager.nextSongInd)
assertEquals(1, gameManager.numPlayedSongs)
}

@Test
fun setFirstSongTest() {
val gameManager = setGameManager()
val intent = Intent(
ApplicationProvider.getApplicationContext(),
LyricsBelongGameActivity::class.java
)
val scn: ActivityScenario<LyricsBelongGameActivity> = ActivityScenario.launch(intent)
scn.onActivity { activity ->
activity.testSetFirstSong(gameManager)
}
assertEquals(getFirstSong().getArtistName(), gameManager.currentSong.getArtistName())
assertEquals(getFirstSong().getTrackName(), gameManager.currentSong.getTrackName())
}

@Test
fun clearResultTest() {
val intent = Intent(
ApplicationProvider.getApplicationContext(),
LyricsBelongGameActivity::class.java
)
val scn: ActivityScenario<LyricsBelongGameActivity> = ActivityScenario.launch(intent)
scn.onActivity { activity ->
activity.testClearResult()
}
onView(withId(R.id.lyricMatchResult)).check(matches(withText("result will show here")))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import ch.sdp.vibester.R
import ch.sdp.vibester.api.LastfmMethod
import ch.sdp.vibester.helper.TypingGameManager
import ch.sdp.vibester.model.Song
import com.google.android.material.color.MaterialColors.getColor
import org.hamcrest.Matchers.allOf
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
Expand All @@ -44,7 +42,7 @@ class TypingGameActivityTest {
var i = 0
var completeMiddle = middle
while(i < numSongs-1){
completeMiddle += ","+middle
completeMiddle += ",$middle"
i++
}
gameManager.setGameSongList(epilogue + completeMiddle + prologue, LastfmMethod.BY_TAG.method)
Expand All @@ -71,7 +69,7 @@ class TypingGameActivityTest {
fun spaceGenTest() {
val height = 10
val width = 10
val spaceeeeee = TypingGameActivity.generateSpace(
val spaceeeeee = GameActivity.generateSpace(
width,
height,
ApplicationProvider.getApplicationContext()
Expand All @@ -84,7 +82,7 @@ class TypingGameActivityTest {
fun textGenTest() {
val txtInput = "hello"
val ctx = ApplicationProvider.getApplicationContext() as Context
val myText = TypingGameActivity.generateText(txtInput, ctx)
val myText = GameActivity.generateText(txtInput, ctx)
assertEquals(txtInput, myText.text.toString())
assertEquals(expectedSize, myText.minHeight)
assertEquals(ContextCompat.getColor(ctx, R.color.black), myText.textColors.defaultColor)
Expand All @@ -103,9 +101,8 @@ class TypingGameActivityTest {

val mySong = Song.singleSong(inputTxt)

val txtInput = "hello"
val ctx = ApplicationProvider.getApplicationContext() as Context
val myTest = TypingGameActivity.generateImage(mySong, ctx)
val myTest = GameActivity.generateImage(mySong, ctx)
assertEquals(expectedSize, myTest.minimumHeight)
assertEquals(expectedSize, myTest.minimumWidth)
}
Expand Down Expand Up @@ -157,7 +154,6 @@ class TypingGameActivityTest {
val songTest = Song.singleSong(inputTxt)
val gameManager = setGameManager()
gameManager.setNextSong()
lateinit var temp: Unit

val intent =
Intent(ApplicationProvider.getApplicationContext(), TypingGameActivity::class.java)
Expand All @@ -166,7 +162,7 @@ class TypingGameActivityTest {
val scn: ActivityScenario<TypingGameActivity> = ActivityScenario.launch(intent)
val ctx = ApplicationProvider.getApplicationContext() as Context
scn.onActivity { activity ->
temp = activity.checkAnswer(ctx, songTest, gameManager)
activity.checkAnswer(ctx, songTest, gameManager)
}
assertEquals(true, gameManager.getScore() == 1)
}
Expand All @@ -185,14 +181,13 @@ class TypingGameActivityTest {
val songTest = Song.singleSong(inputTxt)
val gameManager = setGameManager()
gameManager.setNextSong()
lateinit var temp: Unit

val intent =
Intent(ApplicationProvider.getApplicationContext(), TypingGameActivity::class.java)
val scn: ActivityScenario<TypingGameActivity> = ActivityScenario.launch(intent)
val ctx = ApplicationProvider.getApplicationContext() as Context
scn.onActivity { activity ->
temp = activity.checkAnswer(ctx, songTest, gameManager)
activity.checkAnswer(ctx, songTest, gameManager)
}
assertEquals(true, gameManager.getScore() == 0)
}
Expand Down Expand Up @@ -335,7 +330,7 @@ class TypingGameActivityTest {
intent.putExtra("gameManager", gameManager)
val scn: ActivityScenario<TypingGameActivity> = ActivityScenario.launch(intent)
val ctx = ApplicationProvider.getApplicationContext() as Context
var color:ColorStateList = ctx.getResources().getColorStateList(R.color.floral_white)
var color:ColorStateList = ctx.resources.getColorStateList(R.color.floral_white)

/* Test progress bar is yellow*/
scn.onActivity { activity ->
Expand All @@ -345,7 +340,7 @@ class TypingGameActivityTest {
scn.onActivity { activity->
color = activity.testProgressBarColor()!!
}
assertEquals(color, ctx.getResources().getColorStateList(R.color.maximum_yellow_red))
assertEquals(color, ctx.resources.getColorStateList(R.color.maximum_yellow_red))

/* Test progress bar is red*/
scn.onActivity { activity ->
Expand All @@ -355,7 +350,7 @@ class TypingGameActivityTest {
scn.onActivity { activity->
color = activity.testProgressBarColor()!!
}
assertEquals(color, ctx.getResources().getColorStateList(R.color.light_coral))
assertEquals(color, ctx.resources.getColorStateList(R.color.light_coral))
}

}
Loading