mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
Make File Manager cancellation stop active file I/O
Description:
- Pass the active coroutine job into copy, move, and delete operations so synchronous file I/O observes cancellation while it is running.
- Check for cancellation between copy chunks and recursive delete entries. Remove a newly created partial copy, and stage overwrites so cancelling a copy or move preserves the previous destination and move source.
- Add focused tests for cancelling a copy, move fallback, and recursive delete.
Why it was needed:
- Accept cancelled the Compose coroutine, but FileOps continued its synchronous I/O work. The confirmation could therefore wait for a large copy or delete to finish instead of stopping it.
- Copy and move must avoid replacing an existing destination with partial data when cancellation occurs.
Copy and move safeguards adapted from upstream commit a74ff3d6 (#1029).
Added and adjusted:
- Added cancellation checks between file transfer chunks and recursive delete entries, including single-file and batch operations.
- Adjusted FileManagerScreen to pass the active job to FileOps and check it during copy progress. Accept now waits for the file operation to stop before Exit, Return, or Back proceeds.
- Adjusted overwrite handling to use a temporary file so cancellation preserves the previous destination; an incomplete new destination is removed and a move source remains intact.
- Added tests for cancelling a new copy, a move fallback with an existing destination, and a recursive delete. The focused debug unit tests passed.
Localization already included on this branch:
- The earlier File Manager update added translations for Exit, Return to start, and the unavailable starting-folder message.
- It also translated the cancel-operation title and warning, plus the Accept and Decline buttons, across all 32 supported locale resource sets alongside the English defaults.
- These strings let localized users understand the navigation controls and the consequences of cancelling an in-progress operation. They were added in the preceding commit 7928effd; this commit makes Accept actually stop the file I/O.
This commit is contained in:
1 parent
1eb1e154ab
commit
0247831c7e
3 files changed
+130
-17
No files matched your search
@@ -0,0 +1,55 @@
|
||||
package com.droiddeck.launcher.files
|
||||
|
||||
import java.io.File
|
||||
import java.util.concurrent.CancellationException
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertThrows
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class FileOpsCancellationTest {
|
||||
@get:Rule val tmp = TemporaryFolder()
|
||||
|
||||
@Test fun cancellingCopyRemovesAnIncompleteNewFile() {
|
||||
val source = tmp.newFile("source").apply { writeBytes(ByteArray(5 * 1024 * 1024)) }
|
||||
val destination = File(tmp.root, "destination")
|
||||
|
||||
assertThrows(CancellationException::class.java) {
|
||||
FileOps.copyWithProgress(source, destination, { _, _ -> throw CancellationException("cancel") })
|
||||
}
|
||||
|
||||
assertTrue(source.exists())
|
||||
assertFalse(destination.exists())
|
||||
}
|
||||
|
||||
@Test fun cancellingMoveFallbackPreservesTheSourceAndExistingDestination() {
|
||||
val source = tmp.newFile("source").apply { writeText("replacement") }
|
||||
val destination = tmp.newFile("destination").apply { writeText("original") }
|
||||
|
||||
assertThrows(CancellationException::class.java) {
|
||||
FileOps.moveWithProgress(source, destination, { _, _ -> throw CancellationException("cancel") })
|
||||
}
|
||||
|
||||
assertEquals("replacement", source.readText())
|
||||
assertEquals("original", destination.readText())
|
||||
}
|
||||
|
||||
@Test fun cancellingRecursiveDeleteStopsBeforeTheNextFile() {
|
||||
val directory = tmp.newFolder("directory")
|
||||
repeat(3) { File(directory, "file-$it").writeText("content") }
|
||||
var checks = 0
|
||||
|
||||
assertThrows(CancellationException::class.java) {
|
||||
FileOps.delete(directory) { ++checks > 3 }
|
||||
}
|
||||
|
||||
assertTrue(directory.exists())
|
||||
assertEquals(2, directory.listFiles()?.size)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user