diff options
author | 2023-08-24 14:22:57 +0000 | |
---|---|---|
committer | 2023-09-18 21:04:06 +0000 | |
commit | 7d15fae569792288b44a693c8880360eaf91c1ff (patch) | |
tree | 92c6190d88c20d0bfa75d8c0a68d46cede023f9d /tests/InputScreenshotTest | |
parent | 05605b2518f17a4f6d655bfffbe60883fb3e88e4 (diff) |
Add support for Keyboard layout preview
Screenshot for drawable: https://screenshot.googleplex.com/9hBauxZB9asdmNN
Flag: com.android.hardware.input.keyboardLayoutPreviewFlag
Test: atest KeyboardLayoutPreviewTests
Test: atest KeyboardLayoutPreviewScreenshotTests
Bug: 293579375
Change-Id: If66c2108c3a65986278d8d332533fb9d1ca6b266
Diffstat (limited to 'tests/InputScreenshotTest')
18 files changed, 645 insertions, 0 deletions
diff --git a/tests/InputScreenshotTest/Android.bp b/tests/InputScreenshotTest/Android.bp new file mode 100644 index 000000000000..eee486f99748 --- /dev/null +++ b/tests/InputScreenshotTest/Android.bp @@ -0,0 +1,60 @@ +package { + // See: http://go/android-license-faq + // A large-scale-change added 'default_applicable_licenses' to import + // all of the 'license_kinds' from "frameworks_base_license" + // to get the below license kinds: + // SPDX-license-identifier-Apache-2.0 + default_applicable_licenses: ["frameworks_base_license"], +} + +android_test { + name: "InputScreenshotTests", + srcs: [ + "src/**/*.java", + "src/**/*.kt", + ], + platform_apis: true, + certificate: "platform", + static_libs: [ + "androidx.arch.core_core-testing", + "androidx.compose.ui_ui-test-junit4", + "androidx.compose.ui_ui-test-manifest", + "androidx.lifecycle_lifecycle-runtime-testing", + "androidx.compose.animation_animation", + "androidx.compose.material3_material3", + "androidx.compose.material_material-icons-extended", + "androidx.compose.runtime_runtime", + "androidx.compose.runtime_runtime-livedata", + "androidx.compose.ui_ui-tooling-preview", + "androidx.lifecycle_lifecycle-livedata-ktx", + "androidx.lifecycle_lifecycle-runtime-compose", + "androidx.navigation_navigation-compose", + "truth-prebuilt", + "androidx.compose.runtime_runtime", + "androidx.test.core", + "androidx.test.ext.junit", + "androidx.test.ext.truth", + "androidx.test.rules", + "androidx.test.runner", + "androidx.test.uiautomator_uiautomator", + "servicestests-utils", + "frameworks-base-testutils", + "platform-screenshot-diff-core", + "hamcrest-library", + "kotlin-test", + "flag-junit", + "platform-test-annotations", + "services.core.unboosted", + "testables", + "testng", + "truth-prebuilt", + ], + libs: [ + "android.test.mock", + "android.test.base", + ], + test_suites: ["device-tests"], + compile_multilib: "both", + use_embedded_native_libs: false, + asset_dirs: ["assets"], +} diff --git a/tests/InputScreenshotTest/AndroidManifest.xml b/tests/InputScreenshotTest/AndroidManifest.xml new file mode 100644 index 000000000000..9ffbb3a58ebb --- /dev/null +++ b/tests/InputScreenshotTest/AndroidManifest.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright 2023 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.input.screenshot"> + + <uses-sdk android:minSdkVersion="21"/> + + <application> + <uses-library android:name="android.test.runner" /> + </application> + + <instrumentation + android:name="androidx.test.runner.AndroidJUnitRunner" + android:label="Screenshot tests for Input" + android:targetPackage="com.android.input.screenshot"> + </instrumentation> +</manifest> diff --git a/tests/InputScreenshotTest/AndroidTest.xml b/tests/InputScreenshotTest/AndroidTest.xml new file mode 100644 index 000000000000..cc25fa454f06 --- /dev/null +++ b/tests/InputScreenshotTest/AndroidTest.xml @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright 2023 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + --> +<configuration description="Runs Input screendiff tests."> + <option name="test-suite-tag" value="apct-instrumentation" /> + <option name="test-suite-tag" value="apct" /> + <target_preparer class="com.android.tradefed.targetprep.DeviceSetup"> + <option name="optimized-property-setting" value="true" /> + </target_preparer> + <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller"> + <option name="cleanup-apks" value="true" /> + <option name="test-file-name" value="InputScreenshotTests.apk" /> + </target_preparer> + <metrics_collector class="com.android.tradefed.device.metric.FilePullerLogCollector"> + <option name="directory-keys" + value="/data/user/0/com.android.input.screenshot/files/input_screenshots" /> + <option name="collect-on-run-ended-only" value="true" /> + </metrics_collector> + <test class="com.android.tradefed.testtype.AndroidJUnitTest"> + <option name="package" value="com.android.input.screenshot" /> + <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" /> + </test> +</configuration> diff --git a/tests/InputScreenshotTest/OWNERS b/tests/InputScreenshotTest/OWNERS new file mode 100644 index 000000000000..3cffce960b1c --- /dev/null +++ b/tests/InputScreenshotTest/OWNERS @@ -0,0 +1,2 @@ +# Bug component: 136048 +include /core/java/android/hardware/input/OWNERS diff --git a/tests/InputScreenshotTest/TEST_MAPPING b/tests/InputScreenshotTest/TEST_MAPPING new file mode 100644 index 000000000000..727e609d91ac --- /dev/null +++ b/tests/InputScreenshotTest/TEST_MAPPING @@ -0,0 +1,7 @@ +{ + "postsubmit": [ + { + "name": "InputScreenshotTests" + } + ] +} diff --git a/tests/InputScreenshotTest/assets/phone/light_landscape_layout-preview.png b/tests/InputScreenshotTest/assets/phone/light_landscape_layout-preview.png Binary files differnew file mode 100644 index 000000000000..70e4a7101c7f --- /dev/null +++ b/tests/InputScreenshotTest/assets/phone/light_landscape_layout-preview.png diff --git a/tests/InputScreenshotTest/assets/phone/light_portrait_layout-preview-ansi.png b/tests/InputScreenshotTest/assets/phone/light_portrait_layout-preview-ansi.png Binary files differnew file mode 100644 index 000000000000..502c1b4499d4 --- /dev/null +++ b/tests/InputScreenshotTest/assets/phone/light_portrait_layout-preview-ansi.png diff --git a/tests/InputScreenshotTest/assets/phone/light_portrait_layout-preview-jis.png b/tests/InputScreenshotTest/assets/phone/light_portrait_layout-preview-jis.png Binary files differnew file mode 100644 index 000000000000..591b2fa9608e --- /dev/null +++ b/tests/InputScreenshotTest/assets/phone/light_portrait_layout-preview-jis.png diff --git a/tests/InputScreenshotTest/assets/phone/light_portrait_layout-preview.png b/tests/InputScreenshotTest/assets/phone/light_portrait_layout-preview.png Binary files differnew file mode 100644 index 000000000000..0137a853e538 --- /dev/null +++ b/tests/InputScreenshotTest/assets/phone/light_portrait_layout-preview.png diff --git a/tests/InputScreenshotTest/assets/tablet/dark_portrait_layout-preview.png b/tests/InputScreenshotTest/assets/tablet/dark_portrait_layout-preview.png Binary files differnew file mode 100644 index 000000000000..37a91e1fce53 --- /dev/null +++ b/tests/InputScreenshotTest/assets/tablet/dark_portrait_layout-preview.png diff --git a/tests/InputScreenshotTest/src/android/input/screenshot/Bitmap.kt b/tests/InputScreenshotTest/src/android/input/screenshot/Bitmap.kt new file mode 100644 index 000000000000..84c971c750fb --- /dev/null +++ b/tests/InputScreenshotTest/src/android/input/screenshot/Bitmap.kt @@ -0,0 +1,66 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.input.screenshot + +import android.graphics.Bitmap +import android.graphics.Canvas +import android.os.Build +import android.view.View +import platform.test.screenshot.matchers.MSSIMMatcher +import platform.test.screenshot.matchers.PixelPerfectMatcher + +/** Draw this [View] into a [Bitmap]. */ +// TODO(b/195673633): Remove this once Compose screenshot tests use hardware rendering for their +// tests. +fun View.drawIntoBitmap(): Bitmap { + val bitmap = + Bitmap.createBitmap( + measuredWidth, + measuredHeight, + Bitmap.Config.ARGB_8888, + ) + val canvas = Canvas(bitmap) + draw(canvas) + return bitmap +} + +/** + * The [BitmapMatcher][platform.test.screenshot.matchers.BitmapMatcher] that should be used for + * screenshot *unit* tests. + */ +val UnitTestBitmapMatcher = + if (Build.CPU_ABI == "x86_64") { + // Different CPU architectures can sometimes end up rendering differently, so we can't do + // pixel-perfect matching on different architectures using the same golden. Given that our + // presubmits are run on cf_x86_64_phone, our goldens should be perfectly matched on the + // x86_64 architecture and use the Structural Similarity Index on others. + // TODO(b/237511747): Run our screenshot presubmit tests on arm64 instead so that we can + // do pixel perfect matching both at presubmit time and at development time with actual + // devices. + PixelPerfectMatcher() + } else { + MSSIMMatcher() + } + +/** + * The [BitmapMatcher][platform.test.screenshot.matchers.BitmapMatcher] that should be used for + * screenshot *unit* tests. + * + * We use the Structural Similarity Index for integration tests because they usually contain + * additional information and noise that shouldn't break the test. + */ +val IntegrationTestBitmapMatcher = MSSIMMatcher()
\ No newline at end of file diff --git a/tests/InputScreenshotTest/src/android/input/screenshot/DefaultDeviceEmulationSpec.kt b/tests/InputScreenshotTest/src/android/input/screenshot/DefaultDeviceEmulationSpec.kt new file mode 100644 index 000000000000..edddc6b41cf7 --- /dev/null +++ b/tests/InputScreenshotTest/src/android/input/screenshot/DefaultDeviceEmulationSpec.kt @@ -0,0 +1,75 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.input.screenshot + +import platform.test.screenshot.DeviceEmulationSpec +import platform.test.screenshot.DisplaySpec + +/** + * The emulations specs for all 8 permutations of: + * - phone or tablet. + * - dark of light mode. + * - portrait or landscape. + */ +val DeviceEmulationSpec.Companion.PhoneAndTabletFull + get() = PhoneAndTabletFullSpec + +private val PhoneAndTabletFullSpec = + DeviceEmulationSpec.forDisplays(Displays.Phone, Displays.Tablet) + +/** + * The emulations specs of: + * - phone + light mode + portrait. + * - phone + light mode + landscape. + * - tablet + dark mode + portrait. + * + * This allows to test the most important permutations of a screen/layout with only 3 + * configurations. + */ +val DeviceEmulationSpec.Companion.PhoneAndTabletMinimal + get() = PhoneAndTabletMinimalSpec + +private val PhoneAndTabletMinimalSpec = + DeviceEmulationSpec.forDisplays(Displays.Phone, isDarkTheme = false) + + DeviceEmulationSpec.forDisplays(Displays.Tablet, isDarkTheme = true, isLandscape = false) + +/** + * This allows to test only single most important configuration. + */ +val DeviceEmulationSpec.Companion.PhoneMinimal + get() = PhoneMinimalSpec + +private val PhoneMinimalSpec = + DeviceEmulationSpec.forDisplays(Displays.Phone, isDarkTheme = false, isLandscape = false) + +object Displays { + val Phone = + DisplaySpec( + "phone", + width = 1440, + height = 3120, + densityDpi = 560, + ) + + val Tablet = + DisplaySpec( + "tablet", + width = 2560, + height = 1600, + densityDpi = 320, + ) +}
\ No newline at end of file diff --git a/tests/InputScreenshotTest/src/android/input/screenshot/InputGoldenImagePathManager.kt b/tests/InputScreenshotTest/src/android/input/screenshot/InputGoldenImagePathManager.kt new file mode 100644 index 000000000000..8faf22440828 --- /dev/null +++ b/tests/InputScreenshotTest/src/android/input/screenshot/InputGoldenImagePathManager.kt @@ -0,0 +1,44 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.input.screenshot + +import androidx.test.platform.app.InstrumentationRegistry +import platform.test.screenshot.GoldenImagePathManager +import platform.test.screenshot.PathConfig + +/** A [GoldenImagePathManager] that should be used for all Input screenshot tests. */ +class InputGoldenImagePathManager( + pathConfig: PathConfig, + assetsPathRelativeToBuildRoot: String +) : + GoldenImagePathManager( + appContext = InstrumentationRegistry.getInstrumentation().context, + assetsPathRelativeToBuildRoot = assetsPathRelativeToBuildRoot, + deviceLocalPath = + InstrumentationRegistry.getInstrumentation() + .targetContext + .filesDir + .absolutePath + .toString() + "/input_screenshots", + pathConfig = pathConfig, + ) { + override fun toString(): String { + // This string is appended to all actual/expected screenshots on the device, so make sure + // it is a static value. + return "InputGoldenImagePathManager" + } +}
\ No newline at end of file diff --git a/tests/InputScreenshotTest/src/android/input/screenshot/InputScreenshotTestRule.kt b/tests/InputScreenshotTest/src/android/input/screenshot/InputScreenshotTestRule.kt new file mode 100644 index 000000000000..c2c3d5530a00 --- /dev/null +++ b/tests/InputScreenshotTest/src/android/input/screenshot/InputScreenshotTestRule.kt @@ -0,0 +1,87 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.input.screenshot + +import android.content.Context +import android.graphics.Bitmap +import androidx.activity.ComponentActivity +import androidx.compose.foundation.Image +import androidx.compose.ui.platform.ViewRootForTest +import androidx.compose.ui.test.junit4.createAndroidComposeRule +import androidx.compose.ui.test.onRoot +import androidx.compose.ui.graphics.asImageBitmap +import org.junit.rules.RuleChain +import org.junit.rules.TestRule +import org.junit.runner.Description +import org.junit.runners.model.Statement +import platform.test.screenshot.DeviceEmulationRule +import platform.test.screenshot.DeviceEmulationSpec +import platform.test.screenshot.MaterialYouColorsRule +import platform.test.screenshot.ScreenshotTestRule +import platform.test.screenshot.getEmulatedDevicePathConfig + +/** A rule for Input screenshot diff tests. */ +class InputScreenshotTestRule( + emulationSpec: DeviceEmulationSpec, + assetsPathRelativeToBuildRoot: String +) : TestRule { + private val colorsRule = MaterialYouColorsRule() + private val deviceEmulationRule = DeviceEmulationRule(emulationSpec) + private val screenshotRule = + ScreenshotTestRule( + InputGoldenImagePathManager( + getEmulatedDevicePathConfig(emulationSpec), + assetsPathRelativeToBuildRoot + ) + ) + private val composeRule = createAndroidComposeRule<ComponentActivity>() + private val delegateRule = + RuleChain.outerRule(colorsRule) + .around(deviceEmulationRule) + .around(screenshotRule) + .around(composeRule) + private val matcher = UnitTestBitmapMatcher + + override fun apply(base: Statement, description: Description): Statement { + return delegateRule.apply(base, description) + } + + /** + * Compare [content] with the golden image identified by [goldenIdentifier]. + */ + fun screenshotTest( + goldenIdentifier: String, + content: (Context) -> Bitmap, + ) { + // Make sure that the activity draws full screen and fits the whole display. + val activity = composeRule.activity + activity.mainExecutor.execute { activity.window.setDecorFitsSystemWindows(false) } + + // Set the content using the AndroidComposeRule to make sure that the Activity is set up + // correctly. + composeRule.setContent { + Image( + bitmap = content(activity).asImageBitmap(), + contentDescription = null, + ) + } + composeRule.waitForIdle() + + val view = (composeRule.onRoot().fetchSemanticsNode().root as ViewRootForTest).view + screenshotRule.assertBitmapAgainstGolden(view.drawIntoBitmap(), goldenIdentifier, matcher) + } +}
\ No newline at end of file diff --git a/tests/InputScreenshotTest/src/android/input/screenshot/KeyboardLayoutPreviewAnsiScreenshotTest.kt b/tests/InputScreenshotTest/src/android/input/screenshot/KeyboardLayoutPreviewAnsiScreenshotTest.kt new file mode 100644 index 000000000000..e85578663764 --- /dev/null +++ b/tests/InputScreenshotTest/src/android/input/screenshot/KeyboardLayoutPreviewAnsiScreenshotTest.kt @@ -0,0 +1,70 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.input.screenshot + +import android.content.Context +import android.hardware.input.KeyboardLayout +import android.os.LocaleList +import android.platform.test.flag.junit.SetFlagsRule +import com.android.hardware.input.Flags +import java.util.Locale +import org.junit.Rule +import org.junit.Test +import org.junit.rules.RuleChain +import org.junit.runner.RunWith +import org.junit.runners.Parameterized +import platform.test.screenshot.DeviceEmulationSpec + +/** A screenshot test for Keyboard layout preview for Ansi physical layout. */ +@RunWith(Parameterized::class) +class KeyboardLayoutPreviewAnsiScreenshotTest(emulationSpec: DeviceEmulationSpec) { + companion object { + @Parameterized.Parameters(name = "{0}") + @JvmStatic + fun getTestSpecs() = DeviceEmulationSpec.PhoneMinimal + } + + val setFlagsRule = SetFlagsRule() + val screenshotRule = InputScreenshotTestRule( + emulationSpec, + "frameworks/base/tests/InputScreenshotTest/assets" + ) + + @get:Rule + val ruleChain = RuleChain.outerRule(screenshotRule).around(setFlagsRule) + + @Test + fun test() { + setFlagsRule.enableFlags(Flags.FLAG_KEYBOARD_LAYOUT_PREVIEW_FLAG) + screenshotRule.screenshotTest("layout-preview-ansi") { + context: Context -> LayoutPreview.createLayoutPreview( + context, + KeyboardLayout( + "descriptor", + "layout", + /* collection= */null, + /* priority= */0, + LocaleList(Locale.US), + /* layoutType= */0, + /* vid= */0, + /* pid= */0 + ) + ) + } + } + +}
\ No newline at end of file diff --git a/tests/InputScreenshotTest/src/android/input/screenshot/KeyboardLayoutPreviewIsoScreenshotTest.kt b/tests/InputScreenshotTest/src/android/input/screenshot/KeyboardLayoutPreviewIsoScreenshotTest.kt new file mode 100644 index 000000000000..8ae6dfd8b63b --- /dev/null +++ b/tests/InputScreenshotTest/src/android/input/screenshot/KeyboardLayoutPreviewIsoScreenshotTest.kt @@ -0,0 +1,58 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.input.screenshot + +import android.content.Context +import android.hardware.input.KeyboardLayout +import android.os.LocaleList +import android.platform.test.flag.junit.SetFlagsRule +import com.android.hardware.input.Flags +import java.util.Locale +import org.junit.Rule +import org.junit.Test +import org.junit.rules.RuleChain +import org.junit.runner.RunWith +import org.junit.runners.Parameterized +import platform.test.screenshot.DeviceEmulationSpec + +/** A screenshot test for Keyboard layout preview for Iso physical layout. */ +@RunWith(Parameterized::class) +class KeyboardLayoutPreviewIsoScreenshotTest(emulationSpec: DeviceEmulationSpec) { + companion object { + @Parameterized.Parameters(name = "{0}") + @JvmStatic + fun getTestSpecs() = DeviceEmulationSpec.PhoneAndTabletMinimal + } + + val setFlagsRule = SetFlagsRule() + val screenshotRule = InputScreenshotTestRule( + emulationSpec, + "frameworks/base/tests/InputScreenshotTest/assets" + ) + + @get:Rule + val ruleChain = RuleChain.outerRule(screenshotRule).around(setFlagsRule) + + @Test + fun test() { + setFlagsRule.enableFlags(Flags.FLAG_KEYBOARD_LAYOUT_PREVIEW_FLAG) + screenshotRule.screenshotTest("layout-preview") { + context: Context -> LayoutPreview.createLayoutPreview(context, null) + } + } + +}
\ No newline at end of file diff --git a/tests/InputScreenshotTest/src/android/input/screenshot/KeyboardLayoutPreviewJisScreenshotTest.kt b/tests/InputScreenshotTest/src/android/input/screenshot/KeyboardLayoutPreviewJisScreenshotTest.kt new file mode 100644 index 000000000000..5231c14bfc9a --- /dev/null +++ b/tests/InputScreenshotTest/src/android/input/screenshot/KeyboardLayoutPreviewJisScreenshotTest.kt @@ -0,0 +1,70 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.input.screenshot + +import android.content.Context +import android.hardware.input.KeyboardLayout +import android.os.LocaleList +import android.platform.test.flag.junit.SetFlagsRule +import com.android.hardware.input.Flags +import java.util.Locale +import org.junit.Rule +import org.junit.Test +import org.junit.rules.RuleChain +import org.junit.runner.RunWith +import org.junit.runners.Parameterized +import platform.test.screenshot.DeviceEmulationSpec + +/** A screenshot test for Keyboard layout preview for JIS physical layout. */ +@RunWith(Parameterized::class) +class KeyboardLayoutPreviewJisScreenshotTest(emulationSpec: DeviceEmulationSpec) { + companion object { + @Parameterized.Parameters(name = "{0}") + @JvmStatic + fun getTestSpecs() = DeviceEmulationSpec.PhoneMinimal + } + + val setFlagsRule = SetFlagsRule() + val screenshotRule = InputScreenshotTestRule( + emulationSpec, + "frameworks/base/tests/InputScreenshotTest/assets" + ) + + @get:Rule + val ruleChain = RuleChain.outerRule(screenshotRule).around(setFlagsRule) + + @Test + fun test() { + setFlagsRule.enableFlags(Flags.FLAG_KEYBOARD_LAYOUT_PREVIEW_FLAG) + screenshotRule.screenshotTest("layout-preview-jis") { + context: Context -> LayoutPreview.createLayoutPreview( + context, + KeyboardLayout( + "descriptor", + "layout", + /* collection= */null, + /* priority= */0, + LocaleList(Locale.JAPAN), + /* layoutType= */0, + /* vid= */0, + /* pid= */0 + ) + ) + } + } + +}
\ No newline at end of file diff --git a/tests/InputScreenshotTest/src/android/input/screenshot/LayoutPreview.kt b/tests/InputScreenshotTest/src/android/input/screenshot/LayoutPreview.kt new file mode 100644 index 000000000000..76ee3791011b --- /dev/null +++ b/tests/InputScreenshotTest/src/android/input/screenshot/LayoutPreview.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import android.content.Context +import android.graphics.Bitmap +import android.graphics.Canvas +import android.hardware.input.InputManager +import android.hardware.input.KeyboardLayout +import android.util.TypedValue +import kotlin.math.roundToInt + +object LayoutPreview { + fun createLayoutPreview(context: Context, layout: KeyboardLayout?): Bitmap { + val im = context.getSystemService(InputManager::class.java)!! + val width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, + 600.0F, context.getResources().getDisplayMetrics()).roundToInt() + val height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, + 200.0F, context.getResources().getDisplayMetrics()).roundToInt() + val drawable = im.getKeyboardLayoutPreview(layout, width, height)!! + val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) + val canvas = Canvas(bitmap) + drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()) + drawable.draw(canvas) + return bitmap + } +}
\ No newline at end of file |