From 144bfa7d0f6ba1eb243ef4745dc529d42a1d0666 Mon Sep 17 00:00:00 2001 From: manas-yu Date: Thu, 5 Dec 2024 19:50:55 +0530 Subject: [PATCH 1/5] HTML-parser tests --- .../android/app/parser/HtmlParserTest.kt | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt b/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt index 1e1ca843c6a..ad777f81bc0 100644 --- a/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt +++ b/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt @@ -5,14 +5,19 @@ import android.app.Application import android.app.Instrumentation import android.content.Context import android.content.Intent +import android.graphics.Bitmap +import android.graphics.drawable.BitmapDrawable import android.text.Spannable +import android.text.SpannableStringBuilder import android.text.style.ClickableSpan import android.text.style.ImageSpan import android.view.View +import android.widget.ImageView import android.widget.TextView import androidx.annotation.DimenRes import androidx.annotation.IdRes import androidx.appcompat.app.AppCompatActivity +import androidx.core.graphics.drawable.toBitmap import androidx.core.view.ViewCompat import androidx.test.core.app.ActivityScenario import androidx.test.core.app.ApplicationProvider @@ -135,6 +140,7 @@ import org.robolectric.annotation.Config import org.robolectric.annotation.LooperMode import javax.inject.Inject import javax.inject.Singleton +import junit.framework.Assert.fail import kotlin.reflect.KClass // TODO(#277): Add tests for UrlImageParser. @@ -312,6 +318,60 @@ class HtmlParserTest { .check(matches(withText(textView.text.toString()))) } + @Test + fun testHtmlContent_withEscapedNewlineTabsAndDoubleNewlines_removesExtraNewlinesCorrectly() { + val rawHtmlContent = "This is a line.\\n\\t

This is another line with a tab.

\\n\\n

And this is a third line.

" + + val cleanedHtmlContent = rawHtmlContent + .replace("\\n\\t", "") + .replace("\\n\\n", "") + + val htmlParser = htmlParserFactory.create( + resourceBucketName, + entityType = "", + entityId = "", + imageCenterAlign = true, + displayLocale = appLanguageLocaleHandler.getDisplayLocale() + ) + val (textView, parsedHtmlResult) = activityScenarioRule.scenario.runWithActivity { + val textView: TextView = it.findViewById(R.id.test_html_content_text_view) + val spannableResult = htmlParser.parseOppiaHtml(cleanedHtmlContent, textView) + return@runWithActivity textView to spannableResult + } + + assertThat(parsedHtmlResult.toString()).doesNotContain("\\n\\t") + assertThat(parsedHtmlResult.toString()).doesNotContain("\\n\\n") + assertThat(parsedHtmlResult.toString()).isEqualTo(cleanedHtmlContent) + + assertThat(textView.text.toString()).isEqualTo(parsedHtmlResult.toString()) + } + + @Test + fun testHtmlContent_withLeadingTrailingNewlinesAndImage_trimsNewlinesCorrectly() { + val htmlParser = htmlParserFactory.create( + resourceBucketName, + entityType = "", + entityId = "", + imageCenterAlign = true, + displayLocale = appLanguageLocaleHandler.getDisplayLocale() + ) + val rawHtmlContent = "\n" + + "\n" + val (textView, trimmedHtmlResult) = activityScenarioRule.scenario.runWithActivity { + val textView: TextView = it.findViewById(R.id.test_html_content_text_view) + val spannableResult = htmlParser.parseOppiaHtml(rawHtmlContent, textView) + return@runWithActivity textView to spannableResult + } + + assertThat(trimmedHtmlResult.toString()) + .isEqualTo("" + + "") + assertThat(textView.text.toString()).isEqualTo(trimmedHtmlResult.toString()) + + assertThat(trimmedHtmlResult.startsWith("\n")).isFalse() + assertThat(trimmedHtmlResult.endsWith("\n")).isFalse() + } + @Test fun testHtmlContent_handleCustomOppiaTags_parsedHtmlDisplaysStyledText() { val htmlParser = htmlParserFactory.create( From 947f7864dbc7661599a428033d217e4b679678d8 Mon Sep 17 00:00:00 2001 From: manas-yu Date: Thu, 5 Dec 2024 21:14:43 +0530 Subject: [PATCH 2/5] formatting --- .../android/app/parser/HtmlParserTest.kt | 110 ++++++++---------- 1 file changed, 50 insertions(+), 60 deletions(-) diff --git a/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt b/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt index ad777f81bc0..3147eb439d9 100644 --- a/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt +++ b/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt @@ -5,19 +5,14 @@ import android.app.Application import android.app.Instrumentation import android.content.Context import android.content.Intent -import android.graphics.Bitmap -import android.graphics.drawable.BitmapDrawable import android.text.Spannable -import android.text.SpannableStringBuilder import android.text.style.ClickableSpan import android.text.style.ImageSpan import android.view.View -import android.widget.ImageView import android.widget.TextView import androidx.annotation.DimenRes import androidx.annotation.IdRes import androidx.appcompat.app.AppCompatActivity -import androidx.core.graphics.drawable.toBitmap import androidx.core.view.ViewCompat import androidx.test.core.app.ActivityScenario import androidx.test.core.app.ApplicationProvider @@ -140,7 +135,6 @@ import org.robolectric.annotation.Config import org.robolectric.annotation.LooperMode import javax.inject.Inject import javax.inject.Singleton -import junit.framework.Assert.fail import kotlin.reflect.KClass // TODO(#277): Add tests for UrlImageParser. @@ -318,60 +312,6 @@ class HtmlParserTest { .check(matches(withText(textView.text.toString()))) } - @Test - fun testHtmlContent_withEscapedNewlineTabsAndDoubleNewlines_removesExtraNewlinesCorrectly() { - val rawHtmlContent = "This is a line.\\n\\t

This is another line with a tab.

\\n\\n

And this is a third line.

" - - val cleanedHtmlContent = rawHtmlContent - .replace("\\n\\t", "") - .replace("\\n\\n", "") - - val htmlParser = htmlParserFactory.create( - resourceBucketName, - entityType = "", - entityId = "", - imageCenterAlign = true, - displayLocale = appLanguageLocaleHandler.getDisplayLocale() - ) - val (textView, parsedHtmlResult) = activityScenarioRule.scenario.runWithActivity { - val textView: TextView = it.findViewById(R.id.test_html_content_text_view) - val spannableResult = htmlParser.parseOppiaHtml(cleanedHtmlContent, textView) - return@runWithActivity textView to spannableResult - } - - assertThat(parsedHtmlResult.toString()).doesNotContain("\\n\\t") - assertThat(parsedHtmlResult.toString()).doesNotContain("\\n\\n") - assertThat(parsedHtmlResult.toString()).isEqualTo(cleanedHtmlContent) - - assertThat(textView.text.toString()).isEqualTo(parsedHtmlResult.toString()) - } - - @Test - fun testHtmlContent_withLeadingTrailingNewlinesAndImage_trimsNewlinesCorrectly() { - val htmlParser = htmlParserFactory.create( - resourceBucketName, - entityType = "", - entityId = "", - imageCenterAlign = true, - displayLocale = appLanguageLocaleHandler.getDisplayLocale() - ) - val rawHtmlContent = "\n" + - "\n" - val (textView, trimmedHtmlResult) = activityScenarioRule.scenario.runWithActivity { - val textView: TextView = it.findViewById(R.id.test_html_content_text_view) - val spannableResult = htmlParser.parseOppiaHtml(rawHtmlContent, textView) - return@runWithActivity textView to spannableResult - } - - assertThat(trimmedHtmlResult.toString()) - .isEqualTo("" + - "") - assertThat(textView.text.toString()).isEqualTo(trimmedHtmlResult.toString()) - - assertThat(trimmedHtmlResult.startsWith("\n")).isFalse() - assertThat(trimmedHtmlResult.endsWith("\n")).isFalse() - } - @Test fun testHtmlContent_handleCustomOppiaTags_parsedHtmlDisplaysStyledText() { val htmlParser = htmlParserFactory.create( @@ -487,6 +427,56 @@ class HtmlParserTest { assertThat(htmlResult.toString()).endsWith(" ") } + @Test + fun testHtmlContentReplace_removesUnwantedNewlines() { + val htmlParser = htmlParserFactory.create( + resourceBucketName, + entityType = "", + entityId = "", + imageCenterAlign = true, + displayLocale = appLanguageLocaleHandler.getDisplayLocale() + ) + val (_, htmlResult) = activityScenarioRule.scenario.runWithActivity { + val textView: TextView = it.findViewById(R.id.test_html_content_text_view) + val htmlResult = htmlParser.parseOppiaHtml( + "", + textView + ) + textView.text = htmlResult + return@runWithActivity textView to htmlResult + } + assertThat(htmlResult.toString()).isEqualTo( + "The counting numbers (1, 2, 3, 4, 5 ….)\nHow to tell whether one counting " + + "number is bigger or smaller than another" + ) + } + + @Test + fun testHtmlContent_withImageTag_trimsLeadingAndTrailingNewlines() { + val htmlParser = htmlParserFactory.create( + resourceBucketName, + entityType = "", + entityId = "", + imageCenterAlign = true, + displayLocale = appLanguageLocaleHandler.getDisplayLocale() + ) + val htmlResult = activityScenarioRule.scenario.runWithActivity { + val textView: TextView = it.findViewById(R.id.test_html_content_text_view) + return@runWithActivity htmlParser.parseOppiaHtml( + "\n" + + "\n", + textView + ) + } + + val imageSpans = htmlResult.getSpansFromWholeString(ImageSpan::class) + assertThat(imageSpans).hasLength(1) + assertThat(imageSpans.first().source).isEqualTo("test.png") + assertThat(htmlResult.toString().startsWith("\n")).isFalse() + assertThat(htmlResult.toString().endsWith("\n")).isFalse() + } + @Test fun testHtmlContent_changeDeviceToLtr_textViewDirectionIsSetToLtr() { val htmlParser = htmlParserFactory.create( From 50862f45564765d48f4133408af6ea903d85778c Mon Sep 17 00:00:00 2001 From: manas-yu Date: Thu, 5 Dec 2024 21:58:07 +0530 Subject: [PATCH 3/5] test --- .../java/org/oppia/android/app/parser/HtmlParserTest.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt b/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt index 3147eb439d9..7f6b1187da6 100644 --- a/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt +++ b/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt @@ -446,6 +446,7 @@ class HtmlParserTest { textView.text = htmlResult return@runWithActivity textView to htmlResult } + assertThat(htmlResult.toString()).isEqualTo( "The counting numbers (1, 2, 3, 4, 5 ….)\nHow to tell whether one counting " + "number is bigger or smaller than another" @@ -473,6 +474,7 @@ class HtmlParserTest { val imageSpans = htmlResult.getSpansFromWholeString(ImageSpan::class) assertThat(imageSpans).hasLength(1) assertThat(imageSpans.first().source).isEqualTo("test.png") + assertThat(htmlResult.toString().startsWith("\n")).isFalse() assertThat(htmlResult.toString().endsWith("\n")).isFalse() } From 706744b45b6a13bad8e10763718d68578e7b69f6 Mon Sep 17 00:00:00 2001 From: manas-yu Date: Fri, 6 Dec 2024 00:27:50 +0530 Subject: [PATCH 4/5] test --- .../java/org/oppia/android/app/parser/HtmlParserTest.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt b/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt index 7f6b1187da6..cb323852861 100644 --- a/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt +++ b/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt @@ -470,7 +470,6 @@ class HtmlParserTest { textView ) } - val imageSpans = htmlResult.getSpansFromWholeString(ImageSpan::class) assertThat(imageSpans).hasLength(1) assertThat(imageSpans.first().source).isEqualTo("test.png") From be56cb6408350d7644247f0d629e9677654f2aeb Mon Sep 17 00:00:00 2001 From: manas-yu Date: Tue, 10 Dec 2024 10:14:20 +0530 Subject: [PATCH 5/5] test name changes --- .../java/org/oppia/android/app/parser/HtmlParserTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt b/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt index cb323852861..9b3b36f2d6a 100644 --- a/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt +++ b/app/src/sharedTest/java/org/oppia/android/app/parser/HtmlParserTest.kt @@ -428,7 +428,7 @@ class HtmlParserTest { } @Test - fun testHtmlContentReplace_removesUnwantedNewlines() { + fun testHtmlContentParsing_removesUnwantedNewlines() { val htmlParser = htmlParserFactory.create( resourceBucketName, entityType = "", @@ -454,7 +454,7 @@ class HtmlParserTest { } @Test - fun testHtmlContent_withImageTag_trimsLeadingAndTrailingNewlines() { + fun testHtmlContentParsing_withImageTag_trimsLeadingAndTrailingNewlines() { val htmlParser = htmlParserFactory.create( resourceBucketName, entityType = "",