1 /* 2 * Copyright (C) 2021 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.server.display; 18 19 import static com.android.server.display.DensityMapping.Entry; 20 21 import static org.junit.Assert.assertEquals; 22 import static org.junit.Assert.assertThrows; 23 24 import androidx.test.filters.SmallTest; 25 import androidx.test.runner.AndroidJUnit4; 26 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 30 @SmallTest 31 @RunWith(AndroidJUnit4.class) 32 public class DensityMappingTest { 33 34 @Test testConstructor_withBadConfig_throwsException()35 public void testConstructor_withBadConfig_throwsException() { 36 assertThrows(IllegalStateException.class, () -> 37 DensityMapping.createByOwning(new Entry[]{ 38 new Entry(1080, 1920, 320), 39 new Entry(1080, 1920, 320)}) 40 ); 41 42 assertThrows(IllegalStateException.class, () -> 43 DensityMapping.createByOwning(new Entry[]{ 44 new Entry(1080, 1920, 320), 45 new Entry(1920, 1080, 120)}) 46 ); 47 48 assertThrows(IllegalStateException.class, () -> 49 DensityMapping.createByOwning(new Entry[]{ 50 new Entry(1080, 1920, 320), 51 new Entry(2160, 3840, 120)}) 52 ); 53 54 assertThrows(IllegalStateException.class, () -> 55 DensityMapping.createByOwning(new Entry[]{ 56 new Entry(1080, 1920, 320), 57 new Entry(3840, 2160, 120)}) 58 ); 59 60 // Two entries with the same diagonal 61 assertThrows(IllegalStateException.class, () -> 62 DensityMapping.createByOwning(new Entry[]{ 63 new Entry(500, 500, 123), 64 new Entry(100, 700, 456)}) 65 ); 66 } 67 68 @Test testGetDensityForResolution_withResolutionMatch_returnsDensityFromConfig()69 public void testGetDensityForResolution_withResolutionMatch_returnsDensityFromConfig() { 70 DensityMapping densityMapping = DensityMapping.createByOwning(new Entry[]{ 71 new Entry(720, 1280, 213), 72 new Entry(1080, 1920, 320), 73 new Entry(2160, 3840, 640)}); 74 75 assertEquals(213, densityMapping.getDensityForResolution(720, 1280)); 76 assertEquals(213, densityMapping.getDensityForResolution(1280, 720)); 77 78 assertEquals(320, densityMapping.getDensityForResolution(1080, 1920)); 79 assertEquals(320, densityMapping.getDensityForResolution(1920, 1080)); 80 81 assertEquals(640, densityMapping.getDensityForResolution(2160, 3840)); 82 assertEquals(640, densityMapping.getDensityForResolution(3840, 2160)); 83 } 84 85 @Test testGetDensityForResolution_withDiagonalMatch_returnsDensityFromConfig()86 public void testGetDensityForResolution_withDiagonalMatch_returnsDensityFromConfig() { 87 DensityMapping densityMapping = DensityMapping.createByOwning( 88 new Entry[]{ new Entry(500, 500, 123)}); 89 90 // 500x500 has the same diagonal as 100x700 91 assertEquals(123, densityMapping.getDensityForResolution(100, 700)); 92 } 93 94 @Test testGetDensityForResolution_withOneEntry_withNoMatch_returnsExtrapolatedDensity()95 public void testGetDensityForResolution_withOneEntry_withNoMatch_returnsExtrapolatedDensity() { 96 DensityMapping densityMapping = DensityMapping.createByOwning( 97 new Entry[]{ new Entry(1080, 1920, 320)}); 98 99 assertEquals(320, densityMapping.getDensityForResolution(1081, 1920)); 100 assertEquals(320, densityMapping.getDensityForResolution(1080, 1921)); 101 102 assertEquals(640, densityMapping.getDensityForResolution(2160, 3840)); 103 assertEquals(640, densityMapping.getDensityForResolution(3840, 2160)); 104 105 assertEquals(213, densityMapping.getDensityForResolution(720, 1280)); 106 assertEquals(213, densityMapping.getDensityForResolution(1280, 720)); 107 } 108 109 @Test testGetDensityForResolution_withTwoEntries_withNoMatch_returnExtrapolatedDensity()110 public void testGetDensityForResolution_withTwoEntries_withNoMatch_returnExtrapolatedDensity() { 111 DensityMapping densityMapping = DensityMapping.createByOwning(new Entry[]{ 112 new Entry(1080, 1920, 320), 113 new Entry(2160, 3840, 320)}); 114 115 // Resolution is smaller than all entries 116 assertEquals(213, densityMapping.getDensityForResolution(720, 1280)); 117 assertEquals(213, densityMapping.getDensityForResolution(1280, 720)); 118 119 // Resolution is bigger than all entries 120 assertEquals(320 * 2, densityMapping.getDensityForResolution(2160 * 2, 3840 * 2)); 121 assertEquals(320 * 2, densityMapping.getDensityForResolution(3840 * 2, 2160 * 2)); 122 } 123 124 @Test testGetDensityForResolution_withNoMatch_returnsInterpolatedDensity()125 public void testGetDensityForResolution_withNoMatch_returnsInterpolatedDensity() { 126 { 127 DensityMapping densityMapping = DensityMapping.createByOwning(new Entry[]{ 128 new Entry(1080, 1920, 320), 129 new Entry(2160, 3840, 320)}); 130 131 assertEquals(320, densityMapping.getDensityForResolution(2000, 2000)); 132 } 133 134 { 135 DensityMapping densityMapping = DensityMapping.createByOwning(new Entry[]{ 136 new Entry(720, 1280, 213), 137 new Entry(2160, 3840, 640)}); 138 139 assertEquals(320, densityMapping.getDensityForResolution(1080, 1920)); 140 assertEquals(320, densityMapping.getDensityForResolution(1920, 1080)); 141 } 142 } 143 } 144