### Install App and Test App (Flavor2 Debug) Source: https://github.com/googlesamples/android-testing-templates/blob/master/AndroidTestingBlueprint/README.md Install the application and its corresponding test application for Flavor2 Debug using Gradle. ```sh ./gradlew app:installFlavor2Debug app:installFlavor2DebugAndroidTest ``` -------------------------------- ### Example Usage of Flavor1Class (Java) Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/api-reference/flavor-classes.md Demonstrates how to instantiate and check the readiness of Flavor1Class. Catches ClassNotFoundException if the flavor1 variant is not being built. ```java try { Flavor1Class flavor1 = new Flavor1Class(); if (flavor1.isReady()) { System.out.println("Flavor1 features available"); } } catch (ClassNotFoundException e) { System.out.println("Not building flavor1 variant"); } ``` -------------------------------- ### Example Usage of Flavor1Class (Kotlin) Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/api-reference/flavor-classes.md Demonstrates how to instantiate and check the readiness of Flavor1Class. Catches ClassNotFoundException if the flavor1 variant is not being built. ```kotlin try { val flavor1 = Flavor1Class() if (flavor1.isReady()) { println("Flavor1 features available") } } catch (e: ClassNotFoundException) { println("Not building flavor1 variant") } ``` -------------------------------- ### Dependency Injection Example Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/MODULE_ARCHITECTURE.md Shows a basic example of using a dependency injection container to provide instances of module classes. This pattern is recommended for more complex scenarios to manage dependencies. ```kotlin // Container provides instances val container = DIContainer() val plainKotlin = container.providePlainKotlin() val library = container.provideAndroidLibrary() ``` -------------------------------- ### Example Usage of MyPlainKotlinClass.isReady() Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/api-reference/plain-module-kotlin.md Demonstrates how to instantiate MyPlainKotlinClass and check its readiness state. Also shows its usage within an Android activity context. ```kotlin // Create instance val instance = MyPlainKotlinClass() // Check readiness if (instance.isReady()) { // Proceed with operations println("Module is ready") } // In activity context (from HelloTestingBlueprintActivity) plainKotlinClassInstance = MyPlainKotlinClass() val isReady = plainKotlinClassInstance.isReady() ``` -------------------------------- ### Install App and Test Module for ADB Source: https://github.com/googlesamples/android-testing-templates/blob/master/AndroidTestingBlueprint/README.md Install the main application and a specific flavor of the test-only module before running tests via ADB. ```gradle ./gradlew app:installFlavor1Debug module-flavor1-androidTest-only:installDebug ``` -------------------------------- ### Mockito Mock Setup Patterns Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/TESTING_FRAMEWORKS.md Illustrates various ways to set up mock behavior, including simple return values, chaining, exception throwing, and answer callbacks. ```kotlin // Simple return value mockWhen(object.method()).thenReturn(value) // Chain setup mockWhen(object.method1()).thenReturn(obj) .thenReturn(obj2) // Exception throwing mockWhen(object.method()).thenThrow(Exception()) // Answer callbacks mockWhen(object.method()).thenAnswer { invocation -> // Custom logic } ``` -------------------------------- ### Dependency Version Management Example Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/BUILD_SYSTEM.md Demonstrates how to define dependency versions in the root build.gradle file and access them in module build files using ext properties. ```gradle // Root build.gradle defines version ext { supportLibVersion = "27.0.1" } // Module build.gradle uses property implementation "com.android.support:appcompat-v7:$supportLibVersion" ``` -------------------------------- ### Dependency Tree Example Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/MODULE_ARCHITECTURE.md Visualizes the dependency tree of an application module, showing direct and transitive dependencies. This helps in understanding how modules and their libraries are included. ```plaintext app ├── module-plain-kotlin │ └── org.jetbrains.kotlin:kotlin-stdlib-jre7 ├── module-android-library │ └── (no dependencies) └── com.android.support:appcompat-v7 ``` -------------------------------- ### MyPlainKotlinClass Local Unit Test Example Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/api-reference/plain-module-kotlin.md An example of a local unit test for MyPlainKotlinClass using JUnit 4, verifying the behavior of the isReady() method. ```kotlin // Example test structure (based on project patterns) import org.junit.Test import org.junit.Assert.assertTrue class MyPlainKotlinClassTest { @Test fun testIsReady() { val instance = MyPlainKotlinClass() assertTrue(instance.isReady()) } } ``` -------------------------------- ### Resource Localization Example Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/RESOURCE_ORGANIZATION.md Demonstrates how to structure resource directories for multi-language support. Each `strings.xml` file in a language-specific directory (e.g., `values-fr`) provides translations for the same string IDs defined in the default `values/strings.xml`. ```xml Android Testing Blueprint ``` ```xml Plan de Test Android ``` -------------------------------- ### Transitive Dependency Example Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/MODULE_ARCHITECTURE.md Explains how dependencies of included modules are inherited by the main application. This example shows that when the app includes a module, it also inherits that module's dependencies. ```plaintext app depends on module-android-library module-android-library depends on com.android.support:appcompat-v7 Result: app automatically includes appcompat-v7 ``` -------------------------------- ### App Module Integration Example Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/MODULE_ARCHITECTURE.md Shows how the main application activity instantiates and uses classes from other modules (plain Kotlin and Android library). Ensure dependencies are correctly set up in the build files. ```kotlin class HelloTestingBlueprintActivity : AppCompatActivity() { // Dependency from plain module private lateinit var plainKotlinClassInstance: MyPlainKotlinClass // Dependency from Android library module private lateinit var androidLibraryClassInstance: AndroidLibraryModuleClass override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_hello_testing_blueprint) // Instantiate module classes plainKotlinClassInstance = MyPlainKotlinClass() androidLibraryClassInstance = AndroidLibraryModuleClass() } fun onClick(view: View) { // Use module methods if (plainKotlinClassInstance.isReady() && androidLibraryClassInstance.isReady()) { // Update UI } } } ``` -------------------------------- ### List Connected Devices Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/QUICK_START.md Use the adb devices command to list all connected devices and emulators. Ensure the emulator is started if no devices are found. ```bash # List devices ``` ```bash adb devices ``` -------------------------------- ### Example onClick Handler Logic Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/api-reference/espresso-testing.md This Kotlin code snippet shows the logic within an onClick handler that updates a TextView based on module readiness. ```kotlin fun onClick(view: View) { if (plainKotlinClassInstance.isReady() && androidLibraryClassInstance.isReady()) { androidTestingRocksTextView.text = getString(R.string.android_testing_rocks) } } ``` -------------------------------- ### Example Usage of MyPlainJavaClass isReady Method Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/api-reference/plain-module-java.md Demonstrates how to create an instance of MyPlainJavaClass and check its readiness state. This code can be used in various Java environments, including Android activities. ```java // Create instance MyPlainJavaClass instance = new MyPlainJavaClass(); // Check readiness if (instance.isReady()) { // Proceed with operations System.out.println("Module is ready"); } // In activity context (from HelloTestingBlueprintActivity) mPlainJavaClassInstance = new MyPlainJavaClass(); boolean isReady = mPlainJavaClassInstance.isReady(); ``` -------------------------------- ### Hamcrest Assertion Examples Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/TESTING_FRAMEWORKS.md Demonstrates various assertion matchers provided by the Hamcrest library for more expressive assertions. ```kotlin import org.hamcrest.CoreMatchers.* assertThat(actual, is(expected)) assertThat(actual, notNullValue()) assertThat(actual, nullValue()) assertThat(actual, instanceOf(Class::class.java)) assertThat(items, hasItem(expected)) assertThat(text, containsString(substring)) assertThat(number, greaterThan(threshold)) assertThat(number, lessThan(threshold)) ``` -------------------------------- ### Example Test Methods for Extension Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/api-reference/espresso-testing.md These are example method signatures for tests that can be added to extend the testing template. They cover editable fields, list interactions, navigation flows, and error states. ```kotlin @Test fun testEditableFields() { /* ... */ } ``` ```kotlin @Test fun testListInteraction() { /* ... */ } ``` ```kotlin @Test fun testNavigationFlow() { /* ... */ } ``` ```kotlin @Test fun testErrorStates() { /* ... */ } ``` -------------------------------- ### View Binding in Activity (Kotlin/Java) Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/RESOURCE_ORGANIZATION.md Example of how to reference UI elements like TextViews in an Activity using findViewById. ```kotlin // Kotlin androidTestingRocksTextView = findViewById(R.id.text_view_rocks) ``` ```java // Java mAndroidTestingRocksTextView = (TextView) findViewById(R.id.text_view_rocks); ``` -------------------------------- ### Common Strings in Android Flavor Example Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/api-reference/flavor-classes.md Defines common string resources used across all flavors. These can be overridden by flavor-specific resources. ```xml Android Testing Blueprint Android Testing Rocks! ``` -------------------------------- ### Conditional Compilation Example (Commented) Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/api-reference/flavor-classes.md Shows a commented-out Kotlin code example that would cause a compilation error if a flavor-specific class is not available. This pattern is typically avoided. ```kotlin // This would cause compilation error if Flavor1Class unavailable // Typically avoided; instead use reflection or conditional compilation ``` -------------------------------- ### Build Project with Gradle Wrapper Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/QUICK_START.md Build the Android Testing Templates project using the Gradle wrapper. This command ensures the project is built correctly without requiring a separate Gradle installation. ```bash # Using Gradle wrapper (no Gradle installation needed) ./gradlew build ``` -------------------------------- ### Kotlin onClick Example Usage Source: https://github.com/googlesamples/android-testing-templates/blob/master/_autodocs/api-reference/main-activity.md Illustrates how the onClick method is typically invoked via layout XML binding in Kotlin. The actual logic within onClick checks module readiness before updating UI text. ```kotlin // Layout XML binding (typically in activity_hello_testing_blueprint.xml) //