Skip to content

Commit 6e8aa2a

Browse files
committed
test: cover spaces search filter with SpacesListFragmentTest
Extract the personal-space filtering into a testable filterSpaces() extension and add unit coverage for the multi-personal name match, single-personal pin-at-front, and single-personal regression paths.
1 parent cde49b7 commit 6e8aa2a

2 files changed

Lines changed: 49 additions & 15 deletions

File tree

‎owncloudApp/src/main/java/com/owncloud/android/presentation/spaces/SpacesListFragment.kt‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,7 @@ class SpacesListFragment :
181181
collectLatestLifecycleFlow(spacesListViewModel.spacesList) { uiState ->
182182
var spacesToListFiltered: List<OCSpace>
183183
if (uiState.searchFilter != "") {
184-
val searchFilter = uiState.searchFilter.lowercase()
185-
spacesToListFiltered = if (isMultiPersonal) {
186-
uiState.spaces.filter { it.name.lowercase().contains(searchFilter) && shouldShowDisabledSpace(it) }
187-
} else {
188-
uiState.spaces.filter { it.name.lowercase().contains(searchFilter) && !it.isPersonal &&
189-
shouldShowDisabledSpace(it) }
190-
}
191-
if (!isMultiPersonal) {
192-
val personalSpace = uiState.spaces.find { it.isPersonal }
193-
personalSpace?.let {
194-
spacesToListFiltered = spacesToListFiltered.toMutableList().apply {
195-
add(0, personalSpace)
196-
}
197-
}
198-
}
184+
spacesToListFiltered = uiState.spaces.filterSpaces(uiState.searchFilter, isMultiPersonal, ::shouldShowDisabledSpace)
199185
showOrHideEmptyView(spacesToListFiltered)
200186
spacesListAdapter.setData(spacesToListFiltered, isMultiPersonal)
201187
} else {
@@ -524,3 +510,16 @@ class SpacesListFragment :
524510
}
525511

526512
}
513+
514+
internal fun List<OCSpace>.filterSpaces(
515+
query: String,
516+
multi: Boolean,
517+
visible: (OCSpace) -> Boolean,
518+
): List<OCSpace> {
519+
val queryLowercase = query.lowercase()
520+
val spacesMatchingName = filter { it.name.lowercase().contains(queryLowercase) }
521+
val matchingSpaces = spacesMatchingName.filter { (multi || !it.isPersonal) && visible(it) }
522+
if (multi) return matchingSpaces
523+
val personalSpace = find { it.isPersonal } ?: return matchingSpaces
524+
return listOf(personalSpace) + matchingSpaces
525+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* ownCloud Android client application
3+
*
4+
* @author Matt Van Horn
5+
* Copyright (C) 2026 ownCloud GmbH.
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License version 2,
9+
* as published by the Free Software Foundation.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
package com.owncloud.android.presentation.spaces
21+
22+
import com.owncloud.android.testutil.OC_SPACE_PERSONAL
23+
import org.junit.Assert.assertEquals
24+
import org.junit.Test
25+
26+
class SpacesListFragmentTest {
27+
@Test
28+
fun `filterSpaces filters multi-personal names and preserves single-personal pinning`() {
29+
val personalSpaces = listOf(OC_SPACE_PERSONAL.copy(name = "Alice Personal"), OC_SPACE_PERSONAL.copy(name = "Bob Personal"))
30+
val singlePersonalSpaces = listOf(OC_SPACE_PERSONAL.copy(name = "Project Personal"))
31+
assertEquals(personalSpaces, personalSpaces.filterSpaces("personal", true) { true })
32+
assertEquals(listOf(personalSpaces[0]), personalSpaces.filterSpaces("alice", true) { true })
33+
assertEquals(singlePersonalSpaces, singlePersonalSpaces.filterSpaces("project", false) { true })
34+
}
35+
}

0 commit comments

Comments
 (0)