Last active
November 21, 2019 06:48
-
-
Save veena14cs/ae71a96825b021510f86faeb2297aaca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) | |
} | |
} |
Author
veena14cs
commented
Nov 20, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment