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

Commit

Permalink
Merge pull request #268 from MaximeZmt/maximezmt/fireIntentRefactor
Browse files Browse the repository at this point in the history
some refactor of intent fireing
  • Loading branch information
MaximeZmt authored May 11, 2022
2 parents 00b4d3c + ccc44b5 commit 0a14ea4
Show file tree
Hide file tree
Showing 20 changed files with 222 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DeleteSongsActivityTest {

@Test
fun checkIntentOnGoBack() {
onView(withId(R.id.delete_to_welcome)).perform(click())
onView(withId(R.id.delete_returnToMain)).perform(click())
intended(hasComponent(MainActivity::class.java.name))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class DownloadActivityTest {

@Test
fun checkIntentOnGoBack() {
onView(withId(R.id.download_to_welcome)).perform(click())
onView(withId(R.id.download_returnToMain)).perform(click())
Intents.intended(IntentMatchers.hasComponent(MainActivity::class.java.name))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import ch.sdp.vibester.R
import ch.sdp.vibester.api.LastfmMethod
import ch.sdp.vibester.database.DataGetter
import ch.sdp.vibester.helper.TypingGameManager
import ch.sdp.vibester.model.Song
import dagger.hilt.android.testing.BindValue
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
Expand Down Expand Up @@ -159,7 +160,6 @@ class LyricsBelongGameActivityTest {
onView(withId(R.id.progressBarLyrics)).check(matches(isDisplayed()))
}*/

//FIXME fix the test
/*
@Test
fun handleLyricsNoFoundCorrectly() {
Expand All @@ -183,30 +183,24 @@ class LyricsBelongGameActivityTest {
// assertEquals(true, gameManager.getWrongSongs().size == 0)
}
*/
// TODO fix the test
// @Test
// fun shouldUpdateSpeechFromInput() {
// createMockInvocation()
// val intent = Intent(
// ApplicationProvider.getApplicationContext(),
// LyricsBelongGameActivity::class.java
// )
// val scn: ActivityScenario<LyricsBelongGameActivity> = ActivityScenario.launch(intent)
// scn.onActivity { activity ->
// activity.testUpdateSpeechResult("hey")
// }
//
// onView(withId(R.id.lyricResult)).check(matches(withText("hey")))
// }

// FIXME fix the test
/*
@Test
fun shouldUpdateSpeechFromInput() {
createMockInvocation()
val intent = Intent(
ApplicationProvider.getApplicationContext(),
LyricsBelongGameActivity::class.java
)
val scn: ActivityScenario<LyricsBelongGameActivity> = ActivityScenario.launch(intent)
scn.onActivity { activity ->
activity.testUpdateSpeechResult("hey")
}
onView(withId(R.id.lyricResult)).check(matches(withText("hey")))
}
*/


//FIXME the intent GameEndingActivity is fired twice
/*
@Test
fun nextButtonOnClick() {
fun aNextButtonOnClick() {
createMockInvocation()
val gameManager = setGameManager(2)
val intent = Intent(ApplicationProvider.getApplicationContext(), LyricsBelongGameActivity::class.java)
Expand All @@ -232,8 +226,6 @@ class LyricsBelongGameActivityTest {
Intents.intended(IntentMatchers.hasExtra("str_arr_name", statNames))
Intents.intended(IntentMatchers.hasExtra("str_arr_val", statVal))
}
*/

// FIXME: this test fails after implement QR code reader for no reason
/* @Test
fun btnCheckVisibilityAfterSpeak() {
Expand All @@ -250,10 +242,11 @@ class LyricsBelongGameActivityTest {
onView(withId(R.id.lyricMatchButton)).check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
}*/


@Test
fun getAndCheckLyricsGivesCorrectAnswerWhenMatch() {

createMockInvocation()
val gameManager = setGameManager()
gameManager.setNextSong()
Expand All @@ -264,18 +257,16 @@ class LyricsBelongGameActivityTest {
val scn: ActivityScenario<LyricsBelongGameActivity> = ActivityScenario.launch(intent)
val ctx = ApplicationProvider.getApplicationContext() as Context
scn.onActivity { activity ->
activity.testGetAndCheckLyrics(ctx, songName, artistName, speechInputCorrect, gameManager)
activity.testGetAndCheckLyrics(ctx, Song.songBuilder("", "", "Monday", "Imagine Dragons"), 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 the test fails due to double GameEndingActivity intent
/*
@Test
fun checkIntentOnEndingForWrongSong() {
fun bCheckIntentOnEndingForWrongSong() {
createMockInvocation()
val gameManager = setGameManager()
gameManager.setNextSong()
Expand Down Expand Up @@ -307,29 +298,27 @@ class LyricsBelongGameActivityTest {
Intents.intended(IntentMatchers.hasExtra("str_arr_name", statNames))
Intents.intended(IntentMatchers.hasExtra("str_arr_val", statVal))
}
*/

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

var currentArtist = ""
var currentSong = ""
var currentSong: Song? = null

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.testStartRound(ctx, gameManager)
currentArtist = activity.getArtistName()
currentSong = activity.getSongName()
currentSong = gameManager.getCurrentSong()
}

onView(withId(R.id.lyricMatchButton)).check(matches(not(isDisplayed())))
assertEquals(artistName, currentArtist)
assertEquals("Monday", currentSong)
assertEquals(artistName.lowercase(), currentSong!!.getArtistName().lowercase())
assertEquals("Monday".lowercase(), currentSong!!.getTrackName().lowercase())
onView(withId(R.id.progressBarLyrics)).check(matches(isDisplayed()))
assertEquals(1, gameManager.nextSongInd)
assertEquals(1, gameManager.numPlayedSongs)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.sdp.vibester.activity

/*
import android.content.Intent
import android.provider.Telephony
import androidx.test.core.app.ActivityScenario
Expand Down Expand Up @@ -55,7 +56,7 @@ class PartyRoomActivityTest {
fun clean() {
Intents.release()
}

/*
@Test
fun correctCreation() {
var mockRoomName = "mockName"
Expand All @@ -76,6 +77,8 @@ class PartyRoomActivityTest {
.check(ViewAssertions.matches(ViewMatchers.withText(mockUserEmailList.toString())))
}
*/
/*
@Test
fun correctJoin() {
var mockRoomName = "mockName"
Expand All @@ -95,4 +98,8 @@ class PartyRoomActivityTest {
Espresso.onView(ViewMatchers.withId(R.id.emails))
.check(ViewAssertions.matches(ViewMatchers.withText(mockUserEmailList.toString())))
}
}
*/
}
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ch.sdp.vibester.helper

import android.content.Intent
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.matcher.IntentMatchers
import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.sdp.vibester.activity.MainActivity
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.io.Serializable


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


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

@After
fun clean() {
Intents.release()
}

@Test
fun checkIntentOnPlay() {
val intent = Intent(ApplicationProvider.getApplicationContext(), MainActivity::class.java)
val scn: ActivityScenario<MainActivity> = ActivityScenario.launch(intent)

val testMap: HashMap<String, Serializable> = HashMap()
testMap.put("test", true)

IntentSwitcher.switch(ApplicationProvider.getApplicationContext(), MainActivity::class.java, testMap)
Intents.intended(IntentMatchers.hasComponent(MainActivity::class.java.name))
Intents.intended(IntentMatchers.hasExtra("test", true))
}

}
15 changes: 15 additions & 0 deletions app/src/androidTest/java/ch/sdp/vibester/model/SongTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,19 @@ class SongTest {
println(test.get(0).getTrackName())
}

@Test
fun songBuilderTest() {
val songName = "test"
val songArtist = "Singer"
val artworkUrl = "https://none.com"
val previewUrl = "https://none.com"

val mySong = Song.songBuilder(previewUrl, artworkUrl, songName, songArtist)

assertEquals(songName, mySong.getTrackName())
assertEquals(songArtist, mySong.getArtistName())
assertEquals(artworkUrl, mySong.getArtworkUrl())
assertEquals(previewUrl, mySong.getPreviewUrl())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class AuthenticationActivity : AppCompatActivity() {
* Listener bound to the red return button in the Authentication activity.
*/
fun returnToMainListener(view: View) {
IntentSwitcher.switchBackToMain(this)
IntentSwitcher.switch(this, MainActivity::class.java)
finish()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.widget.TextView
import android.widget.Toast
import ch.sdp.vibester.R
import ch.sdp.vibester.helper.IntentSwitcher
import com.google.android.material.floatingactionbutton.FloatingActionButton
import java.io.*
/**
* Class that handles deleting files, more specifically songs in the scope of this project.
Expand All @@ -27,6 +28,10 @@ class DeleteSongsActivity : AppCompatActivity() {

setContentView(R.layout.activity_delete_songs)

findViewById<FloatingActionButton>(R.id.delete_returnToMain).setOnClickListener {
IntentSwitcher.switch(this, MainActivity::class.java)
}

val layout: LinearLayout = findViewById(R.id.delete_songs_linear)
generateButtons(layout)
}
Expand Down Expand Up @@ -131,8 +136,4 @@ class DeleteSongsActivity : AppCompatActivity() {
noDownloadsText.layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
layout.addView(noDownloadsText)
}

fun switchToWelcome(view: View) {
IntentSwitcher.switchBackToMain(this)
}
}
13 changes: 7 additions & 6 deletions app/src/main/java/ch/sdp/vibester/activity/DownloadActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import ch.sdp.vibester.R
import ch.sdp.vibester.api.ItunesMusicApi
import ch.sdp.vibester.helper.IntentSwitcher
import ch.sdp.vibester.model.Song
import com.google.android.material.floatingactionbutton.FloatingActionButton
import okhttp3.OkHttpClient
import java.io.File
import java.lang.IllegalArgumentException
Expand Down Expand Up @@ -53,6 +54,10 @@ class DownloadActivity : AppCompatActivity() {
downloadListener(songNameView)
}

findViewById<FloatingActionButton>(R.id.download_returnToMain).setOnClickListener {
IntentSwitcher.switch(this, MainActivity::class.java)
}

var broadcast = object:BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
var id = intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
Expand All @@ -65,8 +70,8 @@ class DownloadActivity : AppCompatActivity() {
}
}
}
registerReceiver(broadcast, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
}
registerReceiver(broadcast, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
}

/**
* Function that handles deletion button pushes.
Expand Down Expand Up @@ -201,13 +206,9 @@ class DownloadActivity : AppCompatActivity() {
records.appendText("$songName\n")
}

fun switchToWelcome(view: View) {
IntentSwitcher.switchBackToMain(this)
}

fun switchToDeleteSongs(view: View) {
val intent = Intent(this, DeleteSongsActivity::class.java)
startActivity(intent)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.Window
import android.widget.Button
import android.widget.LinearLayout
import android.widget.LinearLayout.LayoutParams
import android.widget.TextView
Expand All @@ -28,6 +29,11 @@ class IncorrectSongsActivity : AppCompatActivity() {

val layout: LinearLayout = findViewById(R.id.incorrect_songs_linear)

findViewById<Button>(R.id.incorrect_songs_back_to_welcome)
.setOnClickListener {
IntentSwitcher.switch(this, MainActivity::class.java)
}

if (intent.hasExtra("str_arr_inc")) {
incorrectSongs = intent.getStringArrayListExtra("str_arr_inc")
}
Expand Down Expand Up @@ -68,8 +74,4 @@ class IncorrectSongsActivity : AppCompatActivity() {
layout.addView(textView)
}
}

fun switchBackToWelcome(view: View) {
IntentSwitcher.switchBackToMain(this)
}
}
Loading

0 comments on commit 0a14ea4

Please sign in to comment.