Skip to content

Add test to run all samples #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ android {
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunner = "com.example.platform.app.AppTestRunner"
vectorDrawables {
useSupportLibrary = true
}
Expand Down Expand Up @@ -88,6 +88,18 @@ dependencies {
samples.forEach {
implementation(project(it))
}

// Testing
kaptAndroidTest(libs.hilt.compiler)
androidTestImplementation(platform(libs.compose.bom))
androidTestImplementation(libs.androidx.navigation.testing)
androidTestImplementation(libs.compose.ui.test.junit4)
androidTestImplementation(libs.androidx.test.core)
androidTestImplementation(libs.androidx.test.espresso.core)
androidTestImplementation(libs.androidx.test.rules)
androidTestImplementation(libs.androidx.test.runner)
androidTestImplementation(libs.hilt.testing)
androidTestImplementation(libs.junit4)
}

tasks.register("syncSamplesInfo", com.example.platform.plugin.SyncSamplesInfo::class.java) {
Expand Down
31 changes: 31 additions & 0 deletions app/src/androidTest/java/com/example/platform/app/AppTestRunner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.platform.app

import android.app.Application
import android.content.Context
import androidx.test.runner.AndroidJUnitRunner
import dagger.hilt.android.testing.HiltTestApplication

/**
* A custom runner to set up the instrumented application class for tests.
*/
class AppTestRunner : AndroidJUnitRunner() {
override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.platform.app

import android.os.Build
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.hasScrollAction
import androidx.compose.ui.test.hasScrollToIndexAction
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onFirst
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollToKey
import androidx.test.espresso.Espresso
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.Rule
import org.junit.Test

/**
* Tests all the samples are present and open.
*
* Note: consider changing the test to use the TestNavController to control navigation instead
*/
@HiltAndroidTest
class NavigationTest {

/**
* Manages the components' state and is used to perform injection on your test
*/
@get:Rule(order = 0)
val hiltRule = HiltAndroidRule(this)

/**
* Use the primary activity to initialize the app normally.
*/
@get:Rule(order = 2)
val composeTestRule = createAndroidComposeRule<MainActivity>()

@Test
fun testSamplesOpen() {
composeTestRule.apply {
val platformLabel = onNodeWithText(activity.getString(R.string.app_name))
val scrollNode = onAllNodes(hasScrollAction() and hasScrollToIndexAction()).onFirst()

// For each sample find it in the list, open it and go back
activity.catalogSamples.forEach {
// Skip disabled samples
if (Build.VERSION.SDK_INT >= it.minSDK) {
scrollNode.performScrollToKey(it.route)
onNode(hasText(it.name) and hasText(it.description)).performClick()

// Go back
Espresso.pressBack()
platformLabel.assertIsDisplayed()
}
}
}
}
}
18 changes: 18 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ ktlint = "0.48.1"
coroutines = "1.6.4"
play-services-location = "21.0.1"
work = "2.8.1"
junit4 = "4.13.2"
androidxEspresso = "3.5.1"
androidxTestCore = "1.5.0"
androidxTestExt = "1.1.5"
androidxTestRules = "1.5.0"
androidxTestRunner = "1.5.2"
androidxUiAutomator = "2.2.0"

[libraries]
accompanist-permissions = { module = "com.google.accompanist:accompanist-permissions", version.ref = "accompanist" }
Expand All @@ -44,8 +51,16 @@ androidx-fragment = "androidx.fragment:fragment-ktx:1.5.6"
androidx-activity-compose = "androidx.activity:activity-compose:1.7.0"
androidx-navigation-fragment = { module = "androidx.navigation:navigation-fragment", version.ref = "androidx-navigation" }
androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "androidx-navigation" }
androidx-navigation-testing = { module = "androidx.navigation:navigation-testing", version.ref = "androidx-navigation" }
androidx-lifecycle-viewmodel-compose = "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1"

androidx-test-core = { group = "androidx.test", name = "core", version.ref = "androidxTestCore" }
androidx-test-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "androidxEspresso" }
androidx-test-ext = { group = "androidx.test.ext", name = "junit-ktx", version.ref = "androidxTestExt" }
androidx-test-rules = { group = "androidx.test", name = "rules", version.ref = "androidxTestRules" }
androidx-test-runner = { group = "androidx.test", name = "runner", version.ref = "androidxTestRunner" }
androidx-test-uiautomator = { group = "androidx.test.uiautomator", name = "uiautomator", version.ref = "androidxUiAutomator" }

androidx-work-runtime-ktx = "androidx.work:work-runtime-ktx:2.8.1"

casa-base = { module = "com.google.android.catalog.framework:casa-base", version.ref = "casa" }
Expand All @@ -71,6 +86,7 @@ google-ksp-api = { module = "com.google.devtools.ksp:symbol-processing-api", ver

hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hilt" }
hilt-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hilt" }
hilt-testing = { group = "com.google.dagger", name = "hilt-android-testing", version.ref = "hilt" }

kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }
kotlin-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" }
Expand All @@ -88,6 +104,8 @@ play-services-location = { module = "com.google.android.gms:play-services-locati
work = { module = "androidx.work:work-runtime", version.ref = "work"}
work-ktx = { module = "androidx.work:work-runtime-ktx", version.ref = "work"}

junit4 = { group = "junit", name = "junit", version.ref = "junit4" }

[plugins]
affectedmoduledetector = { id = "com.dropbox.affectedmoduledetector", version = "0.2.0" }
versionCatalogUpdate = { id = "nl.littlerobots.version-catalog-update", version = "0.7.0" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fun FindDevicesSample() {
val context = LocalContext.current
val bluetoothManager = context.getSystemService<BluetoothManager>()

if (bluetoothManager == null) {
if (bluetoothManager == null || bluetoothManager.adapter == null) {
Text(text = "Sample not supported in this device. Missing the Bluetooth Manager")
} else {
FindBLEDevicesScreen(FindDeviceController(bluetoothManager.adapter))
Expand Down