1 /* 2 * 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.launcher3.folder 18 19 import android.graphics.Point 20 import androidx.test.ext.junit.runners.AndroidJUnit4 21 import androidx.test.filters.SmallTest 22 import org.junit.Test 23 import org.junit.runner.RunWith 24 25 data class TestCase(val maxCountX: Int, val maxCountY: Int, val totalItems: Int) 26 27 @SmallTest 28 @RunWith(AndroidJUnit4::class) 29 class FolderPagedViewTest { 30 31 companion object { makeFolderGridOrganizernull32 private fun makeFolderGridOrganizer(testCase: TestCase): FolderGridOrganizer { 33 val folderGridOrganizer = FolderGridOrganizer(testCase.maxCountX, testCase.maxCountY) 34 folderGridOrganizer.setContentSize(testCase.totalItems) 35 return folderGridOrganizer 36 } 37 } 38 39 @Test setContentSizenull40 fun setContentSize() { 41 assertCountXandY( 42 TestCase(maxCountX = 4, maxCountY = 3, totalItems = 22), 43 expectedCountX = 4, 44 expectedCountY = 3 45 ) 46 assertCountXandY( 47 TestCase(maxCountX = 4, maxCountY = 3, totalItems = 8), 48 expectedCountX = 3, 49 expectedCountY = 3 50 ) 51 assertCountXandY( 52 TestCase(maxCountX = 4, maxCountY = 3, totalItems = 3), 53 expectedCountX = 2, 54 expectedCountY = 2 55 ) 56 } 57 assertCountXandYnull58 private fun assertCountXandY(testCase: TestCase, expectedCountX: Int, expectedCountY: Int) { 59 val folderGridOrganizer = makeFolderGridOrganizer(testCase) 60 assert(folderGridOrganizer.countX == expectedCountX) { 61 "Error on expected countX $expectedCountX got ${folderGridOrganizer.countX} using test case $testCase" 62 } 63 assert(folderGridOrganizer.countY == expectedCountY) { 64 "Error on expected countY $expectedCountY got ${folderGridOrganizer.countY} using test case $testCase" 65 } 66 } 67 68 @Test getPosForRanknull69 fun getPosForRank() { 70 assertFolderRank( 71 TestCase(maxCountX = 4, maxCountY = 3, totalItems = 22), 72 expectedPos = Point(0, 0), 73 rank = 0 74 ) 75 assertFolderRank( 76 TestCase(maxCountX = 4, maxCountY = 3, totalItems = 22), 77 expectedPos = Point(1, 0), 78 rank = 1 79 ) 80 assertFolderRank( 81 TestCase(maxCountX = 4, maxCountY = 3, totalItems = 22), 82 expectedPos = Point(3, 0), 83 rank = 3 84 ) 85 assertFolderRank( 86 TestCase(maxCountX = 4, maxCountY = 3, totalItems = 22), 87 expectedPos = Point(2, 1), 88 rank = 6 89 ) 90 val testCase = TestCase(maxCountX = 4, maxCountY = 3, totalItems = 22) 91 // Rank 16 and 38 should yield the same point since 38 % 12 == 2 92 val folderGridOrganizer = makeFolderGridOrganizer(testCase) 93 assertFolderRank(testCase, expectedPos = folderGridOrganizer.getPosForRank(2), rank = 38) 94 } 95 assertFolderRanknull96 private fun assertFolderRank(testCase: TestCase, expectedPos: Point, rank: Int) { 97 val folderGridOrganizer = makeFolderGridOrganizer(testCase) 98 val pos = folderGridOrganizer.getPosForRank(rank) 99 assert(pos == expectedPos) { 100 "Expected pos = $expectedPos doesn't match pos = $pos for the given rank $rank and the give test case $testCase" 101 } 102 } 103 104 @Test isItemInPreviewnull105 fun isItemInPreview() { 106 val folderGridOrganizer = FolderGridOrganizer(5, 8) 107 folderGridOrganizer.setContentSize(ClippedFolderIconLayoutRule.MAX_NUM_ITEMS_IN_PREVIEW - 1) 108 // Very few items 109 for (i in 0..3) { 110 assertItemsInPreview( 111 TestCase( 112 maxCountX = 5, 113 maxCountY = 8, 114 totalItems = ClippedFolderIconLayoutRule.MAX_NUM_ITEMS_IN_PREVIEW - 1 115 ), 116 expectedIsInPreview = true, 117 page = 0, 118 rank = i 119 ) 120 } 121 for (i in 4..40) { 122 assertItemsInPreview( 123 TestCase( 124 maxCountX = 5, 125 maxCountY = 8, 126 totalItems = ClippedFolderIconLayoutRule.MAX_NUM_ITEMS_IN_PREVIEW - 1 127 ), 128 expectedIsInPreview = false, 129 page = 0, 130 rank = i 131 ) 132 } 133 // Full of items 134 assertItemsInPreview( 135 TestCase(maxCountX = 5, maxCountY = 8, totalItems = 40), 136 expectedIsInPreview = false, 137 page = 0, 138 rank = 2 139 ) 140 assertItemsInPreview( 141 TestCase(maxCountX = 5, maxCountY = 8, totalItems = 40), 142 expectedIsInPreview = false, 143 page = 0, 144 rank = 2 145 ) 146 assertItemsInPreview( 147 TestCase(maxCountX = 5, maxCountY = 8, totalItems = 40), 148 expectedIsInPreview = true, 149 page = 0, 150 rank = 5 151 ) 152 assertItemsInPreview( 153 TestCase(maxCountX = 5, maxCountY = 8, totalItems = 40), 154 expectedIsInPreview = true, 155 page = 0, 156 rank = 6 157 ) 158 } 159 assertItemsInPreviewnull160 private fun assertItemsInPreview( 161 testCase: TestCase, 162 expectedIsInPreview: Boolean, 163 page: Int, 164 rank: Int 165 ) { 166 val folderGridOrganizer = makeFolderGridOrganizer(testCase) 167 val isInPreview = folderGridOrganizer.isItemInPreview(page, rank) 168 assert(isInPreview == expectedIsInPreview) { 169 "Item preview state should be $expectedIsInPreview but got $isInPreview, for page $page and rank $rank, for test case $testCase" 170 } 171 } 172 } 173