Skip to content

Commit cf0748b

Browse files
Fix Part of #277 : Added UrlImage Loading Tests (#5681)
**Explanation:** Fix part of #277 by introducing a set of test cases to verify the functionality of image loading via the `UrlImageParser` class using the `GlideImageLoader`. The tests ensure that images (including PNG, SVG, and SVGZ formats) are correctly loaded from URLs and that math drawables (e.g., inline LaTeX) are rendered as expected. It covers scenarios for: - Loading images from URLs of different formats (PNG, SVG, SVGZ). - Verifying the behavior when loading multiple images at once. - Ensuring that the image loading process finishes properly using Glide. **Fixes:** - This PR addresses the pending test cases for loading images from URLs (as mentioned in issue #277) and resolves the related testing block. ## 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: ...".) - [ ] 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)). **All Tests Passed:** ![Image](https://github.com/user-attachments/assets/df5c4015-1583-4faf-8f98-3d361c2b5bbc)
1 parent 2dcbbf2 commit cf0748b

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

utility/src/test/java/org/oppia/android/util/parser/image/UrlImageParserTest.kt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.oppia.android.util.parser.image
22

33
import android.app.Application
44
import android.content.Context
5+
import android.os.Looper
56
import android.widget.TextView
67
import androidx.test.core.app.ApplicationProvider
78
import androidx.test.ext.junit.runners.AndroidJUnit4
@@ -26,6 +27,7 @@ import org.oppia.android.util.locale.LocaleProdModule
2627
import org.oppia.android.util.logging.LoggerModule
2728
import org.oppia.android.util.parser.html.CustomHtmlContentHandler.ImageRetriever.Type.BLOCK_IMAGE
2829
import org.oppia.android.util.parser.html.CustomHtmlContentHandler.ImageRetriever.Type.INLINE_TEXT_IMAGE
30+
import org.robolectric.Shadows
2931
import org.robolectric.annotation.LooperMode
3032
import javax.inject.Inject
3133
import javax.inject.Singleton
@@ -79,6 +81,79 @@ class UrlImageParserTest {
7981
assertThat(loadedBitmaps.first()).contains("test_image.svg")
8082
}
8183

84+
@Test
85+
fun testLoadDrawable_bitmapFromUrl_loadsBlockImage() {
86+
val imageUrl = "https://example.com/test_image.png"
87+
urlImageParser.loadDrawable(imageUrl, BLOCK_IMAGE)
88+
89+
val loadedBitmaps = testGlideImageLoader.getLoadedBitmaps()
90+
assertThat(loadedBitmaps).hasSize(1)
91+
assertThat(loadedBitmaps.first()).contains("test_image.png")
92+
}
93+
94+
@Test
95+
fun testLoadDrawable_svgFromUrlBlockType_loadsBlockSvgImage() {
96+
val imageUrl = "https://example.com/test_image.svg"
97+
urlImageParser.loadDrawable(imageUrl, BLOCK_IMAGE)
98+
99+
Shadows.shadowOf(Looper.getMainLooper()).idle()
100+
101+
val loadedSvgs = testGlideImageLoader.getLoadedBlockSvgs()
102+
assertThat(loadedSvgs).hasSize(1)
103+
assertThat(loadedSvgs.first()).contains("test_image.svg")
104+
}
105+
106+
@Test
107+
fun testLoadDrawable_svgzFromUrlBlockType_loadsBlockSvgImage() {
108+
val imageUrl = "https://example.com/test_image.svgz"
109+
urlImageParser.loadDrawable(imageUrl, BLOCK_IMAGE)
110+
111+
Shadows.shadowOf(Looper.getMainLooper()).idle()
112+
113+
val loadedSvgs = testGlideImageLoader.getLoadedBlockSvgs()
114+
assertThat(loadedSvgs).hasSize(1)
115+
assertThat(loadedSvgs.first()).contains("test_image.svgz")
116+
}
117+
118+
@Test
119+
fun testLoadMathDrawable_latexInlineType_loadsInlineLatexImage() {
120+
val rawLatex = "\\frac{2}{6}"
121+
urlImageParser.loadMathDrawable(
122+
rawLatex = rawLatex,
123+
lineHeight = 20f,
124+
type = INLINE_TEXT_IMAGE
125+
)
126+
127+
Shadows.shadowOf(Looper.getMainLooper()).idle()
128+
129+
val loadedMathDrawables = testGlideImageLoader.getLoadedMathDrawables()
130+
assertThat(loadedMathDrawables).hasSize(1)
131+
assertThat(loadedMathDrawables.first().rawLatex).isEqualTo("\\frac{2}{6}")
132+
assertThat(loadedMathDrawables.first().useInlineRendering).isTrue()
133+
}
134+
135+
@Test
136+
fun testLoadDrawable_multipleImages_loadsAllImages() {
137+
val imageUrl1 = "https://example.com/test_image1.png"
138+
val imageUrl2 = "https://example.com/test_image2.svg"
139+
val imageUrl3 = "https://example.com/test_image3.svgz"
140+
141+
urlImageParser.loadDrawable(imageUrl1, BLOCK_IMAGE)
142+
urlImageParser.loadDrawable(imageUrl2, BLOCK_IMAGE)
143+
urlImageParser.loadDrawable(imageUrl3, BLOCK_IMAGE)
144+
145+
Shadows.shadowOf(Looper.getMainLooper()).idle()
146+
147+
val loadedBitmaps = testGlideImageLoader.getLoadedBitmaps()
148+
val loadedBlockSvgs = testGlideImageLoader.getLoadedBlockSvgs()
149+
150+
assertThat(loadedBitmaps).hasSize(1)
151+
assertThat(loadedBlockSvgs).hasSize(2)
152+
assertThat(loadedBitmaps.first()).contains("test_image1.png")
153+
assertThat(loadedBlockSvgs.first()).contains("test_image2.svg")
154+
assertThat(loadedBlockSvgs[1]).contains("test_image3.svgz")
155+
}
156+
82157
@Test
83158
fun testGetDrawable_svgz_loadsSvgzBlockImage() {
84159
urlImageParser.getDrawable("test_image.svgz")

0 commit comments

Comments
 (0)