Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / office / src / test / java / com / vayunmathur / office / Slice4Pass2MenusOverlaysTest.kt

Slice4Pass2MenusOverlaysTest.kt279 lines · 11.9 KiBRaw
1package com.vayunmathur.office
2
3import android.content.Context
4import androidx.activity.ComponentActivity
5import androidx.compose.runtime.mutableIntStateOf
6import androidx.compose.runtime.mutableStateOf
7import androidx.compose.ui.test.assertIsDisplayed
8import androidx.compose.ui.test.junit4.createAndroidComposeRule
9import androidx.compose.ui.test.onNodeWithContentDescription
10import androidx.compose.ui.test.onNodeWithText
11import androidx.compose.ui.test.performClick
12import androidx.compose.ui.test.performScrollTo
13import androidx.test.core.app.ApplicationProvider
14import androidx.test.ext.junit.runners.AndroidJUnit4
15import com.vayunmathur.library.ui.odf.OdfCell
16import com.vayunmathur.library.ui.odf.OdfContentBlock
17import com.vayunmathur.library.ui.odf.OdfDocument
18import com.vayunmathur.library.ui.odf.OdfFrame
19import com.vayunmathur.library.ui.odf.OdfParagraph
20import com.vayunmathur.library.ui.odf.OdfRow
21import com.vayunmathur.library.ui.odf.OdfSheet
22import com.vayunmathur.library.ui.odf.OdfSlide
23import com.vayunmathur.library.ui.odf.OdfSlideElement
24import com.vayunmathur.library.ui.odf.OdfSpan
25import com.vayunmathur.library.ui.odf.OdfTable
26import com.vayunmathur.library.ui.odf.OdfTableCell
27import com.vayunmathur.library.ui.odf.OdfTableRow
28import com.vayunmathur.office.platform.OfficeViewModel
29import com.vayunmathur.office.platform.cellCommentText
30import org.junit.Rule
31import org.junit.Test
32import org.junit.runner.RunWith
33import kotlin.test.assertEquals
34import kotlin.test.assertFalse
35import kotlin.test.assertTrue
36
37import com.vayunmathur.office.ui.CellOverlays
38import com.vayunmathur.office.ui.DocumentOverlayState
39import com.vayunmathur.office.ui.DocumentScreenState
40import com.vayunmathur.office.ui.InsertMenu
41import com.vayunmathur.office.ui.rememberDocumentLaunchers
42/** Slice 4 pass 2: InsertMenu items, cell/slide color+comment+resize overlays. */
43@RunWith(AndroidJUnit4::class)
44class Slice4Pass2MenusOverlaysTest {
45
46 @get:Rule val rule = createAndroidComposeRule<ComponentActivity>()
47 private val ctx: Context get() = ApplicationProvider.getApplicationContext()
48 private fun str(id: Int) = ctx.getString(id)
49
50 private fun vm(doc: OdfDocument): OfficeViewModel =
51 OfficeViewModel(ApplicationProvider.getApplicationContext()).apply {
52 stateMutable.value = OfficeViewModel.ViewState.Loaded(doc)
53 }
54
55 private fun textDoc() = OdfDocument.TextDocument(
56 "Doc.odt",
57 listOf(
58 OdfContentBlock.Paragraph(OdfParagraph(listOf(OdfSpan("body text")), style = com.vayunmathur.library.ui.odf.ParagraphStyle.HEADING1)),
59 OdfContentBlock.Table(
60 OdfTable(
61 name = "T",
62 rows = listOf(
63 OdfTableRow(listOf(OdfTableCell(listOf(OdfParagraph(listOf(OdfSpan("c00"))))), OdfTableCell())),
64 OdfTableRow(listOf(OdfTableCell(), OdfTableCell())),
65 ),
66 )
67 ),
68 ),
69 )
70
71 private fun sheetDoc() = OdfDocument.Spreadsheet(
72 "Book.ods",
73 listOf(OdfSheet("S", listOf(OdfRow(listOf(OdfCell("a1"), OdfCell("b1")))))),
74 )
75
76 private fun deckDoc() = OdfDocument.Presentation(
77 "Deck.odp",
78 listOf(
79 OdfSlide(
80 "S1",
81 listOf(
82 OdfSlideElement.Frame(
83 OdfFrame(0f, 0f, 100f, 50f, listOf(OdfParagraph(listOf(OdfSpan("el")))))
84 ),
85 ),
86 ),
87 ),
88 )
89
90 private fun loadedText(vm: OfficeViewModel) =
91 (vm.state.value as OfficeViewModel.ViewState.Loaded).document as OdfDocument.TextDocument
92
93 // ---- InsertMenu ----
94
95 @Test fun insertMenuHiddenForNonTextDoc() {
96 val s = DocumentScreenState()
97 val viewModel = vm(textDoc())
98 rule.setContent {
99 val launchers = rememberDocumentLaunchers(viewModel, s, onPickImageBytes = { _, _ -> })
100 InsertMenu(s = s, viewModel = viewModel, isTextDoc = false, focusedPara = 0, launchers = launchers)
101 }
102 rule.onNodeWithText(str(R.string.insert)).assertDoesNotExist()
103 }
104
105 @Test fun insertMenuOpensAndChartItemFlagsEditor() {
106 val s = DocumentScreenState()
107 val viewModel = vm(textDoc())
108 rule.setContent {
109 val launchers = rememberDocumentLaunchers(viewModel, s, onPickImageBytes = { _, _ -> })
110 InsertMenu(s = s, viewModel = viewModel, isTextDoc = true, focusedPara = 0, launchers = launchers)
111 }
112 rule.onNodeWithText(str(R.string.insert)).performClick()
113 assertTrue(s.insertMenu)
114 rule.onNodeWithText(str(R.string.chart)).performClick()
115 assertTrue(s.showChartEditor)
116 assertEquals(-1, s.editingChartBlock)
117 assertFalse(s.insertMenu)
118 }
119
120 @Test fun insertMenuDialogItemsSetStateFlags() {
121 val s = DocumentScreenState().apply {
122 activeRunStart = 0; activeRunEnd = 0; selStart = 0; selEnd = 4
123 }
124 val viewModel = vm(textDoc())
125 rule.setContent {
126 val launchers = rememberDocumentLaunchers(viewModel, s, onPickImageBytes = { _, _ -> })
127 InsertMenu(s = s, viewModel = viewModel, isTextDoc = true, focusedPara = 0, launchers = launchers)
128 }
129 fun openAndClick(label: String) {
130 s.insertMenu = true
131 rule.waitForIdle()
132 rule.onNodeWithText(label).performScrollTo().performClick()
133 rule.waitForIdle()
134 }
135 openAndClick(str(R.string.special_character_1)); assertTrue(s.showSpecialChars); s.showSpecialChars = false
136 openAndClick(str(R.string.bookmark)); assertTrue(s.showAddBookmark); s.showAddBookmark = false
137 openAndClick(str(R.string.footnote)); assertTrue(s.showFootnote); s.showFootnote = false
138 openAndClick(str(R.string.comment_1)); assertTrue(s.showComment); s.showComment = false
139 openAndClick(str(R.string.header_footer)); assertTrue(s.showHeaderFooter); s.showHeaderFooter = false
140 }
141
142 @Test fun insertMenuFieldAndStructuralItemsMutateDocument() {
143 val s = DocumentScreenState().apply {
144 activeRunStart = 0; activeRunEnd = 0; selStart = 0; selEnd = 4
145 }
146 val viewModel = vm(textDoc())
147 rule.setContent {
148 val launchers = rememberDocumentLaunchers(viewModel, s, onPickImageBytes = { _, _ -> })
149 InsertMenu(s = s, viewModel = viewModel, isTextDoc = true, focusedPara = 0, launchers = launchers)
150 }
151 fun openAndClick(label: String) {
152 s.insertMenu = true
153 rule.waitForIdle()
154 rule.onNodeWithText(label).performScrollTo().performClick()
155 rule.waitForIdle()
156 }
157 openAndClick("Date (field)")
158 val afterField = loadedText(viewModel)
159 val spans = (afterField.content[0] as OdfContentBlock.Paragraph).paragraph.spans
160 assertTrue(spans.any { it.field == "date" }, "date field span inserted: $spans")
161
162 val before = loadedText(viewModel).content.size
163 openAndClick(str(R.string.horizontal_line))
164 assertTrue(loadedText(viewModel).content.size >= before)
165 openAndClick(str(R.string.page_break))
166 assertTrue(
167 loadedText(viewModel).content.any { it is OdfContentBlock.PageBreak },
168 "page break block inserted",
169 )
170 openAndClick(str(R.string.table_of_contents))
171 assertTrue(
172 loadedText(viewModel).content.any { it is OdfContentBlock.TableOfContents },
173 "TOC block inserted",
174 )
175 }
176
177 // ---- Cell / slide overlays ----
178
179 private fun showCellOverlays(
180 state: DocumentOverlayState,
181 doc: OdfDocument,
182 cell: Triple<Int, Int, Int>? = Triple(0, 0, 0),
183 slideEl: Int = -1,
184 ): OfficeViewModel {
185 val viewModel = vm(doc)
186 val activeCell = mutableStateOf(cell)
187 val activeSlide = mutableIntStateOf(0)
188 val activeSlideEl = mutableIntStateOf(slideEl)
189 rule.setContent {
190 CellOverlays(
191 state = state,
192 viewModel = viewModel,
193 activeCell = activeCell,
194 activeSlide = activeSlide,
195 activeSlideEl = activeSlideEl,
196 )
197 }
198 return viewModel
199 }
200
201 @Test fun cellBgColorOverlayApplies() {
202 val bgState = DocumentOverlayState().apply { showCellBgColor = true }
203 showCellOverlays(bgState, sheetDoc())
204 rule.onNodeWithText("Background Color").assertIsDisplayed()
205 rule.onNodeWithText("∅").performClick()
206 assertFalse(bgState.showCellBgColor)
207 }
208
209 @Test fun cellBorderColorOverlayApplies() {
210 val borderState = DocumentOverlayState().apply { showCellBorderColor = true }
211 showCellOverlays(borderState, sheetDoc())
212 rule.onNodeWithText("Border Color").assertIsDisplayed()
213 rule.onNodeWithText("∅").performClick()
214 assertFalse(borderState.showCellBorderColor)
215 }
216
217 @Test fun cellCommentOverlaySaveAndCancel() {
218 val state = DocumentOverlayState().apply { showCellComment = true }
219 val viewModel = showCellOverlays(state, sheetDoc())
220 rule.onNodeWithText(str(R.string.cell_comment), substring = true).assertExists()
221 // Save with the prefilled (empty) comment text.
222 rule.onNodeWithText(str(com.vayunmathur.library.ui.R.string.save), substring = true).performClick()
223 assertFalse(state.showCellComment)
224 assertEquals("", viewModel.cellCommentText(0, 0, 0))
225 }
226
227 @Test fun slideElementTextColorOverlayApplies() {
228 val textState = DocumentOverlayState().apply { showSlideTextColor = true }
229 showCellOverlays(textState, deckDoc(), cell = null, slideEl = 0)
230 rule.onNodeWithText("Text Color").assertIsDisplayed()
231 rule.onNodeWithText("∅").performClick()
232 assertFalse(textState.showSlideTextColor)
233 }
234
235 @Test fun slideElementFillColorOverlayApplies() {
236 val fillState = DocumentOverlayState().apply { showSlideFillColor = true }
237 showCellOverlays(fillState, deckDoc(), cell = null, slideEl = 0)
238 rule.onNodeWithText("Fill Color").assertIsDisplayed()
239 rule.onNodeWithText("∅").performClick()
240 assertFalse(fillState.showSlideFillColor)
241 }
242
243 @Test fun slideElementStrokeColorOverlayApplies() {
244 val strokeState = DocumentOverlayState().apply { showSlideStrokeColor = true }
245 showCellOverlays(strokeState, deckDoc(), cell = null, slideEl = 0)
246 rule.onNodeWithText("Border Color").assertIsDisplayed()
247 rule.onNodeWithText("∅").performClick()
248 assertFalse(strokeState.showSlideStrokeColor)
249 }
250
251 @Test fun insertMenuImageItemLaunchesPicker() {
252 val s = DocumentScreenState()
253 val viewModel = vm(textDoc())
254 rule.setContent {
255 val launchers = rememberDocumentLaunchers(viewModel, s, onPickImageBytes = { _, _ -> })
256 InsertMenu(s = s, viewModel = viewModel, isTextDoc = true, focusedPara = 0, launchers = launchers)
257 }
258 s.insertMenu = true
259 rule.waitForIdle()
260 // Launching the picker opens a system UI under Robolectric; the menu must close first.
261 rule.onNodeWithText(str(R.string.image_1)).performClick()
262 assertFalse(s.insertMenu)
263 }
264
265 @Test fun cellResizeOverlayAppliesSizes() {
266 val state = DocumentOverlayState().apply { showCellResize = true }
267 showCellOverlays(state, sheetDoc())
268 rule.onNodeWithText(str(R.string.row_column_size_1), substring = true).assertExists()
269 rule.onNodeWithText(str(com.vayunmathur.library.ui.R.string.apply)).performClick()
270 assertFalse(state.showCellResize, "apply dismisses the resize dialog even with empty fields")
271 }
272
273 @Test fun cellCommentOverlayCancelDismisses() {
274 val state = DocumentOverlayState().apply { showCellComment = true }
275 showCellOverlays(state, sheetDoc())
276 rule.onNodeWithText(str(com.vayunmathur.library.ui.R.string.cancel)).performClick()
277 assertFalse(state.showCellComment)
278 }
279}