Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / office / src / test / java / com / vayunmathur / office / platform / W4SaveTest.kt
W4SaveTest.kt137 lines · 4.7 KiBRaw
| 1 | package com.vayunmathur.office.platform |
| 2 | |
| 3 | import android.net.Uri |
| 4 | import androidx.test.core.app.ApplicationProvider |
| 5 | import com.vayunmathur.library.ui.odf.OdfContentBlock |
| 6 | import com.vayunmathur.library.ui.odf.OdfDocument |
| 7 | import com.vayunmathur.library.ui.odf.OdfParagraph |
| 8 | import com.vayunmathur.library.ui.odf.OdfSpan |
| 9 | import com.vayunmathur.office.data.odf.Slice4ImportExportTest |
| 10 | import java.io.File |
| 11 | import kotlin.test.AfterTest |
| 12 | import kotlin.test.BeforeTest |
| 13 | import kotlin.test.Test |
| 14 | import kotlin.test.assertEquals |
| 15 | import kotlin.test.assertFalse |
| 16 | import kotlin.test.assertTrue |
| 17 | import kotlinx.coroutines.Dispatchers |
| 18 | import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 19 | import kotlinx.coroutines.test.UnconfinedTestDispatcher |
| 20 | import kotlinx.coroutines.test.resetMain |
| 21 | import kotlinx.coroutines.test.setMain |
| 22 | import org.junit.runner.RunWith |
| 23 | import org.robolectric.Robolectric |
| 24 | import org.robolectric.RobolectricTestRunner |
| 25 | |
| 26 | /** |
| 27 | * W4: [OfficeViewModel.save] — ODF and OOXML targets written for real |
| 28 | * through the test content provider, the no-target no-op, and the |
| 29 | * unwritable-target failure branch. |
| 30 | */ |
| 31 | @RunWith(RobolectricTestRunner::class) |
| 32 | @OptIn(ExperimentalCoroutinesApi::class) |
| 33 | class W4SaveTest { |
| 34 | |
| 35 | private val roboApp: android.app.Application |
| 36 | get() = ApplicationProvider.getApplicationContext() |
| 37 | |
| 38 | init { |
| 39 | Robolectric.setupContentProvider( |
| 40 | Slice4ImportExportTest.Slice4Provider::class.java, |
| 41 | "com.vayunmathur.office.w4save") |
| 42 | } |
| 43 | |
| 44 | private var outTarget: File? |
| 45 | get() = Slice4ImportExportTest.Slice4Provider.outTarget |
| 46 | set(v) { Slice4ImportExportTest.Slice4Provider.outTarget = v } |
| 47 | |
| 48 | private fun contentUri(name: String): Uri = Uri.parse("content://com.vayunmathur.office.w4save/$name") |
| 49 | |
| 50 | @BeforeTest fun setUp() { Dispatchers.setMain(UnconfinedTestDispatcher()) } |
| 51 | @AfterTest fun tearDown() { |
| 52 | Dispatchers.resetMain() |
| 53 | outTarget = null |
| 54 | } |
| 55 | |
| 56 | private fun loadedVm(): OfficeViewModel { |
| 57 | val v = OfficeViewModel(roboApp) |
| 58 | v.stateMutable.value = OfficeViewModel.ViewState.Loaded( |
| 59 | OdfDocument.TextDocument( |
| 60 | title = "Savable", |
| 61 | content = listOf( |
| 62 | OdfContentBlock.Paragraph(OdfParagraph(listOf(OdfSpan("save me"))))))) |
| 63 | v.hasUnsavedChangesMutable.value = true |
| 64 | return v |
| 65 | } |
| 66 | |
| 67 | private fun awaitIdle(v: OfficeViewModel) { |
| 68 | val deadline = System.currentTimeMillis() + 15_000 |
| 69 | while (System.currentTimeMillis() < deadline) { |
| 70 | if (!v.isSaving.value) return |
| 71 | Thread.sleep(25) |
| 72 | } |
| 73 | assertFalse(v.isSaving.value, "save should finish") |
| 74 | } |
| 75 | |
| 76 | private fun zipEntryNames(file: File): Set<String> { |
| 77 | val names = mutableSetOf<String>() |
| 78 | java.util.zip.ZipFile(file).use { z -> |
| 79 | val e = z.entries() |
| 80 | while (e.hasMoreElements()) names.add(e.nextElement().name) |
| 81 | } |
| 82 | return names |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | fun `save writes an odt package and clears the dirty flag`() { |
| 87 | val out = File(roboApp.cacheDir, "w4-save.odt") |
| 88 | out.delete() |
| 89 | outTarget = out |
| 90 | val v = loadedVm() |
| 91 | try { |
| 92 | v.save(contentUri("w4-save.odt")) |
| 93 | awaitIdle(v) |
| 94 | assertTrue(out.exists() && out.length() > 100, "saved file missing") |
| 95 | assertTrue(zipEntryNames(out).contains("content.xml")) |
| 96 | assertFalse(v.hasUnsavedChanges.value) |
| 97 | assertEquals(contentUri("w4-save.odt"), v.originalUri) |
| 98 | } finally { |
| 99 | out.delete() |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | fun `save to a docx target uses the ooxml exporter`() { |
| 105 | val out = File(roboApp.cacheDir, "w4-save.docx") |
| 106 | out.delete() |
| 107 | outTarget = out |
| 108 | val v = loadedVm() |
| 109 | try { |
| 110 | v.save(contentUri("w4-save.docx")) |
| 111 | awaitIdle(v) |
| 112 | assertTrue(out.exists() && out.length() > 100, "saved file missing") |
| 113 | assertTrue(zipEntryNames(out).any { it.contains("document.xml") }, zipEntryNames(out).toString()) |
| 114 | assertFalse(v.hasUnsavedChanges.value) |
| 115 | } finally { |
| 116 | out.delete() |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | fun `save without any target is a no-op`() { |
| 122 | val v = loadedVm() |
| 123 | v.originalUri = null |
| 124 | v.save() |
| 125 | assertFalse(v.isSaving.value) |
| 126 | assertTrue(v.hasUnsavedChanges.value, "dirty flag must survive a no-op save") |
| 127 | } |
| 128 | |
| 129 | @Test |
| 130 | fun `save to an unwritable target reports failure and clears saving`() { |
| 131 | val v = loadedVm() |
| 132 | // No provider registered for this authority: the stream open fails. |
| 133 | v.save(Uri.parse("content://com.vayunmathur.office.nowhere/x.odt")) |
| 134 | awaitIdle(v) |
| 135 | assertTrue(v.hasUnsavedChanges.value, "failed save must stay dirty") |
| 136 | } |
| 137 | } |