Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added testcases for LazyVideos and the corresponding .ccz as well #2568

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package org.commcare.androidTests


import android.content.Intent
import android.os.Build
import android.util.Log
import androidx.test.espresso.Espresso
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.RootMatchers
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.filters.SdkSuppress
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import org.commcare.CommCareApplication
import org.commcare.annotations.BrowserstackTests
import org.commcare.dalvik.R
import org.commcare.utils.CustomMatchers
import org.commcare.utils.InstrumentationUtility
import org.commcare.utils.isPresent
import org.hamcrest.CoreMatchers
import org.hamcrest.Matchers.`is`
import org.hamcrest.Matchers.allOf
import org.junit.After
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith


@RunWith(AndroidJUnit4::class)
@LargeTest
@BrowserstackTests
class LazyVideosTests : BaseTest() {

companion object {
const val CCZ_NAME = "lazy_videos_no_videos.ccz"
const val APP_NAME = "Lazy Videos"
}

@Before
fun setup() {
if (CommCareApplication.instance().currentApp == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious why are not we using the norma installApp()method as other tests ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if installApp() is used then the app is not uninstalled everytime and the videos remains in the app, hence failing the tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, it would be good to add a comment above this line stating this as It's not very obvious.

InstrumentationUtility.installApp(CCZ_NAME)
} else {
InstrumentationUtility.uninstallCurrentApp()
InstrumentationUtility.installApp(CCZ_NAME)
}
Espresso.pressBack()
updateApp()
}

@After
fun tearDown() {
InstrumentationUtility.logout()
}

@Test
fun testVideosWithReferences() {
testVideosWithValidReference()
testVideosWithNoReferences()
}

private fun updateApp(){
InstrumentationUtility.openOptionsMenu()
onView(withText("Go To App Manager"))
.perform(click())
onView(withText(APP_NAME)).perform(click())
onView(withText("Update"))
.perform(click())
InstrumentationUtility.waitForView(withSubstring("Update to version"))
gotoLogin()
}

private fun gotoLogin() {
for (i in 0..2) { // Try atmost 3 times.
if (onView(withId(R.id.edit_username)).isPresent()) {
return
} else {
Espresso.pressBack()
}
}
}
private fun testVideosWithValidReference() {
InstrumentationUtility.login("test1", "123")
InstrumentationUtility.openForm(1, 0)
InstrumentationUtility.waitForView(withId(R.id.video_button))
onView(withTagValue(CoreMatchers.`is`(R.drawable.update_download_icon))).check(matches(isDisplayed()))
InstrumentationUtility.stubIntentWithAction(Intent.ACTION_SEND)
onView(withTagValue(CoreMatchers.`is`(R.drawable.update_download_icon))).perform(click())
onView(withId(R.id.video_button)).perform(click())
InstrumentationUtility.waitForView(withSubstring("Download in Progress"))
InstrumentationUtility.waitForView(withSubstring("Download complete"))
InstrumentationUtility.waitForView(withTagValue(CoreMatchers.`is`(android.R.drawable.ic_media_play)))
InstrumentationUtility.stubIntentWithAction(Intent.ACTION_VIEW)
onView(withId(R.id.video_button)).perform(click())
InstrumentationUtility.nextPage()
Thread.sleep(5000)
assertTrue(onView(allOf(withId(R.id.inline_video_view))).isPresent())
InstrumentationUtility.nextPage()
InstrumentationUtility.waitForView(CustomMatchers.withDrawable(
CommCareApplication.instance(),
R.drawable.icon_info_outline_lightcool
))

onView(
CustomMatchers.withDrawable(
CommCareApplication.instance(),
R.drawable.icon_info_outline_lightcool
)
).perform(click())
onView(withTagValue(CoreMatchers.`is`(android.R.drawable.ic_media_play))).check(matches(isDisplayed()))
InstrumentationUtility.stubIntentWithAction(Intent.ACTION_VIEW)
onView(withId(R.id.video_button)).perform(click())
onView(withText("OK")).perform(click())
InstrumentationUtility.nextPage()

onView(
CustomMatchers.withDrawable(
CommCareApplication.instance(),
R.drawable.icon_info_outline_lightcool
)
).perform(click())
onView(withTagValue(CoreMatchers.`is`(android.R.drawable.ic_media_play))).check(matches(isDisplayed()))
InstrumentationUtility.stubIntentWithAction(Intent.ACTION_VIEW)
onView(withId(R.id.video_button)).perform(click())
onView(withText("OK")).perform(click())
InstrumentationUtility.submitForm()
assertTrue(onView(withText("1 form sent to server!")).isPresent())
InstrumentationUtility.logout()
}

private fun testVideosWithNoReferences() {
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
InstrumentationUtility.login("test1", "123")
InstrumentationUtility.openForm(1, 1)
InstrumentationUtility.waitForView(withText(R.string.video_download_prompt))
onView(withTagValue(CoreMatchers.`is`(R.drawable.update_download_icon))).check(matches(isDisplayed()))
onView(withId(R.id.video_button)).perform(click())
InstrumentationUtility.waitForView(withText(R.string.download_complete))
InstrumentationUtility.waitForView(withText("Media not found in the application"))
InstrumentationUtility.nextPage()
assertTrue(onView(withId(R.id.missing_media_view)).isPresent())
InstrumentationUtility.submitForm()
assertTrue(onView(withText("1 form sent to server!")).isPresent())
}
}
28 changes: 28 additions & 0 deletions app/instrumentation-tests/src/org/commcare/utils/CustomMatchers.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package org.commcare.utils

import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import androidx.appcompat.view.menu.ActionMenuItemView
import androidx.test.espresso.matcher.BoundedMatcher
import androidx.test.espresso.util.TreeIterables
import org.commcare.android.database.global.models.AppAvailableToInstall
Expand Down Expand Up @@ -145,4 +151,26 @@ object CustomMatchers {
}
}

fun withDrawable(targetContext: Context, expectedId: Int): Matcher<View> {
return object : TypeSafeMatcher<View>(View::class.java) {

override fun describeTo(description: Description?) {
description!!.appendText("with drawable from resource id: $expectedId")
targetContext.resources.getResourceEntryName(expectedId)?.let { description.appendText("[$it]") }
}

override fun matchesSafely(view: View?): Boolean {
val drawable: Drawable = when (view) {
is ActionMenuItemView -> view.itemData.icon
is ImageView -> view.drawable
is ImageButton -> view.drawable
else -> null
} ?: return false

val resources: Resources = view!!.context.resources
val expectedDrawable: Drawable? = resources.getDrawable(expectedId, targetContext.theme)
return expectedDrawable?.constantState?.let { it == drawable.constantState } ?: false
}
}
}
}
4 changes: 3 additions & 1 deletion app/src/org/commcare/views/media/MediaLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ private void setupStandardAudio(String audioURI, int questionIndex) {
private void setupVideoButton(String videoURI) {
if (videoURI != null) {
boolean mediaPresent = FileUtil.referenceFileExists(videoURI);
videoButton.setImageResource(mediaPresent ? android.R.drawable.ic_media_play : R.drawable.update_download_icon);
int imageResource = mediaPresent ? android.R.drawable.ic_media_play : R.drawable.update_download_icon;
videoButton.setImageResource(imageResource);
videoButton.setTag(imageResource); // this is to easily test drawables with Espresso
if (!mediaPresent) {
AndroidUtil.showToast(getContext(), R.string.video_download_prompt);
}
Expand Down