Skip to content

Instantly share code, notes, and snippets.

@veena14cs
Last active November 21, 2019 06:48
Show Gist options
  • Save veena14cs/ae71a96825b021510f86faeb2297aaca to your computer and use it in GitHub Desktop.
Save veena14cs/ae71a96825b021510f86faeb2297aaca to your computer and use it in GitHub Desktop.
@RunWith(AndroidJUnit4::class)
class HtmlParserTest {
private lateinit var launchedActivity: Activity
@Inject
lateinit var htmlParserFactory: HtmlParser.Factory
@Inject
lateinit var urlImageParserFactory : UrlImageParser.Factory
@get:Rule
var activityTestRule: ActivityTestRule<HtmlParserTestActivity> = ActivityTestRule(
HtmlParserTestActivity::class.java, /* initialTouchMode= */ true, /* launchActivity= */ false
)
@Before
fun setUp() {
setUpTestApplicationComponent()
Intents.init()
val intent = Intent(Intent.ACTION_PICK)
launchedActivity = activityTestRule.launchActivity(intent)
}
private fun setUpTestApplicationComponent() {
DaggerHtmlParser_TestApplicationComponent.builder()
.setApplication(ApplicationProvider.getApplicationContext())
.build()
.inject(this)
}
@Test
fun testHtmlContent_handleCustomOppiaTags_parsedHtmlDisplaysStyledText() {
val textView = activityTestRule.activity.findViewById(R.id.test_html_content_text_view) as TextView
val htmlParser = htmlParserFactory.create(/* entityType= */ "", /* entityId= */ "")
val htmlResult: Spannable = htmlParser.parseOppiaHtml(
"\u003cp\u003e\"Let's try one last question,\" said Mr. Baker. \"Here's a pineapple cake cut into pieces.\"\u003c/p\u003e\u003coppia-noninteractive-image " +
"alt-with-value=\"\u0026amp;quot;Pineapple" +
" cake with 7/9 having cherries.\u0026amp;quot;\" caption-with-value=\"\u0026amp;quot;\u0026amp;quot;\" filepath-with-value=\"\u0026amp;quot;" +
"pineapple_cake_height_479_width_480.png\u0026amp;quot;\"\u003e\u003c/" +
"oppia-noninteractive-image\u003e\u003cp\u003e\u00a0\u003c/p\u003e\u003cp\u003e\u003cstrong\u003eQuestion 6\u003c/strong\u003e: What " +
"fraction of the cake has big red cherries in the pineapple slices?\u003c/p\u003e",
textView
)
assertThat(textView.text.toString()).isEqualTo(htmlResult.toString())
onView(withId(R.id.test_html_content_text_view)).check(matches(isDisplayed()))
onView(withId(R.id.test_html_content_text_view)).check(matches(withText(textView.text.toString())))
}
@Test
fun testHtmlContent_calculateImageWidthHeight() {
val htmlContentTextView = activityTestRule.activity.findViewById(R.id.test_html_content_text_view) as TextView
val htmlParser = htmlParserFactory.create(/* entityType= */ "", /* entityId= */ "")
val htmlResult: Spannable = htmlParser.parseOppiaHtml(
"\u003cp\u003e\"Let's try one last question,\" said Mr. Baker. \"Here's a pineapple cake cut into pieces.\"\u003c/p\u003e\u003coppia--image " +
"alt-with-value=\"\u0026amp;quot;Pineapple cake with 7/9 having cherries.\u0026amp;quot;\" caption-with-value=\"\u0026amp;quot;\u0026amp;quot;\"" +
" filepath-value=\"\u0026amp;quot;pineapple_cake_height_479_width_480.png\u0026amp;quot;\"\u003e\u003c/oppia-noninteractive-image" +
"\u003e\u003cp\u003e\u00a0\u003c/p\u003e\u003cp\u003e\u003cstrongQuestion 6\u003c/strong\u003e: What fraction of the cake has big " +
"red cherries in the pineapple slices?\u003c/p\u003e",
htmlContentTextView
)
htmlContentTextView.text = htmlResult
val imageGetter = urlImageParserFactory.create(htmlContentTextView, "exploration", "DIWZiVgs0km-")
val result1 = imageGetter.calculateDrawableSize(100,200,htmlContentTextView)
assertWithMessage("Width = " + result1.first)
assertWithMessage("Height = " + result1.second)
val result2 = imageGetter.calculateDrawableSize(400,200,htmlContentTextView)
assertWithMessage("Width = " + result2.first)
assertWithMessage("Height = " + result2.second)
}
@After
fun tearDown() {
Intents.release()
}
// TODO(#89): Move this to a common test application component.
@Module
class TestModule {
@Provides
@Singleton
fun provideContext(application: Application): Context {
return application
}
@Provides
@DefaultGcsPrefix
@Singleton
fun provideDefaultGcsPrefix(): String {
return "https://storage.googleapis.com/"
}
@Provides
@DefaultGcsResource
@Singleton
fun provideDefaultGcsResource(): String {
return "oppiaserver-resources/"
}
@Provides
@ImageDownloadUrlTemplate
@Singleton
fun provideImageDownloadUrlTemplate(): String {
return "%s/%s/assets/image/%s"
}
}
@Module
abstract class ImageTestModule {
@Binds
abstract fun provideGlideImageLoader(impl: GlideImageLoader): ImageLoader
}
@Singleton
@Component(modules = [TestModule::class
,ImageTestModule::class])
interface TestApplicationComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun setApplication(application: Application): Builder
fun build(): TestApplicationComponent
}
fun inject(htmlParserTest: HtmlParserTest)
}
}
@veena14cs
Copy link
Author

veena14cs commented Nov 20, 2019

> Task :app:compileDebugAndroidTestKotlin FAILED
e: /Users/veenakumar/Documents/GitHub/oppia-android/app/src/sharedTest/java/org/oppia/app/parser/HtmlParserTest.kt: (77, 5): Unresolved reference: DaggerTestApplicationComponent
e: /Users/veenakumar/Documents/GitHub/oppia-android/app/src/sharedTest/java/org/oppia/app/parser/HtmlParserTest.kt: (78, 43): Type inference failed: Not enough information to infer parameter T in fun <T : Context!> getApplicationContext(): T!
Please specify it explicitly.



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment