From 48fa4fc80b0df6a2ed97bcc11b733d9184e1584e Mon Sep 17 00:00:00 2001 From: Mihir Badkur <85284909+mihir02badkur@users.noreply.github.com> Date: Tue, 7 Dec 2021 17:41:54 +0530 Subject: [PATCH] Add tests for validating youtube video link regex * Get bitmap from PDF * Bitmap of PDF * Delete deploymentTargetDropDown.xml * Delete gradle.xml * Delete misc.xml * Bitmap of PDF * Bitmap of PDF * Updated * Update runConfigurations.xml * Update runConfigurations.xml * Update runConfigurations.xml * Changes done * Get Youtube video thumbnail * Youtube video thumbnail * Delete gradle.xml * Delete misc.xml * Youtube video thumbnail * Update MainActivity.kt * Update activity_main.xml * Update Previewer.kt * Update strings.xml * Update build.gradle * Changes done * Remove gradle.xml & runConfigurations.xml from changelog * Changes in comments and tried using URL * Add suspend function for fetching thumbnail * Return null instead of throwing exception Co-authored-by: arpit * Reformat files Co-authored-by: Raina Jain <84968175+RainaJain5@users.noreply.github.com> Co-authored-by: arpit * Add support for generating video thumbnail * Added glide dependency * Added support for generating video thumbnail * added necessary imports * Bitmap of PDF * Delete deploymentTargetDropDown.xml * Delete gradle.xml * Delete misc.xml * Bitmap of PDF * Bitmap of PDF * Updated * Update runConfigurations.xml * Update runConfigurations.xml * Update runConfigurations.xml * Changes done * Added Demo code to get video from storage and show its thumbnail * removed blank line * removed .asBitmap() * removed Previewer.init() call from MainActivity.kt * Implemented viewBinding * Removed unused imports * renamed getVideoThumbnail function to setVideoThumbnailFromUri * renamed activityLauncher to activityResultLauncherForVideoUri * added code to demonstrate creation of thumbnail from Internet Video * added empty line after declaration * renamed uriVideo to videoUri * Removed redundant code * Added internet permission * Fixed the typo * Reformat files Co-authored-by: ayushigupta931 Co-authored-by: Ayushi Gupta <85010904+ayushigupta931@users.noreply.github.com> Co-authored-by: arpit * Create separate activities for all functions * Regex tests for Youtube URLs * reformat code * internal functions marked * delete unnecessary files * Delete BitmapPDF.kt * Delete BitmapVideo.kt * Delete BitmapYoutube.kt * Delete activity_bitmap_pdf.xml * Delete activity_bitmap_video.xml * Delete activity_bitmap_youtube.xml Co-authored-by: Ayushi Gupta <85010904+ayushigupta931@users.noreply.github.com> Co-authored-by: Raina Jain <84968175+RainaJain5@users.noreply.github.com> Co-authored-by: arpit Co-authored-by: Puranjay Khanijo <85157266+puranjayK@users.noreply.github.com> Co-authored-by: ayushigupta931 --- .../cops/iitbhu/previewer/lib/Previewer.kt | 10 ++--- .../cops/iitbhu/previewer/lib/RegexTest.kt | 45 +++++++++++++++++-- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/lib/src/main/java/com/cops/iitbhu/previewer/lib/Previewer.kt b/lib/src/main/java/com/cops/iitbhu/previewer/lib/Previewer.kt index dd8ffe9..4e0f1dd 100644 --- a/lib/src/main/java/com/cops/iitbhu/previewer/lib/Previewer.kt +++ b/lib/src/main/java/com/cops/iitbhu/previewer/lib/Previewer.kt @@ -30,7 +30,7 @@ object Previewer { * @param youtubeLink * @return Image ID */ - fun youtubeLinkToImageUrl(youtubeLink: String): String? { + internal fun youtubeLinkToImageUrl(youtubeLink: String): String? { val regex = "^((?:https?:)?//)?((?:www|m)\\.)?(youtube\\.com|youtu.be|youtube-nocookie.com)(/(?:[\\w\\-]+\\?v=|feature=|watch\\?|e/|embed/|v/)?)([\\w\\-]+)(\\S+)?\$" return Regex(regex).matchEntire(youtubeLink)?.groupValues?.get(5) @@ -126,14 +126,14 @@ object Previewer { /** * Generates a bitmap for remote video file - * @param uri Uri of the remote video file as a String - * @return Bitmap of the first frame of the video corresponding to given uri + * @param url Url of the remote video file as a String + * @return Bitmap of the first frame of the video corresponding to given url */ - suspend fun getThumbnailFromRemoteVideoUri(uri: String): Bitmap? { + suspend fun getThumbnailFromRemoteVideoUri(url: String): Bitmap? { return withContext(Dispatchers.IO) { try { val mMR = MediaMetadataRetriever() - mMR.setDataSource(uri, mapOf()) + mMR.setDataSource(url, mapOf()) mMR.frameAtTime } catch (e: Exception) { null diff --git a/lib/src/test/java/com/cops/iitbhu/previewer/lib/RegexTest.kt b/lib/src/test/java/com/cops/iitbhu/previewer/lib/RegexTest.kt index 9b7846a..ebd2024 100644 --- a/lib/src/test/java/com/cops/iitbhu/previewer/lib/RegexTest.kt +++ b/lib/src/test/java/com/cops/iitbhu/previewer/lib/RegexTest.kt @@ -8,9 +8,46 @@ class RegexTest { @Test fun checkRegexIsValid() { - val youtubeLink = "https://www.youtube.com/watch?v=Pur_0KroFR8" - val expected = "Pur_0KroFR8" - val actual = Previewer.youtubeLinkToImageUrl(youtubeLink) - assertEquals(expected, actual) + val links = listOf( + "https://www.youtube.com/watch?v=DFYRQ_zQ-gk", + "https://www.youtube.com/watch?v=DFYRQ_zQ-gk&feature=featured", + "https://www.youtube.com/watch?v=DFYRQ_zQ-gk", + "http://www.youtube.com/watch?v=DFYRQ_zQ-gk", + "//www.youtube.com/watch?v=DFYRQ_zQ-gk", + "www.youtube.com/watch?v=DFYRQ_zQ-gk", + "https://youtube.com/watch?v=DFYRQ_zQ-gk", + "http://youtube.com/watch?v=DFYRQ_zQ-gk", + "//youtube.com/watch?v=DFYRQ_zQ-gk", + "youtube.com/watch?v=DFYRQ_zQ-gk", + "https://m.youtube.com/watch?v=DFYRQ_zQ-gk", + "http://m.youtube.com/watch?v=DFYRQ_zQ-gk", + "//m.youtube.com/watch?v=DFYRQ_zQ-gk", + "m.youtube.com/watch?v=DFYRQ_zQ-gk", + "https://www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US", + "http://www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US", + "//www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US", + "www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US", + "youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US", + "https://www.youtube.com/embed/DFYRQ_zQ-gk?autoplay=1", + "https://www.youtube.com/embed/DFYRQ_zQ-gk", + "http://www.youtube.com/embed/DFYRQ_zQ-gk", + "//www.youtube.com/embed/DFYRQ_zQ-gk", + "www.youtube.com/embed/DFYRQ_zQ-gk", + "https://youtube.com/embed/DFYRQ_zQ-gk", + "http://youtube.com/embed/DFYRQ_zQ-gk", + "//youtube.com/embed/DFYRQ_zQ-gk", + "youtube.com/embed/DFYRQ_zQ-gk", + "https://youtu.be/DFYRQ_zQ-gk?t=120", + "https://youtu.be/DFYRQ_zQ-gk", + "http://youtu.be/DFYRQ_zQ-gk", + "//youtu.be/DFYRQ_zQ-gk", + "youtu.be/DFYRQ_zQ-gk", + "https://www.youtube.com/HamdiKickProduction?v=DFYRQ_zQ-gk", + ) + val expected = "DFYRQ_zQ-gk" + links.forEach { link -> + val actual = Previewer.youtubeLinkToImageUrl(link) + assertEquals(expected, actual) + } } } \ No newline at end of file