From edb01a868d07bcdee620425aa194d0f1184db7f8 Mon Sep 17 00:00:00 2001 From: Manas <119405883+manas-yu@users.noreply.github.com> Date: Tue, 10 Dec 2024 23:29:28 +0530 Subject: [PATCH] Fix #455: Improve Html parser tests (#5589) ## Explanation Fix #455 - `testHtmlContentReplace_removesUnwantedNewlines`: Ensures that unwanted newlines are removed from HTML content when parsed. - `testHtmlContent_withImageTag_trimsLeadingAndTrailingNewlines`: Verifies that leading and trailing newlines are trimmed from HTML content containing an image tag. ## Essential Checklist - [x] The PR title and explanation each start with "Fix #bugnum: " (If this PR fixes part of an issue, prefix the title with "Fix part of #bugnum: ...".) - [x] Any changes to [scripts/assets](https://github.com/oppia/oppia-android/tree/develop/scripts/assets) files have their rationale included in the PR explanation. - [x] The PR follows the [style guide](https://github.com/oppia/oppia-android/wiki/Coding-style-guide). - [x] The PR does not contain any unnecessary code changes from Android Studio ([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#undo-unnecessary-changes)). - [x] The PR is made from a branch that's **not** called "develop" and is up-to-date with "develop". - [x] The PR is **assigned** to the appropriate reviewers ([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#clarification-regarding-assignees-and-reviewers-section)). --- .../android/app/parser/HtmlParserTest.kt | 51 +++++++++++++++++++ 1 file changed, 51 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..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 @@ -427,6 +427,57 @@ class HtmlParserTest { assertThat(htmlResult.toString()).endsWith(" ") } + @Test + fun testHtmlContentParsing_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 testHtmlContentParsing_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(