Skip to content

Commit

Permalink
Adds more espresso-device samples
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseAlcerreca committed Jan 18, 2024
1 parent 910bd7c commit b976d81
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 8 deletions.
20 changes: 18 additions & 2 deletions ui/espresso/EspressoDeviceSample/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {
compileSdk 33
defaultConfig {
applicationId "com.example.android.testing.espresso.EspressoDeviceSample"
minSdkVersion 14
minSdkVersion 19
targetSdkVersion 33
versionCode 1
versionName "1.0"
Expand All @@ -15,13 +15,19 @@ android {
}
productFlavors {
}

testOptions {

emulatorControl {
enable = true
}
unitTests {

includeAndroidResources = true
}
managedDevices {
devices {
// run with ../gradlew nexusOneApi30DebugAndroidTest
// run with ./gradlew nexusOneApi30DebugAndroidTest
nexusOneApi30(com.android.build.api.dsl.ManagedVirtualDevice) {
// A lower resolution device is used here for better emulator performance
device = "Nexus One"
Expand All @@ -31,6 +37,16 @@ android {
// to take a screenshot
systemImageSource = "aosp-atd"
}
// run with ./gradlew foldApi34DebugAndroidTest
foldApi34(com.android.build.api.dsl.ManagedVirtualDevice) {
// A lower resolution device is used here for better emulator performance
device = "Pixel Fold"
apiLevel = 34
// Also use the AOSP ATD image for better emulator performance
// The androidx.test screenshot APIs will automatically enable hardware rendering
// to take a screenshot
systemImageSource = "aosp"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
android:versionCode="1"
android:versionName="1.0">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permissions.ACCESS_NETWORK_STATE" />

<instrumentation android:targetPackage="com.example.android.testing.espresso.EspressoDeviceSample"
android:name="androidx.test.runner.AndroidJUnitRunner"/>

<application tools:replace="label" android:label="EspressoDeviceSampleTest" />

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.android.testing.espresso.EspressoDeviceSample

import androidx.test.espresso.Espresso.closeSoftKeyboard
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.typeText
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.device.DeviceInteraction.Companion.setBookMode
import androidx.test.espresso.device.DeviceInteraction.Companion.setClosedMode
import androidx.test.espresso.device.DeviceInteraction.Companion.setFlatMode
import androidx.test.espresso.device.DeviceInteraction.Companion.setTabletopMode
import androidx.test.espresso.device.EspressoDevice.Companion.onDevice
import androidx.test.espresso.device.action.ScreenOrientation
import androidx.test.espresso.device.action.setClosedMode
import androidx.test.espresso.device.action.setFlatMode
import androidx.test.espresso.device.action.setTabletopMode
import androidx.test.espresso.device.controller.DeviceMode.TABLETOP
import androidx.test.espresso.device.filter.RequiresDeviceMode
import androidx.test.espresso.device.rules.ScreenOrientationRule
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/*
* Illustrates usage of [onDevice] API to change the mode of foldable devices.
*/
@RunWith(AndroidJUnit4::class)
class OnDeviceTest {

@get:Rule var activityScenarioRule = activityScenarioRule<MainActivity>()

@get:Rule val screenOrientationRule: ScreenOrientationRule =
ScreenOrientationRule(ScreenOrientation.PORTRAIT)

@Test
@RequiresDeviceMode(TABLETOP)
fun changeText_sameActivity() {

onDevice().setBookMode()
// Type text and then press the button.
onView(withId(R.id.hello_tv)).check(matches(isDisplayed()))
onDevice().setTabletopMode()
onDevice().setClosedMode()
onView(withId(R.id.hello_tv)).check(matches(isDisplayed()))
onDevice().setFlatMode()
onView(withId(R.id.hello_tv)).check(matches(isDisplayed()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.example.android.testing.espresso.EspressoDeviceSample

import androidx.test.espresso.device.EspressoDevice.Companion.onDevice
import androidx.test.espresso.device.action.setTabletopMode
import androidx.test.espresso.device.filter.RequiresDisplay
import androidx.test.espresso.device.sizeclass.HeightSizeClass
import androidx.test.espresso.device.sizeclass.WidthSizeClass
Expand All @@ -27,6 +29,7 @@ import org.junit.runner.RunWith
*/
@RunWith(AndroidJUnit4::class)
class RequiresDisplayTest {

@RequiresDisplay(
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.COMPACT,
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.COMPACT
Expand Down Expand Up @@ -90,4 +93,4 @@ class RequiresDisplayTest {
)
@Test
fun testOnDevicesWithExpandedWidthAndHeight() {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />

<application
android:icon="@drawable/ic_launcher"
Expand All @@ -31,5 +32,4 @@
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.testing.espresso.EspressoDeviceSample" >

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="28" />
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="28" />
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/hello_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
4 changes: 2 additions & 2 deletions ui/espresso/EspressoDeviceSample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

buildscript {
ext.kotlinVersion = "1.7.10"
ext.agpVersion = "8.1.1"
ext.agpVersion = "8.3.0-beta02"
repositories {
// Insert local test repo here
google()
Expand All @@ -29,5 +29,5 @@ ext {
robolectricVersion = "4.10.3"
extTruthVersion = "1.6.0-alpha01"
extJUnitVersion = "1.2.0-alpha01"
espressoDeviceVersion = "1.0.0-alpha01"
espressoDeviceVersion = "1.0.0-alpha07"
}
1 change: 1 addition & 0 deletions ui/espresso/EspressoDeviceSample/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ android.defaults.buildfeatures.buildconfig=true
android.nonFinalResIds=false
android.nonTransitiveRClass=false
android.useAndroidX=true
android.experimental.androidTest.enableEmulatorControl=true
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Thu Jan 18 11:36:24 UTC 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

0 comments on commit b976d81

Please sign in to comment.