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 ResponsiveCellSpecsProviderTest : 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 testResourceHelper = TestResourceHelper(context, "valid_cell_specs_file".xmlToId()) 46 val provider = ResponsiveCellSpecsProvider.create(testResourceHelper) 47 48 // Validate Portrait 49 val aspectRatioPortrait = 1.0f 50 val expectedPortraitSpecs = 51 listOf( 52 CellSpec( 53 maxAvailableSize = 606.dpToPx(), 54 dimensionType = ResponsiveSpec.DimensionType.HEIGHT, 55 specType = ResponsiveSpecType.Cell, 56 iconSize = SizeSpec(48f.dpToPx()), 57 iconTextSize = SizeSpec(12f.dpToPx()), 58 iconDrawablePadding = SizeSpec(8f.dpToPx()) 59 ), 60 CellSpec( 61 maxAvailableSize = 9999.dpToPx(), 62 dimensionType = ResponsiveSpec.DimensionType.HEIGHT, 63 specType = ResponsiveSpecType.Cell, 64 iconSize = SizeSpec(52f.dpToPx()), 65 iconTextSize = SizeSpec(12f.dpToPx()), 66 iconDrawablePadding = SizeSpec(11f.dpToPx()) 67 ) 68 ) 69 70 val portraitSpecs = provider.getSpecsByAspectRatio(aspectRatioPortrait) 71 72 assertThat(portraitSpecs.aspectRatio).isAtLeast(aspectRatioPortrait) 73 assertThat(portraitSpecs.widthSpecs.size).isEqualTo(0) 74 assertThat(portraitSpecs.heightSpecs.size).isEqualTo(2) 75 assertThat(portraitSpecs.heightSpecs[0]).isEqualTo(expectedPortraitSpecs[0]) 76 assertThat(portraitSpecs.heightSpecs[1]).isEqualTo(expectedPortraitSpecs[1]) 77 78 // Validate Landscape 79 val aspectRatioLandscape = 1.051f 80 val expectedLandscapeSpec = 81 CellSpec( 82 maxAvailableSize = 9999.dpToPx(), 83 dimensionType = ResponsiveSpec.DimensionType.HEIGHT, 84 specType = ResponsiveSpecType.Cell, 85 iconSize = SizeSpec(52f.dpToPx()), 86 iconTextSize = SizeSpec(0f), 87 iconDrawablePadding = SizeSpec(0f) 88 ) 89 val landscapeSpecs = provider.getSpecsByAspectRatio(aspectRatioLandscape) 90 91 assertThat(landscapeSpecs.aspectRatio).isAtLeast(aspectRatioLandscape) 92 assertThat(landscapeSpecs.widthSpecs.size).isEqualTo(0) 93 assertThat(landscapeSpecs.heightSpecs.size).isEqualTo(1) 94 assertThat(landscapeSpecs.heightSpecs[0]).isEqualTo(expectedLandscapeSpec) 95 } 96 97 @Test(expected = IllegalStateException::class) parseInvalidFile_IsNotFixedSizeOrMatchWorkspace_throwsErrornull98 fun parseInvalidFile_IsNotFixedSizeOrMatchWorkspace_throwsError() { 99 ResponsiveCellSpecsProvider.create( 100 TestResourceHelper(context, "invalid_cell_specs_1".xmlToId()) 101 ) 102 } 103 104 @Test(expected = IllegalStateException::class) parseInvalidFile_dimensionTypeIsNotHeight_throwsErrornull105 fun parseInvalidFile_dimensionTypeIsNotHeight_throwsError() { 106 ResponsiveCellSpecsProvider.create( 107 TestResourceHelper(context, "invalid_cell_specs_2".xmlToId()) 108 ) 109 } 110 111 @Test(expected = IllegalStateException::class) parseInvalidFile_invalidFixedSize_throwsErrornull112 fun parseInvalidFile_invalidFixedSize_throwsError() { 113 ResponsiveCellSpecsProvider.create( 114 TestResourceHelper(context, "invalid_cell_specs_3".xmlToId()) 115 ) 116 } 117 } 118