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 as TestR 25 import com.android.launcher3.util.TestResourceHelper 26 import com.google.common.truth.Truth.assertThat 27 import org.junit.Test 28 import org.junit.runner.RunWith 29 30 @SmallTest 31 @RunWith(AndroidJUnit4::class) 32 class CalculatedWorkspaceSpecTest : AbstractDeviceProfileTest() { 33 override val runningContext: Context = InstrumentationRegistry.getInstrumentation().context 34 35 /** 36 * This test tests: 37 * - (height spec) gets the correct breakpoint from the XML - skips the first 2 breakpoints 38 * - (height spec) do the correct calculations for available space and fixed size 39 * - (width spec) do the correct calculations for remainder space and fixed size 40 */ 41 @Test normalPhone_returnsThirdBreakpointSpecnull42 fun normalPhone_returnsThirdBreakpointSpec() { 43 val deviceSpec = deviceSpecs["phone"]!! 44 initializeVarsForPhone(deviceSpec) 45 46 val availableWidth = deviceSpec.naturalSize.first 47 // Hotseat size is roughly 495px on a real device, 48 // it doesn't need to be precise on unit tests 49 val availableHeight = deviceSpec.naturalSize.second - deviceSpec.statusBarNaturalPx - 495 50 51 val workspaceSpecs = 52 WorkspaceSpecs.create(TestResourceHelper(context!!, TestR.xml.valid_workspace_file)) 53 val widthSpec = workspaceSpecs.getCalculatedWidthSpec(4, availableWidth) 54 val heightSpec = workspaceSpecs.getCalculatedHeightSpec(5, availableHeight) 55 56 assertThat(widthSpec.availableSpace).isEqualTo(availableWidth) 57 assertThat(widthSpec.cells).isEqualTo(4) 58 assertThat(widthSpec.startPaddingPx).isEqualTo(58) 59 assertThat(widthSpec.endPaddingPx).isEqualTo(58) 60 assertThat(widthSpec.gutterPx).isEqualTo(42) 61 assertThat(widthSpec.cellSizePx).isEqualTo(210) 62 63 assertThat(heightSpec.availableSpace).isEqualTo(availableHeight) 64 assertThat(heightSpec.cells).isEqualTo(5) 65 assertThat(heightSpec.startPaddingPx).isEqualTo(21) 66 assertThat(heightSpec.endPaddingPx).isEqualTo(233) 67 assertThat(heightSpec.gutterPx).isEqualTo(42) 68 assertThat(heightSpec.cellSizePx).isEqualTo(273) 69 } 70 71 /** 72 * This test tests: 73 * - (height spec) gets the correct breakpoint from the XML - use the first breakpoint 74 * - (height spec) do the correct calculations for remainder space and fixed size 75 * - (width spec) do the correct calculations for remainder space and fixed size 76 */ 77 @Test smallPhone_returnsFirstBreakpointSpecnull78 fun smallPhone_returnsFirstBreakpointSpec() { 79 val deviceSpec = deviceSpecs["phone"]!! 80 deviceSpec.densityDpi = 540 // larger display size 81 initializeVarsForPhone(deviceSpec) 82 83 val availableWidth = deviceSpec.naturalSize.first 84 // Hotseat size is roughly 640px on a real device, 85 // it doesn't need to be precise on unit tests 86 val availableHeight = deviceSpec.naturalSize.second - deviceSpec.statusBarNaturalPx - 640 87 88 val workspaceSpecs = 89 WorkspaceSpecs.create(TestResourceHelper(context!!, TestR.xml.valid_workspace_file)) 90 val widthSpec = workspaceSpecs.getCalculatedWidthSpec(4, availableWidth) 91 val heightSpec = workspaceSpecs.getCalculatedHeightSpec(5, availableHeight) 92 93 assertThat(widthSpec.availableSpace).isEqualTo(availableWidth) 94 assertThat(widthSpec.cells).isEqualTo(4) 95 assertThat(widthSpec.startPaddingPx).isEqualTo(74) 96 assertThat(widthSpec.endPaddingPx).isEqualTo(74) 97 assertThat(widthSpec.gutterPx).isEqualTo(54) 98 assertThat(widthSpec.cellSizePx).isEqualTo(193) 99 100 assertThat(heightSpec.availableSpace).isEqualTo(availableHeight) 101 assertThat(heightSpec.cells).isEqualTo(5) 102 assertThat(heightSpec.startPaddingPx).isEqualTo(0) 103 assertThat(heightSpec.endPaddingPx).isEqualTo(108) 104 assertThat(heightSpec.gutterPx).isEqualTo(54) 105 assertThat(heightSpec.cellSizePx).isEqualTo(260) 106 } 107 } 108