|
| 1 | +/* |
| 2 | + * Nextcloud - Android Client |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2026 Raphael Vieira raphaelecv.projects@gmail.com |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only |
| 6 | + */ |
| 7 | + |
| 8 | +package com.nextcloud.client.jobs.upload |
| 9 | + |
| 10 | +import com.nextcloud.client.account.User |
| 11 | +import com.nextcloud.client.di.AppComponent |
| 12 | +import com.owncloud.android.MainApp |
| 13 | +import com.owncloud.android.datamodel.OCFile |
| 14 | +import com.owncloud.android.db.OCUpload |
| 15 | +import com.owncloud.android.operations.UploadFileOperation |
| 16 | +import io.mockk.every |
| 17 | +import io.mockk.mockk |
| 18 | +import io.mockk.mockkStatic |
| 19 | +import io.mockk.unmockkAll |
| 20 | +import org.junit.After |
| 21 | +import org.junit.Assert.assertFalse |
| 22 | +import org.junit.Assert.assertTrue |
| 23 | +import org.junit.Before |
| 24 | +import org.junit.Test |
| 25 | + |
| 26 | +class FileUploadHelperTest { |
| 27 | + |
| 28 | + private lateinit var fileUploadHelper: FileUploadHelper |
| 29 | + |
| 30 | + @Before |
| 31 | + fun setUp() { |
| 32 | + mockkStatic(MainApp::class) |
| 33 | + val appComponent = mockk<AppComponent>(relaxed = true) |
| 34 | + every { MainApp.getAppComponent() } returns appComponent |
| 35 | + |
| 36 | + fileUploadHelper = FileUploadHelper() |
| 37 | + FileUploadWorker.activeUploadFileOperations.clear() |
| 38 | + } |
| 39 | + |
| 40 | + @After |
| 41 | + fun tearDown() { |
| 42 | + unmockkAll() |
| 43 | + FileUploadWorker.activeUploadFileOperations.clear() |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `isUploadingNow returns false for null upload`() { |
| 48 | + assertFalse(fileUploadHelper.isUploadingNow(null)) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `isUploadingNow returns false when no active operations`() { |
| 53 | + val upload = mockk<OCUpload>() |
| 54 | + every { upload.accountName } returns "account" |
| 55 | + every { upload.remotePath } returns "/file.txt" |
| 56 | + |
| 57 | + assertFalse(fileUploadHelper.isUploadingNow(upload)) |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun `isUploadingNow returns true when remotePath matches`() { |
| 62 | + val accountName = "account" |
| 63 | + val remotePath = "/file.txt" |
| 64 | + |
| 65 | + val upload = mockk<OCUpload>() |
| 66 | + every { upload.accountName } returns accountName |
| 67 | + every { upload.remotePath } returns remotePath |
| 68 | + |
| 69 | + val operation = mockk<UploadFileOperation>() |
| 70 | + val user = mockk<User>() |
| 71 | + every { user.accountName } returns accountName |
| 72 | + every { operation.user } returns user |
| 73 | + every { operation.remotePath } returns remotePath |
| 74 | + every { operation.oldFile } returns null |
| 75 | + |
| 76 | + FileUploadWorker.activeUploadFileOperations["key"] = operation |
| 77 | + |
| 78 | + assertTrue(fileUploadHelper.isUploadingNow(upload)) |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + fun `isUploadingNow returns true when old remotePath matches`() { |
| 83 | + val accountName = "account" |
| 84 | + val remotePath = "/file_renamed.txt" |
| 85 | + val oldRemotePath = "/file.txt" |
| 86 | + |
| 87 | + val upload = mockk<OCUpload>() |
| 88 | + every { upload.accountName } returns accountName |
| 89 | + every { upload.remotePath } returns oldRemotePath |
| 90 | + |
| 91 | + val operation = mockk<UploadFileOperation>() |
| 92 | + val user = mockk<User>() |
| 93 | + every { user.accountName } returns accountName |
| 94 | + every { operation.user } returns user |
| 95 | + every { operation.remotePath } returns remotePath |
| 96 | + |
| 97 | + val oldFile = mockk<OCFile>() |
| 98 | + every { oldFile.remotePath } returns oldRemotePath |
| 99 | + every { operation.oldFile } returns oldFile |
| 100 | + |
| 101 | + FileUploadWorker.activeUploadFileOperations["key"] = operation |
| 102 | + |
| 103 | + assertTrue(fileUploadHelper.isUploadingNow(upload)) |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + fun `isUploadingNow returns false when accountName does not match`() { |
| 108 | + val remotePath = "/file.txt" |
| 109 | + |
| 110 | + val upload = mockk<OCUpload>() |
| 111 | + every { upload.accountName } returns "account1" |
| 112 | + every { upload.remotePath } returns remotePath |
| 113 | + |
| 114 | + val operation = mockk<UploadFileOperation>() |
| 115 | + val user = mockk<User>() |
| 116 | + every { user.accountName } returns "account2" |
| 117 | + every { operation.user } returns user |
| 118 | + every { operation.remotePath } returns remotePath |
| 119 | + every { operation.oldFile } returns null |
| 120 | + |
| 121 | + FileUploadWorker.activeUploadFileOperations["key"] = operation |
| 122 | + |
| 123 | + assertFalse(fileUploadHelper.isUploadingNow(upload)) |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + fun `isUploadingNow returns false when paths do not match`() { |
| 128 | + val accountName = "account" |
| 129 | + |
| 130 | + val upload = mockk<OCUpload>() |
| 131 | + every { upload.accountName } returns accountName |
| 132 | + every { upload.remotePath } returns "/other.txt" |
| 133 | + |
| 134 | + val operation = mockk<UploadFileOperation>() |
| 135 | + val user = mockk<User>() |
| 136 | + every { user.accountName } returns accountName |
| 137 | + every { operation.user } returns user |
| 138 | + every { operation.remotePath } returns "/file.txt" |
| 139 | + every { operation.oldFile } returns null |
| 140 | + |
| 141 | + FileUploadWorker.activeUploadFileOperations["key"] = operation |
| 142 | + |
| 143 | + assertFalse(fileUploadHelper.isUploadingNow(upload)) |
| 144 | + } |
| 145 | +} |
0 commit comments