From db8d4f7f8dcdb1774c8a9bc0fb72e35efdd801f7 Mon Sep 17 00:00:00 2001 From: davigonz Date: Thu, 9 May 2019 12:32:44 +0200 Subject: [PATCH 01/23] Include delete share operation in dao and datasources --- owncloud-android-library | 2 +- .../datasource/LocalSharesDataSource.kt | 2 ++ .../datasource/OCLocalSharesDataSource.kt | 2 ++ .../datasource/OCRemoteSharesDataSource.kt | 8 +++++++ .../datasource/RemoteSharesDataSource.kt | 9 +++++++ .../shares/repository/OCShareRepository.kt | 24 +++++++++++++++++++ .../shares/repository/ShareRepository.kt | 7 +++++- 7 files changed, 52 insertions(+), 2 deletions(-) diff --git a/owncloud-android-library b/owncloud-android-library index b20bf8c3115..2cbc32134d6 160000 --- a/owncloud-android-library +++ b/owncloud-android-library @@ -1 +1 @@ -Subproject commit b20bf8c31151189307864961d6f8c0bc57d842ba +Subproject commit 2cbc32134d637b23c2213766ec5ad3db210bd6bb diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/LocalSharesDataSource.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/LocalSharesDataSource.kt index 1b6b0537db3..923722cafd0 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/LocalSharesDataSource.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/LocalSharesDataSource.kt @@ -36,5 +36,7 @@ interface LocalSharesDataSource { fun replaceSharesForFile(ocShares: List): List + fun deleteShare(remoteId: Long) + fun deleteSharesForFile(filePath: String, accountName: String) } diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCLocalSharesDataSource.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCLocalSharesDataSource.kt index e92167209ed..8132d5efd9d 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCLocalSharesDataSource.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCLocalSharesDataSource.kt @@ -43,6 +43,8 @@ class OCLocalSharesDataSource( override fun replaceSharesForFile(ocShares: List): List = ocShareDao.replaceSharesForFile(ocShares) + override fun deleteShare(remoteId: Long) = ocShareDao.deleteShare(remoteId) + override fun deleteSharesForFile(filePath: String, accountName: String) = ocShareDao.deleteSharesForFile(filePath, accountName) } diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCRemoteSharesDataSource.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCRemoteSharesDataSource.kt index 661927c7a55..20a1c502e2a 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCRemoteSharesDataSource.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCRemoteSharesDataSource.kt @@ -23,6 +23,7 @@ import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation +import com.owncloud.android.lib.resources.shares.RemoveRemoteShareOperation import com.owncloud.android.lib.resources.shares.ShareParserResult import com.owncloud.android.lib.resources.shares.ShareType import com.owncloud.android.lib.resources.shares.UpdateRemoteShareOperation @@ -75,4 +76,11 @@ class OCRemoteSharesDataSource( updateRemoteShareOperation.retrieveShareDetails = true return updateRemoteShareOperation.execute(client) } + + override fun deleteShare( + remoteId: Long, + removeRemoteShareOperation: RemoveRemoteShareOperation + ): RemoteOperationResult { + return RemoveRemoteShareOperation(remoteId).execute(client) + } } diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/RemoteSharesDataSource.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/RemoteSharesDataSource.kt index a0a7f50896b..cb66f2c7840 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/RemoteSharesDataSource.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/RemoteSharesDataSource.kt @@ -22,6 +22,7 @@ package com.owncloud.android.shares.datasource import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation +import com.owncloud.android.lib.resources.shares.RemoveRemoteShareOperation import com.owncloud.android.lib.resources.shares.ShareParserResult import com.owncloud.android.lib.resources.shares.ShareType import com.owncloud.android.lib.resources.shares.UpdateRemoteShareOperation @@ -60,4 +61,12 @@ interface RemoteSharesDataSource { remoteId ) ): RemoteOperationResult + + fun deleteShare( + remoteId: Long, + removeRemoteShareOperation: RemoveRemoteShareOperation = + RemoveRemoteShareOperation( + remoteId + ) + ): RemoteOperationResult } 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 2bec13c4051..e30c1d496bd 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 @@ -155,4 +155,28 @@ class OCShareRepository( ) }.asLiveData() } + + override fun deletePublicShare( + filePath: String, + accountName: String, + remoteId: Long + ): LiveData>> { + return object : NetworkBoundResource, ShareParserResult>(appExecutors) { + override fun saveCallResult(item: ShareParserResult) { + // TODO + } + + 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.deleteShare(remoteId) + }.asLiveData() + } } 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 3fd66e09be8..d5ac55fb291 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,7 +20,6 @@ package com.owncloud.android.shares.repository import androidx.lifecycle.LiveData -import com.owncloud.android.datamodel.OCFile import com.owncloud.android.lib.resources.shares.ShareType import com.owncloud.android.shares.db.OCShare import com.owncloud.android.vo.Resource @@ -54,4 +53,10 @@ interface ShareRepository { permissions: Int, publicUpload: Boolean ): LiveData>> + + fun deletePublicShare( + filePath: String, + accountName: String, + remoteId: Long + ): LiveData>> } From 4a914c31fbe9b157de73548925a7d5d1b5f33027 Mon Sep 17 00:00:00 2001 From: davigonz Date: Thu, 9 May 2019 14:46:31 +0200 Subject: [PATCH 02/23] Convert RemoveShareDialogFragment into Kotlin and include some more changes regarding public share deletion --- .../shares/ui/fragment/ShareFileFragment.kt | 8 +- .../shares/viewmodel/OCShareViewModel.kt | 3 + .../ui/dialog/RemoveShareDialogFragment.java | 103 ------------------ .../ui/dialog/RemoveShareDialogFragment.kt | 103 ++++++++++++++++++ 4 files changed, 111 insertions(+), 106 deletions(-) delete mode 100644 owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.java create mode 100644 owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt 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 fe46ca3f398..4dae8e409c3 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 @@ -24,7 +24,6 @@ package com.owncloud.android.shares.ui.fragment import android.accounts.Account -import android.app.Activity import android.content.Context import android.os.Bundle import android.util.Log @@ -452,7 +451,7 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe capabilities = resource.data } else -> { - Log.d(TAG, "Unknown status when loading capabilities") + Log.d(TAG, "Unknown status when loading capabilities in account ${account?.name}") } } } @@ -490,7 +489,10 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe updateListOfPublicLinks() } else -> { - Log.d(TAG, "Unknown status when loading shares") + Log.d( + TAG, "Unknown status when loading shares for file ${file?.fileName} in account" + + "${account?.name}" + ) } } } 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 3ab5e3e8007..6bc381a3181 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 @@ -77,4 +77,7 @@ class OCShareViewModel( filePath, account.name, remoteId, name, password, expirationDateInMillis, permissions, publicUpload ) + fun deletePublicShare( + remoteId: Long + ): LiveData>> = shareRepository.deletePublicShare(filePath, account.name, remoteId) } diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.java b/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.java deleted file mode 100644 index a066d3ddb93..00000000000 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.java +++ /dev/null @@ -1,103 +0,0 @@ -/** - * ownCloud Android client application - * - * @author David A. Velasco - * @author David González Verdugo - * Copyright (C) 2019 ownCloud GmbH. - *

- * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2, - * as published by the Free Software Foundation. - *

- * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - *

- * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.owncloud.android.ui.dialog; - -/** - * Dialog requiring confirmation before removing a share. - * Triggers the removal according to the user response. - */ - -import android.app.Dialog; -import android.os.Bundle; - -import androidx.annotation.NonNull; -import com.owncloud.android.R; -import com.owncloud.android.lib.common.utils.Log_OC; -import com.owncloud.android.lib.resources.shares.RemoteShare; -import com.owncloud.android.shares.db.OCShare; -import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener; -import com.owncloud.android.ui.fragment.ShareFragmentListener; - -public class RemoveShareDialogFragment extends ConfirmationDialogFragment - implements ConfirmationDialogFragmentListener { - - private static final String TAG = RemoveShareDialogFragment.class.getName(); - - private RemoteShare mTargetShare; - - private static final String ARG_TARGET_SHARE = "TARGET_SHARE"; - - /** - * Public factory method to create new RemoveFilesDialogFragment instances. - * - * @param share {@link OCShare} to remove. - * @return Dialog ready to show. - */ - public static RemoveShareDialogFragment newInstance(OCShare share) { - RemoveShareDialogFragment frag = new RemoveShareDialogFragment(); - Bundle args = new Bundle(); - - args.putInt(ARG_MESSAGE_RESOURCE_ID, R.string.confirmation_remove_public_share_message); - args.putStringArray( - ARG_MESSAGE_ARGUMENTS, - new String[]{ - share.getName().length() > 0 ? share.getName() : share.getToken() - } - ); - args.putInt(ARG_TITLE_ID, R.string.confirmation_remove_public_share_title); - args.putInt(ARG_POSITIVE_BTN_RES, R.string.common_yes); - args.putInt(ARG_NEUTRAL_BTN_RES, R.string.common_no); - args.putInt(ARG_NEGATIVE_BTN_RES, -1); - frag.setArguments(args); - - return frag; - } - - @Override - @NonNull - public Dialog onCreateDialog(Bundle savedInstanceState) { - Dialog dialog = super.onCreateDialog(savedInstanceState); - mTargetShare = getArguments().getParcelable(ARG_TARGET_SHARE); - - setOnConfirmationListener(this); - - return dialog; - } - - /** - * Performs the removal of the target share, both locally and in the server. - */ - @Override - public void onConfirmation(String callerTag) { - ShareFragmentListener listener = (ShareFragmentListener) getActivity(); - Log_OC.d(TAG, "Removing public share " + mTargetShare.getName()); - } - - @Override - public void onCancel(String callerTag) { - // nothing to do here - } - - @Override - public void onNeutral(String callerTag) { - // nothing to do here - } -} \ No newline at end of file diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt b/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt new file mode 100644 index 00000000000..c4cee08ed3f --- /dev/null +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt @@ -0,0 +1,103 @@ +/** + * ownCloud Android client application + * + * @author David A. Velasco + * @author David González Verdugo + * Copyright (C) 2019 ownCloud GmbH. + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see //www.gnu.org/licenses/>. + */ + +package com.owncloud.android.ui.dialog + +/** + * Dialog requiring confirmation before removing a share. + * Triggers the removal according to the user response. + */ + +import android.app.Dialog +import android.os.Bundle +import com.owncloud.android.R +import com.owncloud.android.lib.common.utils.Log_OC +import com.owncloud.android.lib.resources.shares.RemoteShare +import com.owncloud.android.shares.db.OCShare +import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener +import com.owncloud.android.ui.fragment.ShareFragmentListener + +class RemoveShareDialogFragment : ConfirmationDialogFragment(), ConfirmationDialogFragmentListener { + + private var targetShare: RemoteShare? = null + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val dialog = super.onCreateDialog(savedInstanceState) + targetShare = arguments!!.getParcelable(ARG_TARGET_SHARE) + + setOnConfirmationListener(this) + + return dialog + } + + /** + * Performs the removal of the target share, both locally and in the server. + */ + override fun onConfirmation(callerTag: String) { + val listener = activity as ShareFragmentListener? + Log_OC.d(TAG, "Removing public share " + targetShare!!.name) + + } + + override fun onCancel(callerTag: String) { + // nothing to do here + } + + override fun onNeutral(callerTag: String) { + // nothing to do here + } + + companion object { + + private val TAG = RemoveShareDialogFragment::class.java.name + + private val ARG_TARGET_SHARE = "TARGET_SHARE" + + /** + * Public factory method to create new RemoveFilesDialogFragment instances. + * + * @param share [OCShare] to remove. + * @return Dialog ready to show. + */ + fun newInstance(share: OCShare): RemoveShareDialogFragment { + val frag = RemoveShareDialogFragment() + val args = Bundle() + + args.putInt( + ARG_MESSAGE_RESOURCE_ID, + R.string.confirmation_remove_public_share_message + ) + args.putStringArray( + ARG_MESSAGE_ARGUMENTS, + arrayOf(if (share.name!!.isNotEmpty()) share.name!! else share.token!!) + ) + args.putInt(ARG_TITLE_ID, R.string.confirmation_remove_public_share_title) + args.putInt(ARG_POSITIVE_BTN_RES, R.string.common_yes) + args.putInt(ARG_NEUTRAL_BTN_RES, R.string.common_no) + args.putInt(ARG_NEGATIVE_BTN_RES, -1) + frag.arguments = args + + return frag + } + } +} From 4b6c4dc85fb732bf249b2520d164a586de65e773 Mon Sep 17 00:00:00 2001 From: davigonz Date: Fri, 10 May 2019 15:12:12 +0200 Subject: [PATCH 03/23] Use listeners to properly show remove share fragment --- .../shares/ui/TestShareFileActivity.kt | 8 +- .../android/shares/ui/ShareActivity.kt | 22 +-- .../shares/ui/fragment/ShareFileFragment.kt | 9 +- .../adapter/SharePublicLinkListAdapter.java | 128 ------------------ .../ui/adapter/SharePublicLinkListAdapter.kt | 86 ++++++++++++ .../ui/dialog/RemoveShareDialogFragment.kt | 6 +- .../ui/fragment/ShareFragmentListener.java | 8 +- 7 files changed, 114 insertions(+), 153 deletions(-) delete mode 100644 owncloudApp/src/main/java/com/owncloud/android/ui/adapter/SharePublicLinkListAdapter.java create mode 100644 owncloudApp/src/main/java/com/owncloud/android/ui/adapter/SharePublicLinkListAdapter.kt 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 196ab4d99a3..e8be625da13 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 @@ -27,8 +27,7 @@ import com.owncloud.android.testing.SingleFragmentActivity import com.owncloud.android.ui.fragment.ShareFragmentListener class TestShareFileActivity : SingleFragmentActivity(), ShareFragmentListener { - - override fun copyOrSendPrivateLink(file: OCFile?) { + override fun copyOrSendPrivateLink(file: OCFile?) { } override fun showSearchUsersAndGroups() { @@ -44,6 +43,11 @@ class TestShareFileActivity : SingleFragmentActivity(), ShareFragmentListener { } override fun showAddPublicShare(defaultLinkName: String) { + + } + + override fun showRemovePublicShare(share: OCShare?) { + } override fun dismissAddPublicShare() { 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 9d414b028aa..2118306450f 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 @@ -45,6 +45,7 @@ import com.owncloud.android.shares.ui.fragment.PublicShareDialogFragment import com.owncloud.android.shares.ui.fragment.ShareFileFragment import com.owncloud.android.ui.activity.FileActivity import com.owncloud.android.ui.asynctasks.GetSharesForFileAsyncTask +import com.owncloud.android.ui.dialog.RemoveShareDialogFragment import com.owncloud.android.ui.fragment.EditShareFragment import com.owncloud.android.ui.fragment.SearchShareesFragment import com.owncloud.android.ui.fragment.ShareFragmentListener @@ -237,32 +238,35 @@ class ShareActivity : FileActivity(), ShareFragmentListener { // dialog, so make our own transaction and take care of that here. // Create and show the dialog - val newFragment = PublicShareDialogFragment.newInstanceToCreate( + val createPublicShareFragment = PublicShareDialogFragment.newInstanceToCreate( file, account, defaultLinkName ) showDialogFragment( - newFragment, + createPublicShareFragment, TAG_PUBLIC_SHARE_DIALOG_FRAGMENT ) } - override fun dismissAddPublicShare() { - publicShareFragment?.dismiss() - } - override fun showEditPublicShare(share: OCShare) { // Create and show the dialog. - val newFragment = PublicShareDialogFragment.newInstanceToUpdate(file, share, account) - + val editPublicShareFragment = PublicShareDialogFragment.newInstanceToUpdate(file, share, account) showDialogFragment( - newFragment, + editPublicShareFragment, TAG_PUBLIC_SHARE_DIALOG_FRAGMENT ) } + override fun showRemovePublicShare(share: OCShare) { + val removePublicShareFragment = RemoveShareDialogFragment.newInstance(share) + showDialogFragment( + removePublicShareFragment, + TAG_REMOVE_SHARE_DIALOG_FRAGMENT + ) + } + override fun copyOrSendPublicLink(share: OCShare) { fileOperationsHelper.copyOrSendPublicLink(share) } 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 4dae8e409c3..119fed908bc 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 @@ -330,11 +330,8 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe } override fun removePublicShare(share: OCShare) { - val dialog = RemoveShareDialogFragment.newInstance(share) - dialog.show( - fragmentManager!!, - ShareActivity.TAG_REMOVE_SHARE_DIALOG_FRAGMENT - ) + // Remove public link from server + listener?.showRemovePublicShare(share) } override fun editPublicShare(share: OCShare) { @@ -506,7 +503,7 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe */ private fun updateListOfPublicLinks() { publicLinksAdapter = SharePublicLinkListAdapter( - activity, + context!!, R.layout.share_public_link_item, publicLinks, this diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/adapter/SharePublicLinkListAdapter.java b/owncloudApp/src/main/java/com/owncloud/android/ui/adapter/SharePublicLinkListAdapter.java deleted file mode 100644 index ce04065cafa..00000000000 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/adapter/SharePublicLinkListAdapter.java +++ /dev/null @@ -1,128 +0,0 @@ -package com.owncloud.android.ui.adapter; - -/** - * ownCloud Android client application - * - * @author David González Verdugo - * @author Christian Schabesberger - * Copyright (C) 2019 ownCloud GmbH. - *

- * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2, - * as published by the Free Software Foundation. - *

- * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - *

- * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.ImageView; -import android.widget.TextView; - -import com.owncloud.android.R; -import com.owncloud.android.shares.db.OCShare; -import com.owncloud.android.utils.PreferenceUtils; - -import java.util.ArrayList; - -/** - * Adapter to show a list of public links - */ -public class SharePublicLinkListAdapter extends ArrayAdapter { - - private Context mContext; - private ArrayList mPublicLinks; - private SharePublicLinkListAdapter.SharePublicLinkAdapterListener mListener; - - public SharePublicLinkListAdapter(Context context, int resource, ArrayList shares, - SharePublicLinkListAdapter.SharePublicLinkAdapterListener listener) { - super(context, resource); - mContext = context; - mPublicLinks = shares; - mListener = listener; - } - - @Override - public int getCount() { - return mPublicLinks.size(); - } - - @Override - public Object getItem(int position) { - return mPublicLinks.get(position); - } - - @Override - public long getItemId(int position) { - return 0; - } - - @Override - public View getView(final int position, View convertView, ViewGroup parent) { - - LayoutInflater inflator = (LayoutInflater) mContext. - getSystemService(Context.LAYOUT_INFLATER_SERVICE); - View view = inflator.inflate(R.layout.share_public_link_item, parent, false); - - // Allow or disallow touches with other visible windows - view.setFilterTouchesWhenObscured( - PreferenceUtils.shouldDisallowTouchesWithOtherVisibleWindows(mContext) - ); - - if (mPublicLinks != null && mPublicLinks.size() > position) { - - OCShare share = mPublicLinks.get(position); - - TextView shareName = view.findViewById(R.id.publicLinkName); - - // If there's no name, set the token as name - shareName.setText(share.getName().equals("") ? share.getToken() : share.getName()); - - // bind listener to get link - final ImageView getPublicLinkButton = view.findViewById(R.id.getPublicLinkButton); - getPublicLinkButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - mListener.copyOrSendPublicLink(mPublicLinks.get(position)); - } - }); - - // bind listener to delete - final ImageView deletePublicLinkButton = view.findViewById(R.id.deletePublicLinkButton); - deletePublicLinkButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - mListener.removePublicShare(mPublicLinks.get(position)); - } - }); - - // bind listener to edit - final ImageView editPublicLinkButton = view.findViewById(R.id.editPublicLinkButton); - editPublicLinkButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - mListener.editPublicShare(mPublicLinks.get(position)); - } - }); - } - - return view; - } - - public interface SharePublicLinkAdapterListener { - void copyOrSendPublicLink(OCShare share); - - void removePublicShare(OCShare share); - - void editPublicShare(OCShare share); - } -} diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/adapter/SharePublicLinkListAdapter.kt b/owncloudApp/src/main/java/com/owncloud/android/ui/adapter/SharePublicLinkListAdapter.kt new file mode 100644 index 00000000000..f7355de8cac --- /dev/null +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/adapter/SharePublicLinkListAdapter.kt @@ -0,0 +1,86 @@ +package com.owncloud.android.ui.adapter + +/** + * ownCloud Android client application + * + * @author David González Verdugo + * @author Christian Schabesberger + * Copyright (C) 2019 ownCloud GmbH. + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see //www.gnu.org/licenses/>. + */ + +import android.content.Context +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ArrayAdapter +import com.owncloud.android.R +import com.owncloud.android.shares.db.OCShare +import com.owncloud.android.utils.PreferenceUtils +import kotlinx.android.synthetic.main.share_public_link_item.view.* +import java.util.ArrayList + +/** + * Adapter to show a list of public links + */ +class SharePublicLinkListAdapter( + private val mContext: Context, resource: Int, private val mPublicLinks: ArrayList?, + private val mListener: SharePublicLinkAdapterListener +) : ArrayAdapter(mContext, resource) { + + override fun getCount(): Int { + return mPublicLinks!!.size + } + + override fun getItem(position: Int): OCShare? = mPublicLinks!![position] + + override fun getItemId(position: Int): Long = 0 + + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + val inflator = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater + val view = inflator.inflate(R.layout.share_public_link_item, parent, false) + + // Allow or disallow touches with other visible windows + view.filterTouchesWhenObscured = PreferenceUtils.shouldDisallowTouchesWithOtherVisibleWindows(mContext) + + if (mPublicLinks != null && mPublicLinks.size > position) { + val share = mPublicLinks[position] + + // If there's no name, set the token as name + view.publicLinkName.text = if (share.name?.isEmpty()!!) share.token else share.name + + // bind listener to get link + view.getPublicLinkButton.setOnClickListener { mListener.copyOrSendPublicLink(mPublicLinks[position]) } + + // bind listener to delete + view.deletePublicLinkButton.setOnClickListener { mListener.removePublicShare(mPublicLinks[position]) } + + // bind listener to edit + view.editPublicLinkButton.setOnClickListener { mListener.editPublicShare(mPublicLinks[position]) } + } + + return view + } + + interface SharePublicLinkAdapterListener { + fun copyOrSendPublicLink(share: OCShare) + + fun removePublicShare(share: OCShare) + + fun editPublicShare(share: OCShare) + } +} diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt b/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt index c4cee08ed3f..37197b8bf91 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt @@ -32,14 +32,12 @@ import android.app.Dialog import android.os.Bundle import com.owncloud.android.R import com.owncloud.android.lib.common.utils.Log_OC -import com.owncloud.android.lib.resources.shares.RemoteShare import com.owncloud.android.shares.db.OCShare import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener import com.owncloud.android.ui.fragment.ShareFragmentListener class RemoveShareDialogFragment : ConfirmationDialogFragment(), ConfirmationDialogFragmentListener { - - private var targetShare: RemoteShare? = null + private var targetShare: OCShare? = null override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val dialog = super.onCreateDialog(savedInstanceState) @@ -56,7 +54,7 @@ class RemoveShareDialogFragment : ConfirmationDialogFragment(), ConfirmationDial override fun onConfirmation(callerTag: String) { val listener = activity as ShareFragmentListener? Log_OC.d(TAG, "Removing public share " + targetShare!!.name) - + listener?.removeShare(targetShare) } override fun onCancel(callerTag: String) { diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/fragment/ShareFragmentListener.java b/owncloudApp/src/main/java/com/owncloud/android/ui/fragment/ShareFragmentListener.java index e0be8ddf423..1db30df70c1 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/fragment/ShareFragmentListener.java +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/fragment/ShareFragmentListener.java @@ -43,13 +43,13 @@ public interface ShareFragmentListener { void refreshSharesFromServer(); - void removeShare(OCShare share); - void showAddPublicShare(String defaultLinkName); - void dismissAddPublicShare(); - void showEditPublicShare(OCShare share); + void showRemovePublicShare(OCShare share); + + void removeShare(OCShare share); + void copyOrSendPublicLink(OCShare share); } From 2b1badb72b79917394e0dad1bebe9a1f95a9df08 Mon Sep 17 00:00:00 2001 From: davigonz Date: Tue, 14 May 2019 12:16:53 +0200 Subject: [PATCH 04/23] Delete share from ShareActivity --- .../shares/repository/OCShareRepository.kt | 2 +- .../android/shares/ui/ShareActivity.kt | 83 ++++++++++++++++--- .../shares/ui/fragment/ShareFileFragment.kt | 7 +- .../ui/dialog/ConfirmationDialogFragment.java | 31 +++---- .../ui/dialog/RemoveShareDialogFragment.kt | 3 +- 5 files changed, 92 insertions(+), 34 deletions(-) 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 e30c1d496bd..6f46e803b2d 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 @@ -163,7 +163,7 @@ class OCShareRepository( ): LiveData>> { return object : NetworkBoundResource, ShareParserResult>(appExecutors) { override fun saveCallResult(item: ShareParserResult) { - // TODO + localSharesDataSource.deleteShare(remoteId) } override fun shouldFetch(data: List?): Boolean { 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 2118306450f..2ce4dc9cc53 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 @@ -27,8 +27,15 @@ package com.owncloud.android.shares.ui import android.app.SearchManager import android.content.Intent import android.os.Bundle +import android.util.Log import android.view.MenuItem +import androidx.lifecycle.Observer +import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.ViewModelProviders +import com.google.android.material.snackbar.Snackbar import com.owncloud.android.R +import com.owncloud.android.ViewModelFactory +import com.owncloud.android.capabilities.viewmodel.OCCapabilityViewModel import com.owncloud.android.datamodel.OCFile import com.owncloud.android.lib.common.operations.RemoteOperation import com.owncloud.android.lib.common.operations.RemoteOperationResult @@ -39,17 +46,21 @@ import com.owncloud.android.lib.resources.shares.ShareType import com.owncloud.android.operations.GetSharesForFileOperation import com.owncloud.android.operations.RemoveShareOperation import com.owncloud.android.operations.UpdateSharePermissionsOperation +import com.owncloud.android.operations.common.OperationType import com.owncloud.android.providers.UsersAndGroupsSearchProvider import com.owncloud.android.shares.db.OCShare import com.owncloud.android.shares.ui.fragment.PublicShareDialogFragment import com.owncloud.android.shares.ui.fragment.ShareFileFragment +import com.owncloud.android.shares.viewmodel.OCShareViewModel import com.owncloud.android.ui.activity.FileActivity import com.owncloud.android.ui.asynctasks.GetSharesForFileAsyncTask import com.owncloud.android.ui.dialog.RemoveShareDialogFragment +import com.owncloud.android.ui.errorhandling.ErrorMessageAdapter import com.owncloud.android.ui.fragment.EditShareFragment import com.owncloud.android.ui.fragment.SearchShareesFragment import com.owncloud.android.ui.fragment.ShareFragmentListener import com.owncloud.android.ui.utils.showDialogFragment +import com.owncloud.android.vo.Status /** * Activity for sharing files @@ -90,6 +101,23 @@ class ShareActivity : FileActivity(), ShareFragmentListener { private val editShareFragment: EditShareFragment? get() = supportFragmentManager.findFragmentByTag(TAG_EDIT_SHARE_FRAGMENT) as EditShareFragment? + var ocShareViewModelFactory: ViewModelProvider.Factory = ViewModelFactory.build { + OCShareViewModel( + account!!, + file?.remotePath!!, + listOf(ShareType.PUBLIC_LINK) + ) + } + + var ocCapabilityViewModelFactory: ViewModelProvider.Factory = ViewModelFactory.build { + OCCapabilityViewModel( + account = account!! + ) + } + + private lateinit var ocShareViewModel: OCShareViewModel + private lateinit var ocCapabilityViewModel: OCCapabilityViewModel + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -111,6 +139,12 @@ class ShareActivity : FileActivity(), ShareFragmentListener { ) ft.commit() } + + ocShareViewModel = ViewModelProviders.of(this, ocShareViewModelFactory) + .get(OCShareViewModel::class.java) + + ocCapabilityViewModel = + ViewModelProviders.of(this, ocCapabilityViewModelFactory).get(OCCapabilityViewModel::class.java) } override fun onAccountSet(stateWasRecovered: Boolean) { @@ -212,12 +246,6 @@ class ShareActivity : FileActivity(), ShareFragmentListener { // Create and show the dialog. val newFragment = EditShareFragment.newInstance(share, file, account) newFragment.show(ft, TAG_EDIT_SHARE_FRAGMENT) - - } - - override// Call to Remove share operation - fun removeShare(share: OCShare) { - fileOperationsHelper.removeShare(share) } /** @@ -267,6 +295,39 @@ class ShareActivity : FileActivity(), ShareFragmentListener { ) } + override fun removeShare(share: OCShare) { + ocShareViewModel.deletePublicShare(share.remoteId).observe( + this, + Observer { resource -> + when (resource?.status) { + Status.SUCCESS -> { + dismissLoadingDialog() + shareFileFragment?.refreshPublicSharesNew(resource.data as ArrayList) + } + Status.ERROR -> { + val errorMessage = ErrorMessageAdapter.getResultMessage( + resource.code, + resource.exception, + OperationType.GET_SHARES, + resources + ) + Snackbar.make(findViewById(android.R.id.content), errorMessage, Snackbar.LENGTH_SHORT).show() + dismissLoadingDialog() + } + Status.LOADING -> { + showLoadingDialog(R.string.common_loading) + } + else -> { + Log.d( + TAG, "Unknown status when removing share ${share.name} " + + "from account ${account?.name}" + ) + } + } + } + ) + } + override fun copyOrSendPublicLink(share: OCShare) { fileOperationsHelper.copyOrSendPublicLink(share) } @@ -337,10 +398,10 @@ class ShareActivity : FileActivity(), ShareFragmentListener { private val TAG = ShareActivity::class.java.simpleName - private val TAG_SHARE_FRAGMENT = "SHARE_FRAGMENT" - private val TAG_SEARCH_FRAGMENT = "SEARCH_USER_AND_GROUPS_FRAGMENT" - private val TAG_EDIT_SHARE_FRAGMENT = "EDIT_SHARE_FRAGMENT" - private val TAG_PUBLIC_SHARE_DIALOG_FRAGMENT = "PUBLIC_SHARE_DIALOG_FRAGMENT" - val TAG_REMOVE_SHARE_DIALOG_FRAGMENT = "REMOVE_SHARE_DIALOG_FRAGMENT" + private const val TAG_SHARE_FRAGMENT = "SHARE_FRAGMENT" + private const val TAG_SEARCH_FRAGMENT = "SEARCH_USER_AND_GROUPS_FRAGMENT" + private const val TAG_EDIT_SHARE_FRAGMENT = "EDIT_SHARE_FRAGMENT" + private const val TAG_PUBLIC_SHARE_DIALOG_FRAGMENT = "PUBLIC_SHARE_DIALOG_FRAGMENT" + private const val TAG_REMOVE_SHARE_DIALOG_FRAGMENT = "REMOVE_SHARE_DIALOG_FRAGMENT" } } 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 119fed908bc..18ed32349dc 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 @@ -50,12 +50,10 @@ import com.owncloud.android.lib.resources.status.CapabilityBooleanType import com.owncloud.android.lib.resources.status.OwnCloudVersion import com.owncloud.android.operations.common.OperationType import com.owncloud.android.shares.db.OCShare -import com.owncloud.android.shares.ui.ShareActivity import com.owncloud.android.shares.viewmodel.OCShareViewModel import com.owncloud.android.ui.activity.BaseActivity import com.owncloud.android.ui.adapter.SharePublicLinkListAdapter import com.owncloud.android.ui.adapter.ShareUserListAdapter -import com.owncloud.android.ui.dialog.RemoveShareDialogFragment import com.owncloud.android.ui.errorhandling.ErrorMessageAdapter import com.owncloud.android.ui.fragment.ShareFragmentListener import com.owncloud.android.utils.DisplayUtils @@ -497,6 +495,11 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe } } + fun refreshPublicSharesNew(publicShares: ArrayList) { + publicLinks = publicShares + updateListOfPublicLinks() + } + /** * Updates in the UI the section about public share with the information in the current * public share bound to file, if any diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/ConfirmationDialogFragment.java b/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/ConfirmationDialogFragment.java index ee26d5269ac..7074377e056 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/ConfirmationDialogFragment.java +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/ConfirmationDialogFragment.java @@ -110,36 +110,29 @@ public Dialog onCreateDialog(Bundle savedInstanceState) { if (posBtn != -1) { builder.setPositiveButton(posBtn, - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - if (mListener != null) { - mListener.onConfirmation(getTag()); - } - dialog.dismiss(); + (dialog, whichButton) -> { + if (mListener != null) { + mListener.onConfirmation(getTag()); } + dialog.dismiss(); }); } if (neuBtn != -1) { builder.setNeutralButton(neuBtn, - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - if (mListener != null) { - mListener.onNeutral(getTag()); - } - dialog.dismiss(); + (dialog, whichButton) -> { + if (mListener != null) { + mListener.onNeutral(getTag()); } + dialog.dismiss(); }); } if (negBtn != -1) { builder.setNegativeButton(negBtn, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - if (mListener != null) { - mListener.onCancel(getTag()); - } - dialog.dismiss(); + (dialog, which) -> { + if (mListener != null) { + mListener.onCancel(getTag()); } + dialog.dismiss(); }); } return builder.create(); diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt b/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt index 37197b8bf91..764bda83534 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt @@ -69,7 +69,7 @@ class RemoveShareDialogFragment : ConfirmationDialogFragment(), ConfirmationDial private val TAG = RemoveShareDialogFragment::class.java.name - private val ARG_TARGET_SHARE = "TARGET_SHARE" + private const val ARG_TARGET_SHARE = "TARGET_SHARE" /** * Public factory method to create new RemoveFilesDialogFragment instances. @@ -93,6 +93,7 @@ class RemoveShareDialogFragment : ConfirmationDialogFragment(), ConfirmationDial args.putInt(ARG_POSITIVE_BTN_RES, R.string.common_yes) args.putInt(ARG_NEUTRAL_BTN_RES, R.string.common_no) args.putInt(ARG_NEGATIVE_BTN_RES, -1) + args.putParcelable(ARG_TARGET_SHARE, share) frag.arguments = args return frag From 818ad27274c914af680f9a9b5f452619db6ce5ad Mon Sep 17 00:00:00 2001 From: davigonz Date: Tue, 14 May 2019 17:50:56 +0200 Subject: [PATCH 05/23] Clean up some dependencies when getting public shares and capabilities --- .../owncloud/android/NetworkBoundResource.kt | 4 +- .../operations/common/OperationType.kt | 3 +- .../android/shares/ui/ShareActivity.kt | 120 ++++++++++++--- .../ui/fragment/PublicShareDialogFragment.kt | 2 +- .../shares/ui/fragment/ShareFileFragment.kt | 141 +++--------------- .../ui/dialog/RemoveShareDialogFragment.kt | 1 + .../ui/errorhandling/ErrorMessageAdapter.kt | 1 + .../ui/fragment/ShareFragmentListener.java | 2 + owncloudApp/src/main/res/values/strings.xml | 2 +- 9 files changed, 125 insertions(+), 151 deletions(-) diff --git a/owncloudApp/src/main/java/com/owncloud/android/NetworkBoundResource.kt b/owncloudApp/src/main/java/com/owncloud/android/NetworkBoundResource.kt index f0634d0341e..dd3b635baf1 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/NetworkBoundResource.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/NetworkBoundResource.kt @@ -78,7 +78,9 @@ abstract class NetworkBoundResource( private fun performNetworkOperation(dbSource: LiveData) { // Let's dispatch dbSource value quickly while network operation is performed result.addSource(dbSource) { newData -> - setValue(Resource.loading(newData)) + if (newData != null) { + setValue(Resource.loading(newData)) + } } try { diff --git a/owncloudApp/src/main/java/com/owncloud/android/operations/common/OperationType.kt b/owncloudApp/src/main/java/com/owncloud/android/operations/common/OperationType.kt index f3d501160a9..113dbc1cd47 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/operations/common/OperationType.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/operations/common/OperationType.kt @@ -14,5 +14,6 @@ enum class OperationType { CREATE_FOLDER, MOVE_FILE, COPY_FILE, - SYNCHRONIZE_FOLDER + SYNCHRONIZE_FOLDER, + GET_CAPABILITIES } 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 2ce4dc9cc53..bfa9df7330d 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 @@ -179,6 +179,101 @@ class ShareActivity : FileActivity(), ShareFragmentListener { getSharesForFileAsyncTask = null } + /** + * Updates the view, reading data from [com.owncloud.android.shares.viewmodel.OCShareViewModel] + */ + private fun refreshSharesFromStorageManager() { + val shareFileFragment = shareFileFragment + if (shareFileFragment?.isAdded == true) { // only if added to the view hierarchy!! + shareFileFragment.refreshUsersOrGroupsListFromDB() + } + + val searchShareesFragment = searchFragment + if (searchShareesFragment?.isAdded == true) { // only if added to the view hierarchy!! + searchShareesFragment.refreshUsersOrGroupsListFromDB() + } + + val editShareFragment = editShareFragment + if (editShareFragment?.isAdded == true) { + editShareFragment.refreshUiFromDB() + } + } + + override fun refreshShares() { + loadCapabilities() + loadShares() + } + + private fun loadCapabilities() { + ocCapabilityViewModel.getCapabilityForAccount().observe( + this, + Observer { resource -> + when (resource?.status) { + Status.SUCCESS -> { + shareFileFragment?.updateCapabilities(resource.data) + dismissLoadingDialog() + } + Status.ERROR -> { + val errorMessage = ErrorMessageAdapter.getResultMessage( + resource.code, + resource.exception, + OperationType.GET_CAPABILITIES, + resources + ) + Snackbar.make(findViewById(android.R.id.content), errorMessage, Snackbar.LENGTH_SHORT).show() + shareFileFragment?.updateCapabilities(resource.data) + dismissLoadingDialog() + } + Status.LOADING -> { + showLoadingDialog(R.string.common_loading) + shareFileFragment?.updateCapabilities(resource.data) + } + else -> { + Log.d(TAG, "Unknown status when loading capabilities in account ${account?.name}") + } + } + } + ) + } + + private fun loadShares() { + ocShareViewModel.getSharesForFile().observe( + this, + Observer { resource -> + when (resource?.status) { + Status.SUCCESS -> { + shareFileFragment?.updatePublicShares(resource.data as ArrayList) + dismissLoadingDialog() + } + Status.ERROR -> { + val errorMessage = ErrorMessageAdapter.getResultMessage( + resource.code, + resource.exception, + OperationType.GET_SHARES, + resources + ) + Snackbar.make( + findViewById(android.R.id.content), + errorMessage, Snackbar.LENGTH_SHORT + ).show() + shareFileFragment?.updatePublicShares(resource.data as ArrayList) + dismissLoadingDialog() + } + Status.LOADING -> { + showLoadingDialog(R.string.common_loading) + shareFileFragment?.updatePublicShares(resource.data as ArrayList) + } + else -> { + Log.d( + TAG, "Unknown status when loading shares for file ${file?.fileName} in account" + + "${account?.name}" + ) + } + } + } + ) + } + override fun copyOrSendPrivateLink(file: OCFile) { fileOperationsHelper.copyOrSendPrivateLink(file) } @@ -301,14 +396,14 @@ class ShareActivity : FileActivity(), ShareFragmentListener { Observer { resource -> when (resource?.status) { Status.SUCCESS -> { + shareFileFragment?.updatePublicShares(resource.data as ArrayList) dismissLoadingDialog() - shareFileFragment?.refreshPublicSharesNew(resource.data as ArrayList) } Status.ERROR -> { val errorMessage = ErrorMessageAdapter.getResultMessage( resource.code, resource.exception, - OperationType.GET_SHARES, + OperationType.REMOVE_SHARE, resources ) Snackbar.make(findViewById(android.R.id.content), errorMessage, Snackbar.LENGTH_SHORT).show() @@ -346,7 +441,6 @@ class ShareActivity : FileActivity(), ShareFragmentListener { RemoteOperationResult.ResultCode.SHARE_NOT_FOUND ) { Log_OC.d(TAG, "Refreshing view on successful operation or finished refresh") - refreshSharesFromStorageManager() if (operation is GetSharesForFileOperation) { getSharesForFileAsyncTask = null } @@ -363,26 +457,6 @@ class ShareActivity : FileActivity(), ShareFragmentListener { } } - /** - * Updates the view, reading data from [com.owncloud.android.datamodel.FileDataStorageManager] - */ - private fun refreshSharesFromStorageManager() { - val shareFileFragment = shareFileFragment - if (shareFileFragment?.isAdded == true) { // only if added to the view hierarchy!! - shareFileFragment.refreshUsersOrGroupsListFromDB() - } - - val searchShareesFragment = searchFragment - if (searchShareesFragment?.isAdded == true) { // only if added to the view hierarchy!! - searchShareesFragment.refreshUsersOrGroupsListFromDB() - } - - val editShareFragment = editShareFragment - if (editShareFragment?.isAdded == true) { - editShareFragment.refreshUiFromDB() - } - } - override fun onOptionsItemSelected(item: MenuItem): Boolean { var retval = true when (item.itemId) { diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt index 635e5af6ef7..190838d350e 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt @@ -345,7 +345,7 @@ class PublicShareDialogFragment : DialogFragment() { val errorMessage: String = resource.msg ?: ErrorMessageAdapter.getResultMessage( resource.code, resource.exception, - OperationType.CREATE_PUBLIC_SHARE, + OperationType.UPDATE_PUBLIC_SHARE, resources ); showError(errorMessage) 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 18ed32349dc..c93758f7c8d 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 @@ -26,39 +26,28 @@ package com.owncloud.android.shares.ui.fragment import android.accounts.Account import android.content.Context import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ListView import android.widget.Toast import androidx.fragment.app.Fragment -import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProvider -import androidx.lifecycle.ViewModelProviders import com.google.android.material.snackbar.Snackbar import com.owncloud.android.R -import com.owncloud.android.ViewModelFactory import com.owncloud.android.authentication.AccountUtils import com.owncloud.android.capabilities.db.OCCapability -import com.owncloud.android.capabilities.viewmodel.OCCapabilityViewModel import com.owncloud.android.datamodel.OCFile import com.owncloud.android.datamodel.ThumbnailsCacheManager import com.owncloud.android.lib.common.utils.Log_OC -import com.owncloud.android.lib.resources.shares.ShareType import com.owncloud.android.lib.resources.status.CapabilityBooleanType import com.owncloud.android.lib.resources.status.OwnCloudVersion -import com.owncloud.android.operations.common.OperationType import com.owncloud.android.shares.db.OCShare -import com.owncloud.android.shares.viewmodel.OCShareViewModel import com.owncloud.android.ui.activity.BaseActivity import com.owncloud.android.ui.adapter.SharePublicLinkListAdapter import com.owncloud.android.ui.adapter.ShareUserListAdapter -import com.owncloud.android.ui.errorhandling.ErrorMessageAdapter import com.owncloud.android.ui.fragment.ShareFragmentListener import com.owncloud.android.utils.DisplayUtils import com.owncloud.android.utils.MimetypeIconUtil -import com.owncloud.android.vo.Status import kotlinx.android.synthetic.main.share_file_layout.* import kotlinx.android.synthetic.main.share_file_layout.view.* import java.util.Collections @@ -193,23 +182,6 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe private val isPublicShareDisabled: Boolean get() = capabilities != null && capabilities?.filesSharingPublicEnabled == CapabilityBooleanType.FALSE.value - var ocShareViewModelFactory: ViewModelProvider.Factory = ViewModelFactory.build { - OCShareViewModel( - account!!, - file?.remotePath!!, - listOf(ShareType.PUBLIC_LINK) - ) - } - - var ocCapabilityViewModelFactory: ViewModelProvider.Factory = ViewModelFactory.build { - OCCapabilityViewModel( - account = account!! - ) - } - - private lateinit var ocShareViewModel: OCShareViewModel - private lateinit var ocCapabilityViewModel: OCCapabilityViewModel - /** * {@inheritDoc} */ @@ -284,12 +256,6 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - ocShareViewModel = ViewModelProviders.of(this, ocShareViewModelFactory) - .get(OCShareViewModel::class.java) - - ocCapabilityViewModel = - ViewModelProviders.of(this, ocCapabilityViewModelFactory).get(OCCapabilityViewModel::class.java) - getPrivateLinkButton?.setOnClickListener { listener?.copyOrSendPrivateLink(file) } getPrivateLinkButton?.setOnLongClickListener { @@ -342,8 +308,7 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe activity!!.setTitle(R.string.share_dialog_title) - // Load data into the list of public shares - refreshPublicShares() + listener?.refreshShares() // Load data into the list of private shares refreshUsersOrGroupsListFromDB() @@ -419,83 +384,28 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe listener?.showEditPrivateShare(share) } - private fun refreshPublicShares() { - // Get capabilities first - ocCapabilityViewModel.getCapabilityForAccount().observe( - this, - Observer { resource -> - when (resource?.status) { - Status.SUCCESS -> { - capabilities = resource.data - loadPublicShares() - (activity as BaseActivity).dismissLoadingDialog() - } - Status.ERROR -> { - val errorMessage = ErrorMessageAdapter.getResultMessage( - resource.code, - resource.exception, - OperationType.GET_SHARES, - resources - ) - view?.let { Snackbar.make(it, errorMessage, Snackbar.LENGTH_SHORT).show() } - capabilities = resource.data - (activity as BaseActivity).dismissLoadingDialog() - } - Status.LOADING -> { - (activity as BaseActivity).showLoadingDialog(R.string.common_loading) - capabilities = resource.data - } - else -> { - Log.d(TAG, "Unknown status when loading capabilities in account ${account?.name}") - } - } - } - ) - } + fun updateCapabilities(capabilities: OCCapability?) { + this.capabilities = capabilities - private fun loadPublicShares() { + // Update view depending on updated capabilities if (isPublicShareDisabled) { - hidePublicShare() + shareViaLinkSection.visibility = View.GONE } else { - ocShareViewModel.getSharesForFile().observe( - this, - Observer { resource -> - when (resource?.status) { - Status.SUCCESS -> { - publicLinks = resource.data as ArrayList - updateListOfPublicLinks() - (activity as BaseActivity).dismissLoadingDialog() - } - Status.ERROR -> { - val errorMessage = ErrorMessageAdapter.getResultMessage( - resource.code, - resource.exception, - OperationType.GET_SHARES, - resources - ) - view?.let { Snackbar.make(it, errorMessage, Snackbar.LENGTH_SHORT).show() } - publicLinks = resource.data as ArrayList - updateListOfPublicLinks() - (activity as BaseActivity).dismissLoadingDialog() - } - Status.LOADING -> { - (activity as BaseActivity).showLoadingDialog(R.string.common_loading) - publicLinks = resource.data as ArrayList - updateListOfPublicLinks() - } - else -> { - Log.d( - TAG, "Unknown status when loading shares for file ${file?.fileName} in account" + - "${account?.name}" - ) - } - } - } - ) + 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?.size!! >= 1) { + addPublicLinkButton.visibility = View.INVISIBLE + return + } + addPublicLinkButton.visibility = View.VISIBLE } } - fun refreshPublicSharesNew(publicShares: ArrayList) { + fun updatePublicShares(publicShares: ArrayList) { publicLinks = publicShares updateListOfPublicLinks() } @@ -523,27 +433,10 @@ 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) } - /** - * Hides all the UI elements related to public share - */ - private fun hidePublicShare() { - shareViaLinkSection.visibility = View.GONE - } - /** * Hide share features sections that are not enabled * diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt b/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt index 764bda83534..2920d020dad 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt @@ -33,6 +33,7 @@ import android.os.Bundle import com.owncloud.android.R import com.owncloud.android.lib.common.utils.Log_OC import com.owncloud.android.shares.db.OCShare +import com.owncloud.android.shares.ui.ShareActivity import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener import com.owncloud.android.ui.fragment.ShareFragmentListener diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/errorhandling/ErrorMessageAdapter.kt b/owncloudApp/src/main/java/com/owncloud/android/ui/errorhandling/ErrorMessageAdapter.kt index 37771ff2a09..9bcb2af0f46 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/errorhandling/ErrorMessageAdapter.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/errorhandling/ErrorMessageAdapter.kt @@ -468,6 +468,7 @@ class ErrorMessageAdapter { f.format(R.string.update_link_file_error) OperationType.MOVE_FILE -> f.format(R.string.move_file_error) OperationType.COPY_FILE -> f.format(R.string.copy_file_error) + OperationType.GET_CAPABILITIES -> f.format(R.string.get_capabilities_error) else -> { if (resultCode == RemoteOperationResult.ResultCode.OK) { f.format(android.R.string.ok) diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/fragment/ShareFragmentListener.java b/owncloudApp/src/main/java/com/owncloud/android/ui/fragment/ShareFragmentListener.java index 1db30df70c1..40ed049df22 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/fragment/ShareFragmentListener.java +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/fragment/ShareFragmentListener.java @@ -35,6 +35,8 @@ * >Communicating with Other Fragments for more information. */ public interface ShareFragmentListener { + void refreshShares(); + void copyOrSendPrivateLink(OCFile file); void showSearchUsersAndGroups(); diff --git a/owncloudApp/src/main/res/values/strings.xml b/owncloudApp/src/main/res/values/strings.xml index 0335712c02f..64c4986a053 100644 --- a/owncloudApp/src/main/res/values/strings.xml +++ b/owncloudApp/src/main/res/values/strings.xml @@ -281,7 +281,6 @@ Cannot authenticate to this server Account does not exist in the device yet - Set as available offline Unset as available offline Rename @@ -370,6 +369,7 @@ %1$s could not be copied to %2$s local folder Picture upload path + An error occurred while trying to retrieve capabilities Sorry, sharing is not enabled on your server. Please contact your administrator. Unable to share. Please check whether the file exists From 9911cec10f76765e4d6edb71ede10a5e4ebdf28e Mon Sep 17 00:00:00 2001 From: davigonz Date: Wed, 15 May 2019 11:14:18 +0200 Subject: [PATCH 06/23] Clean up dependencies when creating/editing shares --- .../shares/ui/TestShareFileActivity.kt | 2 +- .../repository/CapabilityRepository.kt | 3 +- .../repository/OCCapabilityRepository.kt | 14 +- .../viewmodel/OCCapabilityViewModel.kt | 9 +- .../android/shares/ui/ShareActivity.kt | 125 +++++++++++++++-- .../ui/fragment/PublicShareDialogFragment.kt | 130 +----------------- .../shares/ui/fragment/ShareFileFragment.kt | 5 +- .../ui/fragment/ShareFragmentListener.kt | 78 +++++++++++ .../ui/dialog/RemoveShareDialogFragment.kt | 5 +- .../ui/fragment/SearchShareesFragment.java | 4 +- .../ui/fragment/ShareFragmentListener.java | 57 -------- 11 files changed, 219 insertions(+), 213 deletions(-) create mode 100644 owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/ShareFragmentListener.kt delete mode 100644 owncloudApp/src/main/java/com/owncloud/android/ui/fragment/ShareFragmentListener.java 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 e8be625da13..0c55e5fbd42 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 @@ -24,7 +24,7 @@ import com.owncloud.android.datamodel.FileDataStorageManager import com.owncloud.android.datamodel.OCFile import com.owncloud.android.shares.db.OCShare import com.owncloud.android.testing.SingleFragmentActivity -import com.owncloud.android.ui.fragment.ShareFragmentListener +import com.owncloud.android.shares.ui.fragment.ShareFragmentListener class TestShareFileActivity : SingleFragmentActivity(), ShareFragmentListener { override fun copyOrSendPrivateLink(file: OCFile?) { diff --git a/owncloudApp/src/main/java/com/owncloud/android/capabilities/repository/CapabilityRepository.kt b/owncloudApp/src/main/java/com/owncloud/android/capabilities/repository/CapabilityRepository.kt index f1fe5b63cd7..79be15b2fc1 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/capabilities/repository/CapabilityRepository.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/capabilities/repository/CapabilityRepository.kt @@ -6,6 +6,7 @@ import com.owncloud.android.vo.Resource interface CapabilityRepository { fun loadCapabilityForAccount( - accountName: String + accountName: String, + shouldFetchFromNetwork: Boolean = true ): 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 a30e7129970..148fd7417d5 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 @@ -31,25 +31,25 @@ import com.owncloud.android.vo.Resource class OCCapabilityRepository( private val appExecutors: AppExecutors, private val localCapabilitiesDataSource: LocalCapabilitiesDataSource, - private val remoteCapabilitiesDataSource: RemoteCapabilitiesDataSource, - private val shouldFetchFromNetwork: Boolean + private val remoteCapabilitiesDataSource: RemoteCapabilitiesDataSource ) : CapabilityRepository { companion object Factory { fun create( appExecutors: AppExecutors = AppExecutors(), localCapabilitiesDataSource: LocalCapabilitiesDataSource, - remoteCapabilitiesDataSource: RemoteCapabilitiesDataSource, - shouldFetchFromNetwork: Boolean = true + remoteCapabilitiesDataSource: RemoteCapabilitiesDataSource ): OCCapabilityRepository = OCCapabilityRepository( appExecutors, localCapabilitiesDataSource, - remoteCapabilitiesDataSource, - shouldFetchFromNetwork + remoteCapabilitiesDataSource ) } - override fun loadCapabilityForAccount(accountName: String): LiveData> = + override fun loadCapabilityForAccount( + accountName: String, + shouldFetchFromNetwork: Boolean + ): LiveData> = object : NetworkBoundResource(appExecutors) { override fun saveCallResult(item: RemoteCapability) { item.accountName = accountName diff --git a/owncloudApp/src/main/java/com/owncloud/android/capabilities/viewmodel/OCCapabilityViewModel.kt b/owncloudApp/src/main/java/com/owncloud/android/capabilities/viewmodel/OCCapabilityViewModel.kt index 5e68eb8ac5e..5bd46ced8d7 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/capabilities/viewmodel/OCCapabilityViewModel.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/capabilities/viewmodel/OCCapabilityViewModel.kt @@ -39,7 +39,7 @@ import com.owncloud.android.vo.Resource @OpenForTesting class OCCapabilityViewModel( - val shouldFetchFromNetwork: Boolean = true, + var shouldFetchFromNetwork: Boolean = true, val account: Account, val capabilityRepository: CapabilityRepository = OCCapabilityRepository.create( localCapabilitiesDataSource = OCLocalCapabilitiesDataSource(), @@ -48,11 +48,10 @@ class OCCapabilityViewModel( OwnCloudAccount(account, MainApp.getAppContext()), MainApp.getAppContext() ) - ), - shouldFetchFromNetwork = shouldFetchFromNetwork + ) ) ) : ViewModel() { - fun getCapabilityForAccount(): LiveData> = - capabilityRepository.loadCapabilityForAccount(account.name) + fun getCapabilityForAccount(shouldFetchFromNetwork: Boolean): LiveData> = + capabilityRepository.loadCapabilityForAccount(account.name, shouldFetchFromNetwork) } 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 bfa9df7330d..7180d0042d1 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 @@ -51,6 +51,7 @@ import com.owncloud.android.providers.UsersAndGroupsSearchProvider import com.owncloud.android.shares.db.OCShare import com.owncloud.android.shares.ui.fragment.PublicShareDialogFragment import com.owncloud.android.shares.ui.fragment.ShareFileFragment +import com.owncloud.android.shares.ui.fragment.ShareFragmentListener import com.owncloud.android.shares.viewmodel.OCShareViewModel import com.owncloud.android.ui.activity.FileActivity import com.owncloud.android.ui.asynctasks.GetSharesForFileAsyncTask @@ -58,7 +59,6 @@ import com.owncloud.android.ui.dialog.RemoveShareDialogFragment import com.owncloud.android.ui.errorhandling.ErrorMessageAdapter import com.owncloud.android.ui.fragment.EditShareFragment import com.owncloud.android.ui.fragment.SearchShareesFragment -import com.owncloud.android.ui.fragment.ShareFragmentListener import com.owncloud.android.ui.utils.showDialogFragment import com.owncloud.android.vo.Status @@ -200,17 +200,22 @@ class ShareActivity : FileActivity(), ShareFragmentListener { } override fun refreshShares() { - loadCapabilities() + refreshCapabilities() loadShares() } - private fun loadCapabilities() { - ocCapabilityViewModel.getCapabilityForAccount().observe( + override fun refreshCapabilities(shouldFetchFromNetwork: Boolean) { + ocCapabilityViewModel.getCapabilityForAccount(shouldFetchFromNetwork).observe( this, Observer { resource -> when (resource?.status) { Status.SUCCESS -> { - shareFileFragment?.updateCapabilities(resource.data) + if (publicShareFragment != null) { + publicShareFragment?.updateCapabilities(resource.data) + } else { + shareFileFragment?.updateCapabilities(resource.data) + } + dismissLoadingDialog() } Status.ERROR -> { @@ -220,13 +225,22 @@ class ShareActivity : FileActivity(), ShareFragmentListener { OperationType.GET_CAPABILITIES, resources ) - Snackbar.make(findViewById(android.R.id.content), errorMessage, Snackbar.LENGTH_SHORT).show() - shareFileFragment?.updateCapabilities(resource.data) + if (publicShareFragment != null) { + publicShareFragment?.showError(errorMessage) + } else { + Snackbar.make(findViewById(android.R.id.content), errorMessage, Snackbar.LENGTH_SHORT) + .show() + shareFileFragment?.updateCapabilities(resource.data) + } dismissLoadingDialog() } Status.LOADING -> { showLoadingDialog(R.string.common_loading) - shareFileFragment?.updateCapabilities(resource.data) + if (publicShareFragment != null) { + publicShareFragment?.updateCapabilities(resource.data) + } else { + shareFileFragment?.updateCapabilities(resource.data) + } } else -> { Log.d(TAG, "Unknown status when loading capabilities in account ${account?.name}") @@ -274,6 +288,10 @@ class ShareActivity : FileActivity(), ShareFragmentListener { ) } + /** + * PRIVATE SHARES + */ + override fun copyOrSendPrivateLink(file: OCFile) { fileOperationsHelper.copyOrSendPrivateLink(file) } @@ -291,7 +309,6 @@ class ShareActivity : FileActivity(), ShareFragmentListener { } private fun getAppropiatePermissions(shareType: ShareType): Int { - // check if the Share is FERERATED val isFederated = ShareType.FEDERATED == shareType @@ -355,6 +372,9 @@ class ShareActivity : FileActivity(), ShareFragmentListener { getSharesForFileAsyncTask!!.execute(*params) } + /** + * PUBLIC SHARES + */ override fun showAddPublicShare(defaultLinkName: String) { // DialogFragment.show() will take care of adding the fragment // in a transaction. We also want to remove any currently showing @@ -373,6 +393,47 @@ class ShareActivity : FileActivity(), ShareFragmentListener { ) } + override fun createPublicShare( + permissions: Int, + name: String, + password: String, + expirationTimeInMillis: Long, + publicUpload: Boolean + ) { + ocShareViewModel.insertPublicShareForFile( + permissions, + name, + password, + expirationTimeInMillis, + publicUpload + ).observe( + this, + Observer { resource -> + when (resource?.status) { + Status.SUCCESS -> { + dismissLoadingDialog() + } + Status.ERROR -> { + val errorMessage: String = resource.msg ?: ErrorMessageAdapter.getResultMessage( + resource.code, + resource.exception, + OperationType.CREATE_PUBLIC_SHARE, + resources + ); + publicShareFragment?.showError(errorMessage) + dismissLoadingDialog() + } + Status.LOADING -> { + showLoadingDialog(R.string.common_loading) + } + else -> { + Log.d(TAG, "Unknown status when creating public share") + } + } + } + ) + } + override fun showEditPublicShare(share: OCShare) { // Create and show the dialog. val editPublicShareFragment = PublicShareDialogFragment.newInstanceToUpdate(file, share, account) @@ -382,6 +443,49 @@ class ShareActivity : FileActivity(), ShareFragmentListener { ) } + override fun updatePublicShare( + remoteId: Long, + name: String, + password: String?, + expirationDateInMillis: Long, + permissions: Int, + publicUpload: Boolean + ) { + ocShareViewModel.updatePublicShareForFile( + remoteId, + name, + password, + expirationDateInMillis, + permissions, + publicUpload + ).observe( + this, + Observer { resource -> + when (resource?.status) { + Status.SUCCESS -> { + dismissLoadingDialog() + } + Status.ERROR -> { + val errorMessage: String = resource.msg ?: ErrorMessageAdapter.getResultMessage( + resource.code, + resource.exception, + OperationType.UPDATE_PUBLIC_SHARE, + resources + ); + publicShareFragment?.showError(errorMessage) + dismissLoadingDialog() + } + Status.LOADING -> { + showLoadingDialog(R.string.common_loading) + } + else -> { + Log.d(TAG, "Unknown status when updating public share") + } + } + } + ) + } + override fun showRemovePublicShare(share: OCShare) { val removePublicShareFragment = RemoveShareDialogFragment.newInstance(share) showDialogFragment( @@ -390,7 +494,7 @@ class ShareActivity : FileActivity(), ShareFragmentListener { ) } - override fun removeShare(share: OCShare) { + override fun removePublicShare(share: OCShare) { ocShareViewModel.deletePublicShare(share.remoteId).observe( this, Observer { resource -> @@ -469,7 +573,6 @@ class ShareActivity : FileActivity(), ShareFragmentListener { } companion object { - private val TAG = ShareActivity::class.java.simpleName private const val TAG_SHARE_FRAGMENT = "SHARE_FRAGMENT" diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt index 190838d350e..e132ce02f9c 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt @@ -27,7 +27,6 @@ import android.content.Context import android.graphics.drawable.Drawable import android.os.Bundle import android.text.InputType -import android.util.Log import android.view.LayoutInflater import android.view.MotionEvent import android.view.View @@ -38,28 +37,16 @@ import android.widget.CompoundButton import android.widget.TextView import androidx.appcompat.widget.SwitchCompat import androidx.fragment.app.DialogFragment -import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProvider -import androidx.lifecycle.ViewModelProviders import com.owncloud.android.R -import com.owncloud.android.ViewModelFactory 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.utils.Log_OC import com.owncloud.android.lib.resources.shares.RemoteShare -import com.owncloud.android.lib.resources.shares.ShareType import com.owncloud.android.lib.resources.status.CapabilityBooleanType import com.owncloud.android.lib.resources.status.OwnCloudVersion -import com.owncloud.android.operations.common.OperationType import com.owncloud.android.shares.db.OCShare -import com.owncloud.android.shares.viewmodel.OCShareViewModel -import com.owncloud.android.ui.activity.BaseActivity import com.owncloud.android.ui.dialog.ExpirationDatePickerDialogFragment -import com.owncloud.android.ui.errorhandling.ErrorMessageAdapter -import com.owncloud.android.ui.fragment.ShareFragmentListener import com.owncloud.android.utils.DateUtils -import com.owncloud.android.vo.Status import kotlinx.android.synthetic.main.share_public_dialog.* import kotlinx.android.synthetic.main.share_public_dialog.view.* import java.text.ParseException @@ -140,24 +127,6 @@ class PublicShareDialogFragment : DialogFragment() { .time } else -1 - var ocCapabilityViewModelFactory: ViewModelProvider.Factory = ViewModelFactory.build { - OCCapabilityViewModel( - account = account!!, - shouldFetchFromNetwork = false - ) - } - - var ocShareViewModelFactory: ViewModelProvider.Factory = ViewModelFactory.build { - OCShareViewModel( - account!!, - file?.remotePath!!, - listOf(ShareType.PUBLIC_LINK) - ) - } - - private lateinit var ocCapabilityViewModel: OCCapabilityViewModel - private lateinit var ocShareViewModel: OCShareViewModel - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -231,11 +200,6 @@ class PublicShareDialogFragment : DialogFragment() { } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - ocShareViewModel = ViewModelProviders.of(this, ocShareViewModelFactory).get(OCShareViewModel::class.java) - - ocCapabilityViewModel = - ViewModelProviders.of(this, ocCapabilityViewModelFactory).get(OCCapabilityViewModel::class.java) - initPasswordListener() initExpirationListener() initPasswordFocusChangeListener() @@ -285,38 +249,13 @@ class PublicShareDialogFragment : DialogFragment() { } if (!updating()) { // Creating a new public share - ocShareViewModel.insertPublicShareForFile( + dismiss() + listener?.createPublicShare( publicLinkPermissions, publicLinkName, publicLinkPassword!!, publicLinkExpirationDateInMillis, false - ).observe( - this, - Observer { resource -> - when (resource?.status) { - Status.SUCCESS -> { - dismiss() - (activity as BaseActivity).dismissLoadingDialog() - } - Status.ERROR -> { - val errorMessage: String = resource.msg ?: ErrorMessageAdapter.getResultMessage( - resource.code, - resource.exception, - OperationType.CREATE_PUBLIC_SHARE, - resources - ); - showError(errorMessage) - (activity as BaseActivity).dismissLoadingDialog() - } - Status.LOADING -> { - (activity as BaseActivity).showLoadingDialog(R.string.common_loading) - } - else -> { - Log.d(TAG, "Unknown status when creating share") - } - } - } ) } else { // Updating an existing public share if (!shareViaLinkPasswordSwitch.isChecked) { @@ -326,39 +265,13 @@ class PublicShareDialogFragment : DialogFragment() { publicLinkPassword = null } - ocShareViewModel.updatePublicShareForFile( + listener?.updatePublicShare( publicShare?.remoteId!!, publicLinkName, publicLinkPassword, publicLinkExpirationDateInMillis, publicLinkPermissions, publicUploadPermission - ).observe( - this, - Observer { resource -> - when (resource?.status) { - Status.SUCCESS -> { - dismiss() - (activity as BaseActivity).dismissLoadingDialog() - } - Status.ERROR -> { - val errorMessage: String = resource.msg ?: ErrorMessageAdapter.getResultMessage( - resource.code, - resource.exception, - OperationType.UPDATE_PUBLIC_SHARE, - resources - ); - showError(errorMessage) - (activity as BaseActivity).dismissLoadingDialog() - } - Status.LOADING -> { - (activity as BaseActivity).showLoadingDialog(R.string.common_loading) - } - else -> { - Log.d(TAG, "Unknown status when creating share") - } - } - } ) } } @@ -489,7 +402,7 @@ class PublicShareDialogFragment : DialogFragment() { super.onActivityCreated(savedInstanceState) Log_OC.d(TAG, "onActivityCreated") - refreshCapabilities() + listener?.refreshCapabilities(false) } override fun onSaveInstanceState(outState: Bundle) { @@ -640,38 +553,9 @@ class PublicShareDialogFragment : DialogFragment() { } } - private fun refreshCapabilities() { - ocCapabilityViewModel.getCapabilityForAccount().observe( - this, - Observer { resource -> - when (resource?.status) { - Status.SUCCESS -> { - capabilities = resource.data - updateInputFormAccordingToServerCapabilities() - (activity as BaseActivity).dismissLoadingDialog() - } - Status.ERROR -> { - val errorMessage = ErrorMessageAdapter.getResultMessage( - resource.code, - resource.exception, - OperationType.GET_SHARES, - resources - ) - showError(errorMessage) - capabilities = resource.data - (activity as BaseActivity).dismissLoadingDialog() - } - Status.LOADING -> { - (activity as BaseActivity).showLoadingDialog(R.string.common_loading) - capabilities = resource.data - updateInputFormAccordingToServerCapabilities() - } - else -> { - Log.d(TAG, "Unknown status when loading capabilities") - } - } - } - ) + fun updateCapabilities(capabilities: OCCapability?) { + this.capabilities = capabilities + updateInputFormAccordingToServerCapabilities() } /** 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 c93758f7c8d..b97b7bdb051 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 @@ -45,7 +45,6 @@ import com.owncloud.android.shares.db.OCShare import com.owncloud.android.ui.activity.BaseActivity import com.owncloud.android.ui.adapter.SharePublicLinkListAdapter import com.owncloud.android.ui.adapter.ShareUserListAdapter -import com.owncloud.android.ui.fragment.ShareFragmentListener import com.owncloud.android.utils.DisplayUtils import com.owncloud.android.utils.MimetypeIconUtil import kotlinx.android.synthetic.main.share_file_layout.* @@ -256,7 +255,7 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - getPrivateLinkButton?.setOnClickListener { listener?.copyOrSendPrivateLink(file) } + getPrivateLinkButton?.setOnClickListener { listener?.copyOrSendPrivateLink(file!!) } getPrivateLinkButton?.setOnLongClickListener { // Show a toast message explaining what a private link is @@ -375,7 +374,7 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe override fun unshareButtonPressed(share: OCShare) { // Unshare Log_OC.d(TAG, "Removing private share with " + share.sharedWithDisplayName) - listener?.removeShare(share) + listener?.removePublicShare(share) } override fun editShare(share: OCShare) { diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/ShareFragmentListener.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/ShareFragmentListener.kt new file mode 100644 index 00000000000..c9e4d766178 --- /dev/null +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/ShareFragmentListener.kt @@ -0,0 +1,78 @@ +/** + * ownCloud Android client application + * + * @author masensio + * @author David A. Velasco + * @author David González Verdugo + * Copyright (C) 2019 ownCloud GmbH. + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see //www.gnu.org/licenses/>. + */ + +package com.owncloud.android.shares.ui.fragment + +import com.owncloud.android.datamodel.OCFile +import com.owncloud.android.shares.db.OCShare + +/** + * This interface must be implemented by activities that contain this + * fragment to allow an interaction in fragments handling [OCShare]s + * to be communicated to the parent activity and potentially other fragments + * contained in that activity. + * + * + * See the Android Training lesson [Communicating with Other Fragments](http://developer.android.com/training/basics/fragments/communicating.html) for more information. + */ +interface ShareFragmentListener { + fun refreshShares() + + fun createPublicShare( + permissions: Int, + name: String, + password: String, + expirationTimeInMillis: Long, + publicUpload: Boolean + ) + + fun updatePublicShare( + remoteId: Long, + name: String, + password: String?, + expirationDateInMillis: Long, + permissions: Int, + publicUpload: Boolean + ) + + fun removePublicShare(share: OCShare) + + fun refreshCapabilities(shouldFetchFromNetwork: Boolean = true) + + fun copyOrSendPrivateLink(file: OCFile) + + fun showSearchUsersAndGroups() + + fun showEditPrivateShare(share: OCShare) + + fun refreshSharesFromServer() + + fun showAddPublicShare(defaultLinkName: String) + + fun showEditPublicShare(share: OCShare) + + fun showRemovePublicShare(share: OCShare) + + fun copyOrSendPublicLink(share: OCShare) +} diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt b/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt index 2920d020dad..4217511a41c 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/dialog/RemoveShareDialogFragment.kt @@ -33,9 +33,8 @@ import android.os.Bundle import com.owncloud.android.R import com.owncloud.android.lib.common.utils.Log_OC import com.owncloud.android.shares.db.OCShare -import com.owncloud.android.shares.ui.ShareActivity import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener -import com.owncloud.android.ui.fragment.ShareFragmentListener +import com.owncloud.android.shares.ui.fragment.ShareFragmentListener class RemoveShareDialogFragment : ConfirmationDialogFragment(), ConfirmationDialogFragmentListener { private var targetShare: OCShare? = null @@ -55,7 +54,7 @@ class RemoveShareDialogFragment : ConfirmationDialogFragment(), ConfirmationDial override fun onConfirmation(callerTag: String) { val listener = activity as ShareFragmentListener? Log_OC.d(TAG, "Removing public share " + targetShare!!.name) - listener?.removeShare(targetShare) + listener?.removePublicShare(targetShare!!) } override fun onCancel(callerTag: String) { diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/fragment/SearchShareesFragment.java b/owncloudApp/src/main/java/com/owncloud/android/ui/fragment/SearchShareesFragment.java index 6f8ed40d892..4ffe98ad2ce 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/fragment/SearchShareesFragment.java +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/fragment/SearchShareesFragment.java @@ -38,8 +38,8 @@ import com.owncloud.android.R; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.lib.common.utils.Log_OC; -import com.owncloud.android.lib.resources.shares.RemoteShare; import com.owncloud.android.shares.db.OCShare; +import com.owncloud.android.shares.ui.fragment.ShareFragmentListener; import com.owncloud.android.ui.activity.FileActivity; import com.owncloud.android.ui.adapter.ShareUserListAdapter; import com.owncloud.android.utils.PreferenceUtils; @@ -249,7 +249,7 @@ private void hideSoftKeyboard() { @Override public void unshareButtonPressed(OCShare share) { Log_OC.d(TAG, "Removed private share with " + share.getSharedWithDisplayName()); - mListener.removeShare(share); + mListener.removePublicShare(share); } @Override diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/fragment/ShareFragmentListener.java b/owncloudApp/src/main/java/com/owncloud/android/ui/fragment/ShareFragmentListener.java deleted file mode 100644 index 40ed049df22..00000000000 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/fragment/ShareFragmentListener.java +++ /dev/null @@ -1,57 +0,0 @@ -/** - * ownCloud Android client application - * - * @author masensio - * @author David A. Velasco - * @author David González Verdugo - * Copyright (C) 2016 ownCloud GmbH. - *

- * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2, - * as published by the Free Software Foundation. - *

- * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - *

- * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.owncloud.android.ui.fragment; - -import com.owncloud.android.datamodel.OCFile; -import com.owncloud.android.shares.db.OCShare; - -/** - * This interface must be implemented by activities that contain this - * fragment to allow an interaction in fragments handling {@link OCShare}s - * to be communicated to the parent activity and potentially other fragments - * contained in that activity. - *

- * See the Android Training lesson Communicating with Other Fragments for more information. - */ -public interface ShareFragmentListener { - void refreshShares(); - - void copyOrSendPrivateLink(OCFile file); - - void showSearchUsersAndGroups(); - - void showEditPrivateShare(OCShare share); - - void refreshSharesFromServer(); - - void showAddPublicShare(String defaultLinkName); - - void showEditPublicShare(OCShare share); - - void showRemovePublicShare(OCShare share); - - void removeShare(OCShare share); - - void copyOrSendPublicLink(OCShare share); -} From 0faf095e980898c145c8bc26b11e4dd5d39ff324 Mon Sep 17 00:00:00 2001 From: davigonz Date: Wed, 15 May 2019 12:31:06 +0200 Subject: [PATCH 07/23] Dismiss dialog to edit shares --- .../main/java/com/owncloud/android/shares/ui/ShareActivity.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 7180d0042d1..cff31205350 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 @@ -411,7 +411,7 @@ class ShareActivity : FileActivity(), ShareFragmentListener { Observer { resource -> when (resource?.status) { Status.SUCCESS -> { - dismissLoadingDialog() + publicShareFragment?.dismiss() } Status.ERROR -> { val errorMessage: String = resource.msg ?: ErrorMessageAdapter.getResultMessage( @@ -463,7 +463,7 @@ class ShareActivity : FileActivity(), ShareFragmentListener { Observer { resource -> when (resource?.status) { Status.SUCCESS -> { - dismissLoadingDialog() + publicShareFragment?.dismiss() } Status.ERROR -> { val errorMessage: String = resource.msg ?: ErrorMessageAdapter.getResultMessage( From 8ea1bd429b87c2587b3c866eefe640bb1f2a8aa0 Mon Sep 17 00:00:00 2001 From: davigonz Date: Wed, 15 May 2019 12:43:27 +0200 Subject: [PATCH 08/23] Get rid of account dependency in PublicShareDialogFragment --- .../ui/fragment/PublicShareDialogFragment.kt | 33 +++++-------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt index e132ce02f9c..60bafaab215 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt @@ -21,7 +21,6 @@ package com.owncloud.android.shares.ui.fragment -import android.accounts.Account import android.app.Activity import android.content.Context import android.graphics.drawable.Drawable @@ -65,11 +64,6 @@ class PublicShareDialogFragment : DialogFragment() { */ private var publicShare: OCShare? = null - /* - * OC account holding the file to share, received as a parameter in construction time - */ - private var account: Account? = null - /** * Reference to parent listener */ @@ -132,7 +126,6 @@ class PublicShareDialogFragment : DialogFragment() { if (arguments != null) { file = arguments!!.getParcelable(ARG_FILE) - account = arguments!!.getParcelable(ARG_ACCOUNT) publicShare = arguments!!.getParcelable(ARG_SHARE) } @@ -140,7 +133,7 @@ class PublicShareDialogFragment : DialogFragment() { throw IllegalStateException("Both ARG_FILE and ARG_SHARE cannot be NULL") } - setStyle(DialogFragment.STYLE_NO_TITLE, 0) + setStyle(STYLE_NO_TITLE, 0) } private fun updating(): Boolean = publicShare != null @@ -714,45 +707,37 @@ class PublicShareDialogFragment : DialogFragment() { } companion object { - private val TAG = PublicShareDialogFragment::class.java.simpleName /** * The fragment initialization parameters */ - private val ARG_FILE = "FILE" - private val ARG_SHARE = "SHARE" - private val ARG_ACCOUNT = "ACCOUNT" - private val ARG_DEFAULT_LINK_NAME = "DEFAULT_LINK_NAME" - private val KEY_EXPIRATION_DATE = "EXPIRATION_DATE" + private const val ARG_FILE = "FILE" + private const val ARG_SHARE = "SHARE" + private const val ARG_DEFAULT_LINK_NAME = "DEFAULT_LINK_NAME" + private const val KEY_EXPIRATION_DATE = "EXPIRATION_DATE" /** - * Create a new instance of PublicShareDialogFragment, providing fileToShare and account - * as an argument. + * Create a new instance of PublicShareDialogFragment, providing fileToShare as an argument. * * Dialog shown this way is intended to CREATE a new public share. * * @param fileToShare File to share with a new public share. - * @param account Account to get capabilities */ fun newInstanceToCreate( fileToShare: OCFile, - account: Account, defaultLinkName: String ): PublicShareDialogFragment { val publicShareDialogFragment = PublicShareDialogFragment() val args = Bundle() args.putParcelable(ARG_FILE, fileToShare) - args.putParcelable(ARG_ACCOUNT, account) args.putString(ARG_DEFAULT_LINK_NAME, defaultLinkName) - publicShareDialogFragment.arguments = args return publicShareDialogFragment } /** - * Update an instance of PublicShareDialogFragment, providing fileToShare, publicShare and - * account as arguments. + * Update an instance of PublicShareDialogFragment, providing fileToShare, publicShare as arguments. * * Dialog shown this way is intended to UPDATE an existing public share. * @@ -760,13 +745,11 @@ class PublicShareDialogFragment : DialogFragment() { */ fun newInstanceToUpdate( fileToShare: OCFile, - publicShare: OCShare, - account: Account + publicShare: OCShare ): PublicShareDialogFragment { val publicShareDialogFragment = PublicShareDialogFragment() val args = Bundle() args.putParcelable(ARG_FILE, fileToShare) - args.putParcelable(ARG_ACCOUNT, account) args.putParcelable(ARG_SHARE, publicShare) publicShareDialogFragment.arguments = args return publicShareDialogFragment From 3cc62ae38f8ca04444e5ba36a12f7e8ac98fdd8c Mon Sep 17 00:00:00 2001 From: davigonz Date: Wed, 15 May 2019 13:31:32 +0200 Subject: [PATCH 09/23] Fix unit tests --- .../capabilities/viewmodel/OCCapabilityViewModel.kt | 2 +- .../java/com/owncloud/android/shares/ui/ShareActivity.kt | 3 +-- .../shares/repository/RemoteSharesDataSourceTest.kt | 9 ++++++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/owncloudApp/src/main/java/com/owncloud/android/capabilities/viewmodel/OCCapabilityViewModel.kt b/owncloudApp/src/main/java/com/owncloud/android/capabilities/viewmodel/OCCapabilityViewModel.kt index 5bd46ced8d7..fc243fd34e1 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/capabilities/viewmodel/OCCapabilityViewModel.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/capabilities/viewmodel/OCCapabilityViewModel.kt @@ -52,6 +52,6 @@ class OCCapabilityViewModel( ) ) : ViewModel() { - fun getCapabilityForAccount(shouldFetchFromNetwork: Boolean): LiveData> = + fun getCapabilityForAccount(shouldFetchFromNetwork: Boolean = true): LiveData> = capabilityRepository.loadCapabilityForAccount(account.name, shouldFetchFromNetwork) } 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 cff31205350..66977089065 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 @@ -383,7 +383,6 @@ class ShareActivity : FileActivity(), ShareFragmentListener { // Create and show the dialog val createPublicShareFragment = PublicShareDialogFragment.newInstanceToCreate( file, - account, defaultLinkName ) @@ -436,7 +435,7 @@ class ShareActivity : FileActivity(), ShareFragmentListener { override fun showEditPublicShare(share: OCShare) { // Create and show the dialog. - val editPublicShareFragment = PublicShareDialogFragment.newInstanceToUpdate(file, share, account) + val editPublicShareFragment = PublicShareDialogFragment.newInstanceToUpdate(file, share) showDialogFragment( editPublicShareFragment, TAG_PUBLIC_SHARE_DIALOG_FRAGMENT diff --git a/owncloudApp/src/test/java/com/owncloud/android/shares/repository/RemoteSharesDataSourceTest.kt b/owncloudApp/src/test/java/com/owncloud/android/shares/repository/RemoteSharesDataSourceTest.kt index 8bd83431169..ac30b0368e4 100644 --- a/owncloudApp/src/test/java/com/owncloud/android/shares/repository/RemoteSharesDataSourceTest.kt +++ b/owncloudApp/src/test/java/com/owncloud/android/shares/repository/RemoteSharesDataSourceTest.kt @@ -22,6 +22,7 @@ package com.owncloud.android.shares.repository import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation +import com.owncloud.android.lib.resources.shares.RemoveRemoteShareOperation import com.owncloud.android.lib.resources.shares.ShareParserResult import com.owncloud.android.lib.resources.shares.ShareType import com.owncloud.android.lib.resources.shares.UpdateRemoteShareOperation @@ -29,7 +30,6 @@ import com.owncloud.android.shares.datasource.RemoteSharesDataSource class RemoteSharesDataSourceTest(private val remoteOperationResult: RemoteOperationResult) : RemoteSharesDataSource { - override fun getSharesForFile( remoteFilePath: String, reshares: Boolean, @@ -64,4 +64,11 @@ class RemoteSharesDataSourceTest(private val remoteOperationResult: RemoteOperat ): RemoteOperationResult { return remoteOperationResult } + + override fun deleteShare( + remoteId: Long, + removeRemoteShareOperation: RemoveRemoteShareOperation + ): RemoteOperationResult { + return remoteOperationResult + } } From 291f17486e150d31d25ef8c7a37e116d1f377c78 Mon Sep 17 00:00:00 2001 From: davigonz Date: Wed, 15 May 2019 17:32:06 +0200 Subject: [PATCH 10/23] Fixing UI tests after some dependencies cleanup[WIP] --- .../PublicShareCreationDialogFragmentTest.kt | 5 +-- .../PublicShareEditionDialogFragmentTest.kt | 7 ++- .../shares/ui/ShareFileFragmentTest.kt | 21 +++------ .../shares/ui/ShareFolderFragmentTest.kt | 4 +- .../shares/ui/TestShareFileActivity.kt | 44 +++++++++++++------ .../shares/ui/fragment/ShareFileFragment.kt | 13 ++++-- 6 files changed, 53 insertions(+), 41 deletions(-) diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareCreationDialogFragmentTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareCreationDialogFragmentTest.kt index e1c69f4a806..b7fac575ff1 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareCreationDialogFragmentTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareCreationDialogFragmentTest.kt @@ -98,7 +98,6 @@ class PublicShareCreationDialogFragmentTest { val publicShareDialogFragment = PublicShareDialogFragment.newInstanceToCreate( file, - account, defaultLinkName ) @@ -122,8 +121,8 @@ class PublicShareCreationDialogFragmentTest { ) ).thenReturn(sharesLiveData) - publicShareDialogFragment.ocCapabilityViewModelFactory = ViewModelUtil.createFor(ocCapabilityViewModel) - publicShareDialogFragment.ocShareViewModelFactory = ViewModelUtil.createFor(ocShareViewModel) +// publicShareDialogFragment.ocCapabilityViewModelFactory = ViewModelUtil.createFor(ocCapabilityViewModel) +// publicShareDialogFragment.ocShareViewModelFactory = ViewModelUtil.createFor(ocShareViewModel) activityRule.activity.setFragment(publicShareDialogFragment) } diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareEditionDialogFragmentTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareEditionDialogFragmentTest.kt index 2a71539eb43..faf46b0b135 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareEditionDialogFragmentTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareEditionDialogFragmentTest.kt @@ -82,8 +82,7 @@ class PublicShareEditionDialogFragmentTest { val publicShareDialogFragment = PublicShareDialogFragment.newInstanceToUpdate( file, - publicShare, - account + publicShare ) val filePath = "/Documents/doc3" @@ -107,8 +106,8 @@ class PublicShareEditionDialogFragmentTest { ) ).thenReturn(sharesLiveData) - publicShareDialogFragment.ocCapabilityViewModelFactory = ViewModelUtil.createFor(ocCapabilityViewModel) - publicShareDialogFragment.ocShareViewModelFactory = ViewModelUtil.createFor(ocShareViewModel) +// publicShareDialogFragment.ocCapabilityViewModelFactory = ViewModelUtil.createFor(ocCapabilityViewModel) +// publicShareDialogFragment.ocShareViewModelFactory = ViewModelUtil.createFor(ocShareViewModel) activityRule.activity.setFragment(publicShareDialogFragment) } 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 888be8e6e30..11ac9952a3d 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 @@ -32,16 +32,13 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.rule.ActivityTestRule import com.owncloud.android.R 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.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.shares.viewmodel.OCShareViewModel import com.owncloud.android.utils.TestUtil -import com.owncloud.android.utils.ViewModelUtil import com.owncloud.android.vo.Resource import org.junit.Before import org.junit.Rule @@ -55,6 +52,7 @@ class ShareFileFragmentTest { @Rule @JvmField val activityRule = ActivityTestRule(TestShareFileActivity::class.java, true, true) + lateinit var shareFragment: ShareFileFragment private val capabilitiesLiveData = MutableLiveData>() private val sharesLiveData = MutableLiveData>>() @@ -86,21 +84,16 @@ class ShareFileFragmentTest { val ownCloudVersion = mock(OwnCloudVersion::class.java) `when`(ownCloudVersion.isSearchUsersSupported).thenReturn(true) - val shareFragment = ShareFileFragment.newInstance( + shareFragment = ShareFileFragment.newInstance( getOCFileForTesting("image.jpg"), account, ownCloudVersion ) - val ocShareViewModel = mock(OCShareViewModel::class.java) - `when`(ocShareViewModel.getSharesForFile()).thenReturn(sharesLiveData) - shareFragment.ocShareViewModelFactory = ViewModelUtil.createFor(ocShareViewModel) - - val ocCapabilityViewModel = mock(OCCapabilityViewModel::class.java) - `when`(ocCapabilityViewModel.getCapabilityForAccount()).thenReturn(capabilitiesLiveData) - shareFragment.ocCapabilityViewModelFactory = ViewModelUtil.createFor(ocCapabilityViewModel) - activityRule.activity.setFragment(shareFragment) + + shareFragment.updateCapabilities(TestUtil.createCapability()) + shareFragment.updatePublicShares(publicShares) } @Test @@ -120,7 +113,6 @@ class ShareFileFragmentTest { @Test fun showLoadingCapabilitiesDialog() { - capabilitiesLiveData.postValue(Resource.loading(TestUtil.createCapability())) onView(withId(R.id.loadingLayout)).check(matches(isDisplayed())) } @@ -140,8 +132,7 @@ class ShareFileFragmentTest { @Test fun showPublicShares() { - loadCapabilitiesSuccessfully() - loadSharesSuccessfully() +// Thread.sleep(3000) onView(withText("Image link")).check(matches(isDisplayed())) onView(withText("Image link 2")).check(matches(isDisplayed())) 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 8ac7706ea58..3e4f014053d 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 @@ -77,8 +77,8 @@ class ShareFolderFragmentTest { ocShareViewModel = Mockito.mock(OCShareViewModel::class.java) Mockito.`when`(ocShareViewModel.getSharesForFile()).thenReturn(sharesLiveData) - shareFragment.ocShareViewModelFactory = ViewModelUtil.createFor(ocShareViewModel) - shareFragment.ocCapabilityViewModelFactory = ViewModelUtil.createFor(ocCapabilityViewModel) +// shareFragment.ocShareViewModelFactory = ViewModelUtil.createFor(ocShareViewModel) +// shareFragment.ocCapabilityViewModelFactory = ViewModelUtil.createFor(ocCapabilityViewModel) activityRule.activity.setFragment(shareFragment) } 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 0c55e5fbd42..cb3485e1c70 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,46 +20,62 @@ package com.owncloud.android.shares.ui -import com.owncloud.android.datamodel.FileDataStorageManager import com.owncloud.android.datamodel.OCFile import com.owncloud.android.shares.db.OCShare -import com.owncloud.android.testing.SingleFragmentActivity import com.owncloud.android.shares.ui.fragment.ShareFragmentListener +import com.owncloud.android.testing.SingleFragmentActivity class TestShareFileActivity : SingleFragmentActivity(), ShareFragmentListener { - override fun copyOrSendPrivateLink(file: OCFile?) { + override fun refreshShares() { + fragment } - override fun showSearchUsersAndGroups() { + override fun createPublicShare( + permissions: Int, + name: String, + password: String, + expirationTimeInMillis: Long, + publicUpload: Boolean + ) { } - override fun showEditPrivateShare(share: OCShare?) { + override fun updatePublicShare( + remoteId: Long, + name: String, + password: String?, + expirationDateInMillis: Long, + permissions: Int, + publicUpload: Boolean + ) { } - override fun refreshSharesFromServer() { + override fun removePublicShare(share: OCShare) { } - override fun removeShare(share: OCShare?) { + override fun refreshCapabilities(shouldFetchFromNetwork: Boolean) { } - override fun showAddPublicShare(defaultLinkName: String) { + override fun copyOrSendPrivateLink(file: OCFile) { + } + override fun showSearchUsersAndGroups() { } - override fun showRemovePublicShare(share: OCShare?) { + override fun showEditPrivateShare(share: OCShare) { + } + override fun refreshSharesFromServer() { } - override fun dismissAddPublicShare() { + override fun showAddPublicShare(defaultLinkName: String) { } - override fun showEditPublicShare(share: OCShare?) { + override fun showEditPublicShare(share: OCShare) { } - override fun copyOrSendPublicLink(share: OCShare?) { + override fun showRemovePublicShare(share: OCShare) { } - override fun getStorageManager(): FileDataStorageManager? { - return null; + override fun copyOrSendPublicLink(share: OCShare) { } } 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 b97b7bdb051..f8958f6b8dd 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,6 +29,7 @@ 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 @@ -208,7 +209,7 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe // Setup layout // Image - view.shareFileIcon?.setImageResource( + shareFileIcon?.setImageResource( MimetypeIconUtil.getFileTypeIconId( file?.mimetype, file?.fileName @@ -388,14 +389,20 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe // Update view depending on updated capabilities if (isPublicShareDisabled) { - shareViaLinkSection.visibility = View.GONE + view?.findViewById(R.id.shareViaLinkSection)?.visibility = View.GONE +// shareViaLinkSection.visibility = View.GONE } else { - shareViaLinkSection.visibility = View.VISIBLE + 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 From ad233385912c14b833fff8d7284955f2e2b5b4b6 Mon Sep 17 00:00:00 2001 From: davigonz Date: Thu, 16 May 2019 12:01:51 +0200 Subject: [PATCH 11/23] Fix ShareFileFragmentTest UI tests --- .../shares/ui/ShareFileFragmentTest.kt | 143 ++++++------------ .../shares/ui/ShareFolderFragmentTest.kt | 1 - .../shares/ui/TestShareFileActivity.kt | 9 +- .../android/shares/ui/ShareActivity.kt | 1 - .../shares/ui/fragment/ShareFileFragment.kt | 31 ++-- 5 files changed, 64 insertions(+), 121 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..b0268cc65b0 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,33 @@ 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) - + loadShareFileFragment() 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 +108,15 @@ class ShareFileFragmentTest { @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())) @@ -183,10 +125,9 @@ 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))) @@ -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..f4661cfb3dc 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 } } @@ -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) } From aa6f5ae7faba8dd8658053abe795719e1d8b2a28 Mon Sep 17 00:00:00 2001 From: davigonz Date: Thu, 16 May 2019 15:25:19 +0200 Subject: [PATCH 12/23] Fix crash when getting shares the first time --- .../shares/ui/fragment/ShareFileFragment.kt | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) 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 f4661cfb3dc..d3d424f1c0f 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 @@ -386,6 +386,8 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe fun updateCapabilities(capabilities: OCCapability?) { this.capabilities = capabilities + updatePublicLinkButton() + // Update view depending on updated capabilities if (isPublicShareDisabled) { shareViaLinkSection.visibility = View.GONE @@ -396,9 +398,28 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe fun updatePublicShares(publicShares: ArrayList) { publicLinks = publicShares + updatePublicLinkButton() updateListOfPublicLinks() } + /** + * Show or hide button for adding a new public share depending on the capabilities and the server version + */ + private fun updatePublicLinkButton() { + // Since capabilities and publicLinks are loaded asynchronously, let's check whether they both exist + if (capabilities == null || publicLinks == null) { + return + } + + if (!enableMultiplePublicSharing()) { + if (publicLinks?.size == 0) { + addPublicLinkButton.visibility = View.VISIBLE + } else if (publicLinks?.size!! >= 1) { + addPublicLinkButton.visibility = View.INVISIBLE + } + } + } + /** * Updates in the UI the section about public share with the information in the current * public share bound to file, if any @@ -422,16 +443,6 @@ 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) } @@ -468,6 +479,8 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe * @return true if should be enabled, false otherwise */ private fun enableMultiplePublicSharing(): Boolean { + if (capabilities == null) return true + val serverVersion = OwnCloudVersion(capabilities?.versionString!!) return when { From 302d8d1cb004ba88ef6e3153a1abf28042b05d4c Mon Sep 17 00:00:00 2001 From: davigonz Date: Fri, 17 May 2019 10:59:24 +0200 Subject: [PATCH 13/23] Fix PublicShareCreationDialogFragmentTest UI tests --- .../PublicShareCreationDialogFragmentTest.kt | 278 +++++++----------- .../shares/ui/TestShareFileActivity.kt | 11 +- .../ui/fragment/PublicShareDialogFragment.kt | 2 - 3 files changed, 123 insertions(+), 168 deletions(-) diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareCreationDialogFragmentTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareCreationDialogFragmentTest.kt index b7fac575ff1..1f6b4cfda8f 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareCreationDialogFragmentTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareCreationDialogFragmentTest.kt @@ -19,10 +19,8 @@ package com.owncloud.android.shares.ui -import android.accounts.Account import android.text.InputType.TYPE_CLASS_TEXT import android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD -import androidx.lifecycle.MutableLiveData import androidx.test.espresso.Espresso.onView import androidx.test.espresso.action.ViewActions.click import androidx.test.espresso.action.ViewActions.typeText @@ -38,19 +36,12 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.rule.ActivityTestRule import com.owncloud.android.R 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.operations.RemoteOperationResult import com.owncloud.android.lib.resources.status.CapabilityBooleanType -import com.owncloud.android.shares.db.OCShare import com.owncloud.android.shares.ui.fragment.PublicShareDialogFragment -import com.owncloud.android.shares.viewmodel.OCShareViewModel import com.owncloud.android.utils.DateUtils import com.owncloud.android.utils.TestUtil -import com.owncloud.android.utils.ViewModelUtil -import com.owncloud.android.vo.Resource import org.hamcrest.CoreMatchers.not -import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @@ -65,12 +56,9 @@ class PublicShareCreationDialogFragmentTest { @JvmField val activityRule = ActivityTestRule(TestShareFileActivity::class.java, true, true) - private val account = mock(Account::class.java) - private val capabilitiesLiveData = MutableLiveData>() - private val sharesLiveData = MutableLiveData>>() private val file = mock(OCFile::class.java) - private val publicShares = arrayListOf( + private val publicShareList = arrayListOf( TestUtil.createPublicShare( path = "/Documents/", isFolder = true, @@ -92,48 +80,15 @@ class PublicShareCreationDialogFragmentTest { ) ) - @Before - fun setUp() { - val defaultLinkName = "DOC_12112018.jpg link" - - val publicShareDialogFragment = PublicShareDialogFragment.newInstanceToCreate( - file, - defaultLinkName - ) - - val filePath = "/Documents/doc3" - - file.mimetype = ".txt" - `when`(file.remotePath).thenReturn(filePath) - val ocCapabilityViewModel = mock(OCCapabilityViewModel::class.java) - `when`( - ocCapabilityViewModel.getCapabilityForAccount() - ).thenReturn(capabilitiesLiveData) - - val ocShareViewModel = mock(OCShareViewModel::class.java) - `when`( - ocShareViewModel.insertPublicShareForFile( - 1, - defaultLinkName, - "", - -1, - false - ) - ).thenReturn(sharesLiveData) - -// publicShareDialogFragment.ocCapabilityViewModelFactory = ViewModelUtil.createFor(ocCapabilityViewModel) -// publicShareDialogFragment.ocShareViewModelFactory = ViewModelUtil.createFor(ocShareViewModel) - - activityRule.activity.setFragment(publicShareDialogFragment) - } - @Test fun showDialogTitle() { + loadPublicShareDialogFragment() onView(withId(R.id.publicShareDialogTitle)).check(matches(withText(R.string.share_via_link_create_title))) } @Test fun showMandatoryFields() { + loadPublicShareDialogFragment() onView(withId(R.id.shareViaLinkNameSection)).check(matches(isDisplayed())) onView(withId(R.id.shareViaLinkPasswordSection)).check(matches(isDisplayed())) onView(withId(R.id.shareViaLinkExpirationSection)).check(matches(isDisplayed())) @@ -141,32 +96,42 @@ class PublicShareCreationDialogFragmentTest { @Test fun showDialogButtons() { + loadPublicShareDialogFragment() onView(withId(R.id.cancelButton)).check(matches(isDisplayed())) onView(withId(R.id.saveButton)).check(matches(isDisplayed())) } @Test fun showFolderAdditionalFields() { - file.mimetype = "DIR" + loadPublicShareDialogFragment( + TestUtil.createCapability( + versionString = "10.0.1", + sharingPublicUpload = CapabilityBooleanType.TRUE.value, + sharingPublicSupportsUploadOnly = CapabilityBooleanType.TRUE.value + ), + isFolder = true + ) onView(withId(R.id.shareViaLinkEditPermissionGroup)).check(matches(isDisplayed())) } @Test fun showDefaultLinkName() { + loadPublicShareDialogFragment() onView(withId(R.id.shareViaLinkNameValue)).check(matches(withText("DOC_12112018.jpg link"))) } @Test fun enablePasswordSwitch() { + loadPublicShareDialogFragment() onView(withId(R.id.shareViaLinkPasswordSwitch)).perform(click()) onView(withId(R.id.shareViaLinkPasswordValue)).check(matches(isDisplayed())) } @Test fun checkPasswordNotVisible() { + loadPublicShareDialogFragment() onView(withId(R.id.shareViaLinkPasswordSwitch)).perform(click()) onView(withId(R.id.shareViaLinkPasswordValue)).perform(typeText("supersecure")) - onView(withId(R.id.shareViaLinkPasswordValue)).check( matches( withInputType( @@ -178,12 +143,9 @@ class PublicShareCreationDialogFragmentTest { @Test fun checkPasswordEnforced() { - capabilitiesLiveData.postValue( - Resource.success( - TestUtil.createCapability(sharingPublicPasswordEnforced = CapabilityBooleanType.TRUE.value) - ) + loadPublicShareDialogFragment( + capabilities = TestUtil.createCapability(sharingPublicPasswordEnforced = CapabilityBooleanType.TRUE.value) ) - onView(withId(R.id.shareViaLinkPasswordLabel)).check( matches(withText(R.string.share_via_link_password_enforced_label)) ) @@ -195,14 +157,11 @@ class PublicShareCreationDialogFragmentTest { @Test fun checkExpireDateEnforced() { - capabilitiesLiveData.postValue( - Resource.success( - TestUtil.createCapability( - sharingPublicExpireDateEnforced = CapabilityBooleanType.TRUE.value - ) + loadPublicShareDialogFragment( + capabilities = TestUtil.createCapability( + sharingPublicExpireDateEnforced = CapabilityBooleanType.TRUE.value ) ) - onView(withId(R.id.shareViaLinkExpirationLabel)) .check(matches(withText(R.string.share_via_link_expiration_date_enforced_label))) onView(withId(R.id.shareViaLinkExpirationSwitch)) @@ -213,14 +172,11 @@ class PublicShareCreationDialogFragmentTest { @Test fun checkExpireDateNotEnforced() { - capabilitiesLiveData.postValue( - Resource.success( - TestUtil.createCapability( - sharingPublicExpireDateEnforced = CapabilityBooleanType.FALSE.value - ) + loadPublicShareDialogFragment( + capabilities = TestUtil.createCapability( + sharingPublicExpireDateEnforced = CapabilityBooleanType.FALSE.value ) ) - onView(withId(R.id.shareViaLinkExpirationLabel)) .check(matches(withText(R.string.share_via_link_expiration_date_label))) onView(withId(R.id.shareViaLinkExpirationSwitch)) @@ -231,6 +187,7 @@ class PublicShareCreationDialogFragmentTest { @Test fun enableExpirationSwitch() { + loadPublicShareDialogFragment() onView(withId(R.id.shareViaLinkExpirationSwitch)).perform(click()) onView(withId(android.R.id.button1)).perform(click()); onView(withId(R.id.shareViaLinkExpirationValue)) @@ -240,6 +197,7 @@ class PublicShareCreationDialogFragmentTest { @Test fun cancelExpirationSwitch() { + loadPublicShareDialogFragment() onView(withId(R.id.shareViaLinkExpirationSwitch)).perform(click()) onView(withId(android.R.id.button2)).perform(click()); onView(withId(R.id.shareViaLinkExpirationValue)) @@ -248,26 +206,21 @@ class PublicShareCreationDialogFragmentTest { @Test fun showError() { - onView(withId(R.id.saveButton)).perform(click()) - sharesLiveData.postValue( - Resource.error( - RemoteOperationResult.ResultCode.SHARE_NOT_FOUND, - data = publicShares - ) + loadPublicShareDialogFragment( + errorMessage = "Unable to share. Please check whether the file exists" ) + onView(withId(R.id.saveButton)).perform(click()) onView(withId(R.id.public_link_error_message)).check(matches(isDisplayed())) onView(withId(R.id.public_link_error_message)).check(matches(withText(R.string.share_link_file_no_exist))) } @Test fun uploadPermissionsWithFolderDisplayed() { - capabilitiesLiveData.postValue( - Resource.success( - TestUtil.createCapability( - versionString = "10.1.1", - sharingPublicSupportsUploadOnly = CapabilityBooleanType.TRUE.value, - sharingPublicUpload = CapabilityBooleanType.TRUE.value - ) + loadPublicShareDialogFragment( + capabilities = TestUtil.createCapability( + versionString = "10.1.1", + sharingPublicSupportsUploadOnly = CapabilityBooleanType.TRUE.value, + sharingPublicUpload = CapabilityBooleanType.TRUE.value ) ) @@ -277,13 +230,11 @@ class PublicShareCreationDialogFragmentTest { @Test fun uploadPermissionsWithFolderNotDisplayed() { - capabilitiesLiveData.postValue( - Resource.success( - TestUtil.createCapability( - versionString = "10.1.1", - sharingPublicSupportsUploadOnly = CapabilityBooleanType.TRUE.value, - sharingPublicUpload = CapabilityBooleanType.FALSE.value - ) + loadPublicShareDialogFragment( + capabilities = TestUtil.createCapability( + versionString = "10.1.1", + sharingPublicSupportsUploadOnly = CapabilityBooleanType.TRUE.value, + sharingPublicUpload = CapabilityBooleanType.FALSE.value ) ) @@ -294,12 +245,10 @@ class PublicShareCreationDialogFragmentTest { @Test fun expirationDateDays() { val daysToTest = 15 - capabilitiesLiveData.postValue( - Resource.success( - TestUtil.createCapability( - versionString = "10.1.1", - sharingPublicExpireDateDays = daysToTest - ) + loadPublicShareDialogFragment( + capabilities = TestUtil.createCapability( + versionString = "10.1.1", + sharingPublicExpireDateDays = daysToTest ) ) val formattedDate = SimpleDateFormat.getDateInstance().format( @@ -316,12 +265,10 @@ class PublicShareCreationDialogFragmentTest { @Test fun passwordNotEnforced() { - capabilitiesLiveData.postValue( - Resource.success( - TestUtil.createCapability( - versionString = "10.1.1", - sharingPublicPasswordEnforced = CapabilityBooleanType.FALSE.value - ) + loadPublicShareDialogFragment( + capabilities = TestUtil.createCapability( + versionString = "10.1.1", + sharingPublicPasswordEnforced = CapabilityBooleanType.FALSE.value ) ) onView(withId(R.id.shareViaLinkPasswordLabel)) @@ -330,12 +277,10 @@ class PublicShareCreationDialogFragmentTest { @Test fun passwordEnforced() { - capabilitiesLiveData.postValue( - Resource.success( - TestUtil.createCapability( - versionString = "10.1.1", - sharingPublicPasswordEnforced = CapabilityBooleanType.TRUE.value - ) + loadPublicShareDialogFragment( + capabilities = TestUtil.createCapability( + versionString = "10.1.1", + sharingPublicPasswordEnforced = CapabilityBooleanType.TRUE.value ) ) onView(withId(R.id.shareViaLinkPasswordLabel)) @@ -344,18 +289,15 @@ class PublicShareCreationDialogFragmentTest { @Test fun passwordEnforcedReadOnlyFolders() { - capabilitiesLiveData.postValue( - Resource.success( - TestUtil.createCapability( - versionString = "10.1.1", - sharingPublicSupportsUploadOnly = CapabilityBooleanType.TRUE.value, - sharingPublicUpload = CapabilityBooleanType.TRUE.value, - sharingPublicPasswordEnforcedReadOnly = CapabilityBooleanType.TRUE.value, - sharingPublicPasswordEnforced = CapabilityBooleanType.TRUE.value - ) + loadPublicShareDialogFragment( + capabilities = TestUtil.createCapability( + versionString = "10.1.1", + sharingPublicSupportsUploadOnly = CapabilityBooleanType.TRUE.value, + sharingPublicUpload = CapabilityBooleanType.TRUE.value, + sharingPublicPasswordEnforcedReadOnly = CapabilityBooleanType.TRUE.value, + sharingPublicPasswordEnforced = CapabilityBooleanType.TRUE.value ) ) - `when`(file.isFolder).thenReturn(true) onView(withId(R.id.shareViaLinkEditPermissionReadOnly)).check(matches(isDisplayed())) onView(withId(R.id.shareViaLinkEditPermissionReadOnly)).perform(click()) @@ -365,18 +307,15 @@ class PublicShareCreationDialogFragmentTest { @Test fun passwordNotEnforcedReadOnlyFolders() { - capabilitiesLiveData.postValue( - Resource.success( - TestUtil.createCapability( - versionString = "10.1.1", - sharingPublicSupportsUploadOnly = CapabilityBooleanType.TRUE.value, - sharingPublicUpload = CapabilityBooleanType.TRUE.value, - sharingPublicPasswordEnforcedReadOnly = CapabilityBooleanType.FALSE.value, - sharingPublicPasswordEnforced = CapabilityBooleanType.FALSE.value - ) + loadPublicShareDialogFragment( + capabilities = TestUtil.createCapability( + versionString = "10.1.1", + sharingPublicSupportsUploadOnly = CapabilityBooleanType.TRUE.value, + sharingPublicUpload = CapabilityBooleanType.TRUE.value, + sharingPublicPasswordEnforcedReadOnly = CapabilityBooleanType.FALSE.value, + sharingPublicPasswordEnforced = CapabilityBooleanType.FALSE.value ) ) - `when`(file.isFolder).thenReturn(true) onView(withId(R.id.shareViaLinkEditPermissionReadOnly)).check(matches(isDisplayed())) onView(withId(R.id.shareViaLinkEditPermissionReadOnly)).perform(click()) @@ -386,18 +325,15 @@ class PublicShareCreationDialogFragmentTest { @Test fun passwordEnforcedReadWriteFolders() { - capabilitiesLiveData.postValue( - Resource.success( - TestUtil.createCapability( - versionString = "10.1.1", - sharingPublicSupportsUploadOnly = CapabilityBooleanType.TRUE.value, - sharingPublicUpload = CapabilityBooleanType.TRUE.value, - sharingPublicPasswordEnforcedReadWrite = CapabilityBooleanType.TRUE.value, - sharingPublicPasswordEnforced = CapabilityBooleanType.TRUE.value - ) + loadPublicShareDialogFragment( + capabilities = TestUtil.createCapability( + versionString = "10.1.1", + sharingPublicSupportsUploadOnly = CapabilityBooleanType.TRUE.value, + sharingPublicUpload = CapabilityBooleanType.TRUE.value, + sharingPublicPasswordEnforcedReadWrite = CapabilityBooleanType.TRUE.value, + sharingPublicPasswordEnforced = CapabilityBooleanType.TRUE.value ) ) - `when`(file.isFolder).thenReturn(true) onView(withId(R.id.shareViaLinkEditPermissionReadAndWrite)).check(matches(isDisplayed())) onView(withId(R.id.shareViaLinkEditPermissionReadAndWrite)).perform(click()) @@ -407,18 +343,15 @@ class PublicShareCreationDialogFragmentTest { @Test fun passwordNotEnforcedReadWriteFolders() { - capabilitiesLiveData.postValue( - Resource.success( - TestUtil.createCapability( - versionString = "10.1.1", - sharingPublicSupportsUploadOnly = CapabilityBooleanType.TRUE.value, - sharingPublicUpload = CapabilityBooleanType.TRUE.value, - sharingPublicPasswordEnforcedReadWrite = CapabilityBooleanType.FALSE.value, - sharingPublicPasswordEnforced = CapabilityBooleanType.FALSE.value - ) + loadPublicShareDialogFragment( + capabilities = TestUtil.createCapability( + versionString = "10.1.1", + sharingPublicSupportsUploadOnly = CapabilityBooleanType.TRUE.value, + sharingPublicUpload = CapabilityBooleanType.TRUE.value, + sharingPublicPasswordEnforcedReadWrite = CapabilityBooleanType.FALSE.value, + sharingPublicPasswordEnforced = CapabilityBooleanType.FALSE.value ) ) - `when`(file.isFolder).thenReturn(true) onView(withId(R.id.shareViaLinkEditPermissionReadAndWrite)).check(matches(isDisplayed())) onView(withId(R.id.shareViaLinkEditPermissionReadAndWrite)).perform(click()) @@ -428,18 +361,15 @@ class PublicShareCreationDialogFragmentTest { @Test fun passwordEnforcedUploadOnlyFolders() { - capabilitiesLiveData.postValue( - Resource.success( - TestUtil.createCapability( - versionString = "10.1.1", - sharingPublicSupportsUploadOnly = CapabilityBooleanType.TRUE.value, - sharingPublicUpload = CapabilityBooleanType.TRUE.value, - sharingPublicPasswordEnforcedUploadOnly = CapabilityBooleanType.TRUE.value, - sharingPublicPasswordEnforced = CapabilityBooleanType.FALSE.value - ) + loadPublicShareDialogFragment( + capabilities = TestUtil.createCapability( + versionString = "10.1.1", + sharingPublicSupportsUploadOnly = CapabilityBooleanType.TRUE.value, + sharingPublicUpload = CapabilityBooleanType.TRUE.value, + sharingPublicPasswordEnforcedUploadOnly = CapabilityBooleanType.TRUE.value, + sharingPublicPasswordEnforced = CapabilityBooleanType.FALSE.value ) ) - `when`(file.isFolder).thenReturn(true) onView(withId(R.id.shareViaLinkEditPermissionUploadFiles)).check(matches(isDisplayed())) onView(withId(R.id.shareViaLinkEditPermissionUploadFiles)).perform(click()) @@ -449,18 +379,15 @@ class PublicShareCreationDialogFragmentTest { @Test fun passwordNotEnforcedUploadOnlyFolders() { - capabilitiesLiveData.postValue( - Resource.success( - TestUtil.createCapability( - versionString = "10.1.1", - sharingPublicSupportsUploadOnly = CapabilityBooleanType.TRUE.value, - sharingPublicUpload = CapabilityBooleanType.TRUE.value, - sharingPublicPasswordEnforcedUploadOnly = CapabilityBooleanType.FALSE.value, - sharingPublicPasswordEnforced = CapabilityBooleanType.FALSE.value - ) + loadPublicShareDialogFragment( + capabilities = TestUtil.createCapability( + versionString = "10.1.1", + sharingPublicSupportsUploadOnly = CapabilityBooleanType.TRUE.value, + sharingPublicUpload = CapabilityBooleanType.TRUE.value, + sharingPublicPasswordEnforcedUploadOnly = CapabilityBooleanType.FALSE.value, + sharingPublicPasswordEnforced = CapabilityBooleanType.FALSE.value ) ) - `when`(file.isFolder).thenReturn(true) onView(withId(R.id.shareViaLinkEditPermissionUploadFiles)).check(matches(isDisplayed())) onView(withId(R.id.shareViaLinkEditPermissionUploadFiles)).perform(click()) @@ -468,5 +395,26 @@ class PublicShareCreationDialogFragmentTest { .check(matches(withText(R.string.share_via_link_password_label))) } + private fun loadPublicShareDialogFragment( + capabilities: OCCapability = TestUtil.createCapability(), + errorMessage: String = "Common error", + isFolder: Boolean = false + ) { + val defaultLinkName = "DOC_12112018.jpg link" + val filePath = "/Documents/doc3" + val fileMimeType = ".txt" + `when`(file.remotePath).thenReturn(filePath) + `when`(file.mimetype).thenReturn(fileMimeType) + `when`(file.isFolder).thenReturn(isFolder) + + val publicShareDialogFragment = PublicShareDialogFragment.newInstanceToCreate( + file, + defaultLinkName + ) + + activityRule.activity.capabilities = capabilities + activityRule.activity.errorMessage = errorMessage + activityRule.activity.setFragment(publicShareDialogFragment) + } } 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 07e92b1199b..50e3b9ce40d 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 @@ -23,6 +23,7 @@ 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.PublicShareDialogFragment import com.owncloud.android.shares.ui.fragment.ShareFileFragment import com.owncloud.android.shares.ui.fragment.ShareFragmentListener import com.owncloud.android.testing.SingleFragmentActivity @@ -30,9 +31,11 @@ import com.owncloud.android.testing.SingleFragmentActivity class TestShareFileActivity : SingleFragmentActivity(), ShareFragmentListener { lateinit var capabilities: OCCapability lateinit var publicShares: ArrayList + lateinit var errorMessage: String override fun refreshShares() { - val shareFileFragment: ShareFileFragment = supportFragmentManager.fragments.get(0) as ShareFileFragment + val shareFileFragment: ShareFileFragment = + supportFragmentManager.findFragmentByTag("TEST FRAGMENT") as ShareFileFragment shareFileFragment.updateCapabilities(capabilities) shareFileFragment.updatePublicShares(publicShares) } @@ -44,6 +47,9 @@ class TestShareFileActivity : SingleFragmentActivity(), ShareFragmentListener { expirationTimeInMillis: Long, publicUpload: Boolean ) { + val publicShareDialogFragment: PublicShareDialogFragment = + supportFragmentManager.findFragmentByTag("TEST FRAGMENT") as PublicShareDialogFragment + publicShareDialogFragment.showError(errorMessage) } override fun updatePublicShare( @@ -60,6 +66,9 @@ class TestShareFileActivity : SingleFragmentActivity(), ShareFragmentListener { } override fun refreshCapabilities(shouldFetchFromNetwork: Boolean) { + val publicShareDialogFragment: PublicShareDialogFragment = + supportFragmentManager.findFragmentByTag("TEST FRAGMENT") as PublicShareDialogFragment + publicShareDialogFragment.updateCapabilities(capabilities) } override fun copyOrSendPrivateLink(file: OCFile) { diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt index 60bafaab215..e84ded4d17d 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt @@ -242,7 +242,6 @@ class PublicShareDialogFragment : DialogFragment() { } if (!updating()) { // Creating a new public share - dismiss() listener?.createPublicShare( publicLinkPermissions, publicLinkName, @@ -432,7 +431,6 @@ class PublicShareDialogFragment : DialogFragment() { * Listener for user actions that start any update on a password for the public link. */ private inner class OnPasswordInteractionListener : CompoundButton.OnCheckedChangeListener { - /** * Called by R.id.shareViaLinkPasswordSwitch to set or clear the password. * From 60eaaedd304f17b825e0e91a3b57f161a700f696 Mon Sep 17 00:00:00 2001 From: davigonz Date: Fri, 17 May 2019 13:20:08 +0200 Subject: [PATCH 14/23] Fix UI tests in PublicShareEditionDialogFragmentTest and ShareFolderFragmentTest --- .../PublicShareEditionDialogFragmentTest.kt | 50 +++++++----------- .../shares/ui/ShareFolderFragmentTest.kt | 51 ++++++------------- .../ui/fragment/PublicShareDialogFragment.kt | 1 - 3 files changed, 34 insertions(+), 68 deletions(-) diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareEditionDialogFragmentTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareEditionDialogFragmentTest.kt index faf46b0b135..1425de53e23 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareEditionDialogFragmentTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareEditionDialogFragmentTest.kt @@ -19,8 +19,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 @@ -32,21 +30,21 @@ import androidx.test.espresso.matcher.ViewMatchers.withText import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.rule.ActivityTestRule import com.owncloud.android.R -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.resources.shares.RemoteShare import com.owncloud.android.shares.db.OCShare import com.owncloud.android.shares.ui.fragment.PublicShareDialogFragment -import com.owncloud.android.shares.viewmodel.OCShareViewModel -import com.owncloud.android.utils.ViewModelUtil -import com.owncloud.android.vo.Resource +import com.owncloud.android.utils.TestUtil import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mockito.`when` import org.mockito.Mockito.mock +import java.text.DateFormat +import java.text.SimpleDateFormat +import java.util.GregorianCalendar +import java.util.TimeZone @RunWith(AndroidJUnit4::class) class PublicShareEditionDialogFragmentTest { @@ -54,11 +52,9 @@ class PublicShareEditionDialogFragmentTest { @JvmField val activityRule = ActivityTestRule(TestShareFileActivity::class.java, true, true) - private val account = mock(Account::class.java) - private val capabilitiesLiveData = MutableLiveData>() - private val sharesLiveData = MutableLiveData>>() private val file = mock(OCFile::class.java) private val publicShare = mock(OCShare::class.java) + private val expirationDate = 1556575200000 @Before fun setUp() { @@ -76,8 +72,10 @@ class PublicShareEditionDialogFragmentTest { true ) + TimeZone.getDefault() + `when`(publicShare.expirationDate).thenReturn( - 1556575200000 + expirationDate ) val publicShareDialogFragment = PublicShareDialogFragment.newInstanceToUpdate( @@ -90,25 +88,7 @@ class PublicShareEditionDialogFragmentTest { file.mimetype = ".txt" `when`(file.remotePath).thenReturn(filePath) - val ocCapabilityViewModel = mock(OCCapabilityViewModel::class.java) - `when`( - ocCapabilityViewModel.getCapabilityForAccount() - ).thenReturn(capabilitiesLiveData) - - val ocShareViewModel = mock(OCShareViewModel::class.java) - `when`( - ocShareViewModel.insertPublicShareForFile( - 1, - linkName, - "", - -1, - false - ) - ).thenReturn(sharesLiveData) - -// publicShareDialogFragment.ocCapabilityViewModelFactory = ViewModelUtil.createFor(ocCapabilityViewModel) -// publicShareDialogFragment.ocShareViewModelFactory = ViewModelUtil.createFor(ocShareViewModel) - + activityRule.activity.capabilities = TestUtil.createCapability() activityRule.activity.setFragment(publicShareDialogFragment) } @@ -141,6 +121,14 @@ class PublicShareEditionDialogFragmentTest { @Test fun checkExpirationDateSet() { + val calendar = GregorianCalendar() + calendar.timeInMillis = expirationDate + + val formatter: DateFormat = SimpleDateFormat("MMM dd, yyyy"); + formatter.timeZone = TimeZone.getDefault(); + + val time = formatter.format(calendar.time); + onView(withId(R.id.shareViaLinkExpirationLabel)).check( matches(withText(R.string.share_via_link_expiration_date_label)) ) @@ -149,6 +137,6 @@ class PublicShareEditionDialogFragmentTest { onView(withId(R.id.shareViaLinkExpirationValue)) .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE))) onView(withId(R.id.shareViaLinkExpirationValue)) - .check(matches(withText("Apr 29, 2019"))) + .check(matches(withText(time))) } } 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 0d5d0855489..e0fe9f9e5b5 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 @@ -21,7 +21,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.isDisplayed @@ -29,15 +28,10 @@ import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.rule.ActivityTestRule import com.owncloud.android.R -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.resources.status.OwnCloudVersion -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.vo.Resource import org.hamcrest.Matchers.not import org.junit.Before import org.junit.Rule @@ -51,10 +45,7 @@ class ShareFolderFragmentTest { @JvmField val activityRule = ActivityTestRule(TestShareFileActivity::class.java, true, true) - private val sharesLiveData = MutableLiveData>>() - private val capabilitiesLiveData = MutableLiveData>() private lateinit var shareFragment: ShareFileFragment - private lateinit var ocShareViewModel: OCShareViewModel @Before fun setUp() { @@ -63,37 +54,26 @@ class ShareFolderFragmentTest { Mockito.`when`(ownCloudVersion.isSearchUsersSupported).thenReturn(true) shareFragment = ShareFileFragment.newInstance( - getOCFolderForTesting("Photos"), - account, - ownCloudVersion + getOCFolderForTesting("Photos"), + account, + ownCloudVersion ) - val ocCapabilityViewModel = Mockito.mock(OCCapabilityViewModel::class.java) - Mockito.`when`( - ocCapabilityViewModel.getCapabilityForAccount() - ).thenReturn(capabilitiesLiveData) - - ocShareViewModel = Mockito.mock(OCShareViewModel::class.java) - Mockito.`when`(ocShareViewModel.getSharesForFile()).thenReturn(sharesLiveData) - -// shareFragment.ocShareViewModelFactory = ViewModelUtil.createFor(ocShareViewModel) -// shareFragment.ocCapabilityViewModelFactory = ViewModelUtil.createFor(ocCapabilityViewModel) + activityRule.activity.capabilities = TestUtil.createCapability() + activityRule.activity.publicShares = arrayListOf( + TestUtil.createPublicShare( + path = "/Photos", + isFolder = true, + name = "Photos link 1", + shareLink = "http://server:port/s/1" + ) + ) activityRule.activity.setFragment(shareFragment) } @Test - fun folderSizeVisible(){ - val publicShares = arrayListOf( - TestUtil.createPublicShare( - path = "/Photos", - isFolder = true, - name = "Photos link 1", - shareLink = "http://server:port/s/1" - ) - ) - sharesLiveData.postValue(Resource.success(publicShares)) + fun folderSizeVisible() { onView(withId(R.id.shareFileSize)).check(matches(not(isDisplayed()))) - } @Test @@ -101,9 +81,8 @@ class ShareFolderFragmentTest { onView(withId(R.id.getPrivateLinkButton)).check(matches(not(isDisplayed()))) } - - fun getOCFolderForTesting(name: String = "default"): OCFile { - var file = OCFile("/Photos") + private fun getOCFolderForTesting(name: String = "default"): OCFile { + val file = OCFile("/Photos") file.availableOfflineStatus = OCFile.AvailableOfflineStatus.NOT_AVAILABLE_OFFLINE file.fileName = name file.fileId = 9456985479 diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt index e84ded4d17d..74d7b8f3931 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/ui/fragment/PublicShareDialogFragment.kt @@ -592,7 +592,6 @@ class PublicShareDialogFragment : DialogFragment() { // Show default date enforced by the server, if any if (!updating() && capabilities?.filesSharingPublicExpireDateDays!! > 0) { - setExpirationDateSwitchChecked(checked = true) val formattedDate = SimpleDateFormat.getDateInstance().format( From 993ddf640dfabd6a30fca4dcb5039c00af3475c6 Mon Sep 17 00:00:00 2001 From: davigonz Date: Fri, 17 May 2019 14:38:35 +0200 Subject: [PATCH 15/23] Use room implementation in Content Provider to delete shares + test for local and remote datasources --- .../android/providers/FileContentProvider.kt | 5 +++-- .../datasource/LocalSharesDataSource.kt | 2 +- .../datasource/OCLocalSharesDataSource.kt | 2 +- .../datasource/OCRemoteSharesDataSource.kt | 2 +- .../owncloud/android/shares/db/OCShareDao.kt | 2 +- .../datasource/OCLocalShareDataSourceTest.kt | 18 +++++++++++++++-- .../datasource/OCRemoteShareDataSourceTest.kt | 20 +++++++++++++++++++ 7 files changed, 43 insertions(+), 8 deletions(-) diff --git a/owncloudApp/src/main/java/com/owncloud/android/providers/FileContentProvider.kt b/owncloudApp/src/main/java/com/owncloud/android/providers/FileContentProvider.kt index 45756406956..2b3275f21e2 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/providers/FileContentProvider.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/providers/FileContentProvider.kt @@ -55,7 +55,8 @@ import com.owncloud.android.shares.db.OCShare import com.owncloud.android.utils.FileStorageUtils import java.io.File import java.io.FileNotFoundException -import java.util.* +import java.util.ArrayList +import java.util.HashMap /** * The ContentProvider for the ownCloud App. @@ -149,7 +150,7 @@ class FileContentProvider(val appExecutors: AppExecutors = AppExecutors()) : Con ROOT_DIRECTORY -> //Log_OC.d(TAG, "Removing ROOT!"); count = db.delete(ProviderTableMeta.FILE_TABLE_NAME, where, whereArgs) - SHARES -> count = db.delete(ProviderTableMeta.OCSHARES_TABLE_NAME, where, whereArgs) + SHARES -> count = OwncloudDatabase.getDatabase(context).shareDao().deleteShare(uri.pathSegments[1].toLong()) CAPABILITIES -> count = db.delete(ProviderTableMeta.CAPABILITIES_TABLE_NAME, where, whereArgs) UPLOADS -> count = db.delete(ProviderTableMeta.UPLOADS_TABLE_NAME, where, whereArgs) CAMERA_UPLOADS_SYNC -> count = db.delete(ProviderTableMeta.CAMERA_UPLOADS_SYNC_TABLE_NAME, where, whereArgs) diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/LocalSharesDataSource.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/LocalSharesDataSource.kt index 923722cafd0..8a1f82217b7 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/LocalSharesDataSource.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/LocalSharesDataSource.kt @@ -36,7 +36,7 @@ interface LocalSharesDataSource { fun replaceSharesForFile(ocShares: List): List - fun deleteShare(remoteId: Long) + fun deleteShare(remoteId: Long): Int fun deleteSharesForFile(filePath: String, accountName: String) } diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCLocalSharesDataSource.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCLocalSharesDataSource.kt index 8132d5efd9d..ee84556feb2 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCLocalSharesDataSource.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCLocalSharesDataSource.kt @@ -43,7 +43,7 @@ class OCLocalSharesDataSource( override fun replaceSharesForFile(ocShares: List): List = ocShareDao.replaceSharesForFile(ocShares) - override fun deleteShare(remoteId: Long) = ocShareDao.deleteShare(remoteId) + override fun deleteShare(remoteId: Long): Int = ocShareDao.deleteShare(remoteId) override fun deleteSharesForFile(filePath: String, accountName: String) = ocShareDao.deleteSharesForFile(filePath, accountName) diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCRemoteSharesDataSource.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCRemoteSharesDataSource.kt index 20a1c502e2a..35fc5020481 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCRemoteSharesDataSource.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCRemoteSharesDataSource.kt @@ -81,6 +81,6 @@ class OCRemoteSharesDataSource( remoteId: Long, removeRemoteShareOperation: RemoveRemoteShareOperation ): RemoteOperationResult { - return RemoveRemoteShareOperation(remoteId).execute(client) + return removeRemoteShareOperation.execute(client) } } diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/db/OCShareDao.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/db/OCShareDao.kt index 1b10b4dc4ed..ace4f76bc09 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/db/OCShareDao.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/db/OCShareDao.kt @@ -60,7 +60,7 @@ abstract class OCShareDao { "DELETE from " + ProviderTableMeta.OCSHARES_TABLE_NAME + " WHERE " + ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + " = :remoteId" ) - abstract fun deleteShare(remoteId: Long) + abstract fun deleteShare(remoteId: Long): Int @Query( "DELETE from " + ProviderTableMeta.OCSHARES_TABLE_NAME + " WHERE " + diff --git a/owncloudApp/src/test/java/com/owncloud/android/shares/datasource/OCLocalShareDataSourceTest.kt b/owncloudApp/src/test/java/com/owncloud/android/shares/datasource/OCLocalShareDataSourceTest.kt index 71230e1049b..a21192174aa 100644 --- a/owncloudApp/src/test/java/com/owncloud/android/shares/datasource/OCLocalShareDataSourceTest.kt +++ b/owncloudApp/src/test/java/com/owncloud/android/shares/datasource/OCLocalShareDataSourceTest.kt @@ -107,6 +107,14 @@ class OCLocalDataSourceTest { 8 ) + `when`( + ocSharesDao.deleteShare( + 5 + ) + ).thenReturn( + 1 + ) + ocLocalSharesDataSource = OCLocalSharesDataSource(ocSharesDao) } @@ -143,7 +151,6 @@ class OCLocalDataSourceTest { ) ) ) - assertEquals(insertedShareId.size, 1) assertEquals(insertedShareId[0], 7) } @@ -158,7 +165,14 @@ class OCLocalDataSourceTest { shareLink = "http://server:port/s/2" ) ) - assertEquals(updatedShareId, 8) } + + @Test + fun deletePublicShare() { + val deletedRows = ocLocalSharesDataSource.deleteShare( + 5 + ) + assertEquals(deletedRows, 1) + } } diff --git a/owncloudApp/src/test/java/com/owncloud/android/shares/datasource/OCRemoteShareDataSourceTest.kt b/owncloudApp/src/test/java/com/owncloud/android/shares/datasource/OCRemoteShareDataSourceTest.kt index 42df11583b2..fbd2f1a66ee 100644 --- a/owncloudApp/src/test/java/com/owncloud/android/shares/datasource/OCRemoteShareDataSourceTest.kt +++ b/owncloudApp/src/test/java/com/owncloud/android/shares/datasource/OCRemoteShareDataSourceTest.kt @@ -24,6 +24,7 @@ import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation +import com.owncloud.android.lib.resources.shares.RemoveRemoteShareOperation import com.owncloud.android.lib.resources.shares.ShareParserResult import com.owncloud.android.lib.resources.shares.ShareType import com.owncloud.android.lib.resources.shares.UpdateRemoteShareOperation @@ -242,4 +243,23 @@ class OCRemoteShareDataSourceTest { assertEquals(publicShareUpdated.permissions, 1) assertEquals(publicShareUpdated.shareLink, "http://server:port/s/1275farv") } + + @Test + fun deletePublicShare() { + val removeRemoteShareOperation = mock(RemoveRemoteShareOperation::class.java) + + val removeRemoteShareOperationResult = TestUtil.createRemoteOperationResultMock( + ShareParserResult(arrayListOf()), + isSuccess = true + ) + + `when`(removeRemoteShareOperation.execute(ownCloudClient)).thenReturn( + removeRemoteShareOperationResult + ) + + val remoteOperationResult = ocRemoteSharesDataSource.deleteShare(1, removeRemoteShareOperation) + + assertThat(remoteOperationResult, notNullValue()) + assertEquals(remoteOperationResult.isSuccess, true) + } } From 64d34edf73236ef05f50c49233703075a9bd818e Mon Sep 17 00:00:00 2001 From: davigonz Date: Mon, 20 May 2019 11:03:07 +0200 Subject: [PATCH 16/23] Refactor share repository tests --- .../repository/OCShareRepositoryTest.kt | 382 +++++++----------- 1 file changed, 155 insertions(+), 227 deletions(-) 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 0acb923862a..8b93238e367 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 @@ -20,6 +20,7 @@ package com.owncloud.android.shares.repository import androidx.arch.core.executor.testing.InstantTaskExecutorRule +import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.Observer import com.owncloud.android.lib.common.operations.RemoteOperationResult @@ -42,63 +43,55 @@ import org.mockito.Mockito.verify @RunWith(JUnit4::class) class OCShareRepositoryTest { - private lateinit var ocShareRepository: OCShareRepository - - private val localSharesDataSource = mock(LocalSharesDataSource::class.java) - @Rule @JvmField val instantExecutorRule = InstantTaskExecutorRule() - @Test - fun loadPublicSharesForFileFromNetwork() { - val dbData = MutableLiveData>() + private lateinit var ocShareRepository: OCShareRepository + private val localSharesDataSource = mock(LocalSharesDataSource::class.java) - `when`( - localSharesDataSource.getSharesForFileAsLiveData( - "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK) - ) - ).thenReturn( - dbData - ) + private val publicShare = listOf( + TestUtil.createPublicShare( + path = "/Photos/", + isFolder = true, + name = "Photos folder link", + shareLink = "http://server:port/s/1" + ) + ) + + private val remoteShares = arrayListOf( + TestUtil.createRemoteShare( + path = "/Photos/", + isFolder = true, + name = "Photos folder link", + shareLink = "http://server:port/s/1" + ), + TestUtil.createRemoteShare( + path = "/Photos/", + isFolder = false, + name = "Photos folder link 1", + shareLink = "http://server:port/s/2" + ), + TestUtil.createRemoteShare( + path = "/Photos/", + isFolder = false, + name = "Photos folder link 2", + shareLink = "http://server:port/s/3" + ) + ) - val remoteSharesForFile = arrayListOf( - TestUtil.createRemoteShare( - path = "/Photos/", - isFolder = true, - name = "Photos folder link", - shareLink = "http://server:port/s/1" - ), - TestUtil.createRemoteShare( - path = "/Photos/image1.jpg", - isFolder = false, - name = "Image 1 link", - shareLink = "http://server:port/s/2" - ), - TestUtil.createRemoteShare( - path = "/Photos/image2.jpg", - isFolder = false, - name = "Image 2 link", - shareLink = "http://server:port/s/3" - ) - ) + @Test + fun loadPublicSharesForFileFromNetwork() { + val localData = MutableLiveData>() val remoteOperationResult = - TestUtil.createRemoteOperationResultMock(ShareParserResult(remoteSharesForFile), true) - - val remoteSharesDataSource = RemoteSharesDataSourceTest(remoteOperationResult) - - ocShareRepository = - OCShareRepository.create(InstantAppExecutors(), localSharesDataSource, remoteSharesDataSource) - - val data = ocShareRepository.loadSharesForFile( - "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK), reshares = true, subfiles = false - ) + TestUtil.createRemoteOperationResultMock(ShareParserResult(remoteShares), true) + val data = loadShares(localData, remoteOperationResult) val observer = mock>>>() data.observeForever(observer) - dbData.postValue(null) + localData.postValue(null) // Get public shares from database to observe them, is called twice (one showing current db shares while // getting shares from server and another one with db shares already updated with server ones) @@ -110,55 +103,31 @@ class OCShareRepositoryTest { // Public shares are always retrieved from server and inserted in database if not empty list verify(localSharesDataSource).replaceSharesForFile( - remoteSharesForFile.map { remoteShare -> + remoteShares.map { remoteShare -> OCShare.fromRemoteShare(remoteShare).also { it.accountOwner = "admin@server" } } ) // Observe changes in database livedata when there's a new public share - val newPublicShare = listOf( - TestUtil.createPublicShare( - path = "/Documents/doc1", - isFolder = false, - name = "Doc 1 link", - shareLink = "http://server:port/s/1" - ) + localData.postValue( + publicShare ) - dbData.postValue( - newPublicShare - ) - - verify(observer).onChanged(Resource.success(newPublicShare)) + verify(observer).onChanged(Resource.success(publicShare)) } @Test fun loadEmptyPublicSharesForFileFromNetwork() { - val dbData = MutableLiveData>() - - `when`( - localSharesDataSource.getSharesForFileAsLiveData( - "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK) - ) - ).thenReturn( - dbData - ) - - val remoteOperationResult = TestUtil.createRemoteOperationResultMock(ShareParserResult(arrayListOf()), true) + val localData = MutableLiveData>() - val remoteSharesDataSource = RemoteSharesDataSourceTest(remoteOperationResult) - - ocShareRepository = - OCShareRepository.create(InstantAppExecutors(), localSharesDataSource, remoteSharesDataSource) - - val data = ocShareRepository.loadSharesForFile( - "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK), true, false - ) + val remoteOperationResult = + TestUtil.createRemoteOperationResultMock(ShareParserResult(arrayListOf()), true) + val data = loadShares(localData, remoteOperationResult) val observer = mock>>>() data.observeForever(observer) - dbData.postValue(null) + localData.postValue(null) // Get public shares from database to observe them, is called twice (one showing current db shares while // getting shares from server and another one with db shares already updated with server ones) @@ -172,31 +141,15 @@ class OCShareRepositoryTest { verify(localSharesDataSource).deleteSharesForFile("/Photos/", "admin@server") // Observe changes in database livedata when the list of shares is empty - dbData.postValue(listOf()) + localData.postValue(listOf()) verify(observer).onChanged(Resource.success(listOf())) } @Test fun loadPublicSharesForFileFromNetworkWithError() { - val dbData = MutableLiveData>() - - dbData.value = listOf( - TestUtil.createPublicShare( - path = "/Images/image1", - isFolder = false, - name = "Image 1 link", - shareLink = "http://server:port/s/1" - ) - ) - - `when`( - localSharesDataSource.getSharesForFileAsLiveData( - "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK) - ) - ).thenReturn( - dbData - ) + val localData = MutableLiveData>() + localData.value = publicShare val exception = Exception("Error when retrieving shares") @@ -207,14 +160,7 @@ class OCShareRepositoryTest { exception = exception ) - val remoteSharesDataSource = RemoteSharesDataSourceTest(remoteOperationResult) - - ocShareRepository = - OCShareRepository.create(InstantAppExecutors(), localSharesDataSource, remoteSharesDataSource) - - val data = ocShareRepository.loadSharesForFile( - "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK), true, false - ) + val data = loadShares(localData, remoteOperationResult) // Get public shares from database to observe them verify(localSharesDataSource).getSharesForFileAsLiveData( @@ -229,74 +175,35 @@ class OCShareRepositoryTest { verify(observer).onChanged( Resource.error( - RemoteOperationResult.ResultCode.FORBIDDEN, dbData.value, exception = exception + RemoteOperationResult.ResultCode.FORBIDDEN, localData.value, exception = exception ) ) } @Test fun insertPublicShareForFileOnNetwork() { - val dbData = MutableLiveData>() - - dbData.value = listOf( - TestUtil.createPublicShare( - path = "/Videos/", - isFolder = true, - name = "Videos folder link 1", - shareLink = "http://server:port/s/10" - ) - ) - - `when`( - localSharesDataSource.getSharesForFileAsLiveData( - "/Videos/", "admin@server", listOf(ShareType.PUBLIC_LINK) - ) - ).thenReturn( - dbData - ) - - val justCreatedRemoteShare = arrayListOf( - TestUtil.createRemoteShare( - path = "/Videos/", - isFolder = true, - name = "Videos folder link 2", - shareLink = "http://server:port/s/11" - ) - ) + val localData = MutableLiveData>() + localData.value = publicShare val remoteOperationResult = TestUtil.createRemoteOperationResultMock( - ShareParserResult(justCreatedRemoteShare), true - ) - - val remoteSharesDataSource = RemoteSharesDataSourceTest(remoteOperationResult) - - ocShareRepository = - OCShareRepository.create(InstantAppExecutors(), localSharesDataSource, remoteSharesDataSource) - - val data = ocShareRepository.insertPublicShareForFile( - "/Videos/", - "admin@server", - 1, - "Videos folder link 2", - "1234", - -1, - true + ShareParserResult(arrayListOf(remoteShares[1])), true ) + val data = insertShare(localData, remoteOperationResult) 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( - "/Videos/", "admin@server", listOf(ShareType.PUBLIC_LINK) + "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK) ) // Retrieving public shares from server... // Public shares are always retrieved from server and inserted in database if not empty list verify(localSharesDataSource).insert( - justCreatedRemoteShare.map { remoteShare -> + arrayListOf(remoteShares[1]).map { remoteShare -> OCShare.fromRemoteShare(remoteShare).also { it.accountOwner = "admin@server" } } ) @@ -304,24 +211,8 @@ class OCShareRepositoryTest { @Test fun insertPublicShareForFileOnNetworkWithError() { - val dbData = MutableLiveData>() - - dbData.value = listOf( - TestUtil.createPublicShare( - path = "/Slides/budget.pptx", - isFolder = false, - name = "Budget link 1", - shareLink = "http://server:port/s/1" - ) - ) - - `when`( - localSharesDataSource.getSharesForFileAsLiveData( - "/Slides/budget.pptx", "user@server", listOf(ShareType.PUBLIC_LINK) - ) - ).thenReturn( - dbData - ) + val localData = MutableLiveData>() + localData.value = publicShare val exception = Exception("Error when retrieving shares") @@ -332,24 +223,11 @@ class OCShareRepositoryTest { exception = exception ) - val remoteSharesDataSource = RemoteSharesDataSourceTest(remoteOperationResult) - - ocShareRepository = - OCShareRepository.create(InstantAppExecutors(), localSharesDataSource, remoteSharesDataSource) - - val data = ocShareRepository.insertPublicShareForFile( - "/Slides/budget.pptx", - "user@server", - 1, - "Budget link 2", - "78910", - -1, - false - ) + val data = insertShare(localData, remoteOperationResult) // Get public shares from database to observe them verify(localSharesDataSource).getSharesForFileAsLiveData( - "/Slides/budget.pptx", "user@server", listOf(ShareType.PUBLIC_LINK) + "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK) ) // Retrieving public shares from server... @@ -360,77 +238,127 @@ class OCShareRepositoryTest { verify(observer).onChanged( Resource.error( - RemoteOperationResult.ResultCode.SHARE_NOT_FOUND, dbData.value, exception = exception + RemoteOperationResult.ResultCode.SHARE_NOT_FOUND, localData.value, exception = exception ) ) } @Test fun updatePublicShareForFileOnNetwork() { - val dbData = MutableLiveData>() - - dbData.value = listOf( - TestUtil.createPublicShare( - path = "/Documents/doc1.docx", - expirationDate = 1000, - isFolder = false, - name = "Doc 1 link", - shareLink = "http://server:port/s/1" - ) - ) + val localData = MutableLiveData>() + localData.value = publicShare - `when`( - localSharesDataSource.getSharesForFileAsLiveData( - "/Documents/doc1.docx", "admin@server", listOf(ShareType.PUBLIC_LINK) - ) - ).thenReturn( - dbData + val remoteOperationResult = TestUtil.createRemoteOperationResultMock( + ShareParserResult(arrayListOf(remoteShares[2])), true ) - val justUpdatedRemoteShare = arrayListOf( - TestUtil.createRemoteShare( - path = "/Documents/doc1.docx", - expirationDate = 2000, - isFolder = false, - name = "Doc 1 link updated", - shareLink = "http://server:port/s/11" - ) - ) + val data = updateShare(localData, remoteOperationResult) - val remoteOperationResult = TestUtil.createRemoteOperationResultMock( - ShareParserResult(justUpdatedRemoteShare), true + 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( + "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK) ) - val remoteSharesDataSource = RemoteSharesDataSourceTest(remoteOperationResult) + // Retrieving public shares from server... - ocShareRepository = - OCShareRepository.create(InstantAppExecutors(), localSharesDataSource, remoteSharesDataSource) + // Public shares are always retrieved from server and updated in database + verify(localSharesDataSource).update( + OCShare.fromRemoteShare(remoteShares[2]).also { it.accountOwner = "admin@server" } + ) + } - val data = ocShareRepository.updatePublicShareForFile( - "/Documents/doc1.docx", - "admin@server", - 1, - "Doc 1 link updated", - "1234", - 2000, - 1, - false + @Test + fun deletePublicShareForFileOnNetwork() { + val localData = MutableLiveData>() + localData.value = publicShare + + val remoteOperationResult = TestUtil.createRemoteOperationResultMock( + ShareParserResult(arrayListOf(remoteShares[2])), true ) + val data = updateShare(localData, remoteOperationResult) + 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( - "/Documents/doc1.docx", "admin@server", listOf(ShareType.PUBLIC_LINK) + "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK) ) // Retrieving public shares from server... // Public shares are always retrieved from server and updated in database verify(localSharesDataSource).update( - OCShare.fromRemoteShare(justUpdatedRemoteShare.get(0)).also { it.accountOwner = "admin@server" } + OCShare.fromRemoteShare(remoteShares[2]).also { it.accountOwner = "admin@server" } + ) + } + + private fun createRepositoryWithData( + localData: MutableLiveData>, + remoteOperationResult: RemoteOperationResult + ): OCShareRepository { + `when`( + localSharesDataSource.getSharesForFileAsLiveData( + "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK) + ) + ).thenReturn( + localData + ) + + val remoteSharesDataSource = RemoteSharesDataSourceTest(remoteOperationResult) + + return OCShareRepository.create(InstantAppExecutors(), localSharesDataSource, remoteSharesDataSource) + } + + private fun loadShares( + localData: MutableLiveData>, + remoteOperationResult: RemoteOperationResult + ): LiveData>> { + val ocShareRepository = createRepositoryWithData(localData, remoteOperationResult) + + return ocShareRepository.loadSharesForFile( + "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK), reshares = true, subfiles = false + ) + } + + private fun insertShare( + localData: MutableLiveData>, + remoteOperationResult: RemoteOperationResult + ): LiveData>> { + val ocShareRepository = createRepositoryWithData(localData, remoteOperationResult) + + return ocShareRepository.insertPublicShareForFile( + "/Photos/", + "admin@server", + 1, + "Photos folder link 3", + "1234", + -1, + true + ) + } + + private fun updateShare( + localData: MutableLiveData>, + remoteOperationResult: RemoteOperationResult + ): LiveData>> { + val ocShareRepository = createRepositoryWithData(localData, remoteOperationResult) + + return ocShareRepository.updatePublicShareForFile( + "/Photos/", + "admin@server", + 1, + "Photos folder link updated", + "123456", + 2000, + 1, + false ) } } From 0e37f988abedb4482aed55ca98372bea7bdc7a07 Mon Sep 17 00:00:00 2001 From: davigonz Date: Mon, 20 May 2019 13:24:54 +0200 Subject: [PATCH 17/23] Include tests for public sharing deletion in repository --- .../repository/OCShareRepositoryTest.kt | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) 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 8b93238e367..50ffd4e82f8 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 @@ -47,7 +47,6 @@ class OCShareRepositoryTest { @JvmField val instantExecutorRule = InstantTaskExecutorRule() - private lateinit var ocShareRepository: OCShareRepository private val localSharesDataSource = mock(LocalSharesDataSource::class.java) private val publicShare = listOf( @@ -101,7 +100,7 @@ class OCShareRepositoryTest { // Retrieving public shares from server... - // Public shares are always retrieved from server and inserted in database if not empty list + // Public shares are retrieved from server and inserted in database if not empty list verify(localSharesDataSource).replaceSharesForFile( remoteShares.map { remoteShare -> OCShare.fromRemoteShare(remoteShare).also { it.accountOwner = "admin@server" } @@ -201,7 +200,7 @@ class OCShareRepositoryTest { // Retrieving public shares from server... - // Public shares are always retrieved from server and inserted in database if not empty list + // Public shares are retrieved from server and inserted in database if not empty list verify(localSharesDataSource).insert( arrayListOf(remoteShares[1]).map { remoteShare -> OCShare.fromRemoteShare(remoteShare).also { it.accountOwner = "admin@server" } @@ -265,7 +264,7 @@ class OCShareRepositoryTest { // Retrieving public shares from server... - // Public shares are always retrieved from server and updated in database + // Public shares are retrieved from server and updated in database verify(localSharesDataSource).update( OCShare.fromRemoteShare(remoteShares[2]).also { it.accountOwner = "admin@server" } ) @@ -277,25 +276,23 @@ class OCShareRepositoryTest { localData.value = publicShare val remoteOperationResult = TestUtil.createRemoteOperationResultMock( - ShareParserResult(arrayListOf(remoteShares[2])), true + ShareParserResult(arrayListOf()), true ) - val data = updateShare(localData, remoteOperationResult) + val data = deleteShare(localData, remoteOperationResult) 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) + // deleting share on server and another one with db shares already updated with just deleted share) verify(localSharesDataSource, times(2)).getSharesForFileAsLiveData( "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK) ) // Retrieving public shares from server... - - // Public shares are always retrieved from server and updated in database - verify(localSharesDataSource).update( - OCShare.fromRemoteShare(remoteShares[2]).also { it.accountOwner = "admin@server" } + verify(localSharesDataSource).deleteShare( + 1 ) } @@ -361,4 +358,17 @@ class OCShareRepositoryTest { false ) } + + private fun deleteShare( + localData: MutableLiveData>, + remoteOperationResult: RemoteOperationResult + ): LiveData>> { + val ocShareRepository = createRepositoryWithData(localData, remoteOperationResult) + + return ocShareRepository.deletePublicShare( + "/Photos/", + "admin@server", + 1 + ) + } } From ef99a025e2bb00e7cc143c9e5352ef6536c28a2d Mon Sep 17 00:00:00 2001 From: davigonz Date: Mon, 20 May 2019 14:04:17 +0200 Subject: [PATCH 18/23] Move filePath and account variables to ShareRepository creation --- .../viewmodel/OCCapabilityViewModel.kt | 1 - .../shares/repository/OCShareRepository.kt | 23 +++++++++---------- .../shares/repository/ShareRepository.kt | 8 ------- .../android/shares/ui/ShareActivity.kt | 6 ++--- .../shares/viewmodel/OCShareViewModel.kt | 14 ++++++----- .../repository/OCShareRepositoryTest.kt | 16 ++++++------- .../shares/viewmodel/OCShareViewModelTest.kt | 21 ++++++----------- 7 files changed, 37 insertions(+), 52 deletions(-) diff --git a/owncloudApp/src/main/java/com/owncloud/android/capabilities/viewmodel/OCCapabilityViewModel.kt b/owncloudApp/src/main/java/com/owncloud/android/capabilities/viewmodel/OCCapabilityViewModel.kt index fc243fd34e1..c6510dd09f1 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/capabilities/viewmodel/OCCapabilityViewModel.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/capabilities/viewmodel/OCCapabilityViewModel.kt @@ -39,7 +39,6 @@ import com.owncloud.android.vo.Resource @OpenForTesting class OCCapabilityViewModel( - var shouldFetchFromNetwork: Boolean = true, val account: Account, val capabilityRepository: CapabilityRepository = OCCapabilityRepository.create( localCapabilitiesDataSource = OCLocalCapabilitiesDataSource(), 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 6f46e803b2d..8ec3fcc8e00 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 @@ -32,24 +32,28 @@ import com.owncloud.android.vo.Resource class OCShareRepository( private val appExecutors: AppExecutors, private val localSharesDataSource: LocalSharesDataSource, - private val remoteSharesDataSource: RemoteSharesDataSource + private val remoteSharesDataSource: RemoteSharesDataSource, + val filePath: String, + val accountName: String ) : ShareRepository { companion object Factory { fun create( appExecutors: AppExecutors = AppExecutors(), localSharesDataSource: LocalSharesDataSource, - remoteSharesDataSource: RemoteSharesDataSource + remoteSharesDataSource: RemoteSharesDataSource, + filePathToShare: String, + accountName: String ): OCShareRepository = OCShareRepository( appExecutors, localSharesDataSource, - remoteSharesDataSource + remoteSharesDataSource, + filePathToShare, + accountName ) } override fun loadSharesForFile( - filePath: String, - accountName: String, shareTypes: List, reshares: Boolean, subfiles: Boolean @@ -72,14 +76,13 @@ class OCShareRepository( override fun loadFromDb(): LiveData> = localSharesDataSource.getSharesForFileAsLiveData(filePath, accountName, shareTypes) - override fun createCall() = remoteSharesDataSource.getSharesForFile(filePath, reshares, subfiles) + override fun createCall() = + remoteSharesDataSource.getSharesForFile(filePath, reshares, subfiles) }.asLiveData() } override fun insertPublicShareForFile( - filePath: String, - accountName: String, permissions: Int, name: String, password: String, @@ -117,8 +120,6 @@ class OCShareRepository( } override fun updatePublicShareForFile( - filePath: String, - accountName: String, remoteId: Long, name: String, password: String?, @@ -157,8 +158,6 @@ class OCShareRepository( } override fun deletePublicShare( - filePath: String, - accountName: String, remoteId: Long ): LiveData>> { return object : NetworkBoundResource, ShareParserResult>(appExecutors) { 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 d5ac55fb291..c920d0665a7 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 @@ -26,16 +26,12 @@ import com.owncloud.android.vo.Resource interface ShareRepository { fun loadSharesForFile( - filePath: String, - accountName: String, shareTypes: List, reshares: Boolean, subfiles: Boolean ): LiveData>> fun insertPublicShareForFile( - filePath: String, - accountName: String, permissions: Int, name: String, password: String, @@ -44,8 +40,6 @@ interface ShareRepository { ): LiveData>> fun updatePublicShareForFile( - filePath: String, - accountName: String, remoteId: Long, name: String, password: String?, @@ -55,8 +49,6 @@ interface ShareRepository { ): LiveData>> fun deletePublicShare( - filePath: String, - accountName: String, remoteId: Long ): 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 138e10d59b5..7d25757af1f 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 @@ -100,15 +100,15 @@ class ShareActivity : FileActivity(), ShareFragmentListener { private val editShareFragment: EditShareFragment? get() = supportFragmentManager.findFragmentByTag(TAG_EDIT_SHARE_FRAGMENT) as EditShareFragment? - var ocShareViewModelFactory: ViewModelProvider.Factory = ViewModelFactory.build { + private var ocShareViewModelFactory: ViewModelProvider.Factory = ViewModelFactory.build { OCShareViewModel( - account!!, file?.remotePath!!, + account!!, listOf(ShareType.PUBLIC_LINK) ) } - var ocCapabilityViewModelFactory: ViewModelProvider.Factory = ViewModelFactory.build { + private var ocCapabilityViewModelFactory: ViewModelProvider.Factory = ViewModelFactory.build { OCCapabilityViewModel( account = account!! ) 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 6bc381a3181..e734a6b8caa 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 @@ -39,8 +39,8 @@ import com.owncloud.android.vo.Resource */ @OpenForTesting class OCShareViewModel( - val account: Account, val filePath: String, + val account: Account, val shareTypes: List, val shareRepository: ShareRepository = OCShareRepository.create( localSharesDataSource = OCLocalSharesDataSource(), @@ -49,12 +49,14 @@ class OCShareViewModel( OwnCloudAccount(account, MainApp.getAppContext()), MainApp.getAppContext() ) - ) + ), + filePathToShare = filePath, + accountName = account.name ) ) : ViewModel() { fun getSharesForFile(): LiveData>> = - shareRepository.loadSharesForFile(filePath, account.name, shareTypes, reshares = true, subfiles = false) + shareRepository.loadSharesForFile(shareTypes, reshares = true, subfiles = false) fun insertPublicShareForFile( permissions: Int, @@ -63,7 +65,7 @@ class OCShareViewModel( expirationTimeInMillis: Long, publicUpload: Boolean ): LiveData>> = shareRepository.insertPublicShareForFile( - filePath, account.name, permissions, name, password, expirationTimeInMillis, publicUpload + permissions, name, password, expirationTimeInMillis, publicUpload ) fun updatePublicShareForFile( @@ -74,10 +76,10 @@ class OCShareViewModel( permissions: Int, publicUpload: Boolean ): LiveData>> = shareRepository.updatePublicShareForFile( - filePath, account.name, remoteId, name, password, expirationDateInMillis, permissions, publicUpload + remoteId, name, password, expirationDateInMillis, permissions, publicUpload ) fun deletePublicShare( remoteId: Long - ): LiveData>> = shareRepository.deletePublicShare(filePath, account.name, remoteId) + ): LiveData>> = shareRepository.deletePublicShare(remoteId) } 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 50ffd4e82f8..fdeea1d9d74 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 @@ -310,7 +310,13 @@ class OCShareRepositoryTest { val remoteSharesDataSource = RemoteSharesDataSourceTest(remoteOperationResult) - return OCShareRepository.create(InstantAppExecutors(), localSharesDataSource, remoteSharesDataSource) + return OCShareRepository.create( + InstantAppExecutors(), + localSharesDataSource, + remoteSharesDataSource, + "/Photos/", + "admin@server" + ) } private fun loadShares( @@ -320,7 +326,7 @@ class OCShareRepositoryTest { val ocShareRepository = createRepositoryWithData(localData, remoteOperationResult) return ocShareRepository.loadSharesForFile( - "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK), reshares = true, subfiles = false + listOf(ShareType.PUBLIC_LINK), reshares = true, subfiles = false ) } @@ -331,8 +337,6 @@ class OCShareRepositoryTest { val ocShareRepository = createRepositoryWithData(localData, remoteOperationResult) return ocShareRepository.insertPublicShareForFile( - "/Photos/", - "admin@server", 1, "Photos folder link 3", "1234", @@ -348,8 +352,6 @@ class OCShareRepositoryTest { val ocShareRepository = createRepositoryWithData(localData, remoteOperationResult) return ocShareRepository.updatePublicShareForFile( - "/Photos/", - "admin@server", 1, "Photos folder link updated", "123456", @@ -366,8 +368,6 @@ class OCShareRepositoryTest { val ocShareRepository = createRepositoryWithData(localData, remoteOperationResult) return ocShareRepository.deletePublicShare( - "/Photos/", - "admin@server", 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 bb4a3774fc9..00d8b887ee2 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 @@ -38,13 +38,14 @@ import org.mockito.Mockito.mock @RunWith(JUnit4::class) class OCShareViewModelTest { - private var testAccount: Account = TestUtil.createAccount("admin@server", "test") - private val publicShareResourcesAsLiveData: MutableLiveData>> = MutableLiveData() - @Rule @JvmField 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() { val publicShares = mutableListOf( @@ -67,15 +68,11 @@ class OCShareViewModelTest { @Test fun loadPublicShares() { - val ocShareRepository = mock(OCShareRepository::class.java) - `when`( ocShareRepository.loadSharesForFile( - "/Photos/image.jpg", - "admin@server", listOf(ShareType.PUBLIC_LINK), - true, - false + reshares = true, + subfiles = false ) ).thenReturn( publicShareResourcesAsLiveData @@ -94,8 +91,6 @@ class OCShareViewModelTest { `when`( ocShareRepository.insertPublicShareForFile( - "/Photos/image.jpg", - "admin@server", 1, "Photos 2 link", "1234", @@ -126,8 +121,6 @@ class OCShareViewModelTest { `when`( ocShareRepository.updatePublicShareForFile( - "/Photos/image.jpg", - "admin@server", 1, "Photos 2 link", "1234", @@ -156,8 +149,8 @@ class OCShareViewModelTest { private fun createOCShareViewModel(ocShareRepository: OCShareRepository): OCShareViewModel = OCShareViewModel( - testAccount, "/Photos/image.jpg", + testAccount, listOf(ShareType.PUBLIC_LINK), ocShareRepository ) From fe8723f3a289935f5cd0d5315f4646f0692467d2 Mon Sep 17 00:00:00 2001 From: davigonz Date: Mon, 20 May 2019 15:08:46 +0200 Subject: [PATCH 19/23] Tests for sharing deletion within OCShareViewmodel and OCShareDao --- .../android/shares/db/OCShareDaoTest.kt | 220 +++++++----------- .../shares/viewmodel/OCShareViewModelTest.kt | 22 ++ 2 files changed, 110 insertions(+), 132 deletions(-) diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/db/OCShareDaoTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/db/OCShareDaoTest.kt index 8057767ba5f..dd15985e901 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/db/OCShareDaoTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/db/OCShareDaoTest.kt @@ -38,12 +38,12 @@ import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) @SmallTest class OCShareDaoTest { - private lateinit var ocShareDao: OCShareDao - @Rule @JvmField val instantExecutorRule = InstantTaskExecutorRule() + private lateinit var ocShareDao: OCShareDao + @Before fun setUp() { val context = InstrumentationRegistry.getInstrumentation().targetContext @@ -61,9 +61,8 @@ class OCShareDaoTest { "/Test/", "admin@server", listOf(ShareType.PUBLIC_LINK.value) ) ) - assertThat(shares, notNullValue()) - assertEquals(shares.size, 0) + assertEquals(0, shares.size) } @Test @@ -96,42 +95,39 @@ class OCShareDaoTest { "/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK.value) ) ) - assertThat(photosFolderShares, notNullValue()) - assertEquals(photosFolderShares.size, 1) - assertEquals(photosFolderShares.get(0).path, "/Photos/") - assertEquals(photosFolderShares.get(0).isFolder, true) - assertEquals(photosFolderShares.get(0).accountOwner, "admin@server") - assertEquals(photosFolderShares.get(0).name, "Photos folder link") - assertEquals(photosFolderShares.get(0).shareLink, "http://server:port/s/1") + assertEquals(1, photosFolderShares.size) + assertEquals("/Photos/", photosFolderShares.get(0).path) + assertEquals(true, photosFolderShares.get(0).isFolder) + assertEquals("admin@server", photosFolderShares.get(0).accountOwner) + assertEquals("Photos folder link", photosFolderShares.get(0).name) + assertEquals("http://server:port/s/1", photosFolderShares.get(0).shareLink) val image1Shares = getValue( ocShareDao.getSharesForFileAsLiveData( "/Photos/image1.jpg", "admin@server", listOf(ShareType.PUBLIC_LINK.value) ) ) - assertThat(image1Shares, notNullValue()) - assertEquals(image1Shares.size, 1) - assertEquals(image1Shares.get(0).path, "/Photos/image1.jpg") - assertEquals(image1Shares.get(0).isFolder, false) - assertEquals(image1Shares.get(0).accountOwner, "admin@server") - assertEquals(image1Shares.get(0).name, "Image 1 link") - assertEquals(image1Shares.get(0).shareLink, "http://server:port/s/2") + assertEquals(1, image1Shares.size) + assertEquals("/Photos/image1.jpg", image1Shares.get(0).path) + assertEquals(false, image1Shares.get(0).isFolder) + assertEquals("admin@server", image1Shares.get(0).accountOwner) + assertEquals("Image 1 link", image1Shares.get(0).name) + assertEquals("http://server:port/s/2", image1Shares.get(0).shareLink) val image2Shares = getValue( ocShareDao.getSharesForFileAsLiveData( "/Photos/image2.jpg", "admin@server", listOf(ShareType.PUBLIC_LINK.value) ) ) - assertThat(image2Shares, notNullValue()) - assertEquals(image2Shares.size, 1) - assertEquals(image2Shares.get(0).path, "/Photos/image2.jpg") - assertEquals(image2Shares.get(0).isFolder, false) - assertEquals(image1Shares.get(0).accountOwner, "admin@server") - assertEquals(image2Shares.get(0).name, "Image 2 link") - assertEquals(image2Shares.get(0).shareLink, "http://server:port/s/3") + assertEquals(1, image2Shares.size) + assertEquals("/Photos/image2.jpg", image2Shares.get(0).path) + assertEquals(false, image2Shares.get(0).isFolder) + assertEquals("admin@server", image1Shares.get(0).accountOwner) + assertEquals("Image 2 link", image2Shares.get(0).name) + assertEquals("http://server:port/s/3", image2Shares.get(0).shareLink) } @Test @@ -167,90 +163,60 @@ class OCShareDaoTest { "/Documents/document1.docx", "user1@server", listOf(ShareType.PUBLIC_LINK.value) ) ) - assertThat(document1SharesForUser1, notNullValue()) - assertEquals(document1SharesForUser1.size, 1) - assertEquals(document1SharesForUser1.get(0).path, "/Documents/document1.docx") - assertEquals(document1SharesForUser1.get(0).isFolder, false) - assertEquals(document1SharesForUser1.get(0).accountOwner, "user1@server") - assertEquals(document1SharesForUser1.get(0).name, "Document 1 link") - assertEquals(document1SharesForUser1.get(0).shareLink, "http://server:port/s/1") + assertEquals(1, document1SharesForUser1.size) + assertEquals("/Documents/document1.docx", document1SharesForUser1.get(0).path) + assertEquals(false, document1SharesForUser1.get(0).isFolder) + assertEquals("user1@server", document1SharesForUser1.get(0).accountOwner) + assertEquals("Document 1 link", document1SharesForUser1.get(0).name) + assertEquals("http://server:port/s/1", document1SharesForUser1.get(0).shareLink) val document1SharesForUser2 = getValue( ocShareDao.getSharesForFileAsLiveData( "/Documents/document1.docx", "user2@server", listOf(ShareType.PUBLIC_LINK.value) ) ) - assertThat(document1SharesForUser2, notNullValue()) - assertEquals(document1SharesForUser2.size, 1) - assertEquals(document1SharesForUser2.get(0).path, "/Documents/document1.docx") - assertEquals(document1SharesForUser2.get(0).isFolder, false) - assertEquals(document1SharesForUser2.get(0).accountOwner, "user2@server") - assertEquals(document1SharesForUser2.get(0).name, "Document 1 link") - assertEquals(document1SharesForUser2.get(0).shareLink, "http://server:port/s/2") + assertEquals(1, document1SharesForUser2.size) + assertEquals("/Documents/document1.docx", document1SharesForUser2.get(0).path) + assertEquals(false, document1SharesForUser2.get(0).isFolder) + assertEquals("user2@server", document1SharesForUser2.get(0).accountOwner) + assertEquals("Document 1 link", document1SharesForUser2.get(0).name) + assertEquals("http://server:port/s/2", document1SharesForUser2.get(0).shareLink) val document1SharesForUser3 = getValue( ocShareDao.getSharesForFileAsLiveData( "/Documents/document1.docx", "user3@server", listOf(ShareType.PUBLIC_LINK.value) ) ) - assertThat(document1SharesForUser3, notNullValue()) - assertEquals(document1SharesForUser3.size, 1) - assertEquals(document1SharesForUser3.get(0).path, "/Documents/document1.docx") - assertEquals(document1SharesForUser3.get(0).isFolder, false) - assertEquals(document1SharesForUser3.get(0).accountOwner, "user3@server") - assertEquals(document1SharesForUser3.get(0).name, "Document 1 link") - assertEquals(document1SharesForUser3.get(0).shareLink, "http://server:port/s/3") + assertEquals(1, document1SharesForUser3.size) + assertEquals("/Documents/document1.docx", document1SharesForUser3.get(0).path) + assertEquals(false, document1SharesForUser3.get(0).isFolder) + assertEquals("user3@server", document1SharesForUser3.get(0).accountOwner) + assertEquals("Document 1 link", document1SharesForUser3.get(0).name) + assertEquals("http://server:port/s/3", document1SharesForUser3.get(0).shareLink) } @Test fun getNonExistingPublicShare() { - ocShareDao.insert( - listOf( - TestUtil.createPublicShare( - path = "/Videos/video1.mp4", - isFolder = false, - accountOwner = "user@server", - name = "Video 1 link", - shareLink = "http://server:port/s/1" - ) - ) - ) + ocShareDao.insert(listOf(newPublicShare())) - val video2Shares = getValue( + val nonExistingShare = getValue( ocShareDao.getSharesForFileAsLiveData( - "/Videos/video2.mp4", "user@server", listOf(ShareType.PUBLIC_LINK.value) + "/Texts/text2.txt", "user@server", listOf(ShareType.PUBLIC_LINK.value) ) ) - - assertThat(video2Shares, notNullValue()) - assertEquals(video2Shares.size, 0) + assertThat(nonExistingShare, notNullValue()) + assertEquals(0, nonExistingShare.size) } @Test fun replacePublicShareIfAlreadyExists_exists() { - ocShareDao.insert( - listOf( - TestUtil.createPublicShare( - path = "/Texts/text1.txt", - isFolder = false, - name = "Text 1 link", - shareLink = "http://server:port/s/1" - ) - ) - ) + ocShareDao.insert(listOf(newPublicShare())) ocShareDao.replaceSharesForFile( - listOf( // Update link name - TestUtil.createPublicShare( - path = "/Texts/text1.txt", - isFolder = false, - name = "Text 2 link", - shareLink = "http://server:port/s/1" - ) - ) + listOf(newPublicShare(name = "Text 2 link")) ) val textShares = getValue( @@ -258,34 +224,17 @@ class OCShareDaoTest { "/Texts/text1.txt", "admin@server", listOf(ShareType.PUBLIC_LINK.value) ) ) - assertThat(textShares, notNullValue()) - assertEquals(textShares.size, 1) - assertEquals(textShares.get(0).name, "Text 2 link") + assertEquals(1, textShares.size) + assertEquals("Text 2 link", textShares.get(0).name) } @Test fun replacePublicShareIfAlreadyExists_doesNotExist() { - ocShareDao.insert( - listOf( - TestUtil.createPublicShare( - path = "/Texts/text1.txt", - isFolder = false, - name = "Text 1 link", - shareLink = "http://server:port/s/1" - ) - ) - ) + ocShareDao.insert(listOf(newPublicShare())) ocShareDao.replaceSharesForFile( - listOf( // New link - TestUtil.createPublicShare( - path = "/Texts/text2.txt", - isFolder = false, - name = "Text 2 link", - shareLink = "http://server:port/s/2" - ) - ) + listOf(newPublicShare(path = "/Texts/text2.txt", name = "Text 2 link")) ) val text1Shares = getValue( @@ -293,10 +242,9 @@ class OCShareDaoTest { "/Texts/text1.txt", "admin@server", listOf(ShareType.PUBLIC_LINK.value) ) ) - assertThat(text1Shares, notNullValue()) - assertEquals(text1Shares.size, 1) - assertEquals(text1Shares.get(0).name, "Text 1 link") + assertEquals(1, text1Shares.size) + assertEquals("Text 1 link", text1Shares.get(0).name) // text2 link didn't exist before, it should not replace the old one but be created val text2Shares = getValue( @@ -304,46 +252,54 @@ class OCShareDaoTest { "/Texts/text2.txt", "admin@server", listOf(ShareType.PUBLIC_LINK.value) ) ) - assertThat(text2Shares, notNullValue()) - assertEquals(text2Shares.size, 1) - assertEquals(text2Shares.get(0).name, "Text 2 link") + assertEquals(1, text2Shares.size) + assertEquals("Text 2 link", text2Shares.get(0).name) } @Test fun updatePublicShare() { - ocShareDao.insert( - listOf( - TestUtil.createPublicShare( - path = "/Images/image.png", - expirationDate = 1000, - isFolder = false, - name = "Image link", - shareLink = "http://server:port/s/1" - ) - ) - ) + ocShareDao.insert(listOf(newPublicShare())) ocShareDao.update( - // Updated link - TestUtil.createPublicShare( - path = "/Images/image.png", - expirationDate = 2000, - isFolder = false, - name = "Image link updated", - shareLink = "http://server:port/s/1" - ) + newPublicShare(name = "Text 1 link updated", expirationDate = 2000) ) - val imageShares = getValue( + val textShares = getValue( ocShareDao.getSharesForFileAsLiveData( - "/Images/image.png", "admin@server", listOf(ShareType.PUBLIC_LINK.value) + "/Texts/text1.txt", "admin@server", listOf(ShareType.PUBLIC_LINK.value) ) ) + assertThat(textShares, notNullValue()) + assertEquals(1, textShares.size) + assertEquals("Text 1 link updated", textShares.get(0).name) + assertEquals(2000, textShares.get(0).expirationDate) + } + + @Test + fun deletePublicShare() { + ocShareDao.insert(listOf(newPublicShare())) + + ocShareDao.deleteShare(1) - assertThat(imageShares, notNullValue()) - assertEquals(imageShares.size, 1) - assertEquals(imageShares.get(0).name, "Image link updated") - assertEquals(imageShares.get(0).expirationDate, 2000) + val textShares = getValue( + ocShareDao.getSharesForFileAsLiveData( + "/Texts/text1.txt", "admin@server", listOf(ShareType.PUBLIC_LINK.value) + ) + ) + assertThat(textShares, notNullValue()) + assertEquals(0, textShares.size) // List of textShares empty after deleting the existing share } + + private fun newPublicShare( + path: String = "/Texts/text1.txt", + expirationDate: Long = 1000, + name: String = "Text 1 link" + ) = TestUtil.createPublicShare( + path = path, + expirationDate = expirationDate, + isFolder = false, + name = name, + shareLink = "http://server:port/s/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 00d8b887ee2..566a91bb635 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 @@ -147,6 +147,28 @@ class OCShareViewModelTest { assertShareParameters(resource?.data) } + @Test + fun deletePublicShare() { + val ocShareRepository = mock(OCShareRepository::class.java) + + `when`( + ocShareRepository.deletePublicShare( + 3 + ) + ).thenReturn( + publicShareResourcesAsLiveData + ) + + // Viewmodel that will ask ocShareRepository for shares + val ocShareViewModel = createOCShareViewModel(ocShareRepository) + + val resource: Resource>? = ocShareViewModel.deletePublicShare( + 3 + ).value + + assertShareParameters(resource?.data) + } + private fun createOCShareViewModel(ocShareRepository: OCShareRepository): OCShareViewModel = OCShareViewModel( "/Photos/image.jpg", From 6e014571941d09a25fbcc2b9e62ffdedf89477d2 Mon Sep 17 00:00:00 2001 From: davigonz Date: Mon, 20 May 2019 15:25:15 +0200 Subject: [PATCH 20/23] Swap expected and actual parameters for every assertEquals call --- .../capabilities/db/OCCapabilityDaoTest.kt | 52 ++++++++----------- .../OCLocalCapabilitiesDataSourceTest.kt | 18 +++---- .../OCRemoteCapabilitiesDataSourceTest.kt | 8 +-- .../viewmodel/OCCapabilityViewModelTest.kt | 8 +-- .../shares/viewmodel/OCShareViewModelTest.kt | 18 +++---- 5 files changed, 48 insertions(+), 56 deletions(-) diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/capabilities/db/OCCapabilityDaoTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/capabilities/db/OCCapabilityDaoTest.kt index c9099f1f799..d5718946d27 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/capabilities/db/OCCapabilityDaoTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/capabilities/db/OCCapabilityDaoTest.kt @@ -24,7 +24,6 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.SmallTest import androidx.test.platform.app.InstrumentationRegistry import com.owncloud.android.db.OwncloudDatabase -import com.owncloud.android.lib.resources.shares.ShareType import com.owncloud.android.lib.resources.status.CapabilityBooleanType import com.owncloud.android.utils.LiveDataTestUtil.getValue import com.owncloud.android.utils.TestUtil @@ -68,13 +67,12 @@ class OCCapabilityDaoTest { "user2@server" ) ) - assertThat(capability, notNullValue()) - assertEquals(capability.accountName, "user2@server") - assertEquals(capability.versionMayor, 6) - assertEquals(capability.versionMinor, 5) - assertEquals(capability.versionMicro, 4) - assertEquals(capability.versionString, "6.0") + assertEquals("user2@server", capability.accountName) + assertEquals(6, capability.versionMayor) + assertEquals(5, capability.versionMinor) + assertEquals(4, capability.versionMicro) + assertEquals("6.0", capability.versionString) } @Test @@ -90,7 +88,6 @@ class OCCapabilityDaoTest { "user@server2" ) ) - assertThat(capability, nullValue()) } @@ -127,14 +124,13 @@ class OCCapabilityDaoTest { "admin@server" ) ) - assertThat(capability, notNullValue()) - assertEquals(capability.accountName, "admin@server") - assertEquals(capability.versionMayor, 3) - assertEquals(capability.versionMinor, 2) - assertEquals(capability.versionMicro, 1) - assertEquals(capability.versionString, "3.7.5") - assertEquals(capability.filesSharingPublicPasswordEnforced, 1) + assertEquals("admin@server", capability.accountName) + assertEquals(3, capability.versionMayor) + assertEquals(2, capability.versionMinor) + assertEquals(1, capability.versionMicro) + assertEquals("3.7.5", capability.versionString) + assertEquals(1, capability.filesSharingPublicPasswordEnforced) } @Test @@ -170,14 +166,13 @@ class OCCapabilityDaoTest { "cto@server" ) ) - assertThat(capability1, notNullValue()) - assertEquals(capability1.accountName, "cto@server") - assertEquals(capability1.versionMayor, 10) - assertEquals(capability1.versionMinor, 8) - assertEquals(capability1.versionMicro, 6) - assertEquals(capability1.versionString, "10.0.2") - assertEquals(capability1.filesSharingPublicPasswordEnforcedReadOnly, CapabilityBooleanType.FALSE.value) + assertEquals("cto@server", capability1.accountName) + assertEquals(10, capability1.versionMayor) + assertEquals(8, capability1.versionMinor) + assertEquals(6, capability1.versionMicro) + assertEquals("10.0.2", capability1.versionString) + assertEquals(CapabilityBooleanType.FALSE.value, capability1.filesSharingPublicPasswordEnforcedReadOnly) // capability2 didn't exist before, it should not replace the old one but got created val capability2 = getValue( @@ -185,13 +180,12 @@ class OCCapabilityDaoTest { "seo@server" ) ) - assertThat(capability2, notNullValue()) - assertEquals(capability2.accountName, "seo@server") - assertEquals(capability2.versionMayor, 14) - assertEquals(capability2.versionMinor, 13) - assertEquals(capability2.versionMicro, 12) - assertEquals(capability2.versionString, "14.3.8") - assertEquals(capability2.filesSharingPublicPasswordEnforcedReadOnly, CapabilityBooleanType.TRUE.value) + assertEquals("seo@server", capability2.accountName) + assertEquals(14, capability2.versionMayor) + assertEquals(13, capability2.versionMinor) + assertEquals(12, capability2.versionMicro) + assertEquals("14.3.8", capability2.versionString) + assertEquals(CapabilityBooleanType.TRUE.value, capability2.filesSharingPublicPasswordEnforcedReadOnly) } } diff --git a/owncloudApp/src/test/java/com/owncloud/android/capabilities/datasource/OCLocalCapabilitiesDataSourceTest.kt b/owncloudApp/src/test/java/com/owncloud/android/capabilities/datasource/OCLocalCapabilitiesDataSourceTest.kt index 3b5c851a5f8..00e288f397f 100644 --- a/owncloudApp/src/test/java/com/owncloud/android/capabilities/datasource/OCLocalCapabilitiesDataSourceTest.kt +++ b/owncloudApp/src/test/java/com/owncloud/android/capabilities/datasource/OCLocalCapabilitiesDataSourceTest.kt @@ -83,11 +83,10 @@ class OCLocalCapabilitiesDataSourceTest { "user@server1" ) ) - - assertEquals(capability.accountName, "user@server1") - assertEquals(capability.versionMayor, 5) - assertEquals(capability.versionMinor, 4) - assertEquals(capability.versionMicro, 3) + assertEquals("user@server1", capability.accountName) + assertEquals(5, capability.versionMayor) + assertEquals(4, capability.versionMinor) + assertEquals(3, capability.versionMicro) } @Test @@ -108,10 +107,9 @@ class OCLocalCapabilitiesDataSourceTest { "user@server2" ) ) - - assertEquals(capability.accountName, "user@server2") - assertEquals(capability.versionMayor, 2) - assertEquals(capability.versionMinor, 1) - assertEquals(capability.versionMicro, 0) + assertEquals("user@server2", capability.accountName) + assertEquals(2, capability.versionMayor) + assertEquals(1, capability.versionMinor) + assertEquals(0, capability.versionMicro) } } diff --git a/owncloudApp/src/test/java/com/owncloud/android/capabilities/datasource/OCRemoteCapabilitiesDataSourceTest.kt b/owncloudApp/src/test/java/com/owncloud/android/capabilities/datasource/OCRemoteCapabilitiesDataSourceTest.kt index 05363554045..45e12f2729a 100644 --- a/owncloudApp/src/test/java/com/owncloud/android/capabilities/datasource/OCRemoteCapabilitiesDataSourceTest.kt +++ b/owncloudApp/src/test/java/com/owncloud/android/capabilities/datasource/OCRemoteCapabilitiesDataSourceTest.kt @@ -67,9 +67,9 @@ class OCRemoteCapabilitiesDataSourceTest { val capability = remoteOperationResult.data - assertEquals(capability.accountName, "ceo@server") - assertEquals(capability.versionMayor, 15) - assertEquals(capability.versionMinor, 14) - assertEquals(capability.versionMicro, 13) + assertEquals("ceo@server", capability.accountName) + assertEquals(15, capability.versionMayor) + assertEquals(14, capability.versionMinor) + assertEquals(13, capability.versionMicro) } } diff --git a/owncloudApp/src/test/java/com/owncloud/android/capabilities/viewmodel/OCCapabilityViewModelTest.kt b/owncloudApp/src/test/java/com/owncloud/android/capabilities/viewmodel/OCCapabilityViewModelTest.kt index d0b7acf7bad..2f162bf4eca 100644 --- a/owncloudApp/src/test/java/com/owncloud/android/capabilities/viewmodel/OCCapabilityViewModelTest.kt +++ b/owncloudApp/src/test/java/com/owncloud/android/capabilities/viewmodel/OCCapabilityViewModelTest.kt @@ -73,9 +73,9 @@ class OCCapabilityViewModelTest { val resource: Resource? = ocCapabilityViewModel.getCapabilityForAccount().value val capability: OCCapability? = resource?.data - assertEquals(capability?.accountName, "admin@server") - assertEquals(capability?.versionMayor, 2) - assertEquals(capability?.versionMinor, 1) - assertEquals(capability?.versionMicro, 0) + assertEquals("admin@server", capability?.accountName) + assertEquals(2, capability?.versionMayor) + assertEquals(1, capability?.versionMinor) + assertEquals(0, capability?.versionMicro) } } 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 566a91bb635..68aff91b32c 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 @@ -178,16 +178,16 @@ class OCShareViewModelTest { ) private fun assertShareParameters(shares: List?) { - assertEquals(shares?.size, 2) + assertEquals(2, shares?.size) - assertEquals(shares?.get(0)?.path, "/Photos/image.jpg") - assertEquals(shares?.get(0)?.isFolder, true) - assertEquals(shares?.get(0)?.name, "Photos 1 link") - assertEquals(shares?.get(0)?.shareLink, "http://server:port/s/1") + assertEquals("/Photos/image.jpg", shares?.get(0)?.path) + assertEquals(true, shares?.get(0)?.isFolder) + assertEquals("Photos 1 link", shares?.get(0)?.name) + assertEquals("http://server:port/s/1", shares?.get(0)?.shareLink) - assertEquals(shares?.get(1)?.path, "/Photos/image.jpg") - assertEquals(shares?.get(1)?.isFolder, false) - assertEquals(shares?.get(1)?.name, "Photos 2 link") - assertEquals(shares?.get(1)?.shareLink, "http://server:port/s/2") + assertEquals("/Photos/image.jpg", shares?.get(1)?.path) + assertEquals(false, shares?.get(1)?.isFolder) + assertEquals("Photos 2 link", shares?.get(1)?.name) + assertEquals("http://server:port/s/2", shares?.get(1)?.shareLink) } } From 4482591b071f57c41b60a0371bbd9c7c4d1179a2 Mon Sep 17 00:00:00 2001 From: davigonz Date: Mon, 27 May 2019 09:33:24 +0200 Subject: [PATCH 21/23] Delete unused list --- .../PublicShareCreationDialogFragmentTest.kt | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareCreationDialogFragmentTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareCreationDialogFragmentTest.kt index 1f6b4cfda8f..81c6011670a 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareCreationDialogFragmentTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareCreationDialogFragmentTest.kt @@ -58,28 +58,6 @@ class PublicShareCreationDialogFragmentTest { private val file = mock(OCFile::class.java) - private val publicShareList = arrayListOf( - TestUtil.createPublicShare( - path = "/Documents/", - isFolder = true, - name = "Document link", - shareLink = "http://server:port/s/1" - ), - TestUtil.createPublicShare( - path = "/Documents/doc1", - isFolder = false, - name = "Document link", - shareLink = "http://server:port/s/2" - ), - TestUtil.createPublicShare( - shareWith = "1fa2P4QAq3412", - path = "/Documents/doc2", - isFolder = false, - name = "Document link 2", - shareLink = "http://server:port/s/3" - ) - ) - @Test fun showDialogTitle() { loadPublicShareDialogFragment() From 3d3669bd412e8e10bb6ecee7db42e3a45ab7a3be Mon Sep 17 00:00:00 2001 From: davigonz Date: Thu, 30 May 2019 13:14:24 +0200 Subject: [PATCH 22/23] Apply CR changes --- .../capabilities/db/OCCapabilityDaoTest.kt | 36 +++++------ .../android/shares/db/OCShareDaoTest.kt | 10 +-- .../PublicShareEditionDialogFragmentTest.kt | 2 +- .../capabilities/db/OCCapabilityDao.kt | 3 + .../android/providers/FileContentProvider.kt | 13 +++- .../datasource/LocalSharesDataSource.kt | 2 + .../datasource/OCLocalSharesDataSource.kt | 2 + .../owncloud/android/shares/db/OCShareDao.kt | 5 +- .../shares/ui/fragment/ShareFileFragment.kt | 6 +- .../ui/adapter/SharePublicLinkListAdapter.kt | 10 +-- .../datasource/OCLocalShareDataSourceTest.kt | 41 ++++++------ .../datasource/OCRemoteShareDataSourceTest.kt | 62 +++++++++---------- 12 files changed, 100 insertions(+), 92 deletions(-) diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/capabilities/db/OCCapabilityDaoTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/capabilities/db/OCCapabilityDaoTest.kt index d5718946d27..ed5ea6bb7bb 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/capabilities/db/OCCapabilityDaoTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/capabilities/db/OCCapabilityDaoTest.kt @@ -78,9 +78,7 @@ class OCCapabilityDaoTest { @Test fun getNonExistingCapabilities() { ocCapabilityDao.insert( - listOf( - TestUtil.createCapability("user@server", 10, 9, 8, "10.1.4") - ) + TestUtil.createCapability("user@server", 10, 9, 8, "10.1.4") ) val capability = getValue( @@ -94,15 +92,13 @@ class OCCapabilityDaoTest { @Test fun replaceCapabilityIfAlreadyExists_exists() { ocCapabilityDao.insert( - listOf( - TestUtil.createCapability( - "admin@server", - 3, - 2, - 1, - "3.7.5", - sharingPublicPasswordEnforced = CapabilityBooleanType.FALSE.value - ) + TestUtil.createCapability( + "admin@server", + 3, + 2, + 1, + "3.7.5", + sharingPublicPasswordEnforced = CapabilityBooleanType.FALSE.value ) ) @@ -136,15 +132,13 @@ class OCCapabilityDaoTest { @Test fun replacePublicShareIfAlreadyExists_doesNotExist() { ocCapabilityDao.insert( - listOf( - TestUtil.createCapability( - "cto@server", - 10, - 8, - 6, - "10.0.2", - sharingPublicPasswordEnforcedReadOnly = CapabilityBooleanType.FALSE.value - ) + TestUtil.createCapability( + "cto@server", + 10, + 8, + 6, + "10.0.2", + sharingPublicPasswordEnforcedReadOnly = CapabilityBooleanType.FALSE.value ) ) diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/db/OCShareDaoTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/db/OCShareDaoTest.kt index dd15985e901..a6622797c90 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/db/OCShareDaoTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/db/OCShareDaoTest.kt @@ -200,7 +200,7 @@ class OCShareDaoTest { @Test fun getNonExistingPublicShare() { - ocShareDao.insert(listOf(newPublicShare())) + ocShareDao.insert(newPublicShare()) val nonExistingShare = getValue( ocShareDao.getSharesForFileAsLiveData( @@ -213,7 +213,7 @@ class OCShareDaoTest { @Test fun replacePublicShareIfAlreadyExists_exists() { - ocShareDao.insert(listOf(newPublicShare())) + ocShareDao.insert(newPublicShare()) ocShareDao.replaceSharesForFile( listOf(newPublicShare(name = "Text 2 link")) @@ -231,7 +231,7 @@ class OCShareDaoTest { @Test fun replacePublicShareIfAlreadyExists_doesNotExist() { - ocShareDao.insert(listOf(newPublicShare())) + ocShareDao.insert(newPublicShare()) ocShareDao.replaceSharesForFile( listOf(newPublicShare(path = "/Texts/text2.txt", name = "Text 2 link")) @@ -259,7 +259,7 @@ class OCShareDaoTest { @Test fun updatePublicShare() { - ocShareDao.insert(listOf(newPublicShare())) + ocShareDao.insert(newPublicShare()) ocShareDao.update( newPublicShare(name = "Text 1 link updated", expirationDate = 2000) @@ -278,7 +278,7 @@ class OCShareDaoTest { @Test fun deletePublicShare() { - ocShareDao.insert(listOf(newPublicShare())) + ocShareDao.insert(newPublicShare()) ocShareDao.deleteShare(1) diff --git a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareEditionDialogFragmentTest.kt b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareEditionDialogFragmentTest.kt index 1425de53e23..4321b58b644 100644 --- a/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareEditionDialogFragmentTest.kt +++ b/owncloudApp/src/androidTest/java/com/owncloud/android/shares/ui/PublicShareEditionDialogFragmentTest.kt @@ -54,7 +54,7 @@ class PublicShareEditionDialogFragmentTest { private val file = mock(OCFile::class.java) private val publicShare = mock(OCShare::class.java) - private val expirationDate = 1556575200000 + private val expirationDate = 1556575200000 // GMT: Monday, April 29, 2019 10:00:00 PM @Before fun setUp() { diff --git a/owncloudApp/src/main/java/com/owncloud/android/capabilities/db/OCCapabilityDao.kt b/owncloudApp/src/main/java/com/owncloud/android/capabilities/db/OCCapabilityDao.kt index e051de97bba..ba588aa454e 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/capabilities/db/OCCapabilityDao.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/capabilities/db/OCCapabilityDao.kt @@ -37,6 +37,9 @@ abstract class OCCapabilityDao { accountName: String ): LiveData + @Insert(onConflict = OnConflictStrategy.REPLACE) + abstract fun insert(ocCapability: OCCapability): Long + @Insert(onConflict = OnConflictStrategy.REPLACE) abstract fun insert(ocCapabilities: List): List diff --git a/owncloudApp/src/main/java/com/owncloud/android/providers/FileContentProvider.kt b/owncloudApp/src/main/java/com/owncloud/android/providers/FileContentProvider.kt index 2b3275f21e2..a6ad66f9211 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/providers/FileContentProvider.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/providers/FileContentProvider.kt @@ -25,7 +25,14 @@ package com.owncloud.android.providers import android.accounts.AccountManager -import android.content.* +import android.content.ContentProvider +import android.content.ContentProviderOperation +import android.content.ContentProviderResult +import android.content.ContentUris +import android.content.ContentValues +import android.content.Context +import android.content.OperationApplicationException +import android.content.UriMatcher import android.database.Cursor import android.database.SQLException import android.database.sqlite.SQLiteDatabase @@ -211,8 +218,8 @@ class FileContentProvider(val appExecutors: AppExecutors = AppExecutors()) : Con } SHARES -> { val shareId = OwncloudDatabase.getDatabase(context).shareDao().insert( - listOf(OCShare.fromContentValues(values)) - )[0] + OCShare.fromContentValues(values) + ) if (shareId <= 0) throw SQLException("ERROR $uri") return ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, shareId) diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/LocalSharesDataSource.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/LocalSharesDataSource.kt index 8a1f82217b7..e968db91ea3 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/LocalSharesDataSource.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/LocalSharesDataSource.kt @@ -30,6 +30,8 @@ interface LocalSharesDataSource { shareTypes: List ): LiveData> + fun insert(ocShare: OCShare): Long + fun insert(ocShares: List): List fun update(ocShare: OCShare): Long diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCLocalSharesDataSource.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCLocalSharesDataSource.kt index ee84556feb2..c2c1b3a1b49 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCLocalSharesDataSource.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/datasource/OCLocalSharesDataSource.kt @@ -37,6 +37,8 @@ class OCLocalSharesDataSource( ): LiveData> = ocShareDao.getSharesForFileAsLiveData(filePath, accountName, shareTypes.map { it.value }) + override fun insert(ocShare: OCShare): Long = ocShareDao.insert(ocShare) + override fun insert(ocShares: List): List = ocShareDao.insert(ocShares) override fun update(ocShare: OCShare): Long = ocShareDao.update(ocShare) diff --git a/owncloudApp/src/main/java/com/owncloud/android/shares/db/OCShareDao.kt b/owncloudApp/src/main/java/com/owncloud/android/shares/db/OCShareDao.kt index ace4f76bc09..ed4109f7aef 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/shares/db/OCShareDao.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/shares/db/OCShareDao.kt @@ -39,13 +39,16 @@ abstract class OCShareDao { filePath: String, accountOwner: String, shareTypes: List ): LiveData> + @Insert(onConflict = OnConflictStrategy.REPLACE) + abstract fun insert(ocShare: OCShare): Long + @Insert(onConflict = OnConflictStrategy.REPLACE) abstract fun insert(ocShares: List): List @Transaction open fun update(ocShare: OCShare): Long { deleteShare(ocShare.remoteId) - return insert(listOf(ocShare))[0] + return insert(ocShare) } @Transaction 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 d3d424f1c0f..f99da3c1bc5 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 @@ -412,11 +412,11 @@ class ShareFileFragment : Fragment(), ShareUserListAdapter.ShareUserAdapterListe } if (!enableMultiplePublicSharing()) { - if (publicLinks?.size == 0) { + if (publicLinks?.isNullOrEmpty() == true) { addPublicLinkButton.visibility = View.VISIBLE - } else if (publicLinks?.size!! >= 1) { - addPublicLinkButton.visibility = View.INVISIBLE + return } + addPublicLinkButton.visibility = View.INVISIBLE } } diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/adapter/SharePublicLinkListAdapter.kt b/owncloudApp/src/main/java/com/owncloud/android/ui/adapter/SharePublicLinkListAdapter.kt index f7355de8cac..56bdbad77a8 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/adapter/SharePublicLinkListAdapter.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/adapter/SharePublicLinkListAdapter.kt @@ -38,13 +38,13 @@ import java.util.ArrayList * Adapter to show a list of public links */ class SharePublicLinkListAdapter( - private val mContext: Context, resource: Int, private val mPublicLinks: ArrayList?, + private val mContext: Context, + resource: Int, + private val mPublicLinks: ArrayList?, private val mListener: SharePublicLinkAdapterListener ) : ArrayAdapter(mContext, resource) { - override fun getCount(): Int { - return mPublicLinks!!.size - } + override fun getCount(): Int = mPublicLinks?.size ?: 0 override fun getItem(position: Int): OCShare? = mPublicLinks!![position] @@ -61,7 +61,7 @@ class SharePublicLinkListAdapter( val share = mPublicLinks[position] // If there's no name, set the token as name - view.publicLinkName.text = if (share.name?.isEmpty()!!) share.token else share.name + view.publicLinkName.text = if (share.name?.isEmpty() == true) share.token else share.name // bind listener to get link view.getPublicLinkButton.setOnClickListener { mListener.copyOrSendPublicLink(mPublicLinks[position]) } diff --git a/owncloudApp/src/test/java/com/owncloud/android/shares/datasource/OCLocalShareDataSourceTest.kt b/owncloudApp/src/test/java/com/owncloud/android/shares/datasource/OCLocalShareDataSourceTest.kt index a21192174aa..17677685854 100644 --- a/owncloudApp/src/test/java/com/owncloud/android/shares/datasource/OCLocalShareDataSourceTest.kt +++ b/owncloudApp/src/test/java/com/owncloud/android/shares/datasource/OCLocalShareDataSourceTest.kt @@ -93,10 +93,10 @@ class OCLocalDataSourceTest { `when`( ocSharesDao.insert( - listOf(sharesAsLiveData.value!![0]) + sharesAsLiveData.value!![0] ) ).thenReturn( - listOf(7) + 7 ) `when`( @@ -126,33 +126,30 @@ class OCLocalDataSourceTest { ) ) - assertEquals(shares.size, 2) + assertEquals(2, shares.size) - assertEquals(shares.get(0).path, "/Photos/") - assertEquals(shares.get(0).isFolder, true) - assertEquals(shares.get(0).name, "Photos link") - assertEquals(shares.get(0).shareLink, "http://server:port/s/1") + assertEquals("/Photos/", shares.get(0).path) + assertEquals(true, shares.get(0).isFolder) + assertEquals("Photos link", shares.get(0).name) + assertEquals("http://server:port/s/1", shares.get(0).shareLink) - assertEquals(shares.get(1).path, "/Photos/image.jpg") - assertEquals(shares.get(1).isFolder, false) - assertEquals(shares.get(1).name, "Image link") - assertEquals(shares.get(1).shareLink, "http://server:port/s/2") + assertEquals("/Photos/image.jpg", shares.get(1).path) + assertEquals(false, shares.get(1).isFolder) + assertEquals("Image link", shares.get(1).name) + assertEquals("http://server:port/s/2", shares.get(1).shareLink) } @Test fun insertPublicShares() { val insertedShareId = ocLocalSharesDataSource.insert( - listOf( - TestUtil.createPublicShare( - path = "/Photos/", - isFolder = true, - name = "Photos link", - shareLink = "http://server:port/s/1" - ) + TestUtil.createPublicShare( + path = "/Photos/", + isFolder = true, + name = "Photos link", + shareLink = "http://server:port/s/1" ) ) - assertEquals(insertedShareId.size, 1) - assertEquals(insertedShareId[0], 7) + assertEquals(7, insertedShareId) } @Test @@ -165,7 +162,7 @@ class OCLocalDataSourceTest { shareLink = "http://server:port/s/2" ) ) - assertEquals(updatedShareId, 8) + assertEquals(8, updatedShareId) } @Test @@ -173,6 +170,6 @@ class OCLocalDataSourceTest { val deletedRows = ocLocalSharesDataSource.deleteShare( 5 ) - assertEquals(deletedRows, 1) + assertEquals(1, deletedRows) } } diff --git a/owncloudApp/src/test/java/com/owncloud/android/shares/datasource/OCRemoteShareDataSourceTest.kt b/owncloudApp/src/test/java/com/owncloud/android/shares/datasource/OCRemoteShareDataSourceTest.kt index fbd2f1a66ee..c546f564125 100644 --- a/owncloudApp/src/test/java/com/owncloud/android/shares/datasource/OCRemoteShareDataSourceTest.kt +++ b/owncloudApp/src/test/java/com/owncloud/android/shares/datasource/OCRemoteShareDataSourceTest.kt @@ -89,28 +89,28 @@ class OCRemoteShareDataSourceTest { ) assertThat(remoteOperationResult, notNullValue()) - assertEquals(remoteOperationResult.data.shares.size, 3) + assertEquals(3, remoteOperationResult.data.shares.size) val publicShare1 = remoteOperationResult.data.shares.get(0) - assertEquals(publicShare1.path, "/Photos/") - assertEquals(publicShare1.isFolder, true) - assertEquals(publicShare1.name, "Photos folder link") - assertEquals(publicShare1.shareLink, "http://server:port/s/1") + assertEquals("/Photos/", publicShare1.path) + assertEquals(true, publicShare1.isFolder) + assertEquals("Photos folder link", publicShare1.name) + assertEquals("http://server:port/s/1", publicShare1.shareLink) val publicShare2 = remoteOperationResult.data.shares.get(1) - assertEquals(publicShare2.path, "/Photos/image1.jpg") - assertEquals(publicShare2.isFolder, false) - assertEquals(publicShare2.name, "Image 1 link") - assertEquals(publicShare2.shareLink, "http://server:port/s/2") + assertEquals("/Photos/image1.jpg", publicShare2.path) + assertEquals(false, publicShare2.isFolder) + assertEquals("Image 1 link", publicShare2.name) + assertEquals("http://server:port/s/2", publicShare2.shareLink) val publicShare3 = remoteOperationResult.data.shares.get(2) - assertEquals(publicShare3.path, "/Photos/image2.jpg") - assertEquals(publicShare3.isFolder, false) - assertEquals(publicShare3.name, "Image 2 link") - assertEquals(publicShare3.shareLink, "http://server:port/s/3") + assertEquals("/Photos/image2.jpg", publicShare3.path) + assertEquals(false, publicShare3.isFolder) + assertEquals("Image 2 link", publicShare3.name) + assertEquals("http://server:port/s/3", publicShare3.shareLink) } @Test @@ -149,16 +149,16 @@ class OCRemoteShareDataSourceTest { ) assertThat(remoteOperationResult, notNullValue()) - assertEquals(remoteOperationResult.data.shares.size, 1) + assertEquals(1, remoteOperationResult.data.shares.size) val publicShareAdded = remoteOperationResult.data.shares.get(0) - assertEquals(publicShareAdded.shareWith, "") - assertEquals(publicShareAdded.permissions, 1) - assertEquals(publicShareAdded.name, "img1 link") - assertEquals(publicShareAdded.path, "Photos/img1.png") - assertEquals(publicShareAdded.isFolder, false) - assertEquals(publicShareAdded.shareLink, "http://server:port/s/112ejbhdasyd1") + assertEquals("", publicShareAdded.shareWith) + assertEquals(1, publicShareAdded.permissions) + assertEquals("img1 link", publicShareAdded.name) + assertEquals("Photos/img1.png", publicShareAdded.path) + assertEquals(false, publicShareAdded.isFolder) + assertEquals("http://server:port/s/112ejbhdasyd1", publicShareAdded.shareLink) } @Test @@ -191,9 +191,9 @@ class OCRemoteShareDataSourceTest { val publicSharesAdded = remoteOperationResult.data - assertEquals(publicSharesAdded.shares.size, 0) - assertEquals(remoteOperationResult.code, RemoteOperationResult.ResultCode.SHARE_NOT_FOUND) - assertEquals(remoteOperationResult.httpPhrase, httpPhrase) + assertEquals(0, publicSharesAdded.shares.size) + assertEquals(RemoteOperationResult.ResultCode.SHARE_NOT_FOUND, remoteOperationResult.code) + assertEquals(httpPhrase, remoteOperationResult.httpPhrase) } @Test @@ -232,16 +232,16 @@ class OCRemoteShareDataSourceTest { ) assertThat(remoteOperationResult, notNullValue()) - assertEquals(remoteOperationResult.data.shares.size, 1) + assertEquals(1, remoteOperationResult.data.shares.size) val publicShareUpdated = remoteOperationResult.data.shares[0] - assertEquals(publicShareUpdated.name, "video1 link updated") - assertEquals(publicShareUpdated.path, "Videos/video1.mp4") - assertEquals(publicShareUpdated.isFolder, false) - assertEquals(publicShareUpdated.expirationDate, 2000) - assertEquals(publicShareUpdated.permissions, 1) - assertEquals(publicShareUpdated.shareLink, "http://server:port/s/1275farv") + assertEquals("video1 link updated", publicShareUpdated.name) + assertEquals("Videos/video1.mp4", publicShareUpdated.path) + assertEquals(false, publicShareUpdated.isFolder) + assertEquals(2000, publicShareUpdated.expirationDate) + assertEquals(1, publicShareUpdated.permissions) + assertEquals("http://server:port/s/1275farv", publicShareUpdated.shareLink) } @Test @@ -260,6 +260,6 @@ class OCRemoteShareDataSourceTest { val remoteOperationResult = ocRemoteSharesDataSource.deleteShare(1, removeRemoteShareOperation) assertThat(remoteOperationResult, notNullValue()) - assertEquals(remoteOperationResult.isSuccess, true) + assertEquals(true, remoteOperationResult.isSuccess) } } From 27fe03caa4d8443bebf8167ac11f1dcb9ff62b25 Mon Sep 17 00:00:00 2001 From: davigonz Date: Thu, 30 May 2019 14:05:43 +0200 Subject: [PATCH 23/23] Apply additional changes requested in CR --- .../owncloud/android/ui/adapter/SharePublicLinkListAdapter.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/adapter/SharePublicLinkListAdapter.kt b/owncloudApp/src/main/java/com/owncloud/android/ui/adapter/SharePublicLinkListAdapter.kt index 56bdbad77a8..535a2d5cd57 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/adapter/SharePublicLinkListAdapter.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/adapter/SharePublicLinkListAdapter.kt @@ -61,7 +61,7 @@ class SharePublicLinkListAdapter( val share = mPublicLinks[position] // If there's no name, set the token as name - view.publicLinkName.text = if (share.name?.isEmpty() == true) share.token else share.name + view.publicLinkName.text = if (share.name.isNullOrEmpty()) share.token else share.name // bind listener to get link view.getPublicLinkButton.setOnClickListener { mListener.copyOrSendPublicLink(mPublicLinks[position]) }