|
| 1 | +/* |
| 2 | + * Nextcloud Android client application |
| 3 | + * |
| 4 | + * @author Álvaro Brey Vilas |
| 5 | + * Copyright (C) 2022 Álvaro Brey Vilas |
| 6 | + * Copyright (C) 2022 Nextcloud GmbH |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation, either version 3 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License |
| 19 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | + */ |
| 21 | +package com.nmc.android |
| 22 | + |
| 23 | +import androidx.test.core.app.launchActivity |
| 24 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 25 | +import com.nextcloud.client.account.User |
| 26 | +import com.nextcloud.test.TestActivity |
| 27 | +import com.nextcloud.utils.EditorUtils |
| 28 | +import com.owncloud.android.AbstractIT |
| 29 | +import com.owncloud.android.R |
| 30 | +import com.owncloud.android.datamodel.ArbitraryDataProvider |
| 31 | +import com.owncloud.android.datamodel.FileDataStorageManager |
| 32 | +import com.owncloud.android.datamodel.OCFile |
| 33 | +import com.owncloud.android.files.FileMenuFilter |
| 34 | +import com.owncloud.android.files.services.FileDownloader |
| 35 | +import com.owncloud.android.files.services.FileUploader |
| 36 | +import com.owncloud.android.lib.resources.status.CapabilityBooleanType |
| 37 | +import com.owncloud.android.lib.resources.status.OCCapability |
| 38 | +import com.owncloud.android.services.OperationsService |
| 39 | +import com.owncloud.android.ui.activity.ComponentsGetter |
| 40 | +import com.owncloud.android.utils.MimeType |
| 41 | +import io.mockk.MockKAnnotations |
| 42 | +import io.mockk.every |
| 43 | +import io.mockk.impl.annotations.MockK |
| 44 | +import org.junit.Assert.assertFalse |
| 45 | +import org.junit.Assert.assertTrue |
| 46 | +import org.junit.Before |
| 47 | +import org.junit.Test |
| 48 | +import org.junit.runner.RunWith |
| 49 | +import java.security.SecureRandom |
| 50 | + |
| 51 | +@RunWith(AndroidJUnit4::class) |
| 52 | +class FileMenuFilterIT : AbstractIT() { |
| 53 | + |
| 54 | + @MockK |
| 55 | + private lateinit var mockComponentsGetter: ComponentsGetter |
| 56 | + |
| 57 | + @MockK |
| 58 | + private lateinit var mockStorageManager: FileDataStorageManager |
| 59 | + |
| 60 | + @MockK |
| 61 | + private lateinit var mockFileUploaderBinder: FileUploader.FileUploaderBinder |
| 62 | + |
| 63 | + @MockK |
| 64 | + private lateinit var mockFileDownloaderBinder: FileDownloader.FileDownloaderBinder |
| 65 | + |
| 66 | + @MockK |
| 67 | + private lateinit var mockOperationsServiceBinder: OperationsService.OperationsServiceBinder |
| 68 | + |
| 69 | + @MockK |
| 70 | + private lateinit var mockArbitraryDataProvider: ArbitraryDataProvider |
| 71 | + |
| 72 | + private lateinit var editorUtils: EditorUtils |
| 73 | + |
| 74 | + @Before |
| 75 | + fun setup() { |
| 76 | + MockKAnnotations.init(this) |
| 77 | + every { mockFileUploaderBinder.isUploading(any(), any()) } returns false |
| 78 | + every { mockComponentsGetter.fileUploaderBinder } returns mockFileUploaderBinder |
| 79 | + every { mockFileDownloaderBinder.isDownloading(any(), any()) } returns false |
| 80 | + every { mockComponentsGetter.fileDownloaderBinder } returns mockFileDownloaderBinder |
| 81 | + every { mockOperationsServiceBinder.isSynchronizing(any(), any()) } returns false |
| 82 | + every { mockComponentsGetter.operationsServiceBinder } returns mockOperationsServiceBinder |
| 83 | + every { mockStorageManager.getFileById(any()) } returns OCFile("/") |
| 84 | + every { mockStorageManager.getFolderContent(any(), any()) } returns ArrayList<OCFile>() |
| 85 | + every { mockArbitraryDataProvider.getValue(any<User>(), any()) } returns "" |
| 86 | + editorUtils = EditorUtils(mockArbitraryDataProvider) |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun hide_shareAndFavouriteMenu_encryptedFolder() { |
| 91 | + val capability = OCCapability().apply { |
| 92 | + endToEndEncryption = CapabilityBooleanType.TRUE |
| 93 | + } |
| 94 | + |
| 95 | + val encryptedFolder = OCFile("/encryptedFolder/").apply { |
| 96 | + isEncrypted = true |
| 97 | + mimeType = MimeType.DIRECTORY |
| 98 | + fileLength = SecureRandom().nextLong() |
| 99 | + } |
| 100 | + |
| 101 | + configureCapability(capability) |
| 102 | + |
| 103 | + launchActivity<TestActivity>().use { |
| 104 | + it.onActivity { activity -> |
| 105 | + val filterFactory = |
| 106 | + FileMenuFilter.Factory(mockStorageManager, activity, editorUtils) |
| 107 | + |
| 108 | + val sut = filterFactory.newInstance(encryptedFolder, mockComponentsGetter, true, user) |
| 109 | + val toHide = sut.getToHide(false) |
| 110 | + |
| 111 | + // encrypted folder |
| 112 | + assertTrue(toHide.contains(R.id.action_see_details)) |
| 113 | + assertTrue(toHide.contains(R.id.action_favorite)) |
| 114 | + assertTrue(toHide.contains(R.id.action_unset_favorite)) |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + fun show_shareAndFavouriteMenu_normalFolder() { |
| 121 | + val capability = OCCapability().apply { |
| 122 | + endToEndEncryption = CapabilityBooleanType.TRUE |
| 123 | + } |
| 124 | + |
| 125 | + val normalFolder = OCFile("/folder/").apply { |
| 126 | + mimeType = MimeType.DIRECTORY |
| 127 | + fileLength = SecureRandom().nextLong() |
| 128 | + } |
| 129 | + |
| 130 | + configureCapability(capability) |
| 131 | + |
| 132 | + launchActivity<TestActivity>().use { |
| 133 | + it.onActivity { activity -> |
| 134 | + val filterFactory = |
| 135 | + FileMenuFilter.Factory(mockStorageManager, activity, editorUtils) |
| 136 | + |
| 137 | + val sut = filterFactory.newInstance(normalFolder, mockComponentsGetter, true, user) |
| 138 | + val toHide = sut.getToHide(false) |
| 139 | + |
| 140 | + // normal folder |
| 141 | + assertFalse(toHide.contains(R.id.action_see_details)) |
| 142 | + assertFalse(toHide.contains(R.id.action_favorite)) |
| 143 | + assertTrue(toHide.contains(R.id.action_unset_favorite)) |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private fun configureCapability(capability: OCCapability) { |
| 149 | + every { mockStorageManager.getCapability(any<User>()) } returns capability |
| 150 | + every { mockStorageManager.getCapability(any<String>()) } returns capability |
| 151 | + } |
| 152 | +} |
0 commit comments