Skip to content

Commit f590b02

Browse files
authored
Merge pull request #449 from kmcauliffe6/espressoDeviceSample
Add a testing sample for Espresso Device
2 parents f533f18 + f729369 commit f590b02

File tree

30 files changed

+888
-1
lines changed

30 files changed

+888
-1
lines changed

projects.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ ui/espresso/AccessibilitySample
55
ui/espresso/BasicSample
66
ui/espresso/CustomMatcherSample
77
ui/espresso/DataAdapterSample
8+
ui/espresso/EspressoDeviceSample
89
ui/espresso/FragmentScenarioSample
910
ui/espresso/IdlingResourceSample
1011
ui/espresso/IntentsAdvancedSample
@@ -15,4 +16,4 @@ ui/espresso/RecyclerViewSample
1516
ui/espresso/ScreenshotSample
1617
ui/espresso/WebBasicSample
1718
ui/uiautomator/BasicSample
18-
unit/BasicNativeAndroidTest
19+
unit/BasicNativeAndroidTest
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.gradle
2+
local.properties
3+
.idea
4+
.DS_Store
5+
build
6+
*.iml
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
licenses(["notice"]) # Apache 2.0
2+
3+
load("//:common_defs.bzl", "minSdkVersion", "targetSdkVersion")
4+
load("@rules_jvm_external//:defs.bzl", "artifact")
5+
6+
android_library(
7+
name = "EspressoDeviceSampleLib",
8+
srcs = glob(["app/src/main/**/*.java"]),
9+
custom_package = "com.example.android.testing.espresso.EspressoDeviceSample",
10+
manifest = "app/src/main/AndroidManifest.xml",
11+
resource_files = glob(["app/src/main/res/**/*"]),
12+
)
13+
14+
android_binary(
15+
name = "EspressoDeviceSample",
16+
custom_package = "com.example.android.testing.espresso.EspressoDeviceSample",
17+
manifest = "app/src/main/AppManifest.xml",
18+
manifest_values = {
19+
"minSdkVersion": minSdkVersion,
20+
"targetSdkVersion": targetSdkVersion,
21+
},
22+
deps = [":EspressoDeviceSampleLib"],
23+
)
24+
25+
android_library(
26+
name = "EspressoDeviceSampleTestLib",
27+
srcs = glob(["app/src/androidTest/**/*.java"]),
28+
custom_package = "com.example.android.testing.espresso.EspressoDeviceSample.test",
29+
deps = [
30+
":EspressoDeviceSampleLib",
31+
"//:test_deps",
32+
],
33+
)
34+
35+
android_binary(
36+
name = "EspressoDeviceSampleTest",
37+
custom_package = "com.example.android.testing.espresso.EspressoDeviceSample.test",
38+
instruments = ":EspressoDeviceSample",
39+
manifest = "app/src/androidTest/AndroidManifest.xml",
40+
manifest_values = {
41+
"minSdkVersion": minSdkVersion,
42+
"targetSdkVersion": targetSdkVersion,
43+
},
44+
deps = [":EspressoDeviceSampleTestLib"],
45+
)
46+
47+
API_LEVELS = [
48+
"19_x86",
49+
"21_x86",
50+
"22_x86",
51+
"23_x86",
52+
]
53+
54+
[android_instrumentation_test(
55+
name = "EspressoDeviceSampleInstrumentationTest_%s" % API_LEVEL,
56+
target_device = "@android_test_support//tools/android/emulated_devices/generic_phone:android_%s_qemu2" % API_LEVEL,
57+
test_app = ":EspressoDeviceSampleTest",
58+
) for API_LEVEL in API_LEVELS]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Basic sample for Espresso Device
2+
3+
The Espresso Device API enables synchronized interactions with the test device. This API is experimental and subject to change. Currently, it is only supported on instrumentation tests run on emulators.
4+
5+
To skip tests on devices that do not have certain display attributes, such as display width and height, annotate your test with @RequiresDisplay. This annotation takes in a WidthSizeClassEnum and a HeightSizeClassEnum. It can be applied to test classes or test methods. For details on these size classes, see https://developer.android.com/guide/topics/large-screens/support-different-screen-sizes.
6+
7+
This project uses the Gradle build system. You don't need an IDE to build and execute it but Android Studio 3.4 is recommended.
8+
9+
1. Download the project code, preferably using `git clone`.
10+
1. In Android Studio, select *File* | *Open...* and point to the `./build.gradle` file.
11+
1. Check out the relevant code:
12+
* The application under test is located in `src/main/java`
13+
* Instrumentation Tests are in `src/androidTest/java`
14+
* Local Tests are in `src/test/java`
15+
1. Create and run the Instrumented test configuration
16+
* Open *Run* menu | *Edit Configurations*
17+
* Add a new *Android Instrumented Tests* configuration
18+
* Choose the `app` module
19+
* Connect a device or start an emulator
20+
* Turn animations off.
21+
(On your device, under Settings->Developer options disable the following 3 settings: "Window animation scale", "Transition animation scale" and "Animator duration scale")
22+
* Run the newly created configuration
23+
* The application will be started on the device/emulator and a series of actions will be performed automatically.
24+
25+
If you are using Android Studio, the *Run* window will show the test results.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
apply plugin: 'com.android.application'
2+
3+
apply plugin: 'kotlin-android'
4+
5+
apply plugin: 'kotlin-android-extensions'
6+
7+
android {
8+
compileSdkVersion 33
9+
buildToolsVersion rootProject.buildToolsVersion
10+
defaultConfig {
11+
applicationId "com.example.android.testing.espresso.EspressoDeviceSample"
12+
minSdkVersion 14
13+
targetSdkVersion 33
14+
versionCode 1
15+
versionName "1.0"
16+
17+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
18+
}
19+
lintOptions {
20+
abortOnError false
21+
}
22+
productFlavors {
23+
}
24+
testOptions {
25+
unitTests {
26+
includeAndroidResources = true
27+
}
28+
managedDevices {
29+
devices {
30+
// run with ../gradlew nexusOneApi30DebugAndroidTest
31+
nexusOneApi30(com.android.build.api.dsl.ManagedVirtualDevice) {
32+
// A lower resolution device is used here for better emulator performance
33+
device = "Nexus One"
34+
apiLevel = 30
35+
// Also use the AOSP ATD image for better emulator performance
36+
// The androidx.test screenshot APIs will automatically enable hardware rendering
37+
// to take a screenshot
38+
systemImageSource = "aosp-atd"
39+
}
40+
}
41+
}
42+
}
43+
}
44+
45+
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
46+
kotlinOptions {
47+
jvmTarget = "1.8"
48+
}
49+
}
50+
51+
dependencies {
52+
androidTestImplementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
53+
androidTestImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
54+
androidTestImplementation 'androidx.test.ext:junit-ktx:' + rootProject.extJUnitVersion
55+
androidTestImplementation 'androidx.test.espresso:espresso-device:' + rootProject.espressoDeviceVersion
56+
57+
testImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
58+
testImplementation 'junit:junit:4.12'
59+
testImplementation 'org.robolectric:robolectric:' + rootProject.robolectricVersion
60+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2022 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:tools="http://schemas.android.com/tools"
19+
package="com.example.android.testing.espresso.EspressoDeviceSample.test"
20+
android:versionCode="1"
21+
android:versionName="1.0">
22+
23+
<instrumentation android:targetPackage="com.example.android.testing.espresso.EspressoDeviceSample"
24+
android:name="androidx.test.runner.AndroidJUnitRunner"/>
25+
26+
<application tools:replace="label" android:label="EspressoDeviceSampleTest" />
27+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
package com.example.android.testing.espresso.EspressoDeviceSample
17+
18+
import androidx.test.espresso.device.filter.RequiresDisplay
19+
import androidx.test.espresso.device.sizeclass.HeightSizeClass
20+
import androidx.test.espresso.device.sizeclass.WidthSizeClass
21+
import androidx.test.ext.junit.runners.AndroidJUnit4
22+
import org.junit.Test
23+
import org.junit.runner.RunWith
24+
25+
/*
26+
* Illustrates usage of @RequiresDisplay API to filter tests based on display attributes such a screen size.
27+
*/
28+
@RunWith(AndroidJUnit4::class)
29+
class RequiresDisplayTest {
30+
@RequiresDisplay(
31+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.COMPACT,
32+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.COMPACT
33+
)
34+
@Test
35+
fun testOnDevicesWithCompactWidthAndHeight() {}
36+
37+
@RequiresDisplay(
38+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.COMPACT,
39+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.MEDIUM
40+
)
41+
@Test
42+
fun testOnDevicesWithCompactWidthAndMediumHeight() {}
43+
44+
@RequiresDisplay(
45+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.MEDIUM,
46+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.COMPACT
47+
)
48+
@Test
49+
fun testOnDevicesWithMediumWidthAndCompactHeight() {}
50+
51+
@RequiresDisplay(
52+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.COMPACT,
53+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.EXPANDED
54+
)
55+
@Test
56+
fun testOnDevicesWithCompactWidthAndExpandedHeight() {}
57+
58+
@RequiresDisplay(
59+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.EXPANDED,
60+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.COMPACT
61+
)
62+
@Test
63+
fun testOnDevicesWithExpandedWidthAndCompactHeight() {}
64+
65+
@RequiresDisplay(
66+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.MEDIUM,
67+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.MEDIUM
68+
)
69+
@Test
70+
fun testOnDevicesWithMediumWidthAndHeight() {}
71+
72+
@RequiresDisplay(
73+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.EXPANDED,
74+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.MEDIUM,
75+
)
76+
@Test
77+
fun testOnDevicesWithExpandedWidthAndMediumHeight() {}
78+
79+
80+
@RequiresDisplay(
81+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.MEDIUM,
82+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.EXPANDED
83+
)
84+
@Test
85+
fun testOnDevicesWithMediumWidthAndExpandedHeight() {}
86+
87+
@RequiresDisplay(
88+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.EXPANDED,
89+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.EXPANDED
90+
)
91+
@Test
92+
fun testOnDevicesWithExpandedWidthAndHeight() {}
93+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2022 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
19+
package="com.example.android.testing.espresso.EspressoDeviceSample" >
20+
21+
<application
22+
android:icon="@drawable/ic_launcher"
23+
android:label="@string/app_name"
24+
android:theme="@style/AppTheme" >
25+
<activity
26+
android:name="com.example.android.testing.espresso.EspressoDeviceSample.MainActivity"
27+
android:label="@string/app_name"
28+
android:exported="true">
29+
<intent-filter>
30+
<action android:name="android.intent.action.MAIN" />
31+
<category android:name="android.intent.category.LAUNCHER" />
32+
</intent-filter>
33+
</activity>
34+
</application>
35+
36+
</manifest>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2022 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
19+
package="com.example.android.testing.espresso.EspressoDeviceSample" >
20+
21+
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="28" />
22+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 com.example.android.testing.espresso.EspressoDeviceSample;
18+
19+
import android.app.Activity;
20+
import android.os.Bundle;
21+
22+
/**
23+
* An empty {@link Activity} that Espresso Device APIs are tested against.
24+
*/
25+
public class MainActivity extends Activity {
26+
@Override
27+
protected void onCreate(Bundle savedInstanceState) {
28+
super.onCreate(savedInstanceState);
29+
setContentView(R.layout.activity_main);
30+
}
31+
}

0 commit comments

Comments
 (0)