250 lines · 11.6 KB · KotlinRaw
package com.cottondb.albumsimport android.app.Applicationimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onAllNodesWithTextimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport com.cottondb.albums.library.LibraryItemimport com.cottondb.albums.library.MediaAccessimport com.cottondb.albums.source.AlbumsActsimport com.cottondb.albums.source.Answerimport com.cottondb.albums.source.Fixtureimport com.cottondb.albums.source.ListKindimport com.cottondb.albums.source.Marksimport com.cottondb.albums.source.Measureimport com.cottondb.albums.source.Measuredimport com.cottondb.albums.source.PhoneTrashimport kotlinx.coroutines.runBlockingimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.RuntimeEnvironmentimport org.robolectric.annotation.Config/** * THE LOOK-ONLY BUILD (the owner, 27 Nov 2026; [AlbumsBuild]): with the one switch off, Tidy, a Set, a list, an album, * You and the sheets draw nothing that bins or removes, the look-only line stands on Tidy, Sets, a Set and a list, and * no act reaches the source's marks or the phone's trash (MediaStore.createTrashRequest). Each screen is drawn again * with the switch on, to show the test would see what it hides. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h900dp-mdpi", application = Application::class)class AlbumsLookOnlyTest { @get:Rule val compose = createComposeRule() private val t0 = 1_700_000_000_000L private val words: (String, Map<String, String>) -> String = { k, f -> AlbumsWords.of(null, k, f) } private val line = AlbumsWords.of(null, "look_only") private fun item(id: Long, taken: Long, path: String = "DCIM/Camera/", name: String = "IMG_$id.jpg") = LibraryItem(id, false, "external_primary", 1, "Camera", path, name, taken, taken / 1000, taken / 1000, 1_000_000, 4000, 3000, "image/jpeg", null, false, null, id, false) private fun print(scene: Int, shift: Int = 0) = ByteArray(256) { i -> (((i * (scene * 37 + 11)) % 251) + if (i % 7 == 0) shift else 0).coerceIn(0, 255).toByte() } private fun measure(id: Long, scene: Int, sharp: Float = 100f, shift: Int = 0, shot: Boolean = false) = Measure(id, if (scene == 1) 0x0F0F0F0F0F0F0F0FL else 0x7A3C1E5B92D4C681L * scene, print(scene, shift), sharp, 0.5f, 0.8f, shot, false, 0, null, false) /** One moment of five shots (a Set), another scene, and two screenshots (a list); [binned] puts the screenshots in the Bin. */ private fun fixture(binned: Boolean = false): Fixture { val items = (1L..5L).map { item(it, t0 + it * 1_000) } + item(6, t0 + 60_000) + listOf(item(7, t0 + 120_000, "Pictures/Screenshots/", "Screenshot_1.png"), item(8, t0 + 130_000, "Pictures/Screenshots/", "Screenshot_2.png")) val looks = (1L..5L).map { measure(it, 1, sharp = 100f + it * 40, shift = it.toInt()) } + measure(6, 2) + measure(7, 3, shot = true) + measure(8, 4, shot = true) val f = Fixture(Marks(null), { Measured(looks, emptyList()) }, words, now = { t0 + 86_400_000 }, facesOn = { true }) runBlocking { f.refresh(items, emptyList(), emptyList()) } if (binned) assertTrue(f.binMove(listOf(7L, 8L), 2, "screenshots") is Answer.Done) return f } private fun state(canRemove: Boolean, f: Fixture = fixture()) = AlbumsState(null, RuntimeEnvironment.getApplication(), canRemove, f) private fun gone(tag: String) = compose.onNodeWithTag(tag, useUnmergedTree = true).assertDoesNotExist() private fun shown(tag: String) = compose.onNodeWithTag(tag, useUnmergedTree = true).assertExists() private fun noWord(vararg ws: String) = ws.forEach { w -> assertEquals("\"$w\" is drawn", 0, compose.onAllNodesWithText(w, substring = true, useUnmergedTree = true).fetchSemanticsNodes().size) } @Test fun the_build_ships_look_only() { assertFalse(AlbumsBuild.canRemove) assertFalse(state(canRemove = AlbumsBuild.canRemove).canRemove) assertEquals("This build only looks. Nothing can be removed yet.", line) } @Test fun look_only_tidy_draws_no_bin_row_even_with_photos_in_the_bin_and_says_the_line() { val s = state(canRemove = false, fixture(binned = true)) compose.setContent { TidyScreen(s) } gone("albums-tidy-bin"); gone("albums-tidy-delete") noWord("In the Bin", "Delete") compose.onNodeWithText(line).assertIsDisplayed() } @Test fun look_only_tidy_never_says_what_he_could_free_only_the_sizes() { assertEquals("Found so far", TidyHead.of(canRemove = false, passFinished = false, couldFree = 52_000_000_000, w = words)) assertNull(TidyHead.of(canRemove = false, passFinished = true, couldFree = 52_000_000_000, w = words)) // The hidden path: with the switch on, the finished pass says what he could free. assertEquals("You could free about 52.0 GB more", TidyHead.of(canRemove = true, passFinished = true, couldFree = 52_000_000_000, w = words)) } @Test fun hidden_path_tidy_draws_the_bin_row_with_the_switch_on() { val s = state(canRemove = true, fixture(binned = true)) compose.setContent { TidyScreen(s) } shown("albums-tidy-bin"); shown("albums-tidy-delete") gone("albums-look-only") } @Test fun look_only_sets_draw_no_keep_on_a_row_and_say_the_line() { val s = state(canRemove = false) compose.setContent { SetsScreen(s) } shown("albums-set-open") gone("albums-set-keep") noWord("Keep", "Bin") compose.onNodeWithText(line).assertIsDisplayed() } @Test fun look_only_a_set_draws_no_keep_n_bin_m_or_keep_all_and_says_the_line() { val one = state(canRemove = false) val id = one.source.sets().single().id compose.setContent { SetScreen(one, id) } gone("albums-set-go"); gone("albums-set-keep-all"); gone("albums-undo") noWord("Keep", "Bin") compose.onNodeWithText(line).assertIsDisplayed() compose.onNodeWithTag("albums-set-title").assertExists() } @Test fun hidden_path_a_set_draws_keep_n_bin_m_and_keep_all_with_the_switch_on() { val s = state(canRemove = true) val id = s.source.sets().single().id compose.setContent { SetScreen(s, id) } shown("albums-set-go"); shown("albums-set-keep-all") gone("albums-look-only") } @Test fun look_only_a_list_draws_no_select_keep_bin_or_bin_all_and_says_the_line() { val s = state(canRemove = false) s.selecting = true // even if selecting were left on, no Keep or Bin is drawn s.chosen[7L] = true compose.setContent { ListScreen(s, ListKind.Screenshots) } gone("albums-bin-all"); gone("albums-select"); gone("albums-select-keep"); gone("albums-select-bin") noWord("Bin") compose.onNodeWithText(line).assertIsDisplayed() } @Test fun hidden_path_a_list_draws_bin_all_and_select_with_the_switch_on() { val s = state(canRemove = true) compose.setContent { ListScreen(s, ListKind.Screenshots) } shown("albums-bin-all"); shown("albums-select") gone("albums-look-only") } @Test fun look_only_an_album_selection_offers_add_to_album_but_never_bin() { val s = state(canRemove = false) s.selecting = true s.chosen[1L] = true compose.setContent { AlbumScreen(s, "all", choosingFor = null) } shown("albums-select-add"); gone("albums-select-bin") } @Test fun hidden_path_an_album_selection_offers_bin_with_the_switch_on() { val on = state(canRemove = true).apply { selecting = true; chosen[1L] = true } compose.setContent { AlbumScreen(on, "all", choosingFor = null) } shown("albums-select-add"); shown("albums-select-bin") } @Test fun look_only_draws_no_delete_or_bin_all_sheet() { val s = state(canRemove = false) s.sheet = Sheet.Delete(listOf(7L, 8L), 2, 2_000_000) compose.setContent { s.sheet?.let { Sheets(s, it) } } compose.waitForIdle() gone("albums-sheet"); gone("albums-delete-title"); gone("albums-delete-go") assertNull(s.sheet) s.sheet = Sheet.BinAll(ListKind.Screenshots, listOf(7L, 8L), 2) compose.waitForIdle() gone("albums-sheet"); gone("albums-bin-all-go") assertNull(s.sheet) } @Test fun hidden_path_draws_the_delete_sheet_with_the_switch_on() { val s = state(canRemove = true) s.sheet = Sheet.Delete(listOf(7L, 8L), 2, 2_000_000) compose.setContent { s.sheet?.let { Sheets(s, it) } } shown("albums-delete-title"); shown("albums-delete-go") } @Test fun look_only_has_no_bin_tab_no_bin_row_in_you_no_delete_in_one_step_and_no_way_to_the_bin() { val context = RuntimeEnvironment.getApplication() val s = state(canRemove = false, fixture(binned = true)) assertFalse(s.tabs.any { "bin" in it }) val ids = youRows(s, context, 0).map { it.id } assertFalse("bin" in ids) assertFalse("manage" in ids) assertFalse(s.offerManage) s.push(Screen.BinScreen) assertTrue(s.stack.isEmpty()) // The hidden path: the rows stand with the switch on. val on = state(canRemove = true, fixture(binned = true)) val onIds = youRows(on, context, 0).map { it.id } assertTrue("bin" in onIds) assertEquals(MediaAccess.canOfferManage, "manage" in onIds) on.push(Screen.BinScreen) assertEquals(listOf<Screen>(Screen.BinScreen), on.stack.toList()) } @Test fun look_only_every_removing_act_is_refused_before_the_source_and_never_asks_the_phones_trash() { val f = fixture(binned = true) val asked = ArrayList<List<Long>>() val acts = AlbumsActs(f, PhoneTrash { _, ids -> asked += ids }) val set = f.sets().single() val members = set.members.map { it.id } val bin = f.bin() val refused = listOf( acts.run { it.setKeep(set.id, members, members.size, set.keepers) }, acts.run { it.keep(listOf(6L), 1) }, acts.run { it.binMove(listOf(6L), 1, "album") }, acts.run { it.bringBack(bin.ids, bin.count) }, acts.run { it.undo("act-1") }, // The Bin exactly as shown, the one act that would build createTrashRequest: refused, never asked. acts.run { it.free(bin.ids, bin.count) }, ) refused.forEach { assertEquals(Answer.Changed(line), it) } assertEquals(emptyList<List<Long>>(), asked) // Nothing was marked: the Set still stands, the Bin is as it was, 6 is in no Bin. assertEquals(1, f.sets().size) assertEquals(bin.ids.toSet(), f.bin().ids.toSet()) // Albums are not removal: they still go through. assertTrue(acts.run { it.makeAlbum("Goa", listOf(6L), 1) } is Answer.Done) } @Test fun look_only_state_never_queues_a_trash_request_from_an_act_or_from_the_trash_itself() { val s = state(canRemove = false, fixture(binned = true)) val bin = s.source.bin() s.act({ it.free(bin.ids, bin.count) }) assertEquals(line, s.quiet) assertNull(s.trashInFlight) assertTrue(s.trashQueue.isEmpty()) // Even asked directly, the phone's trash is not built in this build. s.phoneTrash.ask("act-x", bin.ids) assertNull(s.trashInFlight) assertTrue(s.trashQueue.isEmpty()) }}