From 2b471b0ffad3d2765fcfa1ed312c114f775a540d Mon Sep 17 00:00:00 2001 From: davigonz Date: Thu, 16 May 2019 12:01:51 +0200 Subject: [PATCH] Fix UI tests in ShareFileFragment --- .../shares/ui/ShareFileFragmentTest.kt | 139 ++++++------------ .../shares/ui/ShareFolderFragmentTest.kt | 1 - .../shares/ui/TestShareFileActivity.kt | 9 +- .../android/shares/ui/ShareActivity.kt | 1 - .../shares/ui/fragment/ShareFileFragment.kt | 35 ++--- 5 files changed, 66 insertions(+), 119 deletions(-) diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/ShareFileFragmentTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/ShareFileFragmentTest.kt index 11ac9952a3d..0dc9bf40477 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/ShareFileFragmentTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/ShareFileFragmentTest.kt @@ -20,7 +20,6 @@ package com.owncloud.android.shares.ui import android.accounts.Account -import androidx.lifecycle.MutableLiveData import androidx.test.espresso.Espresso.onView import androidx.test.espresso.assertion.ViewAssertions.matches import androidx.test.espresso.matcher.ViewMatchers @@ -33,14 +32,11 @@ import androidx.test.rule.ActivityTestRule import com.owncloud.android.R import com.owncloud.android.capabilities.db.OCCapability import com.owncloud.android.datamodel.OCFile -import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.resources.status.CapabilityBooleanType import com.owncloud.android.lib.resources.status.OwnCloudVersion import com.owncloud.android.shares.db.OCShare import com.owncloud.android.shares.ui.fragment.ShareFileFragment import com.owncloud.android.utils.TestUtil -import com.owncloud.android.vo.Resource -import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @@ -51,13 +47,13 @@ import org.mockito.Mockito.mock class ShareFileFragmentTest { @Rule @JvmField - val activityRule = ActivityTestRule(TestShareFileActivity::class.java, true, true) - lateinit var shareFragment: ShareFileFragment - - private val capabilitiesLiveData = MutableLiveData>() - private val sharesLiveData = MutableLiveData>>() + val activityRule = ActivityTestRule( + TestShareFileActivity::class.java, + true, + true + ) - private val publicShares = arrayListOf( + private var publicShareList = arrayListOf( TestUtil.createPublicShare( path = "/Photos/image.jpg", isFolder = false, @@ -78,62 +74,32 @@ class ShareFileFragmentTest { ) ) - @Before - fun setUp() { - val account = mock(Account::class.java) - val ownCloudVersion = mock(OwnCloudVersion::class.java) - `when`(ownCloudVersion.isSearchUsersSupported).thenReturn(true) - - shareFragment = ShareFileFragment.newInstance( - getOCFileForTesting("image.jpg"), - account, - ownCloudVersion - ) - - activityRule.activity.setFragment(shareFragment) - - shareFragment.updateCapabilities(TestUtil.createCapability()) - shareFragment.updatePublicShares(publicShares) - } - @Test fun showHeader() { + loadShareFileFragment() onView(withId(R.id.shareFileName)).check(matches(withText("image.jpg"))) } @Test fun showPrivateLink() { + loadShareFileFragment() onView(withId(R.id.getPrivateLinkButton)).check(matches(isDisplayed())) } @Test fun showUsersAndGroupsSectionTitle() { + loadShareFileFragment() onView(withText(R.string.share_with_user_section_title)).check(matches(isDisplayed())) } - @Test - fun showLoadingCapabilitiesDialog() { - onView(withId(R.id.loadingLayout)).check(matches(isDisplayed())) - } - - @Test - fun showLoadingSharesDialog() { - loadCapabilitiesSuccessfully() - sharesLiveData.postValue(Resource.loading(publicShares)) - onView(withId(R.id.loadingLayout)).check(matches(isDisplayed())) - } - @Test fun showNoPublicShares() { - val publicShares = arrayListOf() - loadSharesSuccessfully(publicShares) - onView(withId(R.id.shareNoPublicLinks)).check(matches(withText(R.string.share_no_public_links))) + loadShareFileFragment(publicShares = arrayListOf()) + onView(withText(R.string.share_no_public_links)).check(matches(isDisplayed())) } @Test fun showPublicShares() { -// Thread.sleep(3000) - onView(withText("Image link")).check(matches(isDisplayed())) onView(withText("Image link 2")).check(matches(isDisplayed())) onView(withText("Image link 3")).check(matches(isDisplayed())) @@ -141,40 +107,15 @@ class ShareFileFragmentTest { @Test fun fileSizeVisible() { - loadSharesSuccessfully() onView(withId(R.id.shareFileSize)).check(matches(isDisplayed())) } - @Test - fun showErrorWhenLoadingCapabilities() { - capabilitiesLiveData.postValue( - Resource.error( - RemoteOperationResult.ResultCode.SERVICE_UNAVAILABLE - ) - ) - - onView(withId(R.id.snackbar_text)).check(matches(withText(R.string.service_unavailable))) - } - - @Test - fun showErrorWhenLoadingShares() { - loadCapabilitiesSuccessfully() - - sharesLiveData.postValue( - Resource.error( - RemoteOperationResult.ResultCode.SERVICE_UNAVAILABLE, - data = publicShares - ) - ) - onView(withId(R.id.snackbar_text)).check(matches(withText(R.string.service_unavailable))) - } - @Test fun showPublicSharesSharingEnabled() { - loadCapabilitiesSuccessfully( + loadShareFileFragment( + capabilities = TestUtil.createCapability(sharingPublicEnabled = CapabilityBooleanType.TRUE.value) ) - loadSharesSuccessfully() onView(withText("Image link")).check(matches(isDisplayed())) onView(withText("Image link 2")).check(matches(isDisplayed())) @@ -183,10 +124,10 @@ class ShareFileFragmentTest { @Test fun hidePublicSharesSharingDisabled() { - loadCapabilitiesSuccessfully( + loadShareFileFragment( + capabilities = TestUtil.createCapability(sharingPublicEnabled = CapabilityBooleanType.FALSE.value) ) - loadSharesSuccessfully() onView(withId(R.id.shareViaLinkSection)) .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE))) @@ -194,44 +135,41 @@ class ShareFileFragmentTest { @Test fun createPublicShareMultipleCapability() { - loadCapabilitiesSuccessfully( - TestUtil.createCapability( + loadShareFileFragment( + capabilities = TestUtil.createCapability( versionString = "10.1.1", sharingPublicMultiple = CapabilityBooleanType.TRUE.value - ) + ), + publicShares = arrayListOf(publicShareList.get(0)) ) - loadSharesSuccessfully(arrayListOf(publicShares.get(0))) - onView(withId(R.id.addPublicLinkButton)) .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE))) } @Test fun cannotCreatePublicShareMultipleCapability() { - loadCapabilitiesSuccessfully( - TestUtil.createCapability( + loadShareFileFragment( + capabilities = TestUtil.createCapability( versionString = "10.1.1", sharingPublicMultiple = CapabilityBooleanType.FALSE.value - ) + ), + publicShares = arrayListOf(publicShareList.get(0)) ) - loadSharesSuccessfully(arrayListOf(publicShares.get(0))) - onView(withId(R.id.addPublicLinkButton)) .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.INVISIBLE))) } @Test fun cannotCreatePublicShareServerCapability() { - loadCapabilitiesSuccessfully( - TestUtil.createCapability( + loadShareFileFragment( + capabilities = TestUtil.createCapability( versionString = "9.3.1" - ) + ), + publicShares = arrayListOf(publicShareList.get(0)) ) - loadSharesSuccessfully(arrayListOf(publicShares.get(0))) - onView(withId(R.id.addPublicLinkButton)) .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.INVISIBLE))) } @@ -244,15 +182,22 @@ class ShareFileFragmentTest { privateLink = "private link" } - private fun loadCapabilitiesSuccessfully(capability: OCCapability = TestUtil.createCapability()) { - capabilitiesLiveData.postValue( - Resource.success( - capability - ) + private fun loadShareFileFragment( + capabilities: OCCapability = TestUtil.createCapability(), + publicShares: ArrayList = publicShareList + ) { + val account = mock(Account::class.java) + val ownCloudVersion = mock(OwnCloudVersion::class.java) + `when`(ownCloudVersion.isSearchUsersSupported).thenReturn(true) + + val shareFileFragment = ShareFileFragment.newInstance( + getOCFileForTesting("image.jpg"), + account, + ownCloudVersion ) - } - private fun loadSharesSuccessfully(shares: ArrayList = publicShares) { - sharesLiveData.postValue(Resource.success(shares)) + activityRule.activity.capabilities = capabilities + activityRule.activity.publicShares = publicShares + activityRule.activity.setFragment(shareFileFragment) } } diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/ShareFolderFragmentTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/ShareFolderFragmentTest.kt index 3e4f014053d..0d5d0855489 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/ShareFolderFragmentTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/ShareFolderFragmentTest.kt @@ -37,7 +37,6 @@ import com.owncloud.android.shares.db.OCShare import com.owncloud.android.shares.ui.fragment.ShareFileFragment import com.owncloud.android.shares.viewmodel.OCShareViewModel import com.owncloud.android.utils.TestUtil -import com.owncloud.android.utils.ViewModelUtil import com.owncloud.android.vo.Resource import org.hamcrest.Matchers.not import org.junit.Before diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/TestShareFileActivity.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/TestShareFileActivity.kt index cb3485e1c70..07e92b1199b 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/TestShareFileActivity.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/TestShareFileActivity.kt @@ -20,14 +20,21 @@ package com.owncloud.android.shares.ui +import com.owncloud.android.capabilities.db.OCCapability import com.owncloud.android.datamodel.OCFile import com.owncloud.android.shares.db.OCShare +import com.owncloud.android.shares.ui.fragment.ShareFileFragment import com.owncloud.android.shares.ui.fragment.ShareFragmentListener import com.owncloud.android.testing.SingleFragmentActivity class TestShareFileActivity : SingleFragmentActivity(), ShareFragmentListener { + lateinit var capabilities: OCCapability + lateinit var publicShares: ArrayList + override fun refreshShares() { - fragment + val shareFileFragment: ShareFileFragment = supportFragmentManager.fragments.get(0) as ShareFileFragment + shareFileFragment.updateCapabilities(capabilities) + shareFileFragment.updatePublicShares(publicShares) } override fun createPublicShare( diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/ui/ShareActivity.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/ui/ShareActivity.kt index 66977089065..138e10d59b5 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/ui/ShareActivity.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/ui/ShareActivity.kt @@ -66,7 +66,6 @@ import com.owncloud.android.vo.Status * Activity for sharing files */ class ShareActivity : FileActivity(), ShareFragmentListener { - private var getSharesForFileAsyncTask: GetSharesForFileAsyncTask? = null /** diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/ShareFileFragment.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/ShareFileFragment.kt index f8958f6b8dd..8cd1b5255d0 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/ShareFileFragment.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/ShareFileFragment.kt @@ -29,7 +29,6 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.LinearLayout import android.widget.ListView import android.widget.Toast import androidx.fragment.app.Fragment @@ -389,25 +388,9 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe // Update view depending on updated capabilities if (isPublicShareDisabled) { - view?.findViewById(R.id.shareViaLinkSection)?.visibility = View.GONE -// shareViaLinkSection.visibility = View.GONE + shareViaLinkSection.visibility = View.GONE } else { - view?.findViewById(R.id.shareViaLinkSection)?.visibility = View.VISIBLE -// shareViaLinkSection.visibility = View.VISIBLE - } - - // Show or hide button for adding a new public share depending on the capabilities and - // the server version - if (!enableMultiplePublicSharing()) { - if (publicLinks == null) { - return - } - - if (publicLinks?.size!! >= 1) { - addPublicLinkButton.visibility = View.INVISIBLE - return - } - addPublicLinkButton.visibility = View.VISIBLE + shareViaLinkSection.visibility = View.VISIBLE } } @@ -428,6 +411,20 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe this ) + // Show or hide button for adding a new public share depending on the capabilities and + // the server version + if (!enableMultiplePublicSharing()) { + if (publicLinks == null) { + return + } + + if (publicLinks?.size!! >= 1) { + addPublicLinkButton.visibility = View.INVISIBLE + return + } + addPublicLinkButton.visibility = View.VISIBLE + } + // Show or hide public links and no public links message if (publicLinks?.size!! > 0) { shareNoPublicLinks?.visibility = View.GONE