1 /* <lambda>null2 * Copyright (C) 2020 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.systemui.animation 18 19 import android.graphics.Paint 20 import android.graphics.fonts.Font 21 import android.graphics.fonts.FontVariationAxis 22 import android.graphics.text.TextRunShaper 23 import androidx.test.ext.junit.runners.AndroidJUnit4 24 import androidx.test.filters.SmallTest 25 import com.android.systemui.SysuiTestCase 26 import com.google.common.truth.Truth.assertThat 27 import org.junit.Test 28 import org.junit.runner.RunWith 29 30 @RunWith(AndroidJUnit4::class) 31 @SmallTest 32 class FontInterpolatorTest : SysuiTestCase() { 33 34 private val sFont = 35 TextRunShaper.shapeTextRun("A", 0, 1, 0, 1, 0f, 0f, false, Paint()).getFont(0) 36 37 private fun assertSameAxes(expect: Font, actual: Font) { 38 val expectAxes = expect.axes?.also { it.sortBy { axis -> axis.tag } } 39 val actualAxes = actual.axes?.also { it.sortBy { axis -> axis.tag } } 40 assertThat(actualAxes).isEqualTo(expectAxes) 41 } 42 43 private fun assertSameAxes(expectVarSettings: String, actual: Font) { 44 45 val expectAxes = 46 FontVariationAxis.fromFontVariationSettings(expectVarSettings)?.also { 47 it.sortBy { axis -> axis.tag } 48 } 49 val actualAxes = actual.axes?.also { it.sortBy { axis -> axis.tag } } 50 assertThat(actualAxes).isEqualTo(expectAxes) 51 } 52 53 @Test 54 fun textInterpolation() { 55 val startFont = 56 Font.Builder(sFont).setFontVariationSettings("'wght' 100, 'ital' 0, 'GRAD' 200").build() 57 val endFont = 58 Font.Builder(sFont).setFontVariationSettings("'wght' 900, 'ital' 1, 'GRAD' 700").build() 59 60 val interp = FontInterpolator() 61 assertSameAxes(startFont, interp.lerp(startFont, endFont, 0f)) 62 assertSameAxes(endFont, interp.lerp(startFont, endFont, 1f)) 63 assertSameAxes("'wght' 500, 'ital' 0.5, 'GRAD' 450", interp.lerp(startFont, endFont, 0.5f)) 64 } 65 66 @Test 67 fun textInterpolation_DefaultValue() { 68 val startFont = Font.Builder(sFont).setFontVariationSettings("'wght' 100").build() 69 val endFont = Font.Builder(sFont).setFontVariationSettings("'ital' 1").build() 70 71 val interp = FontInterpolator() 72 assertSameAxes("'wght' 250, 'ital' 0.5", interp.lerp(startFont, endFont, 0.5f)) 73 } 74 75 @Test 76 fun testInterpCache() { 77 val startFont = Font.Builder(sFont).setFontVariationSettings("'wght' 100").build() 78 val endFont = Font.Builder(sFont).setFontVariationSettings("'ital' 1").build() 79 80 val interp = FontInterpolator() 81 val resultFont = interp.lerp(startFont, endFont, 0.5f) 82 val cachedFont = interp.lerp(startFont, endFont, 0.5f) 83 assertThat(resultFont).isSameInstanceAs(cachedFont) 84 } 85 86 @Test 87 fun testAxesCache() { 88 val startFont = Font.Builder(sFont).setFontVariationSettings("'wght' 100").build() 89 val endFont = Font.Builder(sFont).setFontVariationSettings("'ital' 1").build() 90 91 val interp = FontInterpolator() 92 val resultFont = interp.lerp(startFont, endFont, 0.5f) 93 val reversedFont = interp.lerp(endFont, startFont, 0.5f) 94 assertThat(resultFont).isSameInstanceAs(reversedFont) 95 } 96 97 @Test 98 fun testCacheMaxSize() { 99 val interp = FontInterpolator() 100 101 val startFont = Font.Builder(sFont).setFontVariationSettings("'wght' 100").build() 102 val endFont = Font.Builder(sFont).setFontVariationSettings("'wght' 1").build() 103 val resultFont = interp.lerp(startFont, endFont, 0.5f) 104 for (i in 0..(interp.fontCache as FontCacheImpl).cacheMaxEntries + 1) { 105 val f1 = Font.Builder(sFont).setFontVariationSettings("'wght' ${i * 100}").build() 106 val f2 = Font.Builder(sFont).setFontVariationSettings("'wght' $i").build() 107 interp.lerp(f1, f2, 0.5f) 108 } 109 110 val cachedFont = interp.lerp(startFont, endFont, 0.5f) 111 assertThat(resultFont).isNotSameInstanceAs(cachedFont) 112 } 113 } 114