Skip to content

Commit

Permalink
Fix ShareFileFragmentTest UI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davigonz committed May 16, 2019
1 parent 64fec10 commit b7e58b8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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<Resource<OCCapability>>()
private val sharesLiveData = MutableLiveData<Resource<List<OCShare>>>()
val activityRule = ActivityTestRule(
TestShareFileActivity::class.java,
true,
true
)

private val publicShares = arrayListOf(
private var publicShareList = arrayListOf(
TestUtil.createPublicShare(
path = "/Photos/image.jpg",
isFolder = false,
Expand All @@ -78,103 +74,49 @@ 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<OCShare>()
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)

loadShareFileFragment()
onView(withText("Image link")).check(matches(isDisplayed()))
onView(withText("Image link 2")).check(matches(isDisplayed()))
onView(withText("Image link 3")).check(matches(isDisplayed()))
}

@Test
fun fileSizeVisible() {
loadSharesSuccessfully()
loadShareFileFragment()
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(
TestUtil.createCapability(sharingPublicEnabled = CapabilityBooleanType.TRUE.value)
loadShareFileFragment(
capabilities = TestUtil.createCapability(sharingPublicEnabled = CapabilityBooleanType.TRUE.value)
)
loadSharesSuccessfully()

onView(withText("Image link")).check(matches(isDisplayed()))
onView(withText("Image link 2")).check(matches(isDisplayed()))
Expand All @@ -183,55 +125,51 @@ class ShareFileFragmentTest {

@Test
fun hidePublicSharesSharingDisabled() {
loadCapabilitiesSuccessfully(
TestUtil.createCapability(sharingPublicEnabled = CapabilityBooleanType.FALSE.value)
loadShareFileFragment(
capabilities = TestUtil.createCapability(sharingPublicEnabled = CapabilityBooleanType.FALSE.value)
)
loadSharesSuccessfully()

onView(withId(R.id.shareViaLinkSection))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE)))
}

@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)))
}
Expand All @@ -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<OCShare> = 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<OCShare> = publicShares) {
sharesLiveData.postValue(Resource.success(shares))
activityRule.activity.capabilities = capabilities
activityRule.activity.publicShares = publicShares
activityRule.activity.setFragment(shareFileFragment)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OCShare>

override fun refreshShares() {
fragment
val shareFileFragment: ShareFileFragment = supportFragmentManager.fragments.get(0) as ShareFileFragment
shareFileFragment.updateCapabilities(capabilities)
shareFileFragment.updatePublicShares(publicShares)
}

override fun createPublicShare(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ import com.owncloud.android.vo.Status
* Activity for sharing files
*/
class ShareActivity : FileActivity(), ShareFragmentListener {

private var getSharesForFileAsyncTask: GetSharesForFileAsyncTask? = null

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -389,25 +388,9 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe

// Update view depending on updated capabilities
if (isPublicShareDisabled) {
view?.findViewById<LinearLayout>(R.id.shareViaLinkSection)?.visibility = View.GONE
// shareViaLinkSection.visibility = View.GONE
shareViaLinkSection.visibility = View.GONE
} else {
view?.findViewById<LinearLayout>(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
}
}

Expand Down Expand Up @@ -439,6 +422,16 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe
sharePublicLinksList?.visibility = View.GONE
}

// Show or hide button for adding a new public share depending on the capabilities and
// the server version
if (!enableMultiplePublicSharing()) {
if (publicLinks?.size == 0) {
addPublicLinkButton.visibility = View.VISIBLE
} else if (publicLinks?.size!! >= 1) {
addPublicLinkButton.visibility = View.INVISIBLE
}
}

// Set Scroll to initial position
shareScroll?.scrollTo(0, 0)
}
Expand Down

0 comments on commit b7e58b8

Please sign in to comment.