diff --git a/owncloudApp/build.gradle b/owncloudApp/build.gradle index af1a29ad5fa..4646bd729ef 100644 --- a/owncloudApp/build.gradle +++ b/owncloudApp/build.gradle @@ -175,7 +175,7 @@ android { testOptions { unitTests.returnDefaultValues = true - animationsDisabled = false + animationsDisabled = true } } diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/FragmentVisibilityIdlingResource.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/FragmentVisibilityIdlingResource.kt new file mode 100644 index 00000000000..ae922a45e2e --- /dev/null +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/FragmentVisibilityIdlingResource.kt @@ -0,0 +1,39 @@ +package com.owncloud.android.shares.ui + +import android.view.View +import androidx.fragment.app.Fragment +import androidx.test.espresso.IdlingResource + +class FragmentVisibilityIdlingResource(private val fragment: Fragment?, private val expectedVisibility: Int) : + IdlingResource { + private var idle: Boolean = false + private var resourceCallback: IdlingResource.ResourceCallback? = null + + init { + this.idle = false + this.resourceCallback = null + } + + override fun getName(): String { + return FragmentVisibilityIdlingResource::class.java.simpleName + } + + override fun isIdleNow(): Boolean { + if (fragment == null) return false + + idle = idle || (fragment.isVisible && expectedVisibility == View.VISIBLE) || + (!fragment.isVisible && expectedVisibility == View.INVISIBLE) + + if (idle) { + if (resourceCallback != null) { + resourceCallback!!.onTransitionToIdle() + } + } + + return idle + } + + override fun registerIdleTransitionCallback(resourceCallback: IdlingResource.ResourceCallback) { + this.resourceCallback = resourceCallback + } +} diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/CreatePublicShareTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/CreatePublicShareTest.kt index 8b580e6d26e..d3640587278 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/CreatePublicShareTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/CreatePublicShareTest.kt @@ -24,10 +24,11 @@ import android.accounts.AccountManager import android.content.Context import android.content.Intent import android.os.Parcelable +import android.view.View import androidx.lifecycle.MutableLiveData import androidx.test.core.app.ApplicationProvider import androidx.test.espresso.Espresso.onView -import androidx.test.espresso.action.ViewActions +import androidx.test.espresso.IdlingRegistry import androidx.test.espresso.action.ViewActions.click import androidx.test.espresso.assertion.ViewAssertions.doesNotExist import androidx.test.espresso.assertion.ViewAssertions.matches @@ -45,6 +46,7 @@ import com.owncloud.android.lib.common.accounts.AccountUtils 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.FragmentVisibilityIdlingResource import com.owncloud.android.shares.ui.ShareActivity import com.owncloud.android.shares.viewmodel.OCShareViewModel import com.owncloud.android.ui.activity.FileActivity @@ -123,37 +125,35 @@ class CreatePublicShareTest { // obtaining an AccountManager instance val accountManager = AccountManager.get(targetContext) - Thread(Runnable { - accountManager.addAccountExplicitly(account, "a", null) - - // include account version, user, server version and token with the new account - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_OC_VERSION, - OwnCloudVersion("10.2").toString() - ) - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_OC_BASE_URL, - "serverUrl:port" - ) - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_DISPLAY_NAME, - "admin" - ) - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION, - "1" - ) - - accountManager.setAuthToken( - account, - KEY_AUTH_TOKEN_TYPE, - "AUTH_TOKEN" - ) - }).start() + accountManager.addAccountExplicitly(account, "a", null) + + // include account version, user, server version and token with the new account + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_OC_VERSION, + OwnCloudVersion("10.2").toString() + ) + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_OC_BASE_URL, + "serverUrl:port" + ) + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_DISPLAY_NAME, + "admin" + ) + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION, + "1" + ) + + accountManager.setAuthToken( + account, + KEY_AUTH_TOKEN_TYPE, + "AUTH_TOKEN" + ) } } @@ -194,32 +194,56 @@ class CreatePublicShareTest { loadCapabilitiesSuccessfully() loadSharesSuccessfully(arrayListOf()) + // Create share + onView(withId(R.id.addPublicLinkButton)).perform(click()) + + // Force Espresso to wait until dialog fragment is visible + val createShareFragmentIdlingResource = registerCreateShareFragmentAsIdlingResource() + val newPublicShare = publicShares[0] + savePublicShare(newPublicShare) - createPublicShareSuccesfully(newPublicShare, arrayListOf(newPublicShare)) + // New share properly created + sharesLiveData.postValue( + Resource.success( + arrayListOf(newPublicShare) + ) + ) // Check whether the dialog to create the public share has been properly closed onView(withText(R.string.share_via_link_create_title)).check(doesNotExist()) onView(withText(newPublicShare.name)).check(matches(isDisplayed())) + + unregisterCreateShareFragmentAsIdlingResource(createShareFragmentIdlingResource) } @Test fun createPublicShareWithAlreadyExistingShares() { loadCapabilitiesSuccessfully() - val existingPublicShares = publicShares.take(2) as ArrayList - loadSharesSuccessfully( existingPublicShares ) + onView(withId(R.id.addPublicLinkButton)).perform(click()) + + val createShareFragmentIdlingResource = registerCreateShareFragmentAsIdlingResource() + val newPublicShare = publicShares[2] + savePublicShare(newPublicShare) - createPublicShareSuccesfully(newPublicShare, publicShares) + // New share properly created + sharesLiveData.postValue( + Resource.success( + publicShares + ) + ) // Check whether the dialog to create the public share has been properly closed onView(withText(R.string.share_via_link_create_title)).check(doesNotExist()) onView(withText(newPublicShare.name)).check(matches(isDisplayed())) + + unregisterCreateShareFragmentAsIdlingResource(createShareFragmentIdlingResource) } @Test @@ -230,35 +254,71 @@ class CreatePublicShareTest { /** * 1st public share */ + onView(withId(R.id.addPublicLinkButton)).perform(click()) + + val createShareFragmentIdlingResource = registerCreateShareFragmentAsIdlingResource() + val newPublicShare1 = publicShares[0] + savePublicShare(newPublicShare1) - createPublicShareSuccesfully(newPublicShare1, arrayListOf(newPublicShare1)) + // New share properly created + sharesLiveData.postValue( + Resource.success( + arrayListOf(newPublicShare1) + ) + ) // Check whether the dialog to create the public share has been properly closed onView(withText(R.string.share_via_link_create_title)).check(doesNotExist()) onView(withText(newPublicShare1.name)).check(matches(isDisplayed())) + unregisterCreateShareFragmentAsIdlingResource(createShareFragmentIdlingResource) + /** * 2nd public share */ + onView(withId(R.id.addPublicLinkButton)).perform(click()) + + val create2ndShareFragmentIdlingResource = registerCreateShareFragmentAsIdlingResource() + val newPublicShare2 = publicShares[1] + savePublicShare(newPublicShare2) - createPublicShareSuccesfully(newPublicShare2, publicShares.take(2)) + // New share properly created + sharesLiveData.postValue( + Resource.success( + publicShares.take(2) + ) + ) // Check whether the dialog to create the public share has been properly closed onView(withText(R.string.share_via_link_create_title)).check(doesNotExist()) onView(withText(newPublicShare2.name)).check(matches(isDisplayed())) + unregisterCreateShareFragmentAsIdlingResource(create2ndShareFragmentIdlingResource) + /** * 3rd public share */ + onView(withId(R.id.addPublicLinkButton)).perform(click()) + + val create3rdShareFragmentIdlingResource = registerCreateShareFragmentAsIdlingResource() + val newPublicShare3 = publicShares[2] + savePublicShare(newPublicShare3) - createPublicShareSuccesfully(newPublicShare3, publicShares) + // New share properly created + sharesLiveData.postValue( + Resource.success( + publicShares + ) + ) // Check whether the dialog to create the public share has been properly closed onView(withText(R.string.share_via_link_create_title)).check(doesNotExist()) onView(withText(newPublicShare3.name)).check(matches(isDisplayed())) + + unregisterCreateShareFragmentAsIdlingResource(create3rdShareFragmentIdlingResource) } private fun getOCFileForTesting(name: String = "default") = OCFile("/Photos").apply { @@ -286,7 +346,7 @@ class CreatePublicShareTest { sharesLiveData.postValue(Resource.success(shares)) } - private fun createPublicShareSuccesfully(newShare: OCShare, sharesAfterCreation: List) { + private fun savePublicShare(newShare: OCShare) { `when`( ocShareViewModel.insertPublicShareForFile( 1, @@ -295,19 +355,27 @@ class CreatePublicShareTest { -1, false ) - ).thenReturn(sharesLiveData) - - // 1. Open dialog to create new public share - onView(withId(R.id.addPublicLinkButton)).perform(click()) + ).thenReturn( + MutableLiveData>().apply { + postValue(Resource.success()) + } + ) - // 2. Save share onView(withId(R.id.saveButton)).perform(click()) + } - // 3. New share properly created - sharesLiveData.postValue( - Resource.success( - sharesAfterCreation - ) + private fun registerCreateShareFragmentAsIdlingResource(): FragmentVisibilityIdlingResource { + val idlingResource = FragmentVisibilityIdlingResource( + activityRule.activity.supportFragmentManager.findFragmentByTag(ShareActivity.TAG_PUBLIC_SHARE_DIALOG_FRAGMENT), + View.VISIBLE ) + IdlingRegistry.getInstance().register(idlingResource) + return idlingResource + } + + private fun unregisterCreateShareFragmentAsIdlingResource( + fragmentVisibilityIdlingResource: FragmentVisibilityIdlingResource + ) { + IdlingRegistry.getInstance().unregister(fragmentVisibilityIdlingResource) } } diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/DeletePublicShareTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/DeletePublicShareTest.kt index daf56144be2..3a3f47c9dc7 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/DeletePublicShareTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/DeletePublicShareTest.kt @@ -42,7 +42,6 @@ import com.owncloud.android.capabilities.db.OCCapability import com.owncloud.android.capabilities.viewmodel.OCCapabilityViewModel import com.owncloud.android.datamodel.OCFile import com.owncloud.android.lib.common.accounts.AccountUtils -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 @@ -122,37 +121,35 @@ class DeletePublicShareTest { // obtaining an AccountManager instance val accountManager = AccountManager.get(targetContext) - Thread(Runnable { - accountManager.addAccountExplicitly(account, "a", null) - - // include account version, user, server version and token with the new account - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_OC_VERSION, - OwnCloudVersion("10.2").toString() - ) - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_OC_BASE_URL, - "serverUrl:port" - ) - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_DISPLAY_NAME, - "admin" - ) - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION, - "1" - ) - - accountManager.setAuthToken( - account, - KEY_AUTH_TOKEN_TYPE, - "AUTH_TOKEN" - ) - }).start() + accountManager.addAccountExplicitly(account, "a", null) + + // include account version, user, server version and token with the new account + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_OC_VERSION, + OwnCloudVersion("10.2").toString() + ) + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_OC_BASE_URL, + "serverUrl:port" + ) + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_DISPLAY_NAME, + "admin" + ) + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION, + "1" + ) + + accountManager.setAuthToken( + account, + KEY_AUTH_TOKEN_TYPE, + "AUTH_TOKEN" + ) } } @@ -197,7 +194,11 @@ class DeletePublicShareTest { `when`( ocShareViewModel.deletePublicShare(ArgumentMatchers.anyLong()) - ).thenReturn(sharesLiveData) + ).thenReturn( + MutableLiveData>().apply { + postValue(Resource.success()) + } + ) onView(allOf(withId(R.id.deletePublicLinkButton), hasSibling(withText(existingPublicShare[0].name)))) .perform(click()) @@ -222,7 +223,11 @@ class DeletePublicShareTest { `when`( ocShareViewModel.deletePublicShare(ArgumentMatchers.anyLong()) - ).thenReturn(sharesLiveData) + ).thenReturn( + MutableLiveData>().apply { + postValue(Resource.success()) + } + ) onView(withId(R.id.deletePublicLinkButton)).perform(click()) onView(withId(android.R.id.button1)).perform(click()) @@ -239,7 +244,7 @@ class DeletePublicShareTest { } @Test - fun loadingDeleteShares(){ + fun loadingDeleteShares() { loadCapabilitiesSuccessfully() val existingPublicShare = publicShares[0] diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/EditPublicShareFolderTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/EditPublicShareFolderTest.kt index e397b6bd67a..cf16f667163 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/EditPublicShareFolderTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/EditPublicShareFolderTest.kt @@ -27,13 +27,10 @@ import android.os.Parcelable import androidx.lifecycle.MutableLiveData import androidx.test.core.app.ApplicationProvider import androidx.test.espresso.Espresso.onView -import androidx.test.espresso.action.ViewActions import androidx.test.espresso.action.ViewActions.click import androidx.test.espresso.action.ViewActions.replaceText -import androidx.test.espresso.action.ViewActions.scrollTo import androidx.test.espresso.assertion.ViewAssertions.doesNotExist import androidx.test.espresso.assertion.ViewAssertions.matches -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.withId @@ -60,14 +57,14 @@ import org.junit.Before import org.junit.BeforeClass import org.junit.Rule import org.junit.Test -import org.mockito.Mockito.`when` -import org.mockito.Mockito.mock -import org.mockito.Mockito.spy import org.koin.android.ext.koin.androidContext import org.koin.androidx.viewmodel.dsl.viewModel import org.koin.core.context.startKoin import org.koin.core.context.stopKoin import org.koin.dsl.module +import org.mockito.Mockito.`when` +import org.mockito.Mockito.mock +import org.mockito.Mockito.spy class EditPublicShareFolderTest { @Rule @@ -149,37 +146,35 @@ class EditPublicShareFolderTest { // obtaining an AccountManager instance val accountManager = AccountManager.get(targetContext) - Thread(Runnable { - accountManager.addAccountExplicitly(account, "a", null) - - // include account version, user, server version and token with the new account - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_OC_VERSION, - OwnCloudVersion("10.2").toString() - ) - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_OC_BASE_URL, - "serverUrl:port" - ) - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_DISPLAY_NAME, - "admin" - ) - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION, - "1" - ) - - accountManager.setAuthToken( - account, - KEY_AUTH_TOKEN_TYPE, - "AUTH_TOKEN" - ) - }).start() + accountManager.addAccountExplicitly(account, "a", null) + + // include account version, user, server version and token with the new account + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_OC_VERSION, + OwnCloudVersion("10.2").toString() + ) + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_OC_BASE_URL, + "serverUrl:port" + ) + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_DISPLAY_NAME, + "admin" + ) + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION, + "1" + ) + + accountManager.setAuthToken( + account, + KEY_AUTH_TOKEN_TYPE, + "AUTH_TOKEN" + ) } } @@ -233,7 +228,11 @@ class EditPublicShareFolderTest { 1, false ) - ).thenReturn(sharesLiveData) + ).thenReturn( + MutableLiveData>().apply { + postValue(Resource.success()) + } + ) // 1. Open dialog to edit an existing public share onView(withId(R.id.editPublicLinkButton)).perform(click()) @@ -273,7 +272,11 @@ class EditPublicShareFolderTest { 15, true ) - ).thenReturn(sharesLiveData) + ).thenReturn( + MutableLiveData>().apply { + postValue(Resource.success()) + } + ) // 1. Open dialog to edit an existing public share onView(withId(R.id.editPublicLinkButton)).perform(click()) @@ -314,7 +317,11 @@ class EditPublicShareFolderTest { 4, true ) - ).thenReturn(sharesLiveData) + ).thenReturn( + MutableLiveData>().apply { + postValue(Resource.success()) + } + ) // 1. Open dialog to edit an existing public share onView(withId(R.id.editPublicLinkButton)).perform(click()) @@ -355,7 +362,11 @@ class EditPublicShareFolderTest { 1, false ) - ).thenReturn(sharesLiveData) + ).thenReturn( + MutableLiveData>().apply { + postValue(Resource.success()) + } + ) // 1. Open dialog to edit an existing public share onView(withId(R.id.editPublicLinkButton)).perform(click()) @@ -379,7 +390,6 @@ class EditPublicShareFolderTest { } - private fun getOCFileForTesting(name: String = "default"): OCFile { val file = OCFile("/Photos") file.availableOfflineStatus = OCFile.AvailableOfflineStatus.NOT_AVAILABLE_OFFLINE diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/EditPublicShareTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/EditPublicShareTest.kt index fce494726f4..e2bb155411f 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/EditPublicShareTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/EditPublicShareTest.kt @@ -71,6 +71,8 @@ import org.koin.dsl.module import org.mockito.Mockito.`when` import org.mockito.Mockito.mock import org.mockito.Mockito.spy +import java.util.Calendar +import java.util.GregorianCalendar class EditPublicShareTest { @Rule @@ -146,37 +148,35 @@ class EditPublicShareTest { // obtaining an AccountManager instance val accountManager = AccountManager.get(targetContext) - Thread(Runnable { - accountManager.addAccountExplicitly(account, "a", null) - - // include account version, user, server version and token with the new account - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_OC_VERSION, - OwnCloudVersion("10.2").toString() - ) - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_OC_BASE_URL, - "serverUrl:port" - ) - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_DISPLAY_NAME, - "admin" - ) - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION, - "1" - ) - - accountManager.setAuthToken( - account, - KEY_AUTH_TOKEN_TYPE, - "AUTH_TOKEN" - ) - }).start() + accountManager.addAccountExplicitly(account, "a", null) + + // include account version, user, server version and token with the new account + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_OC_VERSION, + OwnCloudVersion("10.2").toString() + ) + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_OC_BASE_URL, + "serverUrl:port" + ) + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_DISPLAY_NAME, + "admin" + ) + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION, + "1" + ) + + accountManager.setAuthToken( + account, + KEY_AUTH_TOKEN_TYPE, + "AUTH_TOKEN" + ) } } @@ -230,7 +230,11 @@ class EditPublicShareTest { 1, false ) - ).thenReturn(sharesLiveData) + ).thenReturn( + MutableLiveData>().apply { + postValue(Resource.success()) + } + ) // 1. Open dialog to edit an existing public share onView(withId(R.id.editPublicLinkButton)).perform(click()) @@ -271,7 +275,11 @@ class EditPublicShareTest { 1, false ) - ).thenReturn(sharesLiveData) + ).thenReturn( + MutableLiveData>().apply { + postValue(Resource.success()) + } + ) // 1. Open dialog to edit an existing public share onView(withId(R.id.editPublicLinkButton)).perform(click()) @@ -315,7 +323,11 @@ class EditPublicShareTest { 1, false ) - ).thenReturn(sharesLiveData) + ).thenReturn( + MutableLiveData>().apply { + postValue(Resource.success()) + } + ) // 1. Open dialog to edit an existing public share onView(withId(R.id.editPublicLinkButton)).perform(click()) @@ -349,6 +361,9 @@ class EditPublicShareTest { val expirationDate = 1583967600000 + val calendar = GregorianCalendar() + calendar.timeInMillis = expirationDate + `when`( ocShareViewModel.updatePublicShareForFile( 1, @@ -358,15 +373,24 @@ class EditPublicShareTest { 1, false ) - ).thenReturn(sharesLiveData) + ).thenReturn( + MutableLiveData>().apply { + postValue(Resource.success()) + } + ) // 1. Open dialog to edit an existing public share onView(withId(R.id.editPublicLinkButton)).perform(click()) // 2. Enable expiration date and set it onView(withId(R.id.shareViaLinkExpirationSwitch)).perform(click()) - onView(withClassName(Matchers.equalTo(DatePicker::class.java.name))). - perform(PickerActions.setDate(2020, 3, 12)); + onView(withClassName(Matchers.equalTo(DatePicker::class.java.name))).perform( + PickerActions.setDate( + calendar.get(Calendar.YEAR), + calendar.get(Calendar.MONTH) + 1, // January code is 0, so let's add 1 + calendar.get(Calendar.DATE) + ) + ); onView(withId(android.R.id.button1)).perform(click()) // 3. Save updated share @@ -402,7 +426,11 @@ class EditPublicShareTest { 1, false ) - ).thenReturn(sharesLiveData) + ).thenReturn( + MutableLiveData>().apply { + postValue(Resource.success()) + } + ) // 1. Open dialog to edit an existing public share onView(withId(R.id.editPublicLinkButton)).perform(click()) diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/LoadPublicSharesTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/LoadPublicSharesTest.kt index 43d374a3a0b..4aad14dc63e 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/LoadPublicSharesTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/usecases/LoadPublicSharesTest.kt @@ -118,37 +118,35 @@ class LoadPublicSharesTest { // obtaining an AccountManager instance val accountManager = AccountManager.get(targetContext) - Thread(Runnable { - accountManager.addAccountExplicitly(account, "a", null) - - // include account version, user, server version and token with the new account - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_OC_VERSION, - OwnCloudVersion("10.2").toString() - ) - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_OC_BASE_URL, - "serverUrl:port" - ) - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_DISPLAY_NAME, - "admin" - ) - accountManager.setUserData( - account, - AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION, - "1" - ) - - accountManager.setAuthToken( - account, - KEY_AUTH_TOKEN_TYPE, - "AUTH_TOKEN" - ) - }).start() + accountManager.addAccountExplicitly(account, "a", null) + + // include account version, user, server version and token with the new account + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_OC_VERSION, + OwnCloudVersion("10.2").toString() + ) + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_OC_BASE_URL, + "serverUrl:port" + ) + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_DISPLAY_NAME, + "admin" + ) + accountManager.setUserData( + account, + AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION, + "1" + ) + + accountManager.setAuthToken( + account, + KEY_AUTH_TOKEN_TYPE, + "AUTH_TOKEN" + ) } } diff --git a/owncloudApp/src/main/java/com/owncloud/android/NetworkBoundResource.kt b/owncloudApp/src/main/java/com/owncloud/android/NetworkBoundResource.kt index dd3b635baf1..fae35a4c489 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/NetworkBoundResource.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/NetworkBoundResource.kt @@ -39,6 +39,7 @@ import androidx.annotation.MainThread import androidx.annotation.WorkerThread import androidx.lifecycle.LiveData import androidx.lifecycle.MediatorLiveData +import androidx.lifecycle.MutableLiveData import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.vo.Resource @@ -58,8 +59,8 @@ abstract class NetworkBoundResource( val dbSource = loadFromDb() result.addSource(dbSource) { data -> result.removeSource(dbSource) - if (shouldFetch(data)) { - performNetworkOperation(dbSource) + if (shouldFetchFromNetwork(data)) { + fetchFromNetwork(dbSource) } else { result.addSource(dbSource) { newData -> setValue(Resource.success(newData)) @@ -75,7 +76,7 @@ abstract class NetworkBoundResource( } } - private fun performNetworkOperation(dbSource: LiveData) { + private fun fetchFromNetwork(dbSource: LiveData) { // Let's dispatch dbSource value quickly while network operation is performed result.addSource(dbSource) { newData -> if (newData != null) { @@ -132,12 +133,13 @@ abstract class NetworkBoundResource( } fun asLiveData() = result as LiveData> + fun asMutableLiveData() = result as MutableLiveData> @WorkerThread protected abstract fun saveCallResult(item: RequestType) @MainThread - protected abstract fun shouldFetch(data: ResultType?): Boolean + protected abstract fun shouldFetchFromNetwork(data: ResultType?): Boolean @MainThread protected abstract fun loadFromDb(): LiveData diff --git a/owncloudApp/src/main/java/com/owncloud/android/capabilities/repository/OCCapabilityRepository.kt b/owncloudApp/src/main/java/com/owncloud/android/capabilities/repository/OCCapabilityRepository.kt index 148fd7417d5..747f34d7e9a 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/capabilities/repository/OCCapabilityRepository.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/capabilities/repository/OCCapabilityRepository.kt @@ -56,7 +56,7 @@ class OCCapabilityRepository( localCapabilitiesDataSource.insert(listOf(OCCapability.fromRemoteCapability(item))) } - override fun shouldFetch(data: OCCapability?) = shouldFetchFromNetwork + override fun shouldFetchFromNetwork(data: OCCapability?) = shouldFetchFromNetwork override fun loadFromDb(): LiveData = localCapabilitiesDataSource.getCapabilityForAccountAsLiveData(accountName) diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/repository/OCShareRepository.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/repository/OCShareRepository.kt index 8ec3fcc8e00..cc98a65119a 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/repository/OCShareRepository.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/repository/OCShareRepository.kt @@ -20,6 +20,7 @@ package com.owncloud.android.shares.repository import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData import com.owncloud.android.AppExecutors import com.owncloud.android.NetworkBoundResource import com.owncloud.android.lib.resources.shares.ShareParserResult @@ -30,35 +31,15 @@ import com.owncloud.android.shares.db.OCShare import com.owncloud.android.vo.Resource class OCShareRepository( - private val appExecutors: AppExecutors, + private val appExecutors: AppExecutors = AppExecutors(), private val localSharesDataSource: LocalSharesDataSource, private val remoteSharesDataSource: RemoteSharesDataSource, val filePath: String, - val accountName: String + val accountName: String, + val shareTypes: List ) : ShareRepository { - - companion object Factory { - fun create( - appExecutors: AppExecutors = AppExecutors(), - localSharesDataSource: LocalSharesDataSource, - remoteSharesDataSource: RemoteSharesDataSource, - filePathToShare: String, - accountName: String - ): OCShareRepository = OCShareRepository( - appExecutors, - localSharesDataSource, - remoteSharesDataSource, - filePathToShare, - accountName - ) - } - - override fun loadSharesForFile( - shareTypes: List, - reshares: Boolean, - subfiles: Boolean - ): LiveData>> { - return object : NetworkBoundResource, ShareParserResult>(appExecutors) { + private val sharesForFile: MutableLiveData>> = + object : NetworkBoundResource, ShareParserResult>(appExecutors) { override fun saveCallResult(item: ShareParserResult) { val sharesForFileFromServer = item.shares.map { remoteShare -> OCShare.fromRemoteShare(remoteShare).also { it.accountOwner = accountName } @@ -71,15 +52,17 @@ class OCShareRepository( localSharesDataSource.replaceSharesForFile(sharesForFileFromServer) } - override fun shouldFetch(data: List?) = true + override fun shouldFetchFromNetwork(data: List?) = true override fun loadFromDb(): LiveData> = localSharesDataSource.getSharesForFileAsLiveData(filePath, accountName, shareTypes) override fun createCall() = - remoteSharesDataSource.getSharesForFile(filePath, reshares, subfiles) + remoteSharesDataSource.getSharesForFile(filePath, reshares = true, subfiles = false) + }.asMutableLiveData() - }.asLiveData() + override fun getSharesForFile(): LiveData>> { + return sharesForFile } override fun insertPublicShareForFile( @@ -88,25 +71,13 @@ class OCShareRepository( password: String, expirationTimeInMillis: Long, publicUpload: Boolean - ): LiveData>> { - return object : NetworkBoundResource, ShareParserResult>(appExecutors) { - override fun saveCallResult(item: ShareParserResult) { - val newShareForFileFromServer = item.shares.map { remoteShare -> - OCShare.fromRemoteShare(remoteShare).also { it.accountOwner = accountName } - } - - localSharesDataSource.insert(newShareForFileFromServer) - } - - override fun shouldFetch(data: List?) = true - - override fun loadFromDb(): LiveData> { - return localSharesDataSource.getSharesForFileAsLiveData( - filePath, accountName, listOf(ShareType.PUBLIC_LINK) - ) - } + ): LiveData> { + val result = MutableLiveData>() + result.postValue(Resource.loading()) - override fun createCall() = remoteSharesDataSource.insertShareForFile( + appExecutors.networkIO().execute() { + // Perform network operation + val remoteOperationResult = remoteSharesDataSource.insertShareForFile( filePath, ShareType.PUBLIC_LINK, "", @@ -116,7 +87,24 @@ class OCShareRepository( expirationTimeInMillis, publicUpload ) - }.asLiveData() + + if (remoteOperationResult.isSuccess) { + val newShareForFileFromServer = remoteOperationResult.data.shares.map { remoteShare -> + OCShare.fromRemoteShare(remoteShare).also { it.accountOwner = accountName } + } + localSharesDataSource.insert(newShareForFileFromServer) + result.postValue(Resource.success()) // Used to close the share creation dialog + } else { + result.postValue( + Resource.error( + remoteOperationResult.code, + msg = remoteOperationResult.httpPhrase, + exception = remoteOperationResult.exception + ) + ) + } + } + return result } override fun updatePublicShareForFile( @@ -126,27 +114,13 @@ class OCShareRepository( expirationDateInMillis: Long, permissions: Int, publicUpload: Boolean - ): LiveData>> { - return object : NetworkBoundResource, ShareParserResult>(appExecutors) { - override fun saveCallResult(item: ShareParserResult) { - val updatedShareForFileFromServer = item.shares.map { remoteShare -> - OCShare.fromRemoteShare(remoteShare).also { it.accountOwner = accountName } - } + ): LiveData> { + val result = MutableLiveData>() + result.postValue(Resource.loading()) - localSharesDataSource.update(updatedShareForFileFromServer.first()) - } - - override fun shouldFetch(data: List?): Boolean { - return true - } - - override fun loadFromDb(): LiveData> { - return localSharesDataSource.getSharesForFileAsLiveData( - filePath, accountName, listOf(ShareType.PUBLIC_LINK) - ) - } - - override fun createCall() = remoteSharesDataSource.updateShareForFile( + appExecutors.networkIO().execute() { + // Perform network operation + val remoteOperationResult = remoteSharesDataSource.updateShareForFile( remoteId, name, password, @@ -154,28 +128,51 @@ class OCShareRepository( permissions, publicUpload ) - }.asLiveData() + + if (remoteOperationResult.isSuccess) { + val updatedShareForFileFromServer = remoteOperationResult.data.shares.map { remoteShare -> + OCShare.fromRemoteShare(remoteShare).also { it.accountOwner = accountName } + } + localSharesDataSource.update(updatedShareForFileFromServer.first()) + result.postValue(Resource.success()) // Used to close the share edition dialog + } else { + result.postValue( + Resource.error( + remoteOperationResult.code, + msg = remoteOperationResult.httpPhrase, + exception = remoteOperationResult.exception + ) + ) + } + } + return result } override fun deletePublicShare( remoteId: Long - ): LiveData>> { - return object : NetworkBoundResource, ShareParserResult>(appExecutors) { - override fun saveCallResult(item: ShareParserResult) { - localSharesDataSource.deleteShare(remoteId) - } + ): LiveData> { + val result = MutableLiveData>() - override fun shouldFetch(data: List?): Boolean { - return true - } + result.postValue(Resource.loading()) + + // Perform network operation + appExecutors.networkIO().execute() { + // Perform network operation + val remoteOperationResult = remoteSharesDataSource.deleteShare(remoteId) - override fun loadFromDb(): LiveData> { - return localSharesDataSource.getSharesForFileAsLiveData( - filePath, accountName, listOf(ShareType.PUBLIC_LINK) + if (remoteOperationResult.isSuccess) { + localSharesDataSource.deleteShare(remoteId) + result.postValue(Resource.success()) // Used to close the share edition dialog + } else { + result.postValue( + Resource.error( + remoteOperationResult.code, + msg = remoteOperationResult.httpPhrase, + exception = remoteOperationResult.exception + ) ) } - - override fun createCall() = remoteSharesDataSource.deleteShare(remoteId) - }.asLiveData() + } + return result } } diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/repository/ShareRepository.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/repository/ShareRepository.kt index c920d0665a7..16485c15d02 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/repository/ShareRepository.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/repository/ShareRepository.kt @@ -20,16 +20,11 @@ package com.owncloud.android.shares.repository import androidx.lifecycle.LiveData -import com.owncloud.android.lib.resources.shares.ShareType import com.owncloud.android.shares.db.OCShare import com.owncloud.android.vo.Resource interface ShareRepository { - fun loadSharesForFile( - shareTypes: List, - reshares: Boolean, - subfiles: Boolean - ): LiveData>> + fun getSharesForFile(): LiveData>> fun insertPublicShareForFile( permissions: Int, @@ -37,7 +32,7 @@ interface ShareRepository { password: String, expirationTimeInMillis: Long, publicUpload: Boolean - ): LiveData>> + ): LiveData> fun updatePublicShareForFile( remoteId: Long, @@ -46,9 +41,9 @@ interface ShareRepository { expirationDateInMillis: Long, permissions: Int, publicUpload: Boolean - ): LiveData>> + ): LiveData> fun deletePublicShare( remoteId: Long - ): LiveData>> + ): LiveData> } 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 5b2b6dad05e..e43ed7194ff 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 @@ -259,7 +259,8 @@ class ShareActivity : FileActivity(), ShareFragmentListener { ) Snackbar.make( findViewById(android.R.id.content), - errorMessage, Snackbar.LENGTH_SHORT + errorMessage, + Snackbar.LENGTH_SHORT ).show() shareFileFragment?.updatePublicShares(resource.data as ArrayList) dismissLoadingDialog() @@ -402,6 +403,7 @@ class ShareActivity : FileActivity(), ShareFragmentListener { when (resource?.status) { Status.SUCCESS -> { publicShareFragment?.dismiss() + Log_OC.d("TESTS", "Closing share creation dialog") } Status.ERROR -> { val errorMessage: String = resource.msg ?: ErrorMessageAdapter.getResultMessage( @@ -417,7 +419,10 @@ class ShareActivity : FileActivity(), ShareFragmentListener { showLoadingDialog(R.string.common_loading) } else -> { - Log.d(TAG, "Unknown status when creating public share") + Log.d( + TAG, "Unknown status when creating public share with name ${name} \" +" + + "from account ${account?.name}" + ) } } } @@ -469,7 +474,10 @@ class ShareActivity : FileActivity(), ShareFragmentListener { showLoadingDialog(R.string.common_loading) } else -> { - Log.d(TAG, "Unknown status when updating public share") + Log.d( + TAG, "Unknown status when updating public share with name ${name} " + + "from account ${account?.name}" + ) } } } @@ -490,7 +498,6 @@ class ShareActivity : FileActivity(), ShareFragmentListener { Observer { resource -> when (resource?.status) { Status.SUCCESS -> { - shareFileFragment?.updatePublicShares(resource.data as ArrayList) dismissLoadingDialog() } Status.ERROR -> { @@ -508,7 +515,7 @@ class ShareActivity : FileActivity(), ShareFragmentListener { } else -> { Log.d( - TAG, "Unknown status when removing share ${share.name} " + + TAG, "Unknown status when removing public share with name ${share.name} " + "from account ${account?.name}" ) } @@ -570,8 +577,5 @@ class ShareActivity : FileActivity(), ShareFragmentListener { const val TAG_EDIT_SHARE_FRAGMENT = "EDIT_SHARE_FRAGMENT" const val TAG_PUBLIC_SHARE_DIALOG_FRAGMENT = "PUBLIC_SHARE_DIALOG_FRAGMENT" const val TAG_REMOVE_SHARE_DIALOG_FRAGMENT = "REMOVE_SHARE_DIALOG_FRAGMENT" - - const val EXTRA_SHARE_VIEW_MODEL_FACTORY = "SHARE_VIEW_MODEL_FACTORY" - const val EXTRA_CAPABILITY_VIEW_MODEL_FACTORY = "CAPABILITY_VIEW_MODEL_FACTORY" } } diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/viewmodel/OCShareViewModel.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/viewmodel/OCShareViewModel.kt index a53a5120dfd..b8c68a4953d 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/viewmodel/OCShareViewModel.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/viewmodel/OCShareViewModel.kt @@ -42,7 +42,7 @@ class OCShareViewModel( val filePath: String, val account: Account, val shareTypes: List, - val shareRepository: ShareRepository = OCShareRepository.create( + private val shareRepository: ShareRepository = OCShareRepository( localSharesDataSource = OCLocalSharesDataSource(), remoteSharesDataSource = OCRemoteSharesDataSource( OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor( @@ -50,12 +50,16 @@ class OCShareViewModel( MainApp.appContext ) ), - filePathToShare = filePath, - accountName = account.name + filePath = filePath, + accountName = account.name, + shareTypes = shareTypes ) ) : ViewModel() { - fun getSharesForFile(): LiveData>> = - shareRepository.loadSharesForFile(shareTypes, reshares = true, subfiles = false) + private val sharesForFile: LiveData>> = shareRepository.getSharesForFile() + + fun getSharesForFile(): LiveData>> { + return sharesForFile + } fun insertPublicShareForFile( permissions: Int, @@ -63,7 +67,7 @@ class OCShareViewModel( password: String, expirationTimeInMillis: Long, publicUpload: Boolean - ): LiveData>> = shareRepository.insertPublicShareForFile( + ): LiveData> = shareRepository.insertPublicShareForFile( permissions, name, password, expirationTimeInMillis, publicUpload ) @@ -74,11 +78,11 @@ class OCShareViewModel( expirationDateInMillis: Long, permissions: Int, publicUpload: Boolean - ): LiveData>> = shareRepository.updatePublicShareForFile( + ): LiveData> = shareRepository.updatePublicShareForFile( remoteId, name, password, expirationDateInMillis, permissions, publicUpload ) fun deletePublicShare( remoteId: Long - ): LiveData>> = shareRepository.deletePublicShare(remoteId) + ): LiveData> = shareRepository.deletePublicShare(remoteId) } diff --git a/owncloudApp/src/main/java/com/owncloud/android/vo/Resource.kt b/owncloudApp/src/main/java/com/owncloud/android/vo/Resource.kt index 1a5a663d6d3..cde82583448 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/vo/Resource.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/vo/Resource.kt @@ -16,9 +16,10 @@ package com.owncloud.android.vo -import com.owncloud.android.vo.Status.* import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode -import java.lang.Exception +import com.owncloud.android.vo.Status.ERROR +import com.owncloud.android.vo.Status.LOADING +import com.owncloud.android.vo.Status.SUCCESS /** * A generic class that holds a value with its loading status. @@ -32,7 +33,7 @@ data class Resource( val exception: Exception? = null ) { companion object { - fun success(data: T?): Resource { + fun success(data: T? = null): Resource { return Resource(SUCCESS, ResultCode.OK, data) } @@ -45,7 +46,7 @@ data class Resource( return Resource(ERROR, code, data, msg, exception) } - fun loading(data: T?): Resource { + fun loading(data: T? = null): Resource { return Resource(LOADING, data = data) } } diff --git a/owncloudApp/src/test/java/com/owncloud/android/NetworkBoundResourceTest.kt b/owncloudApp/src/test/java/com/owncloud/android/NetworkBoundResourceTest.kt index b30e08dd8be..e3388dbb1f2 100644 --- a/owncloudApp/src/test/java/com/owncloud/android/NetworkBoundResourceTest.kt +++ b/owncloudApp/src/test/java/com/owncloud/android/NetworkBoundResourceTest.kt @@ -78,7 +78,7 @@ class NetworkBoundResourceTest { dbData.value = fetchedDbValue } - override fun shouldFetch(data: List?) = true + override fun shouldFetchFromNetwork(data: List?) = true override fun loadFromDb(): LiveData> = dbData @@ -128,7 +128,7 @@ class NetworkBoundResourceTest { saved.set(true) } - override fun shouldFetch(data: List?) = true + override fun shouldFetchFromNetwork(data: List?) = true override fun loadFromDb(): LiveData> = dbData.apply { value = fetchedDbValue } @@ -170,7 +170,7 @@ class NetworkBoundResourceTest { saved.set(true) } - override fun shouldFetch(data: List?) = true + override fun shouldFetchFromNetwork(data: List?) = true override fun loadFromDb(): LiveData> = dbData diff --git a/owncloudApp/src/test/java/com/owncloud/android/shares/repository/OCShareRepositoryTest.kt b/owncloudApp/src/test/java/com/owncloud/android/shares/repository/OCShareRepositoryTest.kt index fdeea1d9d74..f6aaacaa84f 100644 --- a/owncloudApp/src/test/java/com/owncloud/android/shares/repository/OCShareRepositoryTest.kt +++ b/owncloudApp/src/test/java/com/owncloud/android/shares/repository/OCShareRepositoryTest.kt @@ -189,12 +189,11 @@ class OCShareRepositoryTest { ) val data = insertShare(localData, remoteOperationResult) - val observer = mock>>>() + val observer = mock>>() data.observeForever(observer) - // Get public shares from database to observe them, is called twice (one showing current db shares while - // creating share on server and another one with db shares already updated with just created share) - verify(localSharesDataSource, times(2)).getSharesForFileAsLiveData( + // Get public shares from database is called once, when creating the repository + verify(localSharesDataSource, times(1)).getSharesForFileAsLiveData( "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK) ) @@ -232,12 +231,12 @@ class OCShareRepositoryTest { // Retrieving public shares from server... // Observe changes in database livedata when there's an error from server - val observer = mock>>>() + val observer = mock>>() data.observeForever(observer) verify(observer).onChanged( Resource.error( - RemoteOperationResult.ResultCode.SHARE_NOT_FOUND, localData.value, exception = exception + RemoteOperationResult.ResultCode.SHARE_NOT_FOUND, exception = exception ) ) } @@ -253,12 +252,11 @@ class OCShareRepositoryTest { val data = updateShare(localData, remoteOperationResult) - val observer = mock>>>() + val observer = mock>>() data.observeForever(observer) - // Get public shares from database to observe them, is called twice (one showing current db shares while - // updating share on server and another one with db shares already updated with just updated share) - verify(localSharesDataSource, times(2)).getSharesForFileAsLiveData( + // Get public shares from database is called once, when creating the repository + verify(localSharesDataSource, times(1)).getSharesForFileAsLiveData( "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK) ) @@ -281,12 +279,11 @@ class OCShareRepositoryTest { val data = deleteShare(localData, remoteOperationResult) - val observer = mock>>>() + val observer = mock>>() data.observeForever(observer) - // Get public shares from database to observe them, is called twice (one showing current db shares while - // deleting share on server and another one with db shares already updated with just deleted share) - verify(localSharesDataSource, times(2)).getSharesForFileAsLiveData( + // Get public shares from database is called once, when creating the repository + verify(localSharesDataSource, times(1)).getSharesForFileAsLiveData( "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK) ) @@ -310,12 +307,13 @@ class OCShareRepositoryTest { val remoteSharesDataSource = RemoteSharesDataSourceTest(remoteOperationResult) - return OCShareRepository.create( + return OCShareRepository( InstantAppExecutors(), localSharesDataSource, remoteSharesDataSource, "/Photos/", - "admin@server" + "admin@server", + listOf(ShareType.PUBLIC_LINK) ) } @@ -325,15 +323,13 @@ class OCShareRepositoryTest { ): LiveData>> { val ocShareRepository = createRepositoryWithData(localData, remoteOperationResult) - return ocShareRepository.loadSharesForFile( - listOf(ShareType.PUBLIC_LINK), reshares = true, subfiles = false - ) + return ocShareRepository.getSharesForFile() } private fun insertShare( localData: MutableLiveData>, remoteOperationResult: RemoteOperationResult - ): LiveData>> { + ): LiveData> { val ocShareRepository = createRepositoryWithData(localData, remoteOperationResult) return ocShareRepository.insertPublicShareForFile( @@ -348,7 +344,7 @@ class OCShareRepositoryTest { private fun updateShare( localData: MutableLiveData>, remoteOperationResult: RemoteOperationResult - ): LiveData>> { + ): LiveData> { val ocShareRepository = createRepositoryWithData(localData, remoteOperationResult) return ocShareRepository.updatePublicShareForFile( @@ -364,9 +360,8 @@ class OCShareRepositoryTest { private fun deleteShare( localData: MutableLiveData>, remoteOperationResult: RemoteOperationResult - ): LiveData>> { + ): LiveData> { val ocShareRepository = createRepositoryWithData(localData, remoteOperationResult) - return ocShareRepository.deletePublicShare( 1 ) diff --git a/owncloudApp/src/test/java/com/owncloud/android/shares/viewmodel/OCShareViewModelTest.kt b/owncloudApp/src/test/java/com/owncloud/android/shares/viewmodel/OCShareViewModelTest.kt index 68aff91b32c..a7cd4a31208 100644 --- a/owncloudApp/src/test/java/com/owncloud/android/shares/viewmodel/OCShareViewModelTest.kt +++ b/owncloudApp/src/test/java/com/owncloud/android/shares/viewmodel/OCShareViewModelTest.kt @@ -27,8 +27,8 @@ import com.owncloud.android.shares.db.OCShare import com.owncloud.android.shares.repository.OCShareRepository import com.owncloud.android.utils.TestUtil import com.owncloud.android.vo.Resource +import com.owncloud.android.vo.Status import junit.framework.Assert.assertEquals -import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @@ -43,11 +43,10 @@ class OCShareViewModelTest { val instantExecutorRule = InstantTaskExecutorRule() private var testAccount: Account = TestUtil.createAccount("admin@server", "test") - private val publicShareResourcesAsLiveData: MutableLiveData>> = MutableLiveData() private var ocShareRepository: OCShareRepository = mock(OCShareRepository::class.java) - @Before - fun init() { + @Test + fun loadPublicShares() { val publicShares = mutableListOf( TestUtil.createPublicShare( path = "/Photos/image.jpg", @@ -63,19 +62,12 @@ class OCShareViewModelTest { ) ) - publicShareResourcesAsLiveData.value = Resource.success(publicShares) - } - - @Test - fun loadPublicShares() { `when`( - ocShareRepository.loadSharesForFile( - listOf(ShareType.PUBLIC_LINK), - reshares = true, - subfiles = false - ) + ocShareRepository.getSharesForFile() ).thenReturn( - publicShareResourcesAsLiveData + MutableLiveData>>().apply { + value = Resource.success(publicShares) + } ) // Viewmodel that will ask ocShareRepository for shares @@ -98,13 +90,15 @@ class OCShareViewModelTest { false ) ).thenReturn( - publicShareResourcesAsLiveData + MutableLiveData>().apply { + value = Resource.success() + } ) // Viewmodel that will ask ocShareRepository for shares val ocShareViewModel = createOCShareViewModel(ocShareRepository) - val resource: Resource>? = ocShareViewModel.insertPublicShareForFile( + val resource: Resource? = ocShareViewModel.insertPublicShareForFile( 1, "Photos 2 link", "1234", @@ -112,7 +106,7 @@ class OCShareViewModelTest { false ).value - assertShareParameters(resource?.data) + assertEquals(Status.SUCCESS, resource?.status) } @Test @@ -122,29 +116,31 @@ class OCShareViewModelTest { `when`( ocShareRepository.updatePublicShareForFile( 1, - "Photos 2 link", - "1234", + "Photos 1 link", + "123456", 1000, 1, false ) ).thenReturn( - publicShareResourcesAsLiveData + MutableLiveData>().apply { + value = Resource.success() + } ) // Viewmodel that will ask ocShareRepository for shares val ocShareViewModel = createOCShareViewModel(ocShareRepository) - val resource: Resource>? = ocShareViewModel.updatePublicShareForFile( + val resource: Resource? = ocShareViewModel.updatePublicShareForFile( 1, - "Photos 2 link", - "1234", + "Photos 1 link", + "123456", 1000, 1, false ).value - assertShareParameters(resource?.data) + assertEquals(Status.SUCCESS, resource?.status) } @Test @@ -156,17 +152,19 @@ class OCShareViewModelTest { 3 ) ).thenReturn( - publicShareResourcesAsLiveData + MutableLiveData>().apply { + value = Resource.success() + } ) // Viewmodel that will ask ocShareRepository for shares val ocShareViewModel = createOCShareViewModel(ocShareRepository) - val resource: Resource>? = ocShareViewModel.deletePublicShare( + val resource: Resource? = ocShareViewModel.deletePublicShare( 3 ).value - assertShareParameters(resource?.data) + assertEquals(Status.SUCCESS, resource?.status) } private fun createOCShareViewModel(ocShareRepository: OCShareRepository): OCShareViewModel =