1 /* <lambda>null2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.systemui.qs.shared.model 18 19 import androidx.test.ext.junit.runners.AndroidJUnit4 20 import androidx.test.filters.SmallTest 21 import com.android.systemui.SysuiTestCase 22 import com.google.common.truth.Truth.assertThat 23 import org.junit.Test 24 import org.junit.runner.RunWith 25 26 @SmallTest 27 @RunWith(AndroidJUnit4::class) 28 class GroupAndSortCategoryAndNameTest : SysuiTestCase() { 29 30 private val elements = 31 listOf( 32 CategoryAndName(TileCategory.DISPLAY, "B"), 33 CategoryAndName(TileCategory.PRIVACY, "A"), 34 CategoryAndName(TileCategory.DISPLAY, "C"), 35 CategoryAndName(TileCategory.UTILITIES, "B"), 36 CategoryAndName(TileCategory.CONNECTIVITY, "A"), 37 CategoryAndName(TileCategory.PROVIDED_BY_APP, "B"), 38 CategoryAndName(TileCategory.CONNECTIVITY, "C"), 39 CategoryAndName(TileCategory.ACCESSIBILITY, "A") 40 ) 41 42 @Test 43 fun allElementsInResult() { 44 val grouped = groupAndSort(elements) 45 val allValues = grouped.values.reduce { acc, el -> acc + el } 46 assertThat(allValues).containsExactlyElementsIn(elements) 47 } 48 49 @Test 50 fun groupedByCategory() { 51 val grouped = groupAndSort(elements) 52 grouped.forEach { tileCategory, categoryAndNames -> 53 categoryAndNames.forEach { element -> 54 assertThat(element.category).isEqualTo(tileCategory) 55 } 56 } 57 } 58 59 @Test 60 fun sortedAlphabeticallyInEachCategory() { 61 val grouped = groupAndSort(elements) 62 grouped.values.forEach { elements -> 63 assertThat(elements.map(CategoryAndName::name)).isInOrder() 64 } 65 } 66 67 @Test 68 fun categoriesSortedInNaturalOrder() { 69 val grouped = groupAndSort(elements) 70 assertThat(grouped.keys).isInOrder() 71 } 72 73 @Test 74 fun missingCategoriesAreNotInResult() { 75 val grouped = groupAndSort(elements.filterNot { it.category == TileCategory.CONNECTIVITY }) 76 assertThat(grouped.keys).doesNotContain(TileCategory.CONNECTIVITY) 77 } 78 79 companion object { 80 private fun CategoryAndName(category: TileCategory, name: String): CategoryAndName { 81 return object : CategoryAndName { 82 override val category = category 83 override val name = name 84 } 85 } 86 } 87 } 88