1 /*
2  * Copyright 2024 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.test.input
18 
19 import android.content.Context
20 import android.content.res.Configuration
21 import android.content.res.Resources
22 import android.os.Environment
23 import android.view.ContextThemeWrapper
24 import android.view.PointerIcon
25 import android.view.flags.Flags.enableVectorCursorA11ySettings
26 import android.view.flags.Flags.enableVectorCursors
27 import androidx.test.ext.junit.runners.AndroidJUnit4
28 import androidx.test.filters.SmallTest
29 import androidx.test.platform.app.InstrumentationRegistry
30 import org.junit.Assume.assumeTrue
31 import org.junit.Before
32 import org.junit.Rule
33 import org.junit.Test
34 import org.junit.rules.TestName
35 import org.junit.runner.RunWith
36 import platform.test.screenshot.GoldenPathManager
37 import platform.test.screenshot.PathConfig
38 import platform.test.screenshot.ScreenshotTestRule
39 import platform.test.screenshot.assertAgainstGolden
40 import platform.test.screenshot.matchers.BitmapMatcher
41 import platform.test.screenshot.matchers.PixelPerfectMatcher
42 
43 /**
44  * Unit tests for PointerIcon.
45  *
46  * Run with:
47  * atest InputTests:com.android.test.input.PointerIconLoadingTest
48  */
49 @SmallTest
50 @RunWith(AndroidJUnit4::class)
51 class PointerIconLoadingTest {
52     private lateinit var context: Context
53     private lateinit var exactScreenshotMatcher: BitmapMatcher
54 
55     @get:Rule
56     val testName = TestName()
57 
58     @get:Rule
59     val screenshotRule = ScreenshotTestRule(GoldenPathManager(
60         InstrumentationRegistry.getInstrumentation().getContext(),
61         ASSETS_PATH,
62         TEST_OUTPUT_PATH,
63         PathConfig()
64     ), disableIconPool = false)
65 
66     @Before
setUpnull67     fun setUp() {
68         context = InstrumentationRegistry.getInstrumentation().targetContext
69         val config =
70             Configuration(context.resources.configuration).apply {
71                 densityDpi = DENSITY_DPI
72                 screenWidthDp = SCREEN_WIDTH_DP
73                 screenHeightDp = SCREEN_HEIGHT_DP
74                 smallestScreenWidthDp = SCREEN_WIDTH_DP
75             }
76         context = context.createConfigurationContext(config)
77 
78         exactScreenshotMatcher = PixelPerfectMatcher()
79     }
80 
81     @Test
testPointerFillStylenull82     fun testPointerFillStyle() {
83         assumeTrue(enableVectorCursors())
84         assumeTrue(enableVectorCursorA11ySettings())
85 
86         val theme: Resources.Theme = context.getResources().newTheme()
87         theme.setTo(context.getTheme())
88         theme.applyStyle(
89             PointerIcon.vectorFillStyleToResource(PointerIcon.POINTER_ICON_VECTOR_STYLE_FILL_GREEN),
90             /* force= */ true)
91 
92         val pointerIcon =
93             PointerIcon.getLoadedSystemIcon(
94                 ContextThemeWrapper(context, theme),
95                 PointerIcon.TYPE_ARROW,
96                 /* useLargeIcons= */ false,
97                 /* pointerScale= */ 1f)
98 
99         pointerIcon.getBitmap().assertAgainstGolden(
100             screenshotRule,
101             testName.methodName,
102             exactScreenshotMatcher
103         )
104     }
105 
106     @Test
testPointerScalenull107     fun testPointerScale() {
108         assumeTrue(enableVectorCursors())
109         assumeTrue(enableVectorCursorA11ySettings())
110 
111         val pointerScale = 2f
112 
113         val pointerIcon =
114             PointerIcon.getLoadedSystemIcon(
115                 context,
116                 PointerIcon.TYPE_ARROW,
117                 /* useLargeIcons= */ false,
118                 pointerScale)
119 
120         pointerIcon.getBitmap().assertAgainstGolden(
121             screenshotRule,
122             testName.methodName,
123             exactScreenshotMatcher
124         )
125     }
126 
127     companion object {
128         const val DENSITY_DPI = 160
129         const val SCREEN_WIDTH_DP = 480
130         const val SCREEN_HEIGHT_DP = 800
131         const val ASSETS_PATH = "tests/input/assets"
132         val TEST_OUTPUT_PATH = Environment.getExternalStorageDirectory().absolutePath +
133                 "/InputTests/" +
134                 PointerIconLoadingTest::class.java.simpleName
135     }
136 }
137