|
| 1 | +// |
| 2 | +// Aster Communications Inc. |
| 3 | +// |
| 4 | +// Copyright (c) 2026 Aster Communications Inc. |
| 5 | +// |
| 6 | +// This file is part of this project. |
| 7 | +// |
| 8 | +// This program is free software: you can redistribute it and/or modify |
| 9 | +// it under the terms of the GNU Affero General Public License as published by |
| 10 | +// the Free Software Foundation, either version 3 of the License, or |
| 11 | +// (at your option) any later version. |
| 12 | +// |
| 13 | +// This program is distributed in the hope that it will be useful, |
| 14 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +// GNU Affero General Public License for more details. |
| 17 | +// |
| 18 | +// You should have received a copy of the GNU Affero General Public License |
| 19 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | +// |
| 21 | + |
| 22 | +package org.astermail.android.ui.drawer |
| 23 | + |
| 24 | +import androidx.compose.material.icons.Icons |
| 25 | +import androidx.compose.material.icons.outlined.Folder |
| 26 | +import androidx.compose.ui.test.assert |
| 27 | +import androidx.compose.ui.test.assertIsDisplayed |
| 28 | +import androidx.compose.ui.test.filterToOne |
| 29 | +import androidx.compose.ui.test.hasAnyAncestor |
| 30 | +import androidx.compose.ui.test.hasSetTextAction |
| 31 | +import androidx.compose.ui.test.hasText |
| 32 | +import androidx.compose.ui.test.isPopup |
| 33 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 34 | +import androidx.compose.ui.test.onAllNodesWithText |
| 35 | +import androidx.compose.ui.test.onNodeWithTag |
| 36 | +import androidx.compose.ui.test.onNodeWithText |
| 37 | +import androidx.compose.ui.test.performClick |
| 38 | +import androidx.compose.ui.test.performScrollTo |
| 39 | +import androidx.compose.ui.test.performTextInput |
| 40 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 41 | +import org.astermail.android.design.AsterTheme |
| 42 | +import org.junit.Rule |
| 43 | +import org.junit.Test |
| 44 | +import org.junit.runner.RunWith |
| 45 | + |
| 46 | +@RunWith(AndroidJUnit4::class) |
| 47 | +class FolderNestingDrawerTest { |
| 48 | + |
| 49 | + @get:Rule |
| 50 | + val compose_rule = createComposeRule() |
| 51 | + |
| 52 | + private fun sample_folders() = listOf( |
| 53 | + drawer_folder_item(id = "t1", label = "Test 1", icon = Icons.Outlined.Folder, count = 0, depth = 0), |
| 54 | + drawer_folder_item(id = "apple", label = "Apple", icon = Icons.Outlined.Folder, count = 0, depth = 1), |
| 55 | + drawer_folder_item(id = "boy", label = "Boy", icon = Icons.Outlined.Folder, count = 0, depth = 1), |
| 56 | + drawer_folder_item(id = "cat", label = "Cat", icon = Icons.Outlined.Folder, count = 2, depth = 2), |
| 57 | + ) |
| 58 | + |
| 59 | + private fun sample_parent_options() = listOf( |
| 60 | + folder_parent_option(token = "t1", label = "Test 1", depth = 0, path_label = "Test 1"), |
| 61 | + folder_parent_option(token = "boy", label = "Boy", depth = 1, path_label = "Test 1 · Boy"), |
| 62 | + ) |
| 63 | + |
| 64 | + private fun set_drawer_content( |
| 65 | + on_create_folder: (String, String?) -> Unit = { _, _ -> }, |
| 66 | + ) { |
| 67 | + compose_rule.setContent { |
| 68 | + AsterTheme { |
| 69 | + DrawerContent( |
| 70 | + selected_id = "inbox", |
| 71 | + on_select = {}, |
| 72 | + on_close = {}, |
| 73 | + api_folder_items = sample_folders(), |
| 74 | + folder_parent_options = sample_parent_options(), |
| 75 | + on_create_folder = on_create_folder, |
| 76 | + ) |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + fun renders_nested_folder_rows() { |
| 83 | + set_drawer_content() |
| 84 | + compose_rule.onNodeWithText("Test 1").performScrollTo().assertIsDisplayed() |
| 85 | + compose_rule.onNodeWithText("Apple").performScrollTo().assertIsDisplayed() |
| 86 | + compose_rule.onNodeWithText("Boy").performScrollTo().assertIsDisplayed() |
| 87 | + compose_rule.onNodeWithText("Cat").performScrollTo().assertIsDisplayed() |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun create_folder_dialog_offers_parent_selection() { |
| 92 | + var created_name: String? = null |
| 93 | + var created_parent: String? = null |
| 94 | + set_drawer_content { name, parent -> |
| 95 | + created_name = name |
| 96 | + created_parent = parent |
| 97 | + } |
| 98 | + |
| 99 | + compose_rule.onNodeWithTag("create_folder").performScrollTo().performClick() |
| 100 | + compose_rule.onNodeWithText("Parent folder").assertIsDisplayed() |
| 101 | + compose_rule.onNodeWithTag("parent_folder_selector").assertIsDisplayed() |
| 102 | + |
| 103 | + compose_rule.onNodeWithTag("parent_folder_selector").performClick() |
| 104 | + compose_rule.onAllNodesWithText("None") |
| 105 | + .filterToOne(hasAnyAncestor(isPopup())) |
| 106 | + .assertIsDisplayed() |
| 107 | + compose_rule.onAllNodesWithText("Boy") |
| 108 | + .filterToOne(hasAnyAncestor(isPopup())) |
| 109 | + .performClick() |
| 110 | + |
| 111 | + compose_rule.onNodeWithText("Test 1 · Boy").assertIsDisplayed() |
| 112 | + |
| 113 | + compose_rule.onNode(hasSetTextAction()).performTextInput("Receipts") |
| 114 | + compose_rule.onNodeWithText("Save").performClick() |
| 115 | + |
| 116 | + compose_rule.runOnIdle { |
| 117 | + assert(created_name == "Receipts") |
| 118 | + assert(created_parent == "boy") |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + fun create_folder_defaults_to_no_parent() { |
| 124 | + var created_parent: String? = "sentinel" |
| 125 | + set_drawer_content { _, parent -> |
| 126 | + created_parent = parent |
| 127 | + } |
| 128 | + |
| 129 | + compose_rule.onNodeWithTag("create_folder").performScrollTo().performClick() |
| 130 | + compose_rule.onNode(hasSetTextAction()).performTextInput("Top Level") |
| 131 | + compose_rule.onNodeWithText("Save").performClick() |
| 132 | + |
| 133 | + compose_rule.runOnIdle { |
| 134 | + assert(created_parent == null) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + fun dropdown_none_option_clears_selection() { |
| 140 | + set_drawer_content() |
| 141 | + |
| 142 | + compose_rule.onNodeWithTag("create_folder").performScrollTo().performClick() |
| 143 | + compose_rule.onNodeWithTag("parent_folder_selector").performClick() |
| 144 | + compose_rule.onAllNodesWithText("Test 1") |
| 145 | + .filterToOne(hasAnyAncestor(isPopup())) |
| 146 | + .performClick() |
| 147 | + compose_rule.onNodeWithTag("parent_folder_selector").performClick() |
| 148 | + compose_rule.onAllNodesWithText("None") |
| 149 | + .filterToOne(hasAnyAncestor(isPopup())) |
| 150 | + .performClick() |
| 151 | + compose_rule.onNodeWithTag("parent_folder_selector").assert(hasText("None")) |
| 152 | + } |
| 153 | +} |
0 commit comments