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

Fix #935: Optimised test cases for HDPI devices #942

Closed
wants to merge 9 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class ProfileEditActivity : InjectableAppCompatActivity() {
profileEditActivityPresenter.handleOnCreate()
}

override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
super.onRestoreInstanceState(savedInstanceState)
profileEditActivityPresenter.handleOnRestoreSavedInstanceState()
}

override fun onSupportNavigateUp(): Boolean {
val intent = Intent(this, ProfileListActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,8 @@ class ProfileEditActivityPresenter @Inject constructor(
private fun getProfileEditViewModel(): ProfileEditViewModel {
return viewModelProvider.getForActivity(activity, ProfileEditViewModel::class.java)
}

fun handleOnRestoreSavedInstanceState(){
activity.title=editViewModel.profileName
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: activity.title = editViewModel.profileName

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class ProfileEditViewModel @Inject constructor(
) : ObservableViewModel() {
private lateinit var profileId: ProfileId

lateinit var profileName : String

val profile: LiveData<Profile> by lazy {
Transformations.map(profileManagementController.getProfile(profileId), ::processGetProfileResult)
}
Expand All @@ -45,6 +47,7 @@ class ProfileEditViewModel @Inject constructor(
val switch = activity.findViewById<Switch>(R.id.profile_edit_allow_download_switch)
switch.isChecked = profile.allowDownloadAccess
activity.title = profile.name
profileName = profile.name
isAdmin = profile.isAdmin
return profile
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ package org.oppia.app.administratorcontrols
import android.app.Application
import android.content.Context
import android.content.Intent
import android.view.View
import android.view.ViewParent
import android.widget.FrameLayout
import androidx.core.widget.NestedScrollView
import androidx.recyclerview.widget.RecyclerView
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.PerformException
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.pressBack
import androidx.test.espresso.assertion.ViewAssertions.matches
Expand All @@ -16,13 +23,15 @@ import androidx.test.espresso.intent.Intents.intended
import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
import androidx.test.espresso.intent.matcher.IntentMatchers.hasExtra
import androidx.test.espresso.matcher.RootMatchers.isDialog
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.isChecked
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.isNotChecked
import androidx.test.espresso.matcher.ViewMatchers.isRoot
import androidx.test.espresso.matcher.ViewMatchers.withContentDescription
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.espresso.util.HumanReadables
import androidx.test.ext.junit.runners.AndroidJUnit4
import dagger.BindsInstance
import dagger.Component
Expand All @@ -31,6 +40,7 @@ import dagger.Provides
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import org.hamcrest.Matchers
import org.hamcrest.Matchers.not
import org.junit.After
import org.junit.Before
Expand Down Expand Up @@ -86,7 +96,7 @@ class AdministratorControlsActivityTest {
fun testAdministratorControlsActivity_withAdminProfile_openAdministratorControlsActivityFromNavigationDrawer_onBackPressed_showsHomeActivity() {
ActivityScenario.launch<NavigationDrawerTestActivity>(createNavigationDrawerActivityIntent(0)).use {
onView(withContentDescription(R.string.drawer_open_content_description)).perform(click())
onView(withId(R.id.administrator_controls_linear_layout)).check(matches(isDisplayed())).perform(click())
onView(withId(R.id.administrator_controls_linear_layout)).check(matches(isDisplayed())).perform(nestedScrollTo()).perform(click())
intended(hasComponent(AdministratorControlsActivity::class.java.name))
intended(hasExtra(AdministratorControlsActivity.getIntentKey(), 0))
onView(isRoot()).perform(pressBack())
Expand Down Expand Up @@ -176,7 +186,7 @@ class AdministratorControlsActivityTest {
ActivityScenario.launch<NavigationDrawerTestActivity>(createNavigationDrawerActivityIntent(0))
.use {
onView(withContentDescription(R.string.drawer_open_content_description)).perform(click())
onView(withId(R.id.administrator_controls_linear_layout)).check(matches(isDisplayed()))
onView(withId(R.id.administrator_controls_linear_layout)).check(matches(isDisplayed())).perform(nestedScrollTo())
.perform(click())
intended(hasComponent(AdministratorControlsActivity::class.java.name))
onView(atPositionOnView(R.id.administrator_controls_list, 2, R.id.topic_update_on_wifi_switch))
Expand Down Expand Up @@ -261,6 +271,69 @@ class AdministratorControlsActivityTest {
)
}

fun nestedScrollTo(): ViewAction {
return object: ViewAction {
override fun getDescription(): String {
return "View is not NestedScrollView"
}

override fun getConstraints(): org.hamcrest.Matcher<View> {
return Matchers.allOf(
ViewMatchers.isDescendantOfA(ViewMatchers.isAssignableFrom(NestedScrollView::class.java))
)
}

override fun perform(uiController: UiController, view: View) {
try
{
val nestedScrollView = findFirstParentLayoutOfClass(view, NestedScrollView::class.java!!) as NestedScrollView
if (nestedScrollView != null)
{
nestedScrollView.scrollTo(0, view.getTop())
}
else
{
throw Exception("Unable to find NestedScrollView parent.")
}
}
catch (e:Exception) {
throw PerformException.Builder()
.withActionDescription(this.description)
.withViewDescription(HumanReadables.describe(view))
.withCause(e)
.build()
}
uiController.loopMainThreadUntilIdle()
}
}
}

private fun findFirstParentLayoutOfClass(view: View, parentClass:Class<out View>): View {
var parent : ViewParent = FrameLayout(view.getContext())
lateinit var incrementView: ViewParent
var i = 0
while (parent != null && !(parent.javaClass === parentClass))
{
if (i == 0)
{
parent = findParent(view)
}
else
{
parent = findParent(incrementView)
}
incrementView = parent
i++
}
return parent as View
}
private fun findParent(view: View): ViewParent {
return view.getParent()
}
private fun findParent(view: ViewParent): ViewParent {
return view.getParent()
}

@Qualifier
annotation class TestDispatcher

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,37 @@ class AppVersionActivityTest {

@Test
fun testAppVersionActivity_configurationChange_appVersionIsDisplayedCorrectly(){
onView(isRoot()).perform(orientationLandscape())
onView(
withId(
R.id.app_version_text_view
)
).check(
matches(
withText(
String.format(context.resources.getString(R.string.app_version_name), BuildConfig.VERSION_NAME)
launchAppVersionActivityIntent().use {
onView(isRoot()).perform(orientationLandscape())
onView(
withId(
R.id.app_version_text_view
)
).check(
matches(
withText(
String.format(
context.resources.getString(R.string.app_version_name),
BuildConfig.VERSION_NAME
)
)
)
)
)
onView(
withId(
R.id.app_last_update_date_text_view
)
).check(
matches(
withText(
String.format(context.resources.getString(R.string.app_last_update_date), lastUpdateDate)
onView(
withId(
R.id.app_last_update_date_text_view
)
).check(
matches(
withText(
String.format(
context.resources.getString(R.string.app_last_update_date),
lastUpdateDate
)
)
)
)
)
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ class HomeActivityTest {
getApplicationDependencies()
oppiaClock.setCurrentTimeMs(MORNING_TIMESTAMP)
launch<HomeActivity>(createHomeActivityIntent(internalProfileId)).use {
onView(withId(R.id.home_recycler_view)).perform(scrollToPosition<RecyclerView.ViewHolder>(0))

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this blank line

onView(
atPositionOnView(
R.id.home_recycler_view,
Expand Down Expand Up @@ -205,6 +207,7 @@ class HomeActivityTest {
fun testHomeActivity_recyclerViewIndex0_configurationChange_displaysWelcomeMessageCorrectly() {
launch<HomeActivity>(createHomeActivityIntent(0)).use {
onView(isRoot()).perform(orientationLandscape())
onView(withId(R.id.home_recycler_view)).perform(scrollToPosition<RecyclerView.ViewHolder>(0))
onView(
atPositionOnView(
R.id.home_recycler_view,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,8 @@ class AddProfileActivityTest {
onView(allOf(withId(R.id.input), isDescendantOfA(withId(R.id.input_name)))).perform(scrollTo()).perform(
typeText("Sean"), closeSoftKeyboard()
)
onView(withId(R.id.create_button)).perform(scrollTo())
onView(withId(R.id.create_button)).perform(click())
onView(allOf(withId(R.id.input), isDescendantOfA(withId(R.id.input_name)))).perform(scrollTo())
onView(allOf(withId(R.id.input), isDescendantOfA(withId(R.id.input_name)))).perform(
onView(withId(R.id.create_button)).perform(scrollTo()).perform(click())
onView(allOf(withId(R.id.input), isDescendantOfA(withId(R.id.input_name)))).perform(scrollTo()).perform(
typeText(" "), closeSoftKeyboard()
)
onView(
Expand Down Expand Up @@ -382,10 +380,8 @@ class AddProfileActivityTest {
onView(allOf(withId(R.id.input), isDescendantOfA(withId(R.id.input_name)))).perform(scrollTo()).perform(
typeText("123"), closeSoftKeyboard()
)
onView(withId(R.id.create_button)).perform(scrollTo())
onView(withId(R.id.create_button)).perform(click())
onView(allOf(withId(R.id.input), isDescendantOfA(withId(R.id.input_name)))).perform(scrollTo())
onView(allOf(withId(R.id.input), isDescendantOfA(withId(R.id.input_name)))).perform(
onView(withId(R.id.create_button)).perform(scrollTo()).perform(click())
onView(allOf(withId(R.id.input), isDescendantOfA(withId(R.id.input_name)))).perform(scrollTo()).perform(
typeText(" "), closeSoftKeyboard()
)
onView(
Expand Down Expand Up @@ -700,8 +696,7 @@ class AddProfileActivityTest {
intending(expectedIntent).respondWith(activityResult)
launch(AddProfileActivity::class.java).use {
onView(isRoot()).perform(orientationLandscape())
onView(withId(R.id.upload_image_button)).perform(scrollTo())
onView(withId(R.id.upload_image_button)).perform(click())
onView(withId(R.id.upload_image_button)).perform(scrollTo()).perform(click())
intended(expectedIntent)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class AdminPinActivityTest {
typeText("12345"),
closeSoftKeyboard()
)
onView(withId(R.id.submit_button)).perform(click())
onView(withId(R.id.submit_button)).perform(scrollTo()).perform(click())
intended(hasComponent(AddProfileActivity::class.java.name))
}
}
Expand All @@ -96,7 +96,7 @@ class AdminPinActivityTest {
typeText("12345"),
closeSoftKeyboard()
)
onView(withId(R.id.submit_button)).perform(click())
onView(withId(R.id.submit_button)).perform(scrollTo()).perform(click())
intended(hasComponent(AdministratorControlsActivity::class.java.name))
}
}
Expand Down Expand Up @@ -140,7 +140,7 @@ class AdminPinActivityTest {
typeText("1234"),
closeSoftKeyboard()
)
onView(withId(R.id.submit_button)).perform(click())
onView(withId(R.id.submit_button)).perform(scrollTo()).perform(click())
onView(allOf(withId(R.id.error_text), isDescendantOfA(withId(R.id.input_confirm_pin)))).check(
matches(
withText(
Expand All @@ -162,7 +162,7 @@ class AdminPinActivityTest {
typeText("1234"),
closeSoftKeyboard()
)
onView(withId(R.id.submit_button)).perform(click())
onView(withId(R.id.submit_button)).perform(scrollTo()).perform(click())
onView(allOf(withId(R.id.input), isDescendantOfA(withId(R.id.input_confirm_pin)))).perform(
typeText("5"),
closeSoftKeyboard()
Expand All @@ -181,7 +181,7 @@ class AdminPinActivityTest {
launch<AdminPinActivity>(AdminPinActivity.createAdminPinActivityIntent(context, 0, -10710042, 1)).use {
onView(isRoot()).perform(orientationLandscape())
closeSoftKeyboard()
onView(allOf(withId(R.id.input), isDescendantOfA(withId(R.id.input_pin)))).perform(
onView(allOf(withId(R.id.input), isDescendantOfA(withId(R.id.input_pin)))).perform(scrollTo()).perform(
typeText("12345"),
closeSoftKeyboard()
)
Expand Down Expand Up @@ -310,7 +310,7 @@ class AdminPinActivityTest {
typeText("54321"),
closeSoftKeyboard()
)
onView(withId(R.id.submit_button)).perform(click())
onView(withId(R.id.submit_button)).perform(scrollTo()).perform(click())
onView(isRoot()).perform(orientationLandscape())
onView(
allOf(
Expand Down
Loading