Skip to content

Commit 839077f

Browse files
committed
wip
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 22e12ff commit 839077f

6 files changed

Lines changed: 177 additions & 79 deletions

File tree

app/src/main/java/com/nextcloud/utils/extensions/FileDataStorageManagerExtensions.kt

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,18 @@ fun FileDataStorageManager.getNonEncryptedSubfolders(id: Long, accountName: Stri
6464
suspend fun FileDataStorageManager.getCapabilitiesByAccountName(accountName: String): OCCapability =
6565
capabilityDao.getByAccountName(accountName).toOCCapability()
6666

67+
@Suppress("ReturnCount")
6768
fun FileDataStorageManager.moveFiles(ocFile: OCFile?, targetPath: String, targetParentPath: String) {
6869
Log_OC.d(
6970
FileDataStorageManager.TAG,
70-
("moveLocalFile ==> ocFile: "
71-
+ (ocFile?.remotePath)
72-
+ " targetPath: "
73-
+ targetPath
74-
+ " targetParentPath: "
75-
+ targetParentPath)
71+
(
72+
"moveLocalFile ==> ocFile: " +
73+
(ocFile?.remotePath) +
74+
" targetPath: " +
75+
targetPath +
76+
" targetParentPath: " +
77+
targetParentPath
78+
)
7679
)
7780

7881
if (ocFile == null) {
@@ -113,7 +116,8 @@ fun FileDataStorageManager.moveFiles(ocFile: OCFile?, targetPath: String, target
113116
val originalMediaPaths =
114117
fileDao.moveFilesInDb(oldPath, targetPath, defaultSavePath, targetParent.fileId, accountName)
115118

116-
moveLocalFiles(accountName, ocFile, defaultSavePath, targetPath)
119+
val moved = moveLocalFiles(accountName, ocFile, defaultSavePath, targetPath)
120+
if (!moved) return
117121

118122
for (originalMediaPath in originalMediaPaths) {
119123
deleteFileInMediaScan(originalMediaPath)
@@ -124,11 +128,12 @@ fun FileDataStorageManager.moveFiles(ocFile: OCFile?, targetPath: String, target
124128
}
125129
}
126130

127-
private fun moveLocalFiles(accountName: String, ocFile: OCFile, defaultSavePath: String, targetPath: String) {
131+
@Suppress("ReturnCount")
132+
private fun moveLocalFiles(accountName: String, ocFile: OCFile, defaultSavePath: String, targetPath: String): Boolean {
128133
val localFile = File(FileStorageUtils.getDefaultSavePathFor(accountName, ocFile))
129134
if (!localFile.exists()) {
130135
Log_OC.d(FileDataStorageManager.TAG, "moveLocalFile: no local file to move at " + localFile.absolutePath)
131-
return
136+
return false
132137
}
133138

134139
val targetFile = File(defaultSavePath + targetPath)
@@ -142,11 +147,16 @@ private fun moveLocalFiles(accountName: String, ocFile: OCFile, defaultSavePath:
142147

143148
if (!localFile.renameTo(targetFile)) {
144149
Log_OC.e(
145-
FileDataStorageManager.TAG, ("moveLocalFile: failed to rename " + localFile.absolutePath
146-
+ " to " + targetFile.absolutePath)
150+
FileDataStorageManager.TAG,
151+
(
152+
"moveLocalFile: failed to rename " + localFile.absolutePath +
153+
" to " + targetFile.absolutePath
154+
)
147155
)
148-
return
156+
return false
149157
}
158+
159+
return true
150160
}
151161

152162
private fun FileDao.moveFilesInDb(

app/src/test/java/com/owncloud/android/datamodel/MoveFileEntitiesUpdateTest.kt

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ class MoveFileEntitiesUpdateTest : MoveFilesTestBase() {
1919

2020
private val capturedEntities = slot<List<FileEntity>>()
2121

22-
private fun arrangeAndMove(
23-
entities: List<FileEntity>,
24-
targetPath: String = TARGET_PATH
25-
) {
22+
private fun arrangeAndMove(entities: List<FileEntity>, targetPath: String = TARGET_PATH) {
2623
stubTargetParent()
2724
every { mockFileDao.getFolderWithDescendants("${entities.first().path}%", ACCOUNT_NAME) } returns entities
2825
every { mockFileDao.updateAll(capture(capturedEntities)) } returns Unit
@@ -41,70 +38,81 @@ class MoveFileEntitiesUpdateTest : MoveFilesTestBase() {
4138
@Test
4239
fun testMoveLocalFileWhenValidFileShouldUpdatePathToTargetPath() {
4340
val entities = listOf(createFileEntity(path = OLD_PATH))
41+
val expectedPath = TARGET_PATH
42+
4443
arrangeAndMove(entities)
4544

46-
assertEquals(TARGET_PATH, capturedEntities.captured.single().path)
45+
assertEquals(expectedPath, capturedEntities.captured.single().path)
4746
}
4847

4948
@Test
5049
fun testMoveLocalFileWhenNonEncryptedFileShouldUpdatePathDecryptedToNewPath() {
5150
val entities = listOf(createFileEntity(path = OLD_PATH, pathDecrypted = OLD_PATH, isEncrypted = 0))
51+
val expectedDecryptedPath = TARGET_PATH
52+
5253
arrangeAndMove(entities)
5354

54-
assertEquals(TARGET_PATH, capturedEntities.captured.single().pathDecrypted)
55+
assertEquals(expectedDecryptedPath, capturedEntities.captured.single().pathDecrypted)
5556
}
5657

5758
@Test
5859
fun testMoveLocalFileWhenEncryptedFileShouldNotUpdatePathDecrypted() {
59-
val encryptedDecryptedPath = "/documents/encrypted_name"
60+
val originalDecryptedPath = "/documents/encrypted_name"
6061
val entities = listOf(
61-
createFileEntity(path = OLD_PATH, pathDecrypted = encryptedDecryptedPath, isEncrypted = 1)
62+
createFileEntity(path = OLD_PATH, pathDecrypted = originalDecryptedPath, isEncrypted = 1)
6263
)
64+
val expectedDecryptedPath = originalDecryptedPath
65+
6366
arrangeAndMove(entities)
6467

65-
assertEquals(encryptedDecryptedPath, capturedEntities.captured.single().pathDecrypted)
68+
assertEquals(expectedDecryptedPath, capturedEntities.captured.single().pathDecrypted)
6669
}
6770

6871
@Test
6972
fun testMoveLocalFileWhenFileHasStoragePathUnderSavePathShouldUpdateStoragePath() {
7073
val originalStorage = "$SAVE_PATH$OLD_PATH"
74+
val expectedStoragePath = "$SAVE_PATH$TARGET_PATH"
7175
val entities = listOf(createFileEntity(path = OLD_PATH, storagePath = originalStorage))
76+
7277
arrangeAndMove(entities)
7378

74-
assertEquals("$SAVE_PATH$TARGET_PATH", capturedEntities.captured.single().storagePath)
79+
assertEquals(expectedStoragePath, capturedEntities.captured.single().storagePath)
7580
}
7681

7782
@Test
7883
fun testMoveLocalFileWhenFileHasStoragePathOutsideSavePathShouldKeepOriginalStoragePath() {
79-
val externalPath = "/sdcard/downloads/report.pdf"
80-
val entities = listOf(createFileEntity(path = OLD_PATH, storagePath = externalPath))
84+
val originalStorage = "/sdcard/downloads/report.pdf"
85+
val expectedStoragePath = originalStorage
86+
val entities = listOf(createFileEntity(path = OLD_PATH, storagePath = originalStorage))
87+
8188
arrangeAndMove(entities)
8289

83-
assertEquals(externalPath, capturedEntities.captured.single().storagePath)
90+
assertEquals(expectedStoragePath, capturedEntities.captured.single().storagePath)
8491
}
8592

8693
@Test
8794
fun testMoveLocalFileWhenFileHasNoStoragePathShouldKeepStoragePathNull() {
8895
val entities = listOf(createFileEntity(path = OLD_PATH, storagePath = null))
96+
8997
arrangeAndMove(entities)
9098

9199
assertNull(capturedEntities.captured.single().storagePath)
92100
}
93101

94102
@Test
95103
fun testMoveLocalFileWhenMovingFileShouldUpdateParentIdToTargetParentId() {
96-
val targetParentId = 99L
104+
val expectedParentId = 99L
97105
val entities = listOf(createFileEntity(path = OLD_PATH, parent = 10L))
98106
every { mockFileDao.getFolderWithDescendants("$OLD_PATH%", ACCOUNT_NAME) } returns entities
99107
every { mockFileDao.updateAll(capture(capturedEntities)) } returns Unit
100108
val parent = OCFile(TARGET_PARENT_PATH).apply {
101-
fileId = targetParentId
109+
fileId = expectedParentId
102110
mimeType = com.owncloud.android.utils.MimeType.DIRECTORY
103111
}
104112
every { manager.getFileByPath(TARGET_PARENT_PATH) } returns parent
105113

106114
manager.moveLocalFile(OCFile(OLD_PATH).apply { fileId = 1 }, TARGET_PATH, TARGET_PARENT_PATH)
107115

108-
assertEquals(targetParentId, capturedEntities.captured.single().parent)
116+
assertEquals(expectedParentId, capturedEntities.captured.single().parent)
109117
}
110-
}
118+
}

app/src/test/java/com/owncloud/android/datamodel/MoveFilesFilesystemAndMediaTest.kt

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import com.owncloud.android.utils.FileStorageUtils
1111
import io.mockk.every
1212
import io.mockk.verify
1313
import org.junit.After
14+
import org.junit.Assert.assertEquals
15+
import org.junit.Assert.assertFalse
16+
import org.junit.Assert.assertTrue
1417
import org.junit.Before
1518
import org.junit.Test
1619
import java.io.File
@@ -47,35 +50,39 @@ class MoveFilesFilesystemAndMediaTest : MoveFilesTestBase() {
4750

4851
@Test
4952
fun testMoveLocalFileWhenNoLocalFilePresentShouldSkipRenameAndMediaScan() {
53+
val expectedSourcePath = "${tempDir.absolutePath}$OLD_PATH"
54+
val expectedTargetPath = "${tempDir.absolutePath}$TARGET_PATH"
55+
5056
doMove()
5157

58+
assertFalse(File(expectedSourcePath).exists())
59+
assertFalse(File(expectedTargetPath).exists())
5260
verify(exactly = 0) { manager.deleteFileInMediaScan(any()) }
5361
verify(exactly = 0) { MediaScannerConnection.scanFile(any(), any(), any(), any()) }
5462
}
5563

5664
@Test
5765
fun testMoveLocalFileWhenLocalFilePresentShouldRenameToTargetLocation() {
58-
val sourceFile = File("${tempDir.absolutePath}$OLD_PATH").also {
66+
val expectedSourcePath = "${tempDir.absolutePath}$OLD_PATH"
67+
val expectedTargetPath = "${tempDir.absolutePath}$TARGET_PATH"
68+
File(expectedSourcePath).also {
5969
it.parentFile?.mkdirs()
6070
it.createNewFile()
6171
}
62-
val targetFile = File("${tempDir.absolutePath}$TARGET_PATH")
6372

6473
doMove()
6574

66-
assert(!sourceFile.exists()) { "Source file should have been moved" }
67-
assert(targetFile.exists()) { "Target file should exist after rename" }
75+
assertFalse("Source at $expectedSourcePath should no longer exist", File(expectedSourcePath).exists())
76+
assertTrue("Target at $expectedTargetPath should exist after move", File(expectedTargetPath).exists())
77+
assertEquals(expectedTargetPath, File(expectedTargetPath).absolutePath)
6878
}
6979

7080
@Test
7181
fun testMoveLocalFileWhenRenameFailsShouldNotTriggerMediaScan() {
7282
val oldStoragePath = "${tempDir.absolutePath}$OLD_PATH"
73-
// Source file is NOT created → renameTo returns false
74-
val mediaEntity = createFileEntity(
75-
path = OLD_PATH,
76-
storagePath = oldStoragePath,
77-
contentType = "image/jpeg"
78-
)
83+
val mediaEntity = createFileEntity(path = OLD_PATH, storagePath = oldStoragePath, contentType = "image/jpeg")
84+
// Source file intentionally NOT created → renameTo returns false
85+
7986
doMove(entities = listOf(mediaEntity))
8087

8188
verify(exactly = 0) { manager.deleteFileInMediaScan(any()) }
@@ -84,35 +91,42 @@ class MoveFilesFilesystemAndMediaTest : MoveFilesTestBase() {
8491

8592
@Test
8693
fun testMoveLocalFileWhenMediaFileIsMovedShouldDeleteFromMediaScanAtOriginalPath() {
87-
val oldStoragePath = "${tempDir.absolutePath}$OLD_PATH"
88-
File(oldStoragePath).also { it.parentFile?.mkdirs(); it.createNewFile() }
94+
val expectedDeletedPath = "${tempDir.absolutePath}$OLD_PATH"
95+
File(expectedDeletedPath).also {
96+
it.parentFile?.mkdirs()
97+
it.createNewFile()
98+
}
8999
val mediaEntity = createFileEntity(
90100
path = OLD_PATH,
91-
storagePath = oldStoragePath,
101+
storagePath = expectedDeletedPath,
92102
contentType = "image/jpeg"
93103
)
104+
94105
doMove(entities = listOf(mediaEntity))
95106

96-
verify(exactly = 1) { manager.deleteFileInMediaScan(oldStoragePath) }
107+
verify(exactly = 1) { manager.deleteFileInMediaScan(expectedDeletedPath) }
97108
}
98109

99110
@Test
100111
fun testMoveLocalFileWhenMediaFileIsMovedShouldTriggerMediaScanAtNewStoragePath() {
101-
val savePath = tempDir.absolutePath
102-
val oldStoragePath = "$savePath$OLD_PATH"
103-
val expectedNewStoragePath = "$savePath$TARGET_PATH"
104-
File(oldStoragePath).also { it.parentFile?.mkdirs(); it.createNewFile() }
112+
val oldStoragePath = "${tempDir.absolutePath}$OLD_PATH"
113+
val expectedNewStoragePath = "${tempDir.absolutePath}$TARGET_PATH"
114+
File(oldStoragePath).also {
115+
it.parentFile?.mkdirs()
116+
it.createNewFile()
117+
}
105118
val mediaEntity = createFileEntity(
106119
path = OLD_PATH,
107120
storagePath = oldStoragePath,
108121
contentType = "image/jpeg"
109122
)
123+
110124
doMove(entities = listOf(mediaEntity))
111125

112126
verify {
113127
MediaScannerConnection.scanFile(
114128
any(),
115-
match { paths -> paths.any { it == expectedNewStoragePath } },
129+
match { paths -> paths.single() == expectedNewStoragePath },
116130
any(),
117131
any()
118132
)
@@ -122,15 +136,19 @@ class MoveFilesFilesystemAndMediaTest : MoveFilesTestBase() {
122136
@Test
123137
fun testMoveLocalFileWhenNonMediaFileIsMovedShouldNotTriggerAnyMediaScan() {
124138
val oldStoragePath = "${tempDir.absolutePath}$OLD_PATH"
125-
File(oldStoragePath).also { it.parentFile?.mkdirs(); it.createNewFile() }
139+
File(oldStoragePath).also {
140+
it.parentFile?.mkdirs()
141+
it.createNewFile()
142+
}
126143
val docEntity = createFileEntity(
127144
path = OLD_PATH,
128145
storagePath = oldStoragePath,
129146
contentType = "application/pdf"
130147
)
148+
131149
doMove(entities = listOf(docEntity))
132150

133151
verify(exactly = 0) { manager.deleteFileInMediaScan(any()) }
134152
verify(exactly = 0) { MediaScannerConnection.scanFile(any(), any(), any(), any()) }
135153
}
136-
}
154+
}

app/src/test/java/com/owncloud/android/datamodel/MoveFilesGuardTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,4 @@ class MoveFilesGuardTest : MoveFilesTestBase() {
8080
verify(exactly = 0) { mockFileDao.getFolderWithDescendants(any(), any()) }
8181
verify(exactly = 0) { mockFileDao.updateAll(any()) }
8282
}
83-
}
83+
}

0 commit comments

Comments
 (0)