1 /*
2  * Copyright (C) 2023 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 package com.android.launcher3.util
17 
18 import android.content.Context
19 import android.content.res.Configuration
20 import android.util.DisplayMetrics
21 import androidx.test.core.app.ApplicationProvider
22 import androidx.test.ext.junit.runners.AndroidJUnit4
23 import androidx.test.filters.SmallTest
24 import com.google.common.truth.Truth.assertThat
25 import org.junit.Before
26 import org.junit.Test
27 import org.junit.runner.RunWith
28 
29 @SmallTest
30 @RunWith(AndroidJUnit4::class)
31 class CellContentDimensionsTest {
32     private var context: Context? = null
33     private val runningContext: Context = ApplicationProvider.getApplicationContext()
34     private lateinit var iconSizeSteps: IconSizeSteps
35 
36     @Before
setupnull37     fun setup() {
38         // 160dp makes 1px = 1dp
39         val config =
40             Configuration(runningContext.resources.configuration).apply {
41                 this.densityDpi = DisplayMetrics.DENSITY_DEFAULT
42                 fontScale = 1.0f
43             }
44         context = runningContext.createConfigurationContext(config)
45         iconSizeSteps = IconSizeSteps(context!!.resources)
46     }
47 
48     @Test
dimensionsFitTheCellnull49     fun dimensionsFitTheCell() {
50         val cellSize = Pair(80, 104)
51         val cellContentDimensions =
52             CellContentDimensions(iconSizePx = 66, iconDrawablePaddingPx = 8, iconTextSizePx = 14)
53 
54         val contentHeight =
55             cellContentDimensions.resizeToFitCellHeight(cellSize.second, iconSizeSteps)
56 
57         assertThat(contentHeight).isEqualTo(93)
58         cellContentDimensions.run {
59             assertThat(iconSizePx).isEqualTo(66)
60             assertThat(iconDrawablePaddingPx).isEqualTo(8)
61             assertThat(iconTextSizePx).isEqualTo(14)
62         }
63     }
64 
65     @Test
decreasePaddingnull66     fun decreasePadding() {
67         val cellSize = Pair(67, 87)
68         val cellContentDimensions =
69             CellContentDimensions(iconSizePx = 66, iconDrawablePaddingPx = 8, iconTextSizePx = 14)
70 
71         val contentHeight =
72             cellContentDimensions.resizeToFitCellHeight(cellSize.second, iconSizeSteps)
73 
74         assertThat(contentHeight).isEqualTo(87)
75         cellContentDimensions.run {
76             assertThat(iconSizePx).isEqualTo(66)
77             assertThat(iconDrawablePaddingPx).isEqualTo(2)
78             assertThat(iconTextSizePx).isEqualTo(14)
79         }
80     }
81 
82     @Test
decreaseIconnull83     fun decreaseIcon() {
84         val cellSize = Pair(65, 84)
85         val cellContentDimensions =
86             CellContentDimensions(iconSizePx = 66, iconDrawablePaddingPx = 8, iconTextSizePx = 14)
87 
88         val contentHeight =
89             cellContentDimensions.resizeToFitCellHeight(cellSize.second, iconSizeSteps)
90 
91         assertThat(contentHeight).isEqualTo(82)
92         cellContentDimensions.run {
93             assertThat(iconSizePx).isEqualTo(63)
94             assertThat(iconDrawablePaddingPx).isEqualTo(0)
95             assertThat(iconTextSizePx).isEqualTo(14)
96         }
97     }
98 
99     @Test
decreaseTextnull100     fun decreaseText() {
101         val cellSize = Pair(63, 81)
102         val cellContentDimensions =
103             CellContentDimensions(iconSizePx = 66, iconDrawablePaddingPx = 8, iconTextSizePx = 14)
104 
105         val contentHeight =
106             cellContentDimensions.resizeToFitCellHeight(cellSize.second, iconSizeSteps)
107 
108         assertThat(contentHeight).isEqualTo(81)
109         cellContentDimensions.run {
110             assertThat(iconSizePx).isEqualTo(63)
111             assertThat(iconDrawablePaddingPx).isEqualTo(0)
112             assertThat(iconTextSizePx).isEqualTo(13)
113         }
114     }
115 
116     @Test
decreaseIconAndTextTwoStepsnull117     fun decreaseIconAndTextTwoSteps() {
118         val cellSize = Pair(60, 78)
119         val cellContentDimensions =
120             CellContentDimensions(iconSizePx = 66, iconDrawablePaddingPx = 8, iconTextSizePx = 14)
121 
122         val contentHeight =
123             cellContentDimensions.resizeToFitCellHeight(cellSize.second, iconSizeSteps)
124 
125         assertThat(contentHeight).isEqualTo(77)
126         cellContentDimensions.run {
127             assertThat(iconSizePx).isEqualTo(61)
128             assertThat(iconDrawablePaddingPx).isEqualTo(0)
129             assertThat(iconTextSizePx).isEqualTo(12)
130         }
131     }
132 
133     @Test
decreaseIconAndTextToMinimumnull134     fun decreaseIconAndTextToMinimum() {
135         val cellSize = Pair(52, 63)
136         val cellContentDimensions =
137             CellContentDimensions(iconSizePx = 66, iconDrawablePaddingPx = 8, iconTextSizePx = 14)
138 
139         val contentHeight =
140             cellContentDimensions.resizeToFitCellHeight(cellSize.second, iconSizeSteps)
141 
142         assertThat(contentHeight).isEqualTo(63)
143         cellContentDimensions.run {
144             assertThat(iconSizePx).isEqualTo(52)
145             assertThat(iconDrawablePaddingPx).isEqualTo(0)
146             assertThat(iconTextSizePx).isEqualTo(8)
147         }
148     }
149 }
150