• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.tests.R
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 CalculatedFolderSpecsTest : AbstractDeviceProfileTest() {
34     override val runningContext: Context = InstrumentationRegistry.getInstrumentation().context
35 
36     private val deviceSpec = deviceSpecs["phone"]!!
37 
38     @Before
setupnull39     fun setup() {
40         initializeVarsForPhone(deviceSpec)
41     }
42 
43     @Test
validate_matchWidthWorkspacenull44     fun validate_matchWidthWorkspace() {
45         val columns = 6
46 
47         // Loading workspace specs
48         val resourceHelperWorkspace = TestResourceHelper(context!!, R.xml.valid_workspace_file)
49         val workspaceSpecs = WorkspaceSpecs.create(resourceHelperWorkspace)
50 
51         // Loading folders specs
52         val resourceHelperFolder = TestResourceHelper(context!!, R.xml.valid_folders_specs)
53         val folderSpecs = FolderSpecs.create(resourceHelperFolder)
54 
55         assertThat(folderSpecs.widthSpecs.size).isEqualTo(2)
56         assertThat(folderSpecs.widthSpecs[0].cellSize.matchWorkspace).isEqualTo(true)
57         assertThat(folderSpecs.widthSpecs[1].cellSize.matchWorkspace).isEqualTo(false)
58 
59         // Validate width spec <= 800
60         var availableWidth = deviceSpec.naturalSize.first
61         var calculatedWorkspace = workspaceSpecs.getCalculatedWidthSpec(columns, availableWidth)
62         var calculatedWidthFolderSpec =
63             folderSpecs.getCalculatedWidthSpec(columns, availableWidth, calculatedWorkspace)
64         with(calculatedWidthFolderSpec) {
65             assertThat(availableSpace).isEqualTo(availableWidth)
66             assertThat(cells).isEqualTo(columns)
67             assertThat(startPaddingPx).isEqualTo(16.dpToPx())
68             assertThat(endPaddingPx).isEqualTo(16.dpToPx())
69             assertThat(gutterPx).isEqualTo(16.dpToPx())
70             assertThat(cellSizePx).isEqualTo(calculatedWorkspace.cellSizePx)
71         }
72 
73         // Validate width spec > 800
74         availableWidth = 2000.dpToPx()
75         calculatedWorkspace = workspaceSpecs.getCalculatedWidthSpec(columns, availableWidth)
76         calculatedWidthFolderSpec =
77             folderSpecs.getCalculatedWidthSpec(columns, availableWidth, calculatedWorkspace)
78         with(calculatedWidthFolderSpec) {
79             assertThat(availableSpace).isEqualTo(availableWidth)
80             assertThat(cells).isEqualTo(columns)
81             assertThat(startPaddingPx).isEqualTo(16.dpToPx())
82             assertThat(endPaddingPx).isEqualTo(16.dpToPx())
83             assertThat(gutterPx).isEqualTo(16.dpToPx())
84             assertThat(cellSizePx).isEqualTo(102.dpToPx())
85         }
86     }
87 
88     @Test
validate_matchHeightWorkspacenull89     fun validate_matchHeightWorkspace() {
90         // Hotseat is roughly 495px on a real device, it doesn't need to be precise on unit tests
91         val hotseatSize = 495
92         val statusBarHeight = deviceSpec.statusBarNaturalPx
93         val availableHeight = deviceSpec.naturalSize.second - statusBarHeight - hotseatSize
94         val rows = 5
95 
96         // Loading workspace specs
97         val resourceHelperWorkspace = TestResourceHelper(context!!, R.xml.valid_workspace_file)
98         val workspaceSpecs = WorkspaceSpecs.create(resourceHelperWorkspace)
99 
100         // Loading folders specs
101         val resourceHelperFolder = TestResourceHelper(context!!, R.xml.valid_folders_specs)
102         val folderSpecs = FolderSpecs.create(resourceHelperFolder)
103 
104         assertThat(folderSpecs.heightSpecs.size).isEqualTo(1)
105         assertThat(folderSpecs.heightSpecs[0].cellSize.matchWorkspace).isEqualTo(true)
106 
107         // Validate height spec
108         val calculatedWorkspace = workspaceSpecs.getCalculatedHeightSpec(rows, availableHeight)
109         val calculatedFolderSpec =
110             folderSpecs.getCalculatedHeightSpec(rows, availableHeight, calculatedWorkspace)
111         with(calculatedFolderSpec) {
112             assertThat(availableSpace).isEqualTo(availableHeight)
113             assertThat(cells).isEqualTo(rows)
114             assertThat(startPaddingPx).isEqualTo(24.dpToPx())
115             assertThat(endPaddingPx).isEqualTo(64.dpToPx())
116             assertThat(gutterPx).isEqualTo(16.dpToPx())
117             assertThat(cellSizePx).isEqualTo(calculatedWorkspace.cellSizePx)
118         }
119     }
120 }
121