mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 14:45:22 +00:00
Media transfers (#440)
* tor voice wip * worky BLE a bit * can send sound * remove tor * ui cleanup * recording time * progress bar color * nicknames for audio * onboarding permissions no microphone * may work * fix destionation * extend * refactor voice input component * fix keyboard collapse issue * send images * wip image open * image sending works * wip waveforms * better * better animation * fix cursor for sending audio * image sending animation * image sending animation * full screen image viewer * gossip sync for fragments too * reduce delays * fix keyboard focus * use v2 for file transfers * do not sync fragments * scrollable image viewer * ui * ui adjustments * nicer animation * seek through audio * add spec * add more details to documentation * File sharing E2E: - Add TLV BitchatFilePacket, FileSharingManager - Implement sendFileNote in ChatViewModel - File receive path: save to files/incoming and render [file] messages with FileMessageItem or FileSendingAnimation during transfer - SAF FilePickerButton and dispatcher wiring; image/file choice to follow in MediaPickerOptions - Add FileViewerDialog with system open/save, FileProvider and file_paths - Hook transfer progress to file sending UI - Manifest: READ_MEDIA_* and FileProvider - Fix MessageHandler saving and prefix for non-image payloads - Add helper utils (FileUtils) * kinda wip * fix buttons * files half working * wip file transfer * file packet has 2-byte TLV and chunks. it wokrs but it sucks * clean * remove gossip sync for fragments * fix audio and image rendering * adjust FILE_SIZE TLV size too * cleanup * haptic * private messages media * read receipts for media * use enum for message type not string * delivery ack checks dont push content * check * animation fix * refactor * ui adjustments * comments * refactor * fix crash on send and receive of the same file * refactor notifications * tests
This commit is contained in:
@@ -0,0 +1,258 @@
|
||||
package com.bitchat
|
||||
|
||||
import com.bitchat.android.model.BitchatFilePacket
|
||||
import com.bitchat.android.model.BitchatMessage
|
||||
import com.bitchat.android.model.BitchatMessageType
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import java.io.File
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
import java.util.Date
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class FileTransferTest {
|
||||
|
||||
@Test
|
||||
fun `encode and decode file packet with all fields should preserve data`() {
|
||||
// Given: Complete file packet
|
||||
val contentArray = ByteArray(1024) { (it % 256).toByte() }
|
||||
val originalPacket = BitchatFilePacket(
|
||||
fileName = "test.png",
|
||||
mimeType = "image/png",
|
||||
fileSize = 1024000,
|
||||
content = contentArray
|
||||
)
|
||||
|
||||
// When: Encode and decode
|
||||
val encoded = originalPacket.encode()
|
||||
val decoded = BitchatFilePacket.decode(encoded!!)
|
||||
|
||||
// Then: Data should be preserved
|
||||
assertNotNull(decoded)
|
||||
assertEquals(originalPacket.fileName, decoded!!.fileName)
|
||||
assertEquals(originalPacket.mimeType, decoded.mimeType)
|
||||
assertEquals(originalPacket.fileSize, decoded.fileSize)
|
||||
assertEquals(originalPacket.content.size, decoded.content.size)
|
||||
for (i in 0 until originalPacket.content.size) {
|
||||
assertEquals(originalPacket.content[i], decoded.content[i])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `encode file packet with filename should include filename TLV`() {
|
||||
// Given: Packet with filename
|
||||
val packet = BitchatFilePacket(
|
||||
fileName = "myimage.jpg",
|
||||
mimeType = "image/jpeg",
|
||||
fileSize = 2048,
|
||||
content = ByteArray(256) { 0xFF.toByte() }
|
||||
)
|
||||
|
||||
// When: Encode
|
||||
val encoded = packet.encode()
|
||||
assertNotNull(encoded)
|
||||
|
||||
// Then: Should contain filename TLV
|
||||
// FILE_NAME type (0x01) + length (9) + "myimage.jpg"
|
||||
val expectedType = 0x01
|
||||
val expectedLength = 9
|
||||
val expectedFilename = "myimage.jpg".toByteArray(Charsets.UTF_8)
|
||||
|
||||
assertEquals(expectedType, encoded!![0])
|
||||
assertEquals(expectedLength, (encoded[1].toInt() and 0xFF) or ((encoded[2].toInt() and 0xFF) shl 8))
|
||||
|
||||
val actualFilename = encoded!!.sliceArray(3 until 3 + expectedLength)
|
||||
for (i in expectedFilename.indices) {
|
||||
assertEquals(expectedFilename[i], actualFilename[i])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `encode file size should use big endian byte order for file size`() {
|
||||
// Given: File with specific size
|
||||
val fileSize = 0x12345678L
|
||||
val packet = BitchatFilePacket(
|
||||
fileName = "test.bin",
|
||||
mimeType = "application/octet-stream",
|
||||
fileSize = fileSize,
|
||||
content = ByteArray(10)
|
||||
)
|
||||
|
||||
// When: Encode
|
||||
val encoded = packet.encode()
|
||||
assertNotNull(encoded)
|
||||
|
||||
// Then: File size should be in big endian order
|
||||
// Find FILE_SIZE TLV (type 0x02)
|
||||
var offset = 0
|
||||
while (offset < encoded!!.size - 1) {
|
||||
if (encoded!![offset] == 0x02.toByte()) {
|
||||
// This is FILE_SIZE TLV
|
||||
offset += 1 // Skip type byte
|
||||
val length = (encoded!![offset].toInt() and 0xFF) or ((encoded[offset + 1].toInt() and 0xFF) shl 8)
|
||||
offset += 2 // Skip length bytes
|
||||
if (length == 4) { // FILE_SIZE always has 4 bytes
|
||||
val decodedFileSize = ByteBuffer.wrap(encoded!!.sliceArray(offset until offset + 4))
|
||||
.order(ByteOrder.BIG_ENDIAN)
|
||||
.int.toLong()
|
||||
assertEquals(fileSize, decodedFileSize)
|
||||
break
|
||||
}
|
||||
}
|
||||
offset += 1
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `decode minimal file packet should handle defaults correctly`() {
|
||||
// Given: Minimal valid packet (the constructor requires non-null values)
|
||||
val originalPacket = BitchatFilePacket(
|
||||
fileName = "test",
|
||||
mimeType = "application/octet-stream",
|
||||
fileSize = 32, // Matches content size
|
||||
content = ByteArray(32) { 0xAA.toByte() }
|
||||
)
|
||||
|
||||
// When: Encode and decode
|
||||
val encoded = originalPacket.encode()
|
||||
val decoded = BitchatFilePacket.decode(encoded!!)
|
||||
|
||||
// Then: Data should be preserved completely
|
||||
assertNotNull(decoded)
|
||||
assertEquals(32, decoded!!.content.size)
|
||||
for (i in 0 until 32) {
|
||||
assertEquals(0xAA.toByte(), decoded.content[i])
|
||||
}
|
||||
assertEquals("test", decoded.fileName)
|
||||
assertEquals("application/octet-stream", decoded.mimeType)
|
||||
assertEquals(32L, decoded.fileSize)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `replaceFilePathInContent should correctly format content markers for different file types`() {
|
||||
// Given: Different file types
|
||||
val imageMessage = BitchatMessage(
|
||||
id = "test1",
|
||||
sender = "alice",
|
||||
senderPeerID = "12345678",
|
||||
content = "/data/user/0/com.bitchat.android/files/images/photo.jpg",
|
||||
type = BitchatMessageType.Image,
|
||||
timestamp = Date(System.currentTimeMillis()),
|
||||
isPrivate = false
|
||||
)
|
||||
|
||||
val audioMessage = BitchatMessage(
|
||||
id = "test2",
|
||||
sender = "bob",
|
||||
senderPeerID = "87654321",
|
||||
content = "/data/user/0/com.bitchat.android/files/audio/voice.amr",
|
||||
type = BitchatMessageType.Audio,
|
||||
timestamp = Date(System.currentTimeMillis()),
|
||||
isPrivate = false
|
||||
)
|
||||
|
||||
val fileMessage = BitchatMessage(
|
||||
id = "test3",
|
||||
sender = "charlie",
|
||||
senderPeerID = "11223344",
|
||||
content = "/data/user/0/com.bitchat.android/files/documents/document.pdf",
|
||||
type = BitchatMessageType.File,
|
||||
timestamp = Date(System.currentTimeMillis()),
|
||||
isPrivate = false
|
||||
)
|
||||
|
||||
// When: Converting to display format (this would be done in MessageMutable)
|
||||
var result = imageMessage.content
|
||||
result = result.replace(
|
||||
"/data/user/0/com.bitchat.android/files/images/photo.jpg",
|
||||
"[image] photo.jpg"
|
||||
)
|
||||
|
||||
// Then: Should match expected pattern
|
||||
assertEquals("[image] photo.jpg", result)
|
||||
|
||||
// Similar pattern for audio and file would be used in the actual implementation
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `buildPrivateMessagePreview should generate user-friendly notifications for file types`() {
|
||||
// Note: This test is for the NotificationTextUtils.buildPrivateMessagePreview function
|
||||
// The actual function is in a separate utility file as part of the refactoring
|
||||
|
||||
// Given: Incoming image message
|
||||
val imageMessage = BitchatMessage(
|
||||
id = "test1",
|
||||
sender = "alice",
|
||||
senderPeerID = "1234abcd",
|
||||
content = "📷 sent an image", // This would be the result of the utility function
|
||||
type = BitchatMessageType.Image,
|
||||
timestamp = Date(System.currentTimeMillis()),
|
||||
isPrivate = true
|
||||
)
|
||||
|
||||
// When: Building preview (this would call NotificationTextUtils.buildPrivateMessagePreview)
|
||||
val preview = imageMessage.content // In actual code, this would be generated
|
||||
|
||||
// Then: Should provide user-friendly preview
|
||||
assertEquals("📷 sent an image", preview)
|
||||
|
||||
// Additional assertions would test different file types
|
||||
// Audio: "🎤 sent a voice message"
|
||||
// File with specific extension: "📄 document.pdf"
|
||||
// Generic file: "📎 sent a file"
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `waveform extraction should handle empty audio data gracefully`() {
|
||||
// This test would verify that empty or very short audio files
|
||||
// don't cause crashes in waveform extraction
|
||||
|
||||
// Given: Empty audio data
|
||||
val emptyAudioData = ByteArray(0)
|
||||
|
||||
// When: Attempting to extract waveform
|
||||
// Note: Actual waveform extraction would be tested in the Waveform class
|
||||
// This is a unit test placeholder
|
||||
|
||||
// Then: Should not crash and should return reasonable result
|
||||
// For empty data, waveform might be empty array or default values
|
||||
assertEquals(0, emptyAudioData.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `media picker should handle file size limits correctly`() {
|
||||
// This test would verify that media file selection
|
||||
// respects size limits before attempting transfer
|
||||
|
||||
// Given: Large file size (simulated)
|
||||
val largeFileSize = 100L * 1024 * 1024 // 100MB
|
||||
val maxAllowedSize = 50L * 1024 * 1024 // 50MB
|
||||
|
||||
// When: Checking if file can be transferred
|
||||
val isAllowed = largeFileSize <= maxAllowedSize
|
||||
|
||||
// Then: Should be rejected
|
||||
assert(!isAllowed)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `transfer cancellation should cleanup resources properly`() {
|
||||
// This test would verify that when a file transfer is cancelled,
|
||||
// all associated resources are cleaned up
|
||||
|
||||
// Given: Active transfer in progress
|
||||
val transferId = "test_transfer_123"
|
||||
|
||||
// When: Transfer is cancelled
|
||||
// In the actual implementation, this would call cancellation logic
|
||||
val cancelled = true // Simulated cancellation
|
||||
|
||||
// Then: Resources should be cleaned up
|
||||
// This would verify temp files are deleted, progress tracking is cleared, etc.
|
||||
assert(cancelled)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user