From 94efcaab52ec0f93127ec6250812682140ab3f70 Mon Sep 17 00:00:00 2001 From: Derin Arda Alpay Date: Thu, 7 Apr 2022 17:06:12 +0200 Subject: [PATCH 1/6] Implemented downloading features --- .../vibester/activity/DownloadActivityTest.kt | 108 +++++++++++++++++ app/src/main/AndroidManifest.xml | 4 + .../sdp/vibester/activity/DownloadActivity.kt | 112 ++++++++++++++++++ .../sdp/vibester/activity/WelcomeActivity.kt | 4 + app/src/main/res/layout/activity_download.xml | 48 ++++++++ .../res/layout/activity_welcome_screen.xml | 13 ++ app/src/main/res/values/strings.xml | 3 + 7 files changed, 292 insertions(+) create mode 100644 app/src/androidTest/java/ch/sdp/vibester/activity/DownloadActivityTest.kt create mode 100644 app/src/main/java/ch/sdp/vibester/activity/DownloadActivity.kt create mode 100644 app/src/main/res/layout/activity_download.xml diff --git a/app/src/androidTest/java/ch/sdp/vibester/activity/DownloadActivityTest.kt b/app/src/androidTest/java/ch/sdp/vibester/activity/DownloadActivityTest.kt new file mode 100644 index 000000000..46e56f2b8 --- /dev/null +++ b/app/src/androidTest/java/ch/sdp/vibester/activity/DownloadActivityTest.kt @@ -0,0 +1,108 @@ +package ch.sdp.vibester.activity + +import android.content.Intent +import android.os.Environment +import android.util.Log +import android.view.View +import androidx.test.core.app.ActivityScenario +import androidx.test.core.app.ApplicationProvider +import androidx.test.espresso.Espresso +import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.action.ViewActions +import androidx.test.espresso.action.ViewActions.* +import androidx.test.espresso.assertion.ViewAssertions.matches +import androidx.test.espresso.intent.Intents +import androidx.test.espresso.intent.Intents.intended +import androidx.test.espresso.intent.matcher.IntentMatchers +import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent +import androidx.test.espresso.matcher.RootMatchers +import androidx.test.espresso.matcher.RootMatchers.withDecorView +import androidx.test.espresso.matcher.ViewMatchers +import androidx.test.espresso.matcher.ViewMatchers.* +import androidx.test.ext.junit.rules.ActivityScenarioRule +import androidx.viewpager.widget.ViewPager +import ch.sdp.vibester.R +import org.hamcrest.Matchers.not +import org.junit.After +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import java.io.File + +class DownloadActivityTest { + + @Rule + @JvmField + val activityRule = ActivityScenarioRule(DownloadActivity::class.java) + + private lateinit var decorView : View + + @Before + fun setUp() { + Intents.init() + activityRule.scenario.onActivity { + activity -> decorView = activity.window.decorView + } + } + + @After + fun clean() { + Intents.release() + } + + @Test + fun checkIntentOnBack() { + onView(withId(R.id.download_backtowelcome)).perform(click()) + intended(hasComponent(WelcomeActivity::class.java.name)) + } + + @Test + fun downloadCorrectSong() { + val intent = Intent(ApplicationProvider.getApplicationContext(), DownloadActivity::class.java) + val scn: ActivityScenario = ActivityScenario.launch(intent) + val songName = "imagine dragons believer" + + onView(withId(R.id.download_songName)).perform(typeText(songName), closeSoftKeyboard()) + Thread.sleep(100) + onView(withId(R.id.download_downloadsong)).perform(click()) + Thread.sleep(2000) + + //Commented as it does not work, albeit we "see" the toast. + //TODO: Replace with something else in the future, i.e another type of notifier. + /*onView(withText("Download completed!")) + .inRoot(withDecorView(not(decorView))) + .check(matches(isDisplayed()))*/ + + onView(withId(R.id.download_songName)).check(matches(withText(""))) + onView(withId(R.id.download_songName)).check(matches(withHint("Try another song!"))) + + val extract = File("storage/emulated/0/Download", "extract_of_$songName") + if(extract.exists()) { + assert(true) + } else { + assert(false) + } + } + + @Test + fun downloadIncorrectSong() { + val intent = Intent(ApplicationProvider.getApplicationContext(), DownloadActivity::class.java) + val scn: ActivityScenario = ActivityScenario.launch(intent) + val songName = "adsfasdgyasdfa" + + onView(withId(R.id.download_songName)).perform(typeText(songName), closeSoftKeyboard()) + Thread.sleep(100) + onView(withId(R.id.download_downloadsong)).perform(click()) + Thread.sleep(2000) + + onView(withId(R.id.download_songName)).check(matches(withText(""))) + onView(withId(R.id.download_songName)).check(matches(withHint("Please retry!"))) + + val extract = File("storage/emulated/0/Download", "extract_of_$songName") + if(extract.exists()) { + assert(false) + } else { + assert(true) + } + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bd2dcce52..4a944fe55 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,7 @@ + + diff --git a/app/src/main/java/ch/sdp/vibester/activity/DownloadActivity.kt b/app/src/main/java/ch/sdp/vibester/activity/DownloadActivity.kt new file mode 100644 index 000000000..1400f6086 --- /dev/null +++ b/app/src/main/java/ch/sdp/vibester/activity/DownloadActivity.kt @@ -0,0 +1,112 @@ +package ch.sdp.vibester.activity + +import android.Manifest +import android.app.DownloadManager +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.content.pm.PackageManager +import android.net.Uri +import android.os.Build +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle +import android.os.Environment +import android.view.View +import android.widget.Button +import android.widget.TextView +import android.widget.Toast +import ch.sdp.vibester.R +import ch.sdp.vibester.api.ItunesMusicApi +import ch.sdp.vibester.model.Song +import okhttp3.OkHttpClient +import java.lang.IllegalArgumentException + +class DownloadActivity : AppCompatActivity() { + private val STORAGE_PERMISSION_CODE = 1000 + private lateinit var song: Song + private var songName: String = "imagine dragons believer" + private var downloadId: Long = 0 + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_download) + + val songNameView = findViewById(R.id.download_songName) + + val downloadButton = findViewById