|
| 1 | +package net.thunderbird.core.file |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.net.Uri |
| 5 | +import assertk.assertThat |
| 6 | +import assertk.assertions.isEqualTo |
| 7 | +import kotlinx.io.Buffer |
| 8 | +import org.junit.Rule |
| 9 | +import org.junit.Test |
| 10 | +import org.junit.rules.TemporaryFolder |
| 11 | +import org.junit.runner.RunWith |
| 12 | +import org.robolectric.RobolectricTestRunner |
| 13 | +import org.robolectric.RuntimeEnvironment |
| 14 | +import org.robolectric.annotation.Config |
| 15 | + |
| 16 | +@RunWith(RobolectricTestRunner::class) |
| 17 | +@Config(manifest = Config.NONE) |
| 18 | +class AndroidFileSystemManagerTest { |
| 19 | + |
| 20 | + private val appContext: Context = RuntimeEnvironment.getApplication() |
| 21 | + |
| 22 | + private val testSubject = AndroidFileSystemManager(appContext.contentResolver) |
| 23 | + |
| 24 | + @JvmField |
| 25 | + @Rule |
| 26 | + val folder = TemporaryFolder() |
| 27 | + |
| 28 | + @Test |
| 29 | + fun openSinkAndOpenSource_writeAndReadFileContentRoundtrip() { |
| 30 | + // Arrange |
| 31 | + val tempFile = folder.newFile("tb-file-fs-test-android.txt") |
| 32 | + val uri: Uri = Uri.fromFile(tempFile) |
| 33 | + val testText = "Hello Thunderbird Android!" |
| 34 | + |
| 35 | + // Act |
| 36 | + val sink = checkNotNull(testSubject.openSink(uri.toString(), "wt")) |
| 37 | + val writeBuffer = Buffer().apply { write(testText.encodeToByteArray()) } |
| 38 | + sink.write(writeBuffer, writeBuffer.size) |
| 39 | + sink.flush() |
| 40 | + sink.close() |
| 41 | + |
| 42 | + val source = checkNotNull(testSubject.openSource(uri.toString())) |
| 43 | + val readBuffer = Buffer() |
| 44 | + source.readAtMostTo(readBuffer, 1024) |
| 45 | + val bytes = ByteArray(readBuffer.size.toInt()) |
| 46 | + for (i in bytes.indices) { |
| 47 | + bytes[i] = readBuffer.readByte() |
| 48 | + } |
| 49 | + val result = bytes.decodeToString() |
| 50 | + source.close() |
| 51 | + |
| 52 | + // Assert |
| 53 | + assertThat(result).isEqualTo(testText) |
| 54 | + } |
| 55 | +} |
0 commit comments