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 = TextRunShaper.shapeTextRun("A", 0, 1, 0, 1, 0f, 0f, false, Paint()) 35 .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 = FontVariationAxis.fromFontVariationSettings(expectVarSettings)?.also { 46 it.sortBy { axis -> axis.tag } 47 } 48 val actualAxes = actual.axes?.also { it.sortBy { axis -> axis.tag } } 49 assertThat(actualAxes).isEqualTo(expectAxes) 50 } 51 52 @Test 53 fun textInterpolation() { 54 val startFont = Font.Builder(sFont) 55 .setFontVariationSettings("'wght' 100, 'ital' 0, 'GRAD' 200") 56 .build() 57 val endFont = Font.Builder(sFont) 58 .setFontVariationSettings("'wght' 900, 'ital' 1, 'GRAD' 700") 59 .build() 60 61 val interp = FontInterpolator() 62 assertSameAxes(startFont, interp.lerp(startFont, endFont, 0f)) 63 assertSameAxes(endFont, interp.lerp(startFont, endFont, 1f)) 64 assertSameAxes("'wght' 500, 'ital' 0.5, 'GRAD' 450", interp.lerp(startFont, endFont, 0.5f)) 65 } 66 67 @Test 68 fun textInterpolation_DefaultValue() { 69 val startFont = Font.Builder(sFont) 70 .setFontVariationSettings("'wght' 100") 71 .build() 72 val endFont = Font.Builder(sFont) 73 .setFontVariationSettings("'ital' 1") 74 .build() 75 76 val interp = FontInterpolator() 77 assertSameAxes("'wght' 250, 'ital' 0.5", interp.lerp(startFont, endFont, 0.5f)) 78 } 79 80 @Test 81 fun testInterpCache() { 82 val startFont = Font.Builder(sFont) 83 .setFontVariationSettings("'wght' 100") 84 .build() 85 val endFont = Font.Builder(sFont) 86 .setFontVariationSettings("'ital' 1") 87 .build() 88 89 val interp = FontInterpolator() 90 val resultFont = interp.lerp(startFont, endFont, 0.5f) 91 val cachedFont = interp.lerp(startFont, endFont, 0.5f) 92 assertThat(resultFont).isSameInstanceAs(cachedFont) 93 } 94 95 @Test 96 fun testAxesCache() { 97 val startFont = Font.Builder(sFont) 98 .setFontVariationSettings("'wght' 100") 99 .build() 100 val endFont = Font.Builder(sFont) 101 .setFontVariationSettings("'ital' 1") 102 .build() 103 104 val interp = FontInterpolator() 105 val resultFont = interp.lerp(startFont, endFont, 0.5f) 106 val reversedFont = interp.lerp(endFont, startFont, 0.5f) 107 assertThat(resultFont).isSameInstanceAs(reversedFont) 108 } 109 110 @Test 111 fun testCacheMaxSize() { 112 val interp = FontInterpolator() 113 114 val startFont = Font.Builder(sFont) 115 .setFontVariationSettings("'wght' 100") 116 .build() 117 val endFont = Font.Builder(sFont) 118 .setFontVariationSettings("'wght' 1") 119 .build() 120 val resultFont = interp.lerp(startFont, endFont, 0.5f) 121 for (i in 0..interp.cacheMaxEntries + 1) { 122 val f1 = Font.Builder(sFont) 123 .setFontVariationSettings("'wght' ${i * 100}") 124 .build() 125 val f2 = Font.Builder(sFont) 126 .setFontVariationSettings("'wght' $i") 127 .build() 128 interp.lerp(f1, f2, 0.5f) 129 } 130 131 val cachedFont = interp.lerp(startFont, endFont, 0.5f) 132 assertThat(resultFont).isNotSameInstanceAs(cachedFont) 133 } 134 } 135