### Clone Repository and Setup Source: https://github.com/android/testing-samples/blob/main/README.md Clone the repository and set up the Bazel environment by editing the WORKSPACE file to point to your local SDK. ```bash # Clone the repository if you haven't. $ git clone https://github.com/google/android-testing $ cd android-testing # Edit the path to your local SDK at the top of the WORKSPACE file $ $EDITOR WORKSPACE ``` -------------------------------- ### Uninstall Test Packages from Local Device Source: https://github.com/android/testing-samples/blob/main/README.md Manually uninstall test packages from a local device or emulator after testing, as they are not automatically removed. This command lists and uninstalls packages starting with 'com.example.android.testing'. ```bash # adb shell pm list packages com.example.android.testing | cut -d ':' -f 2 | tr -d ' ' | xargs -L1 -t adb uninstall ``` -------------------------------- ### CMakeLists.txt Configuration Source: https://github.com/android/testing-samples/blob/main/unit/BasicNativeAndroidTest/app/src/main/cpp/CMakeLists.txt Sets up the minimum CMake version, project name, and finds necessary packages like Googletest and JUnit-GTest. Include directories are specified for header files. ```cmake cmake_minimum_required(VERSION 3.10.2) project(junit-gtest-example LANGUAGES CXX) find_package(googletest REQUIRED CONFIG) find_package(junit-gtest REQUIRED CONFIG) include_directories(include) ``` -------------------------------- ### Run Unit Tests with Gradle Source: https://github.com/android/testing-samples/blob/main/unit/BasicSample/README.md Execute all unit tests for the project using the Gradle wrapper script. A successful run will output a 'BUILD SUCCESSFUL' message. ```bash ./gradlew test ``` -------------------------------- ### Test on a Local Device or Emulator Source: https://github.com/android/testing-samples/blob/main/README.md Run tests on a connected local device or emulator. Ensure the device is listed by `adb devices` before executing. ```bash # Test with a local device or emulator. Ensure that `adb devices` lists the device. $ bazel test //... --config=local_device ``` -------------------------------- ### Run All Tests with GUI Enabled Source: https://github.com/android/testing-samples/blob/main/README.md Execute all tests with the GUI configuration enabled. This is typically used when visual feedback or interaction is required during testing. ```bash # Test everything with GUI enabled $ bazel test //... --config=gui ``` -------------------------------- ### Run All Tests in Headless Mode Source: https://github.com/android/testing-samples/blob/main/README.md Execute all tests in a headless mode, which does not require a graphical display. This is useful for CI environments. ```bash # Test everything in a headless mode (no graphical display) $ bazel test //... --config=headless ``` -------------------------------- ### Add Native Libraries Source: https://github.com/android/testing-samples/blob/main/unit/BasicNativeAndroidTest/app/src/main/cpp/CMakeLists.txt Defines shared libraries for the main application logic ('adder') and its tests ('adder-test'). The 'adder' library is built from src/adder.cpp, and the 'adder-test' library from test/adder_test.cpp. ```cmake add_library(adder SHARED src/adder.cpp) ``` ```cmake add_library(adder-test SHARED test/adder_test.cpp) ``` -------------------------------- ### Test a Single Instrumentation Test Source: https://github.com/android/testing-samples/blob/main/README.md Run a specific instrumentation test, identified by its Bazel target and configuration. Ensure to use the --config=headless option for headless execution. ```bash # Test a single test, e.g. ui/espresso/BasicSample/BUILD.bazel $ bazel test //ui/uiautomator/BasicSample:BasicSampleInstrumentationTest_21_x86 --config=headless ``` -------------------------------- ### Query for Android Instrumentation Test Targets Source: https://github.com/android/testing-samples/blob/main/README.md Use Bazel's query command to find all targets of type android_instrumentation_test within the repository. This helps in identifying available tests. ```bash # Query for all android_instrumentation_test targets $ bazel query 'kind(android_instrumentation_test, //...)' //ui/uiautomator/BasicSample:BasicSampleInstrumentationTest_23_x86 //ui/uiautomator/BasicSample:BasicSampleInstrumentationTest_22_x86 //ui/uiautomator/BasicSample:BasicSampleInstrumentationTest_21_x86 //ui/uiautomator/BasicSample:BasicSampleInstrumentationTest_19_x86 //ui/espresso/RecyclerViewSample:RecyclerViewSampleInstrumentationTest_23_x86 //ui/espresso/RecyclerViewSample:RecyclerViewSampleInstrumentationTest_22_x86 //ui/espresso/RecyclerViewSample:RecyclerViewSampleInstrumentationTest_21_x86 //ui/espresso/RecyclerViewSample:RecyclerViewSampleInstrumentationTest_19_x86 //ui/espresso/MultiWindowSample:MultiWindowSampleInstrumentationTest_23_x86 //ui/espresso/MultiWindowSample:MultiWindowSampleInstrumentationTest_22_x86 ... ``` -------------------------------- ### Link Test Library with Dependencies Source: https://github.com/android/testing-samples/blob/main/unit/BasicNativeAndroidTest/app/src/main/cpp/CMakeLists.txt Links the 'adder-test' library with its dependencies: the 'adder' library, Googletest, and JUnit-GTest. This ensures that the test library can access the functionality of the main library and the testing frameworks. ```cmake target_link_libraries(adder-test PRIVATE adder googletest::gtest junit-gtest::junit-gtest ) ``` -------------------------------- ### HTML and CSS for Web Form Source: https://github.com/android/testing-samples/blob/main/ui/espresso/WebBasicSample/app/src/main/assets/web_form_response.html Basic HTML structure and CSS for styling the web form response page. Includes responsive design adjustments for smaller screens. ```html body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 50px; background-color: #fff; border-radius: 1em; } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { body { background-color: #fff; } div { width: auto; margin: 0 auto; border-radius: 0; padding: 1em; } } ``` -------------------------------- ### Test on a Specific Local Device Source: https://github.com/android/testing-samples/blob/main/README.md When multiple devices are connected, specify the target device serial number using the `--test_arg=--device_serial_number=$identifier` flag. ```bash # If multiple devices are connected, add --device_serial_number=$identifier where $identifier is the name of the device in `adb devices` $ bazel test //... --config=local_device --test_arg=--device_serial_number=$identifier ``` -------------------------------- ### HTML Structure for Web Form Source: https://github.com/android/testing-samples/blob/main/ui/espresso/WebBasicSample/app/src/main/assets/web_form.html Basic HTML structure for a web form with input fields and a display area. This is the content loaded into the WebView. ```html body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 50px; background-color: #fff; border-radius: 1em; } #message { margin: 0 auto 12px auto; } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { body { background-color: #fff; } div { width: auto; margin: 0 auto; border-radius: 0; padding: 1em; } } ``` -------------------------------- ### Legacy Support Library Espresso Idling Resource Dependency Source: https://github.com/android/testing-samples/blob/main/ui/espresso/IdlingResourceSample/README.md Add this dependency to your app's build.gradle file to use the espresso-idling-resource library with the older Android support libraries. ```gradle androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.android.support.test.espresso:espresso-idling-resource:3.0.2' ``` -------------------------------- ### AndroidX Espresso Idling Resource Dependency Source: https://github.com/android/testing-samples/blob/main/ui/espresso/IdlingResourceSample/README.md Add this dependency to your app's build.gradle file to use the espresso-idling-resource library with AndroidX. ```gradle androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' implementation 'androidx.test.espresso:espresso-idling-resource:3.1.1' ``` -------------------------------- ### JavaScript to Display Form Input Source: https://github.com/android/testing-samples/blob/main/ui/espresso/WebBasicSample/app/src/main/assets/web_form_response.html JavaScript code to extract a specific parameter from the URL query string and display it on the page. Assumes the input is the first parameter after a '?' and has a 'name=' prefix. ```javascript var query = document.location.href.substring(document.location.href.indexOf("?") + 1); var text_input = query.split("&")[0].split("=")[1]; document.getElementById('response').innerHTML = text_input; ``` -------------------------------- ### JavaScript Function to Update Text Source: https://github.com/android/testing-samples/blob/main/ui/espresso/WebBasicSample/app/src/main/assets/web_form.html A JavaScript function that reads a value from an input field and updates the content of a message element on the page. This can be called from Espresso. ```javascript function changeText() { var typedText = document.getElementById('text_input').value; document.getElementById('message').innerHTML=typedText; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.