From a628b771d2a2e0f3e3eb9aec16daec7509a16ae7 Mon Sep 17 00:00:00 2001 From: zwierski Date: Thu, 9 Jun 2022 22:49:27 +0200 Subject: [PATCH] refactor score updater + rename buzzerGameActivity --- .../ch/sdp/vibester/BuzzerScoreUpdaterTest.kt | 15 +----- .../activity/BuzzerSetupActivityTest.kt | 10 ++-- .../activity/game/BuzzerScreenActivityTest.kt | 50 +++++++++---------- app/src/main/AndroidManifest.xml | 2 +- .../ch/sdp/vibester/BuzzerScoreUpdater.kt | 5 +- .../vibester/activity/BuzzerSetupActivity.kt | 4 +- ...creenActivity.kt => BuzzerGameActivity.kt} | 7 ++- ...er_screen.xml => activity_buzzer_game.xml} | 0 .../main/res/layout/activity_end_multiple.xml | 16 +++--- 9 files changed, 48 insertions(+), 61 deletions(-) rename app/src/main/java/ch/sdp/vibester/activity/game/{BuzzerScreenActivity.kt => BuzzerGameActivity.kt} (96%) rename app/src/main/res/layout/{activity_buzzer_screen.xml => activity_buzzer_game.xml} (100%) diff --git a/app/src/androidTest/java/ch/sdp/vibester/BuzzerScoreUpdaterTest.kt b/app/src/androidTest/java/ch/sdp/vibester/BuzzerScoreUpdaterTest.kt index 6fa02f7a7..0dbabbef0 100644 --- a/app/src/androidTest/java/ch/sdp/vibester/BuzzerScoreUpdaterTest.kt +++ b/app/src/androidTest/java/ch/sdp/vibester/BuzzerScoreUpdaterTest.kt @@ -25,7 +25,7 @@ public class BuzzerScoreUpdaterTest { val scoreArray = arrayOf(0, 0, 0, 0) val testUpdater = BuzzerScoreUpdater(idArray, scoreArray) for (id in idArray) { - testUpdater.updateScoresArray(id, true) + testUpdater.updateScoresArray(id) assertTrue(testUpdater.getMap()[id] == 1) } } @@ -35,23 +35,12 @@ public class BuzzerScoreUpdaterTest { val idArray = arrayListOf(R.id.buzzer_0, R.id.buzzer_1, R.id.buzzer_2, R.id.buzzer_3) val scoreArray = arrayOf(0, 0, 0, 0) val testUpdater = BuzzerScoreUpdater(idArray, scoreArray) - testUpdater.updateScoresArray(-1, true) + testUpdater.updateScoresArray(-1) for (id in idArray) { assertEquals(0, testUpdater.getMap()[id]) } } - @Test - fun penaltyOnScoreZeroMakesTheScoreRemainZero() { - val idArray = arrayListOf(R.id.buzzer_0, R.id.buzzer_1, R.id.buzzer_2, R.id.buzzer_3) - val scoreArray = arrayOf(0, 0, 0, 0) - val testUpdater = BuzzerScoreUpdater(idArray, scoreArray) - for (id in idArray) { - testUpdater.updateScoresArray(id, false) - assertEquals(0, testUpdater.getMap()[id]) - } - } - @Test fun noWinnerTest() { val idArray = arrayListOf(R.id.buzzer_0, R.id.buzzer_1, R.id.buzzer_2, R.id.buzzer_3) diff --git a/app/src/androidTest/java/ch/sdp/vibester/activity/BuzzerSetupActivityTest.kt b/app/src/androidTest/java/ch/sdp/vibester/activity/BuzzerSetupActivityTest.kt index 18fe55786..7118607d5 100644 --- a/app/src/androidTest/java/ch/sdp/vibester/activity/BuzzerSetupActivityTest.kt +++ b/app/src/androidTest/java/ch/sdp/vibester/activity/BuzzerSetupActivityTest.kt @@ -14,7 +14,7 @@ import androidx.test.espresso.matcher.ViewMatchers.* import androidx.test.ext.junit.rules.ActivityScenarioRule import androidx.test.ext.junit.runners.AndroidJUnit4 import ch.sdp.vibester.R -import ch.sdp.vibester.activity.game.BuzzerScreenActivity +import ch.sdp.vibester.activity.game.BuzzerGameActivity import ch.sdp.vibester.api.LastfmMethod import ch.sdp.vibester.helper.GameManager import dagger.hilt.android.testing.HiltAndroidRule @@ -134,7 +134,7 @@ class BuzzerSetupActivityTest { onData(Matchers.anything()).atPosition(0).perform(scrollTo(),click()) enterNames(1) onView(withId(R.id.nb_players_selected)).perform(click()) - intended(hasComponent(BuzzerScreenActivity::class.java.name)) + intended(hasComponent(BuzzerGameActivity::class.java.name)) intended(hasExtra("Number of players", 1)) } @@ -147,7 +147,7 @@ class BuzzerSetupActivityTest { onData(Matchers.anything()).atPosition(1).perform(scrollTo(),click()) enterNames(2) onView(withId(R.id.nb_players_selected)).perform(click()) - intended(hasComponent(BuzzerScreenActivity::class.java.name)) + intended(hasComponent(BuzzerGameActivity::class.java.name)) intended(hasExtra("Number of players", 2)) } @@ -160,7 +160,7 @@ class BuzzerSetupActivityTest { onData(Matchers.anything()).atPosition(2).perform(scrollTo(),click()) enterNames(3) onView(withId(R.id.nb_players_selected)).perform(click()) - intended(hasComponent(BuzzerScreenActivity::class.java.name)) + intended(hasComponent(BuzzerGameActivity::class.java.name)) intended(hasExtra("Number of players", 3)) } @@ -173,7 +173,7 @@ class BuzzerSetupActivityTest { onData(Matchers.anything()).atPosition(3).perform(scrollTo(),click()) enterNames(4) onView(withId(R.id.nb_players_selected)).perform(click()) - intended(hasComponent(BuzzerScreenActivity::class.java.name)) + intended(hasComponent(BuzzerGameActivity::class.java.name)) intended(hasExtra("Number of players", 4)) } diff --git a/app/src/androidTest/java/ch/sdp/vibester/activity/game/BuzzerScreenActivityTest.kt b/app/src/androidTest/java/ch/sdp/vibester/activity/game/BuzzerScreenActivityTest.kt index d5fd70f3b..d323a7b47 100644 --- a/app/src/androidTest/java/ch/sdp/vibester/activity/game/BuzzerScreenActivityTest.kt +++ b/app/src/androidTest/java/ch/sdp/vibester/activity/game/BuzzerScreenActivityTest.kt @@ -30,7 +30,7 @@ class BuzzerScreenActivityTest { @get:Rule(order = 1) val testRule = ActivityScenarioRule( - BuzzerScreenActivity::class.java + BuzzerGameActivity::class.java ) @Before @@ -74,7 +74,7 @@ class BuzzerScreenActivityTest { @Test fun scoresLayoutIsDisplayed() { val intent = - Intent(ApplicationProvider.getApplicationContext(), BuzzerScreenActivity::class.java) + Intent(ApplicationProvider.getApplicationContext(), BuzzerGameActivity::class.java) // Put mock extras inside val mockPlayersNumber = 2 @@ -83,14 +83,14 @@ class BuzzerScreenActivityTest { intent.putExtra("Number of players", mockPlayersNumber) intent.putExtra("Player Names", mockNameArray) intent.putExtra("gameManager", setGameManager()) - val scn: ActivityScenario = ActivityScenario.launch(intent) + val scn: ActivityScenario = ActivityScenario.launch(intent) onView(withId(R.id.scoresTable)).check(matches(isDisplayed())) } @Test fun answerAndNextArePresentButInvisibleOnStartup() { val ctx = ApplicationProvider.getApplicationContext() as Context - val intent = Intent(ctx, BuzzerScreenActivity::class.java) + val intent = Intent(ctx, BuzzerGameActivity::class.java) // Put mock extras inside val mockPlayersNumber = 2 @@ -99,7 +99,7 @@ class BuzzerScreenActivityTest { intent.putExtra("Number of players", mockPlayersNumber) intent.putExtra("Player Names", mockNameArray) intent.putExtra("gameManager", setGameManager()) - val scn: ActivityScenario = ActivityScenario.launch(intent) + val scn: ActivityScenario = ActivityScenario.launch(intent) onView(withId(R.id.answer)).check(matches(withEffectiveVisibility(Visibility.INVISIBLE))) onView(withId(R.id.nextSongBuzzer)).check(matches(withEffectiveVisibility(Visibility.GONE))) } @@ -107,7 +107,7 @@ class BuzzerScreenActivityTest { @Test fun testDisplayOfBuzzersWithNames() { val ctx = ApplicationProvider.getApplicationContext() as Context - val intent = Intent(ctx, BuzzerScreenActivity::class.java) + val intent = Intent(ctx, BuzzerGameActivity::class.java) // Put mock extras inside val mockPlayersNumber = 2 @@ -117,7 +117,7 @@ class BuzzerScreenActivityTest { intent.putExtra("Player Names", mockNameArray) val gameManager = setGameManager() intent.putExtra("gameManager", gameManager) - val scn: ActivityScenario = ActivityScenario.launch(intent) + val scn: ActivityScenario = ActivityScenario.launch(intent) // Did the round start? onView(withId(R.id.buzzer_0)).check(matches(withEffectiveVisibility(Visibility.VISIBLE))) @@ -130,7 +130,7 @@ class BuzzerScreenActivityTest { fun pressingBuzzerDuringRoundMakesAnswerVisible() { val ctx = ApplicationProvider.getApplicationContext() as Context - val intent = Intent(ctx, BuzzerScreenActivity::class.java) + val intent = Intent(ctx, BuzzerGameActivity::class.java) // Put mock extras inside val mockPlayersNumber = 2 @@ -140,7 +140,7 @@ class BuzzerScreenActivityTest { intent.putExtra("Player Names", mockNameArray) val gameManager = setGameManager() intent.putExtra("gameManager", gameManager) - val scn: ActivityScenario = ActivityScenario.launch(intent) + val scn: ActivityScenario = ActivityScenario.launch(intent) onView(withId(R.id.buzzer_0)).check(matches(withEffectiveVisibility(Visibility.VISIBLE))).perform(click()) onView(withId(R.id.answer)).check(matches(withEffectiveVisibility(Visibility.VISIBLE))) @@ -153,7 +153,7 @@ class BuzzerScreenActivityTest { AppPreferences.setStr(ctx.getString(R.string.preferences_game_mode), "local_buzzer") AppPreferences.setStr(ctx.getString(R.string.preferences_game_genre), "BTS") - val intent = Intent(ctx, BuzzerScreenActivity::class.java) + val intent = Intent(ctx, BuzzerGameActivity::class.java) // Put mock extras inside val mockPlayersNumber = 2 @@ -165,7 +165,7 @@ class BuzzerScreenActivityTest { gameManager.setNextSong() intent.putExtra("gameManager", gameManager) - val scn: ActivityScenario = ActivityScenario.launch(intent) + val scn: ActivityScenario = ActivityScenario.launch(intent) scn.onActivity { activity -> activity.timeoutAnswer(ctx, null, gameManager) Assert.assertEquals(false, activity.testGetGameIsOn()) @@ -175,7 +175,7 @@ class BuzzerScreenActivityTest { @Test fun skipTest() { val ctx = ApplicationProvider.getApplicationContext() as Context - val intent = Intent(ctx, BuzzerScreenActivity::class.java) + val intent = Intent(ctx, BuzzerGameActivity::class.java) // Put mock extras inside val mockPlayersNumber = 2 @@ -185,7 +185,7 @@ class BuzzerScreenActivityTest { val gameManager = setGameManager() intent.putExtra("gameManager", gameManager) - val scn: ActivityScenario = ActivityScenario.launch(intent) + val scn: ActivityScenario = ActivityScenario.launch(intent) onView(withId(R.id.skip_buzzer)).check(matches(withEffectiveVisibility(Visibility.VISIBLE))).perform(click()) scn.onActivity { activity -> Assert.assertEquals(false, activity.testGetGameIsOn()) } @@ -194,7 +194,7 @@ class BuzzerScreenActivityTest { @Test fun setAnswerButtonCorrectTest() { val ctx = ApplicationProvider.getApplicationContext() as Context - val intent = Intent(ctx, BuzzerScreenActivity::class.java) + val intent = Intent(ctx, BuzzerGameActivity::class.java) // Put mock extras inside val mockPlayersNumber = 2 @@ -204,7 +204,7 @@ class BuzzerScreenActivityTest { val gameManager = setGameManager() intent.putExtra("gameManager", gameManager) - val scn: ActivityScenario = ActivityScenario.launch(intent) + val scn: ActivityScenario = ActivityScenario.launch(intent) onView(withId(R.id.buzzer_0)).check(matches(withEffectiveVisibility(Visibility.VISIBLE))).perform(click()) onView(withId(R.id.buttonCorrect)).check(matches(withEffectiveVisibility(Visibility.VISIBLE))).perform(click()) @@ -219,7 +219,7 @@ class BuzzerScreenActivityTest { fun setAnswerButtonWrongTest() { val ctx = ApplicationProvider.getApplicationContext() as Context - val intent = Intent(ctx, BuzzerScreenActivity::class.java) + val intent = Intent(ctx, BuzzerGameActivity::class.java) // Put mock extras inside val mockPlayersNumber = 2 @@ -229,7 +229,7 @@ class BuzzerScreenActivityTest { val gameManager = setGameManager() intent.putExtra("gameManager", gameManager) - val scn: ActivityScenario = ActivityScenario.launch(intent) + val scn: ActivityScenario = ActivityScenario.launch(intent) onView(withId(R.id.buzzer_0)).check(matches(withEffectiveVisibility(Visibility.VISIBLE))).perform(click()) onView(withId(R.id.buttonWrong)).check(matches(withEffectiveVisibility(Visibility.VISIBLE))).perform(click()) @@ -247,11 +247,11 @@ class BuzzerScreenActivityTest { val gameManager = setGameManager(2) Assert.assertEquals(gameManager.getSongList().size, 2) - val intent = Intent(ApplicationProvider.getApplicationContext(), BuzzerScreenActivity::class.java) + val intent = Intent(ApplicationProvider.getApplicationContext(), BuzzerGameActivity::class.java) intent.putExtra("Number of players", mockPlayersNumber) intent.putExtra("Player Names", mockNameArray) intent.putExtra("gameManager", gameManager) - val scn: ActivityScenario = ActivityScenario.launch(intent) + val scn: ActivityScenario = ActivityScenario.launch(intent) scn.onActivity { activity -> activity.testProgressBar() } @@ -270,7 +270,7 @@ class BuzzerScreenActivityTest { fun prepareWinnerAnnouncementTestNoWinner() { val ctx = ApplicationProvider.getApplicationContext() as Context - val intent = Intent(ctx, BuzzerScreenActivity::class.java) + val intent = Intent(ctx, BuzzerGameActivity::class.java) // Put mock extras inside val mockPlayersNumber = 4 @@ -281,7 +281,7 @@ class BuzzerScreenActivityTest { val gameManager = setGameManager() intent.putExtra("gameManager", gameManager) - val scn: ActivityScenario = ActivityScenario.launch(intent) + val scn: ActivityScenario = ActivityScenario.launch(intent) val idArray = arrayListOf(R.id.buzzer_0, R.id.buzzer_1, R.id.buzzer_2, R.id.buzzer_3) val scoreArray = arrayOf(0, 0, 0, 0) @@ -299,7 +299,7 @@ class BuzzerScreenActivityTest { fun prepareWinnerAnnouncementTestOneWinner() { val ctx = ApplicationProvider.getApplicationContext() as Context - val intent = Intent(ctx, BuzzerScreenActivity::class.java) + val intent = Intent(ctx, BuzzerGameActivity::class.java) // Put mock extras inside val mockPlayersNumber = 4 @@ -309,7 +309,7 @@ class BuzzerScreenActivityTest { val gameManager = setGameManager() intent.putExtra("gameManager", gameManager) - val scn: ActivityScenario = ActivityScenario.launch(intent) + val scn: ActivityScenario = ActivityScenario.launch(intent) val idArray = arrayListOf(R.id.buzzer_0, R.id.buzzer_1, R.id.buzzer_2, R.id.buzzer_3) val scoreArray = arrayOf(0, 1, 0, 0) @@ -326,7 +326,7 @@ class BuzzerScreenActivityTest { @Test fun prepareWinnerAnnouncementTestMoreThanOneWinner() { val ctx = ApplicationProvider.getApplicationContext() as Context - val intent = Intent(ctx, BuzzerScreenActivity::class.java) + val intent = Intent(ctx, BuzzerGameActivity::class.java) // Put mock extras inside val mockPlayersNumber = 4 @@ -337,7 +337,7 @@ class BuzzerScreenActivityTest { val gameManager = setGameManager() intent.putExtra("gameManager", gameManager) - val scn: ActivityScenario = ActivityScenario.launch(intent) + val scn: ActivityScenario = ActivityScenario.launch(intent) val idArray = arrayListOf(R.id.buzzer_0, R.id.buzzer_1, R.id.buzzer_2, R.id.buzzer_3) val scoreArray = arrayOf(2, 2, 1, 2) val testUpdater = BuzzerScoreUpdater(idArray, scoreArray) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a02d00eda..b1d16a04f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -52,7 +52,7 @@ android:name=".activity.game.TypingGameActivity" android:exported="false" /> , scores: Array) { * updates the score corresponding to the pressed buzzer * checks first if the given id is a buzzer id (and not NO_BUZZER_PRESSED) */ - fun updateScoresArray(id: Int, answerCorrect: Boolean) { + fun updateScoresArray(id: Int) { if (!buzzerToScoreMap.keys.contains(id)) { return } - val point = if (answerCorrect) {1} else {0} - val updatedScore = max(buzzerToScoreMap.getOrDefault(id, 0) + point, 0) // should never get to default + val updatedScore = buzzerToScoreMap.getOrDefault(id, 0) + 1 // should never get to default buzzerToScoreMap.put(id, updatedScore) } diff --git a/app/src/main/java/ch/sdp/vibester/activity/BuzzerSetupActivity.kt b/app/src/main/java/ch/sdp/vibester/activity/BuzzerSetupActivity.kt index 35fd64a2d..bc55b3b6d 100644 --- a/app/src/main/java/ch/sdp/vibester/activity/BuzzerSetupActivity.kt +++ b/app/src/main/java/ch/sdp/vibester/activity/BuzzerSetupActivity.kt @@ -8,7 +8,7 @@ import android.widget.* import androidx.appcompat.app.AppCompatActivity import androidx.core.view.children import ch.sdp.vibester.R -import ch.sdp.vibester.activity.game.BuzzerScreenActivity +import ch.sdp.vibester.activity.game.BuzzerGameActivity import ch.sdp.vibester.helper.GameManager /** @@ -91,7 +91,7 @@ class BuzzerSetupActivity : AppCompatActivity(), AdapterView.OnItemSelectedListe fun switchToGame(view: View) { - val intent = Intent(this, BuzzerScreenActivity::class.java) + val intent = Intent(this, BuzzerGameActivity::class.java) val players = findViewById(R.id.playerNames).children.filter { child: View -> child.visibility == View.VISIBLE } val pNameArray = arrayOfNulls(players.count()) diff --git a/app/src/main/java/ch/sdp/vibester/activity/game/BuzzerScreenActivity.kt b/app/src/main/java/ch/sdp/vibester/activity/game/BuzzerGameActivity.kt similarity index 96% rename from app/src/main/java/ch/sdp/vibester/activity/game/BuzzerScreenActivity.kt rename to app/src/main/java/ch/sdp/vibester/activity/game/BuzzerGameActivity.kt index c06a7eb49..ecd89f2c0 100644 --- a/app/src/main/java/ch/sdp/vibester/activity/game/BuzzerScreenActivity.kt +++ b/app/src/main/java/ch/sdp/vibester/activity/game/BuzzerGameActivity.kt @@ -17,7 +17,7 @@ import com.bumptech.glide.Glide import kotlin.collections.ArrayList import kotlin.collections.HashMap -class BuzzerScreenActivity : GameActivity() { +class BuzzerGameActivity : GameActivity() { private val artworkDim = 200 private val noBuzzerPressed = -1 @@ -58,7 +58,7 @@ class BuzzerScreenActivity : GameActivity() { requestWindowFeature(Window.FEATURE_NO_TITLE) super.onCreate(savedInstanceState) supportActionBar?.hide() - setContentView(R.layout.activity_buzzer_screen) + setContentView(R.layout.activity_buzzer_game) val intentExtras = intent.extras if (intentExtras != null) { @@ -225,10 +225,9 @@ class BuzzerScreenActivity : GameActivity() { answer.visibility = View.INVISIBLE if (pressedBuzzer >= 0) { if(button.id==R.id.buttonCorrect) { - scoreUpdater.updateScoresArray(pressedBuzzer, true) + scoreUpdater.updateScoresArray(pressedBuzzer) gameManager.addCorrectSong() } else { - scoreUpdater.updateScoresArray(pressedBuzzer, false) gameManager.addWrongSong() } val view = map[pressedBuzzer]?.let { it1 -> findViewById(it1) } diff --git a/app/src/main/res/layout/activity_buzzer_screen.xml b/app/src/main/res/layout/activity_buzzer_game.xml similarity index 100% rename from app/src/main/res/layout/activity_buzzer_screen.xml rename to app/src/main/res/layout/activity_buzzer_game.xml diff --git a/app/src/main/res/layout/activity_end_multiple.xml b/app/src/main/res/layout/activity_end_multiple.xml index 41e776def..03ddc12fa 100644 --- a/app/src/main/res/layout/activity_end_multiple.xml +++ b/app/src/main/res/layout/activity_end_multiple.xml @@ -47,7 +47,7 @@