Use EspressoDevice to set display size

Change-Id: Ic944041d91ab21d3c4274832ba92db32f6dac884
jdk/espresso_resize_display
Jonathan Koren 5 months ago
parent 9e4532f0eb
commit 3b895550fd

@ -67,6 +67,9 @@ android {
unitTests {
isIncludeAndroidResources = true
}
emulatorControl {
enable = true
}
}
namespace = "com.google.samples.apps.nowinandroid"
}
@ -124,6 +127,7 @@ dependencies {
androidTestImplementation(projects.core.dataTest)
androidTestImplementation(projects.core.datastoreTest)
androidTestImplementation(libs.androidx.test.espresso.core)
androidTestImplementation(libs.androidx.test.espresso.device)
androidTestImplementation(libs.androidx.navigation.testing)
androidTestImplementation(libs.androidx.compose.ui.test)
androidTestImplementation(libs.hilt.android.testing)

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2024 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
~
~ https://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"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

@ -17,20 +17,18 @@
package com.google.samples.apps.nowinandroid.ui.interests2pane
import androidx.activity.compose.BackHandler
import androidx.compose.material3.adaptive.Posture
import androidx.compose.material3.adaptive.WindowAdaptiveInfo
import androidx.compose.ui.test.DeviceConfigurationOverride
import androidx.compose.ui.test.ForcedSize
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsNotDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.test.espresso.Espresso
import androidx.window.core.layout.WindowSizeClass
import androidx.test.espresso.device.DeviceInteraction.Companion.setDisplaySize
import androidx.test.espresso.device.EspressoDevice
import androidx.test.espresso.device.rules.DisplaySizeRule
import androidx.test.espresso.device.sizeclass.HeightSizeClass
import androidx.test.espresso.device.sizeclass.WidthSizeClass
import com.google.samples.apps.nowinandroid.core.data.repository.TopicsRepository
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
import com.google.samples.apps.nowinandroid.core.model.data.Topic
@ -61,6 +59,9 @@ class InterestsListDetailScreenTest {
@get:Rule(order = 2)
val composeTestRule = createAndroidComposeRule<HiltComponentActivity>()
@get:Rule(order = 3)
val displaySizeRule: DisplaySizeRule = DisplaySizeRule()
@Inject
lateinit var topicsRepository: TopicsRepository
@ -71,17 +72,9 @@ class InterestsListDetailScreenTest {
private val Topic.testTag
get() = "topic:${this.id}"
// Overrides for device sizes.
private enum class TestDeviceConfig(widthDp: Float, heightDp: Float) {
Compact(412f, 915f),
Expanded(1200f, 840f),
;
val sizeOverride = DeviceConfigurationOverride.ForcedSize(DpSize(widthDp.dp, heightDp.dp))
val adaptiveInfo = WindowAdaptiveInfo(
windowSizeClass = WindowSizeClass.compute(widthDp, heightDp),
windowPosture = Posture(),
)
/** Convenience function for getting all topics during tests, */
private fun getTopics(): List<Topic> = runBlocking {
topicsRepository.getTopics().first().sortedBy { it.name }
}
@Before
@ -89,21 +82,16 @@ class InterestsListDetailScreenTest {
hiltRule.inject()
}
/** Convenience function for getting all topics during tests, */
private fun getTopics(): List<Topic> = runBlocking {
topicsRepository.getTopics().first()
}
@Test
fun expandedWidth_initialState_showsTwoPanesWithPlaceholder() {
EspressoDevice.onDevice().setDisplaySize(
widthSizeClass = WidthSizeClass.EXPANDED,
heightSizeClass = HeightSizeClass.EXPANDED,
)
composeTestRule.apply {
setContent {
with(TestDeviceConfig.Expanded) {
DeviceConfigurationOverride(override = sizeOverride) {
NiaTheme {
InterestsListDetailScreen(windowAdaptiveInfo = adaptiveInfo)
}
}
NiaTheme {
InterestsListDetailScreen()
}
}
@ -114,14 +102,14 @@ class InterestsListDetailScreenTest {
@Test
fun compactWidth_initialState_showsListPane() {
EspressoDevice.onDevice().setDisplaySize(
widthSizeClass = WidthSizeClass.COMPACT,
heightSizeClass = HeightSizeClass.MEDIUM,
)
composeTestRule.apply {
setContent {
with(TestDeviceConfig.Compact) {
DeviceConfigurationOverride(override = sizeOverride) {
NiaTheme {
InterestsListDetailScreen(windowAdaptiveInfo = adaptiveInfo)
}
}
NiaTheme {
InterestsListDetailScreen()
}
}
@ -132,14 +120,14 @@ class InterestsListDetailScreenTest {
@Test
fun expandedWidth_topicSelected_updatesDetailPane() {
EspressoDevice.onDevice().setDisplaySize(
widthSizeClass = WidthSizeClass.EXPANDED,
heightSizeClass = HeightSizeClass.EXPANDED,
)
composeTestRule.apply {
setContent {
with(TestDeviceConfig.Expanded) {
DeviceConfigurationOverride(override = sizeOverride) {
NiaTheme {
InterestsListDetailScreen(windowAdaptiveInfo = adaptiveInfo)
}
}
NiaTheme {
InterestsListDetailScreen()
}
}
@ -154,14 +142,14 @@ class InterestsListDetailScreenTest {
@Test
fun compactWidth_topicSelected_showsTopicDetailPane() {
EspressoDevice.onDevice().setDisplaySize(
widthSizeClass = WidthSizeClass.COMPACT,
heightSizeClass = HeightSizeClass.MEDIUM,
)
composeTestRule.apply {
setContent {
with(TestDeviceConfig.Compact) {
DeviceConfigurationOverride(override = sizeOverride) {
NiaTheme {
InterestsListDetailScreen(windowAdaptiveInfo = adaptiveInfo)
}
}
NiaTheme {
InterestsListDetailScreen()
}
}
@ -176,20 +164,21 @@ class InterestsListDetailScreenTest {
@Test
fun expandedWidth_backPressFromTopicDetail_leavesInterests() {
EspressoDevice.onDevice().setDisplaySize(
widthSizeClass = WidthSizeClass.EXPANDED,
heightSizeClass = HeightSizeClass.EXPANDED,
)
var unhandledBackPress = false
composeTestRule.apply {
setContent {
with(TestDeviceConfig.Expanded) {
DeviceConfigurationOverride(override = sizeOverride) {
NiaTheme {
// Back press should not be handled by the two pane layout, and thus
// "fall through" to this BackHandler.
BackHandler {
unhandledBackPress = true
}
InterestsListDetailScreen(windowAdaptiveInfo = adaptiveInfo)
}
NiaTheme {
// Back press should not be handled by the two pane layout, and thus
// "fall through" to this BackHandler.
BackHandler {
unhandledBackPress = true
}
InterestsListDetailScreen()
}
}
@ -204,14 +193,14 @@ class InterestsListDetailScreenTest {
@Test
fun compactWidth_backPressFromTopicDetail_showsListPane() {
EspressoDevice.onDevice().setDisplaySize(
widthSizeClass = WidthSizeClass.COMPACT,
heightSizeClass = HeightSizeClass.MEDIUM,
)
composeTestRule.apply {
setContent {
with(TestDeviceConfig.Compact) {
DeviceConfigurationOverride(override = sizeOverride) {
NiaTheme {
InterestsListDetailScreen(windowAdaptiveInfo = adaptiveInfo)
}
}
NiaTheme {
InterestsListDetailScreen()
}
}

@ -42,3 +42,6 @@ android.defaults.buildfeatures.shaders=false
# Run Roborazzi screenshot tests with the local tests
roborazzi.test.verify=true
# Enable emulator control with Espresso Device API
android.experimental.androidTest.enableEmulatorControl=true

@ -16,6 +16,7 @@ androidxCore = "1.12.0"
androidxCoreSplashscreen = "1.0.1"
androidxDataStore = "1.0.0"
androidxEspresso = "3.5.1"
androidxEspressoDevice = "1.0.1"
androidxHiltNavigationCompose = "1.2.0"
androidxLifecycle = "2.8.3"
androidxMacroBenchmark = "1.2.4"
@ -101,6 +102,7 @@ androidx-navigation-testing = { group = "androidx.navigation", name = "navigatio
androidx-profileinstaller = { group = "androidx.profileinstaller", name = "profileinstaller", version.ref = "androidxProfileinstaller" }
androidx-test-core = { group = "androidx.test", name = "core", version.ref = "androidxTestCore" }
androidx-test-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "androidxEspresso" }
androidx-test-espresso-device = { group = "androidx.test.espresso", name = "espresso-device", version.ref = "androidxEspressoDevice" }
androidx-test-ext = { group = "androidx.test.ext", name = "junit-ktx", version.ref = "androidxTestExt" }
androidx-test-rules = { group = "androidx.test", name = "rules", version.ref = "androidxTestRules" }
androidx-test-runner = { group = "androidx.test", name = "runner", version.ref = "androidxTestRunner" }

Loading…
Cancel
Save