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 
17 package com.android.launcher3.responsive
18 
19 import android.content.Context
20 import androidx.test.ext.junit.runners.AndroidJUnit4
21 import androidx.test.filters.SmallTest
22 import androidx.test.platform.app.InstrumentationRegistry
23 import com.android.launcher3.AbstractDeviceProfileTest
24 import com.android.launcher3.responsive.ResponsiveSpec.Companion.ResponsiveSpecType
25 import com.android.launcher3.util.TestResourceHelper
26 import com.google.common.truth.Truth.assertThat
27 import org.junit.Before
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 
31 @SmallTest
32 @RunWith(AndroidJUnit4::class)
33 class HotseatSpecsProviderTest : AbstractDeviceProfileTest() {
34     override val runningContext: Context = InstrumentationRegistry.getInstrumentation().context
35     val deviceSpec = deviceSpecs["phone"]!!
36     val aspectRatio = deviceSpec.naturalSize.first.toFloat() / deviceSpec.naturalSize.second
37 
38     @Before
setupnull39     fun setup() {
40         initializeVarsForPhone(deviceSpec)
41     }
42 
43     @Test
parseValidFilenull44     fun parseValidFile() {
45         val hotseatSpecsProvider =
46             HotseatSpecsProvider.create(TestResourceHelper(context, "valid_hotseat_file".xmlToId()))
47         val specs = hotseatSpecsProvider.getSpecsByAspectRatio(aspectRatio)
48 
49         val expectedHeightSpecs =
50             listOf(
51                 HotseatSpec(
52                     maxAvailableSize = 847.dpToPx(),
53                     dimensionType = ResponsiveSpec.DimensionType.HEIGHT,
54                     specType = ResponsiveSpecType.Hotseat,
55                     hotseatQsbSpace = SizeSpec(24f.dpToPx()),
56                     edgePadding = SizeSpec(48f.dpToPx())
57                 ),
58                 HotseatSpec(
59                     maxAvailableSize = 9999.dpToPx(),
60                     dimensionType = ResponsiveSpec.DimensionType.HEIGHT,
61                     specType = ResponsiveSpecType.Hotseat,
62                     hotseatQsbSpace = SizeSpec(36f.dpToPx()),
63                     edgePadding = SizeSpec(48f.dpToPx())
64                 ),
65             )
66 
67         assertThat(specs.heightSpecs.size).isEqualTo(expectedHeightSpecs.size)
68         assertThat(specs.heightSpecs[0]).isEqualTo(expectedHeightSpecs[0])
69         assertThat(specs.heightSpecs[1]).isEqualTo(expectedHeightSpecs[1])
70 
71         assertThat(specs.widthSpecs.size).isEqualTo(0)
72     }
73 
74     @Test
parseValidLandscapeFilenull75     fun parseValidLandscapeFile() {
76         val hotseatSpecsProvider =
77             HotseatSpecsProvider.create(
78                 TestResourceHelper(context, "valid_hotseat_land_file".xmlToId())
79             )
80         val specs = hotseatSpecsProvider.getSpecsByAspectRatio(aspectRatio)
81         assertThat(specs.heightSpecs.size).isEqualTo(0)
82 
83         val expectedWidthSpecs =
84             listOf(
85                 HotseatSpec(
86                     maxAvailableSize = 743.dpToPx(),
87                     dimensionType = ResponsiveSpec.DimensionType.WIDTH,
88                     specType = ResponsiveSpecType.Hotseat,
89                     hotseatQsbSpace = SizeSpec(0f),
90                     edgePadding = SizeSpec(48f.dpToPx())
91                 ),
92                 HotseatSpec(
93                     maxAvailableSize = 9999.dpToPx(),
94                     dimensionType = ResponsiveSpec.DimensionType.WIDTH,
95                     specType = ResponsiveSpecType.Hotseat,
96                     hotseatQsbSpace = SizeSpec(0f),
97                     edgePadding = SizeSpec(64f.dpToPx())
98                 ),
99             )
100 
101         assertThat(specs.widthSpecs.size).isEqualTo(expectedWidthSpecs.size)
102         assertThat(specs.widthSpecs[0]).isEqualTo(expectedWidthSpecs[0])
103         assertThat(specs.widthSpecs[1]).isEqualTo(expectedWidthSpecs[1])
104     }
105 
106     @Test(expected = IllegalStateException::class)
parseInvalidFile_spaceIsNotFixedSize_throwsErrornull107     fun parseInvalidFile_spaceIsNotFixedSize_throwsError() {
108         HotseatSpecsProvider.create(
109             TestResourceHelper(context, "invalid_hotseat_file_case_1".xmlToId())
110         )
111     }
112 
113     @Test(expected = IllegalStateException::class)
parseInvalidFile_invalidFixedSize_throwsErrornull114     fun parseInvalidFile_invalidFixedSize_throwsError() {
115         HotseatSpecsProvider.create(
116             TestResourceHelper(context, "invalid_hotseat_file_case_2".xmlToId())
117         )
118     }
119 }
120