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

Jwen/lyric save score #258

Merged
merged 12 commits into from
May 7, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,35 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.sdp.vibester.R
import ch.sdp.vibester.TestMode
import ch.sdp.vibester.api.LastfmMethod
import ch.sdp.vibester.database.DataGetter
import ch.sdp.vibester.helper.BuzzerGameManager
import ch.sdp.vibester.helper.TypingGameManager
import dagger.hilt.android.testing.BindValue
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import io.mockk.every
import io.mockk.mockk
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class BuzzerScreenActivityTest {

@get:Rule
@get:Rule(order = 0)
var hiltRule = HiltAndroidRule(this)

@get:Rule(order = 1)
val testRule = ActivityScenarioRule(
BuzzerScreenActivity::class.java
)

@Before
fun setUp() {
hiltRule.inject()
Intents.init()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.sdp.vibester.R
import ch.sdp.vibester.api.LastfmMethod
import ch.sdp.vibester.helper.BuzzerGameManager
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.hamcrest.Matchers
import org.junit.After
import org.junit.Assert.assertEquals
Expand All @@ -27,8 +29,12 @@ import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class BuzzerSetupActivityTest {
@get: Rule
@get:Rule(order = 0)
var hiltRule = HiltAndroidRule(this)

@get:Rule(order = 1)
val activityRule = ActivityScenarioRule(BuzzerSetupActivity::class.java)

val mockArray = arrayOf("Player1", "Player2", "Player3", "Player4")
Expand All @@ -47,6 +53,7 @@ class BuzzerSetupActivityTest {

@Before
fun setUp() {
hiltRule.inject()
Intents.init()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.GrantPermissionRule
import ch.sdp.vibester.R
import ch.sdp.vibester.api.LastfmMethod
import ch.sdp.vibester.database.DataGetter
import ch.sdp.vibester.helper.TypingGameManager
import dagger.hilt.android.testing.BindValue
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import io.mockk.every
import io.mockk.mockk
import org.hamcrest.CoreMatchers.not
import org.junit.After
import org.junit.Assert.assertEquals
Expand All @@ -26,6 +32,7 @@ import org.junit.runner.RunWith


@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class LyricsBelongGameActivityTest {

private val sleepTime: Long = 5000
Expand Down Expand Up @@ -113,7 +120,10 @@ class LyricsBelongGameActivityTest {
return gameManager
}

@get: Rule
@get:Rule(order = 0)
var hiltRule = HiltAndroidRule(this)

@get:Rule(order = 1)
val activityRule = ActivityScenarioRule(LyricsBelongGameActivity::class.java)

@get:Rule
Expand All @@ -122,26 +132,36 @@ class LyricsBelongGameActivityTest {

@Before
fun setUp() {
hiltRule.inject()
Intents.init()
}

@After
fun clean() {
Intents.release()
}
/*
@Test
fun elementsShouldBeDisplayedOnCreate() {
onView(withId(R.id.btnSpeak)).check(matches(isDisplayed()))
onView(withId(R.id.progressBarLyrics)).check(matches(isDisplayed()))
}

*/
@BindValue
@JvmField
val mockUsersRepo = mockk<DataGetter>()

private fun createMockInvocation() {
every { mockUsersRepo.setSubFieldValue(any(), any(), any(), any()) } answers {}
every { mockUsersRepo.updateFieldInt(any(), any(), any(), any()) } answers {}
every { mockUsersRepo.setFieldValue(any(), any(), any()) } answers {}
every { mockUsersRepo.updateSubFieldInt(any(), any(), any(), any(), any()) } answers {}
}

// FIXME: this test fails after implement QR code reader for no reason
/*@Test
fun elementsShouldBeDisplayedOnCreate() {
onView(withId(R.id.btnSpeak)).check(matches(isDisplayed()))
onView(withId(R.id.progressBarLyrics)).check(matches(isDisplayed()))
}*/

@Test
fun handleLyricsNoFoundCorrectly() {
createMockInvocation()
val gameManager = setGameManager()
val intent = Intent(
ApplicationProvider.getApplicationContext(),
Expand All @@ -154,15 +174,16 @@ class LyricsBelongGameActivityTest {
}
/** 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()))
// 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)
// assertEquals(true, gameManager.getScore() == 0)
// assertEquals(true, gameManager.getWrongSongs().size == 0)
}

@Test
fun shouldUpdateSpeechFromInput() {
createMockInvocation()
val intent = Intent(
ApplicationProvider.getApplicationContext(),
LyricsBelongGameActivity::class.java
Expand All @@ -171,11 +192,13 @@ class LyricsBelongGameActivityTest {
scn.onActivity { activity ->
activity.testUpdateSpeechResult("hey")
}

onView(withId(R.id.lyricResult)).check(matches(withText("hey")))
}

@Test
fun nextButtonOnClick() {
createMockInvocation()
val gameManager = setGameManager(2)
val intent = Intent(ApplicationProvider.getApplicationContext(), LyricsBelongGameActivity::class.java)
intent.putExtra("gameManager", gameManager)
Expand All @@ -194,15 +217,17 @@ class LyricsBelongGameActivityTest {
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
// FIXME: this test fails after implement QR code reader for no reason
/* @Test
fun btnCheckVisibilityAfterSpeak() {
createMockInvocation()
val intent = Intent(
ApplicationProvider.getApplicationContext(),
LyricsBelongGameActivity::class.java
Expand All @@ -212,13 +237,14 @@ class LyricsBelongGameActivityTest {
scn.onActivity { activity ->
activity.testUpdateSpeechResult("hey")
}

onView(withId(R.id.lyricMatchButton)).check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
}
}*/

*/

@Test
fun getAndCheckLyricsGivesCorrectAnswerWhenMatch() {
createMockInvocation()
val gameManager = setGameManager()
gameManager.setNextSong()
val intent = Intent(
Expand All @@ -230,14 +256,15 @@ class LyricsBelongGameActivityTest {
scn.onActivity { activity ->
activity.testGetAndCheckLyrics(ctx, songName, artistName, speechInputCorrect, gameManager)
}
//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)
/*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
fun checkIntentOnEndingForWrongSong() {
createMockInvocation()
val gameManager = setGameManager()
gameManager.setNextSong()
gameManager.gameSize = 1
Expand Down Expand Up @@ -271,6 +298,7 @@ class LyricsBelongGameActivityTest {

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

Expand All @@ -285,6 +313,7 @@ class LyricsBelongGameActivityTest {
currentArtist = activity.getArtistName()
currentSong = activity.getSongName()
}

onView(withId(R.id.lyricMatchButton)).check(matches(not(isDisplayed())))
assertEquals(artistName, currentArtist)
assertEquals("Monday", currentSong)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Whole file) Great job on adding mocks and removing Thread.sleep instances 👍

Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/ch/sdp/vibester/activity/GameActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,30 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import ch.sdp.vibester.R
import ch.sdp.vibester.api.BitmapGetterApi
import ch.sdp.vibester.auth.FireBaseAuthenticator
import ch.sdp.vibester.database.DataGetter
import ch.sdp.vibester.helper.GameManager
import ch.sdp.vibester.model.Song
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
* Common set up for all games (difficulty level, progress bar)
* CHECK IF THE DOCS ARE CORRECT OR NOT!
*/
@AndroidEntryPoint
open class GameActivity : AppCompatActivity() {
open val handler = Handler()
open var maxTime: Int = 30
var runnable: Runnable? = null

@Inject
lateinit var dataGetter: DataGetter

/**
* Sets the countdown timer's maximum(initial) value.
*/
Expand Down Expand Up @@ -124,6 +132,18 @@ open class GameActivity : AppCompatActivity() {
return !gameManager.checkGameStatus() || !gameManager.setNextSong()
}

/**
* Function to set scores in the end of the game
*/
fun setScores(gameManager: GameManager) {
if(FireBaseAuthenticator.isLoggedIn()){
dataGetter.updateFieldInt(FireBaseAuthenticator.getCurrentUID(), "totalGames", 1, method = "sum")
dataGetter.updateFieldInt(FireBaseAuthenticator.getCurrentUID(), "correctSongs", gameManager.getCorrectSongs().size, method = "sum")
dataGetter.updateFieldInt(FireBaseAuthenticator.getCurrentUID(), "bestScore", gameManager.getScore(), method = "best")
dataGetter.updateSubFieldInt(FireBaseAuthenticator.getCurrentUID(), gameManager.getScore(), "scores", gameManager.gameMode, method = "best")
}
}

/**
* Function used for testing. Do not call unless it is for that specific purpose.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class LyricsBelongGameActivity : GameActivity() {
}

override fun endRound(gameManager: GameManager, callback: (() -> Unit)?) {
super.endRound(gameManager, null)
super.endRound(gameManager, this::setScores)
toggleBtnVisibility(R.id.nextSongButton, true)
}

Expand Down Expand Up @@ -201,10 +201,22 @@ class LyricsBelongGameActivity : GameActivity() {
handler.post(runnable!!)
}

/** helper functions to test private functions */
/**
* Function to set scores in the end of the game
*/
private fun setScores() {
if(::gameManager.isInitialized) {
super.setScores(gameManager)
}
}

/*
* The following functions are helper for testing
*/
fun testCheckLyrics(ctx: Context, lyricToBeCheck: String, lyrics: String, gameManager: GameManager) {
checkAnswer(ctx, lyricToBeCheck, lyrics, gameManager)
}

fun testUpdateSpeechResult(speechInput: String) {
updateSpeechResult(speechInput)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ class ProfileActivity : AppCompatActivity() {

if(name == "username"){
dataGetter.setFieldValue(FireBaseAuthenticator.getCurrentUID(), "username", input.text.toString())

}
}

Expand Down
Loading