Skip to content

Commit

Permalink
Fix #455: Improve Html parser tests (#5589)
Browse files Browse the repository at this point in the history
<!-- READ ME FIRST: Please fill in the explanation section below and
check off every point from the Essential Checklist! -->
## 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
<!-- Please tick the relevant boxes by putting an "x" in them. -->
- [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)).
  • Loading branch information
manas-yu authored Dec 10, 2024
1 parent aef5993 commit edb01a8
Showing 1 changed file with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"<ul><li>\n\tThe counting numbers (1, 2, 3, 4, 5 ….)\n\n</li><li>\n\tHow to tell" +
" whether one counting number is bigger or smaller than another.\n\n</li></ul>",
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<oppia-noninteractive-image filepath-with-value=\"test.png\">" +
"</oppia-noninteractive-image>\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(
Expand Down

0 comments on commit edb01a8

Please sign in to comment.