Skip to content

Commit

Permalink
Merge pull request #13625 from nextcloud/fix/flaky-FileDetailFragment…
Browse files Browse the repository at this point in the history
…StaticServerIT-tests

Fix Flaky FileDetailFragmentStaticServerIT Tests
  • Loading branch information
alperozturk96 authored Sep 25, 2024
2 parents b389d37 + 4ccc773 commit dd73e39
Showing 1 changed file with 143 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]>
* SPDX-FileCopyrightText: 2020 Tobias Kaminsky <[email protected]>
* SPDX-FileCopyrightText: 2020 Chris Narkiewicz <[email protected]>
* SPDX-FileCopyrightText: 2020 Chris Narkiewicz <[email protected]>
* SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
*/
package com.owncloud.android.ui.fragment

import androidx.test.espresso.intent.rule.IntentsTestRule
import androidx.annotation.UiThread
import androidx.test.core.app.launchActivity
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.IdlingRegistry
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.isRoot
import com.nextcloud.test.TestActivity
import com.nextcloud.ui.ImageDetailFragment
import com.owncloud.android.AbstractIT
Expand All @@ -18,80 +25,111 @@ import com.owncloud.android.lib.resources.activities.model.Activity
import com.owncloud.android.lib.resources.activities.model.RichElement
import com.owncloud.android.lib.resources.activities.model.RichObject
import com.owncloud.android.lib.resources.activities.models.PreviewObject
import com.owncloud.android.utils.EspressoIdlingResource
import com.owncloud.android.utils.ScreenshotTest
import org.junit.Rule
import org.junit.After
import org.junit.Before
import org.junit.Test
import java.io.File
import java.util.GregorianCalendar

class FileDetailFragmentStaticServerIT : AbstractIT() {
@get:Rule
val testActivityRule = IntentsTestRule(TestActivity::class.java, true, false)
private val testClassName = "com.owncloud.android.ui.fragment.FileDetailFragmentStaticServerIT"

var file = getFile("gps.jpg")
val oCFile = OCFile("/").apply {
@Before
fun registerIdlingResource() {
IdlingRegistry.getInstance().register(EspressoIdlingResource.countingIdlingResource)
}

@After
fun unregisterIdlingResource() {
IdlingRegistry.getInstance().unregister(EspressoIdlingResource.countingIdlingResource)
}

private var file: File = getFile("gps.jpg")
private val oCFile: OCFile = OCFile("/").apply {
storagePath = file.absolutePath
fileId = 12
fileDataStorageManager.saveFile(this)
}

@Test
@UiThread
@ScreenshotTest
fun showFileDetailActivitiesFragment() {
val sut = testActivityRule.launchActivity(null)
sut.addFragment(FileDetailActivitiesFragment.newInstance(oCFile, user))

waitForIdleSync()
shortSleep()
shortSleep()
screenshot(sut)
launchActivity<TestActivity>().use { scenario ->
scenario.onActivity { sut ->
onIdleSync {
EspressoIdlingResource.increment()
sut.addFragment(FileDetailActivitiesFragment.newInstance(oCFile, user))
EspressoIdlingResource.decrement()

val screenShotName = createName(testClassName + "_" + "showFileDetailActivitiesFragment", "")
onView(isRoot()).check(matches(isDisplayed()))
screenshotViaName(sut, screenShotName)
}
}
}
}

@Test
@UiThread
@ScreenshotTest
fun showFileDetailSharingFragment() {
val sut = testActivityRule.launchActivity(null)
sut.addFragment(FileDetailSharingFragment.newInstance(oCFile, user))

waitForIdleSync()
shortSleep()
shortSleep()
screenshot(sut)
launchActivity<TestActivity>().use { scenario ->
scenario.onActivity { sut ->
onIdleSync {
EspressoIdlingResource.increment()
sut.addFragment(FileDetailSharingFragment.newInstance(oCFile, user))
EspressoIdlingResource.decrement()

val screenShotName = createName(testClassName + "_" + "showFileDetailSharingFragment", "")
onView(isRoot()).check(matches(isDisplayed()))
screenshotViaName(sut, screenShotName)
}
}
}
}

@Test
@UiThread
@ScreenshotTest
fun showFileDetailDetailsFragment() {
val activity = testActivityRule.launchActivity(null)
val sut = ImageDetailFragment.newInstance(oCFile, user)
activity.addFragment(sut)

shortSleep()
shortSleep()
waitForIdleSync()

activity.runOnUiThread {
sut.hideMap()
launchActivity<TestActivity>().use { scenario ->
scenario.onActivity { sut ->
onIdleSync {
EspressoIdlingResource.increment()
val fragment = ImageDetailFragment.newInstance(oCFile, user).apply {
hideMap()
}
sut.addFragment(fragment)
EspressoIdlingResource.decrement()

val screenShotName = createName(testClassName + "_" + "showFileDetailDetailsFragment", "")
onView(isRoot()).check(matches(isDisplayed()))
screenshotViaName(sut, screenShotName)
}
}
}

screenshot(activity)
}

@Test
@UiThread
@ScreenshotTest
@Suppress("MagicNumber")
fun showDetailsActivities() {
val date = GregorianCalendar()
date.set(2005, 4, 17, 10, 35, 30) // random date

val richObjectList: ArrayList<RichObject> = ArrayList()
richObjectList.add(RichObject("file", "abc", "text.txt", "/text.txt", "link", "tag"))
richObjectList.add(RichObject("file", "1", "text.txt", "/text.txt", "link", "tag"))
val date = GregorianCalendar().apply {
set(2005, 4, 17, 10, 35, 30)
}

val previewObjectList1: ArrayList<PreviewObject> = ArrayList()
previewObjectList1.add(PreviewObject(1, "source", "link", true, "text/plain", "view", "text.txt"))
val richObjectList = ArrayList<RichObject>().apply {
add(RichObject("file", "abc", "text.txt", "/text.txt", "link", "tag"))
add(RichObject("file", "1", "text.txt", "/text.txt", "link", "tag"))
}

val richObjectList2: ArrayList<RichObject> = ArrayList()
richObjectList2.add(RichObject("user", "admin", "Admin", "", "", ""))
val previewObjectList1 = ArrayList<PreviewObject>().apply {
add(PreviewObject(1, "source", "link", true, "text/plain", "view", "text.txt"))
}

val activities = mutableListOf(
Activity(
Expand Down Expand Up @@ -132,67 +170,85 @@ class FileDetailFragmentStaticServerIT : AbstractIT() {
)
)

val sut = FileDetailFragment.newInstance(oCFile, user, 0)
testActivityRule.launchActivity(null).apply {
addFragment(sut)
waitForIdleSync()
runOnUiThread {
sut.fileDetailActivitiesFragment.populateList(activities as List<Any>?, true)
launchActivity<TestActivity>().use { scenario ->
scenario.onActivity { sut ->
onIdleSync {
EspressoIdlingResource.increment()
val fragment = FileDetailFragment.newInstance(oCFile, user, 0)
sut.addFragment(fragment)
fragment.fileDetailActivitiesFragment.populateList(activities as List<Any>?, true)
EspressoIdlingResource.decrement()

val screenShotName = createName(testClassName + "_" + "showDetailsActivities", "")
onView(isRoot()).check(matches(isDisplayed()))
screenshotViaName(sut, screenShotName)
}
}
longSleep()
screenshot(sut.fileDetailActivitiesFragment.binding.swipeContainingList)
}
}

// @Test
// @ScreenshotTest
@Test
@UiThread
@ScreenshotTest
fun showDetailsActivitiesNone() {
val activity = testActivityRule.launchActivity(null)
val sut = FileDetailFragment.newInstance(oCFile, user, 0)
activity.addFragment(sut)

waitForIdleSync()

activity.runOnUiThread {
sut.fileDetailActivitiesFragment.populateList(emptyList(), true)
launchActivity<TestActivity>().use { scenario ->
scenario.onActivity { sut ->
onIdleSync {
EspressoIdlingResource.increment()
val fragment = FileDetailFragment.newInstance(oCFile, user, 0)
sut.addFragment(fragment)
fragment.fileDetailActivitiesFragment.populateList(emptyList(), true)
EspressoIdlingResource.decrement()

val screenShotName = createName(testClassName + "_" + "showDetailsActivitiesNone", "")
onView(isRoot()).check(matches(isDisplayed()))
screenshotViaName(sut, screenShotName)
}
}
}

shortSleep()
shortSleep()
screenshot(sut.fileDetailActivitiesFragment.binding.list)
}

@Test
@UiThread
@ScreenshotTest
fun showDetailsActivitiesError() {
val activity = testActivityRule.launchActivity(null)
val sut = FileDetailFragment.newInstance(oCFile, user, 0)
activity.addFragment(sut)

waitForIdleSync()

activity.runOnUiThread {
sut.fileDetailActivitiesFragment.disableLoadingActivities()
sut
.fileDetailActivitiesFragment
.setErrorContent(targetContext.resources.getString(R.string.file_detail_activity_error))
launchActivity<TestActivity>().use { scenario ->
scenario.onActivity { sut ->
onIdleSync {
EspressoIdlingResource.increment()
val fragment = FileDetailFragment.newInstance(oCFile, user, 0)
sut.addFragment(fragment)
fragment.fileDetailActivitiesFragment.disableLoadingActivities()
fragment.fileDetailActivitiesFragment.setErrorContent(
targetContext.resources.getString(R.string.file_detail_activity_error)
)
EspressoIdlingResource.decrement()

val screenShotName = createName(testClassName + "_" + "showDetailsActivitiesError", "")
onView(isRoot()).check(matches(isDisplayed()))
screenshotViaName(sut, screenShotName)
}
}
}

shortSleep()
shortSleep()
screenshot(sut.fileDetailActivitiesFragment.binding.emptyList.emptyListView)
}

@Test
@UiThread
@ScreenshotTest
fun showDetailsSharing() {
val sut = testActivityRule.launchActivity(null)
sut.addFragment(FileDetailFragment.newInstance(oCFile, user, 1))

waitForIdleSync()

shortSleep()
shortSleep()
screenshot(sut)
launchActivity<TestActivity>().use { scenario ->
scenario.onActivity { sut ->
onIdleSync {
EspressoIdlingResource.increment()
val fragment = FileDetailFragment.newInstance(oCFile, user, 1)
sut.addFragment(fragment)
EspressoIdlingResource.decrement()

val screenShotName = createName(testClassName + "_" + "showDetailsSharing", "")
onView(isRoot()).check(matches(isDisplayed()))
screenshotViaName(sut, screenShotName)
}
}
}
}
}

0 comments on commit dd73e39

Please sign in to comment.