Skip to content

Commit e83318e

Browse files
author
Paige Mcauliffe
committed
Add testing sample for Espresso Device
1 parent f533f18 commit e83318e

File tree

27 files changed

+903
-0
lines changed

27 files changed

+903
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 = "EspressoDeviceBasicSampleLib",
8+
srcs = glob(["app/src/main/**/*.java"]),
9+
custom_package = "com.example.android.testing.espresso.EspressoDeviceBasicSample",
10+
manifest = "app/src/main/AndroidManifest.xml",
11+
resource_files = glob(["app/src/main/res/**/*"]),
12+
deps = [
13+
artifact("com.google.guava:guava")
14+
],
15+
)
16+
17+
android_binary(
18+
name = "EspressoDeviceBasicSample",
19+
custom_package = "com.example.android.testing.espresso.EspressoDeviceBasicSample",
20+
manifest = "app/src/main/AppManifest.xml",
21+
manifest_values = {
22+
"minSdkVersion": minSdkVersion,
23+
"targetSdkVersion": targetSdkVersion,
24+
},
25+
deps = [":BasicSampleLib"],
26+
)
27+
28+
android_library(
29+
name = "EspressoDeviceBasicSampleTestLib",
30+
srcs = glob(["app/src/androidTest/**/*.java"]),
31+
custom_package = "com.example.android.testing.espresso.EspressoDeviceBasicSample.test",
32+
deps = [
33+
":EspressoDeviceBasicSampleLib",
34+
"//:test_deps",
35+
],
36+
)
37+
38+
android_binary(
39+
name = "EspressoDeviceBasicSampleTest",
40+
custom_package = "com.example.android.testing.espresso.EspressoDeviceBasicSample.test",
41+
instruments = ":EspressoDeviceBasicSample",
42+
manifest = "app/src/androidTest/AndroidManifest.xml",
43+
manifest_values = {
44+
"minSdkVersion": minSdkVersion,
45+
"targetSdkVersion": targetSdkVersion,
46+
},
47+
deps = [":EspressoDeviceBasicSampleTestLib"],
48+
)
49+
50+
API_LEVELS = [
51+
"19_x86",
52+
"21_x86",
53+
"22_x86",
54+
"23_x86",
55+
]
56+
57+
[android_instrumentation_test(
58+
name = "EspressoDeviceBasicSampleInstrumentationTest_%s" % API_LEVEL,
59+
target_device = "@android_test_support//tools/android/emulated_devices/generic_phone:android_%s_qemu2" % API_LEVEL,
60+
test_app = ":EspressoDeviceBasicSampleTest",
61+
) 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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
31+
// run with ../gradlew nexusOneApi30DebugAndroidTest
32+
nexusOneApi30(com.android.build.api.dsl.ManagedVirtualDevice) {
33+
// A lower resolution device is used here for better emulator performance
34+
device = "Nexus One"
35+
apiLevel = 30
36+
// Also use the AOSP ATD image for better emulator performance
37+
systemImageSource = "aosp-atd"
38+
}
39+
}
40+
}
41+
}
42+
}
43+
44+
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
45+
kotlinOptions {
46+
jvmTarget = "1.8"
47+
}
48+
}
49+
50+
dependencies {
51+
// App dependencies
52+
implementation 'androidx.annotation:annotation:' + rootProject.androidxAnnotationVersion;
53+
implementation 'com.google.guava:guava:' + rootProject.guavaVersion
54+
55+
// Testing-only dependencies
56+
androidTestImplementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
57+
androidTestImplementation 'androidx.test:core:' + rootProject.coreVersion
58+
androidTestImplementation 'androidx.test:core-ktx:' + rootProject.coreVersion
59+
androidTestImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
60+
androidTestImplementation 'androidx.test.ext:junit-ktx:' + rootProject.extJUnitVersion
61+
androidTestImplementation 'androidx.test:runner:' + rootProject.runnerVersion
62+
androidTestImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion
63+
androidTestImplementation 'androidx.test.espresso:espresso-device:' + rootProject.espressoDeviceVersion
64+
65+
testImplementation 'androidx.test:core:' + rootProject.coreVersion;
66+
testImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
67+
testImplementation 'junit:junit:4.12'
68+
testImplementation 'org.robolectric:robolectric:' + rootProject.robolectricVersion
69+
testImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion
70+
testImplementation 'androidx.test.espresso:espresso-intents:' + rootProject.espressoVersion
71+
testImplementation 'androidx.test.ext:truth:' + rootProject.extTruthVersion
72+
testImplementation 'androidx.test.espresso:espresso-device:' + rootProject.espressoDeviceVersion
73+
}
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,96 @@
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+
@Test
31+
fun shouldAlwaysRun() {}
32+
33+
@RequiresDisplay(
34+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.COMPACT,
35+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.COMPACT
36+
)
37+
@Test
38+
fun testOnDevicesWithCompactWidthAndHeight() {}
39+
40+
@RequiresDisplay(
41+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.COMPACT,
42+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.MEDIUM
43+
)
44+
@Test
45+
fun testOnDevicesWithCompactWidthAndMediumHeight() {}
46+
47+
@RequiresDisplay(
48+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.MEDIUM,
49+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.COMPACT
50+
)
51+
@Test
52+
fun testOnDevicesWithMediumWidthAndCompactHeight() {}
53+
54+
@RequiresDisplay(
55+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.COMPACT,
56+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.EXPANDED
57+
)
58+
@Test
59+
fun testOnDevicesWithCompactWidthAndExpandedHeight() {}
60+
61+
@RequiresDisplay(
62+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.EXPANDED,
63+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.COMPACT
64+
)
65+
@Test
66+
fun testOnDevicesWithExpandedWidthAndCompactHeight() {}
67+
68+
@RequiresDisplay(
69+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.MEDIUM,
70+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.MEDIUM
71+
)
72+
@Test
73+
fun testOnDevicesWithMediumWidthAndHeight() {}
74+
75+
@RequiresDisplay(
76+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.EXPANDED,
77+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.MEDIUM,
78+
)
79+
@Test
80+
fun testOnDevicesWithExpandedWidthAndMediumHeight() {}
81+
82+
83+
@RequiresDisplay(
84+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.MEDIUM,
85+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.EXPANDED
86+
)
87+
@Test
88+
fun testOnDevicesWithMediumWidthAndExpandedHeight() {}
89+
90+
@RequiresDisplay(
91+
widthSizeClass = WidthSizeClass.Companion.WidthSizeClassEnum.EXPANDED,
92+
heightSizeClass = HeightSizeClass.Companion.HeightSizeClassEnum.EXPANDED
93+
)
94+
@Test
95+
fun testOnDevicesWithExpandedWidthAndHeight() {}
96+
}
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.BasicSample" >
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+
}
2.04 KB
Loading
1.3 KB
Loading

0 commit comments

Comments
 (0)