1 /*
2  * Copyright 2022 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 androidx.compose.ui.text.benchmark.font
18 
19 import android.content.Context
20 import androidx.benchmark.junit4.BenchmarkRule
21 import androidx.benchmark.junit4.measureRepeated
22 import androidx.compose.ui.text.InternalTextApi
23 import androidx.compose.ui.text.benchmark.cartesian
24 import androidx.compose.ui.text.font.FontFamily
25 import androidx.compose.ui.text.font.FontWeight
26 import androidx.compose.ui.text.font.createFontFamilyResolver
27 import androidx.compose.ui.text.font.emptyCacheFontFamilyResolver
28 import androidx.test.filters.LargeTest
29 import androidx.test.platform.app.InstrumentationRegistry
30 import org.junit.Rule
31 import org.junit.Test
32 import org.junit.runner.RunWith
33 import org.junit.runners.Parameterized
34 
35 @LargeTest
36 @RunWith(Parameterized::class)
37 class PlatformFontLookup(val fontFamily: FontFamily, val fontWeight: FontWeight) {
38 
39     companion object {
40         @JvmStatic
41         @Parameterized.Parameters(name = "fontFamily={0} fontWeight={1}")
initParametersnull42         fun initParameters() =
43             cartesian(
44                 arrayOf(
45                     FontFamily.Default,
46                     FontFamily.SansSerif,
47                     FontFamily.Serif,
48                     FontFamily.Cursive,
49                     FontFamily.Monospace
50                 ),
51                 arrayOf(100, 400, 700).map { FontWeight(it) }.toTypedArray()
52             )
53     }
54 
55     @get:Rule val benchmarkRule = BenchmarkRule()
56 
57     private val context: Context = InstrumentationRegistry.getInstrumentation().context
58 
59     @OptIn(InternalTextApi::class)
60     @Test
forceUncachednull61     fun forceUncached() {
62         benchmarkRule.measureRepeated {
63             val fontFamilyResolver = runWithMeasurementDisabled {
64                 emptyCacheFontFamilyResolver(context)
65             }
66             fontFamilyResolver.resolve(fontFamily, fontWeight)
67         }
68     }
69 
70     @Test
cachednull71     fun cached() {
72         benchmarkRule.measureRepeated {
73             val fontFamilyResolver = runWithMeasurementDisabled {
74                 createFontFamilyResolver(context)
75             }
76             fontFamilyResolver.resolve(fontFamily, fontWeight)
77         }
78     }
79 }
80