Skip to content
Prev Previous commit
Next Next commit
[Owl] Initial nav tests
Change-Id: I6fb4a08ee99b96438dc28d25b91abd32f9e0dbc6
  • Loading branch information
JoseAlcerreca committed Nov 5, 2020
commit 2cb58c28ca4a078601b04b2c576bf980967997d6
112 changes: 112 additions & 0 deletions Owl/app/src/androidTest/java/com/example/owl/ui/NavigationTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2020 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.owl.ui

import androidx.activity.ComponentActivity
import androidx.compose.runtime.Providers
import androidx.test.platform.app.InstrumentationRegistry
import androidx.ui.test.createAndroidComposeRule
import androidx.ui.test.onNodeWithLabel
import androidx.ui.test.onNodeWithSubstring
import androidx.ui.test.performClick
import com.example.owl.R
import com.example.owl.model.courses
import com.example.owl.ui.fakes.ProvideTestImageLoader
import com.example.owl.ui.utils.AmbientBackDispatcher
import org.junit.Rule
import org.junit.Test

/**
* Checks that the navigation flows in the app are correct.
*/
class NavigationTest {

@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
lateinit var activity: ComponentActivity

private fun startActivity(startDestination: String? = null) {
composeTestRule.activityRule.scenario.onActivity {
activity = it
}
composeTestRule.setContent {
Providers(AmbientBackDispatcher provides activity.onBackPressedDispatcher) {
ProvideTestImageLoader {
if (startDestination == null) {
NavGraph()
} else {
NavGraph(startDestination)
}
}
}
}
}

@Test
fun firstScreenIsOnboarding() {
// When the app is open
startActivity()
// The first screen should be the onboarding screen.
// Assert that the FAB label for the onboarding screen exists:
composeTestRule.onNodeWithLabel(getOnboardingFabLabel()).assertExists()
}

@Test
fun onboardingToCourses() {
// Given the app in the onboarding screen
startActivity()

// Navigate to the next screen by clicking on the FAB
val fabLabel = getOnboardingFabLabel()
composeTestRule.onNodeWithLabel(fabLabel).performClick()

// The first course should be shown
composeTestRule.onNodeWithSubstring(courses.first().name).assertExists()
}

@Test
fun coursesToDetail() {
// Given the app in the courses screen
startActivity(MainDestinations.Courses.route)

// Navigate to the first course
composeTestRule.onNodeWithSubstring(courses.first().name).performClick()

// Assert navigated to the course details
composeTestRule.onNodeWithSubstring(getCourseDesc().take(15)).assertExists()
}

@Test
fun coursesToDetailAndBack() {
coursesToDetail()
composeTestRule.runOnUiThread {
activity.onBackPressed()
}

// The first course should be shown
composeTestRule.onNodeWithSubstring(courses.first().name).assertExists()
}

private fun getOnboardingFabLabel(): String {
return InstrumentationRegistry.getInstrumentation().targetContext.resources
.getString(R.string.continue_to_courses)
}
private fun getCourseDesc(): String {
return InstrumentationRegistry.getInstrumentation().targetContext.resources
.getString(R.string.course_desc)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2020 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.owl.ui.fakes

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Providers
import coil.ImageLoader
import coil.annotation.ExperimentalCoilApi
import coil.bitmap.BitmapPool
import coil.decode.DataSource
import coil.memory.MemoryCache
import coil.request.DefaultRequestOptions
import coil.request.Disposable
import coil.request.ImageRequest
import coil.request.ImageResult
import coil.request.SuccessResult
import com.example.owl.ui.utils.AmbientImageLoader

@OptIn(ExperimentalCoilApi::class)
@Composable
fun ProvideTestImageLoader(content: @Composable () -> Unit) {

// From https://coil-kt.github.io/coil/image_loaders/
val loader = object : ImageLoader {
private val drawable = ColorDrawable(Color.BLACK)

private val disposable = object : Disposable {
override val isDisposed get() = true
override fun dispose() {}
override suspend fun await() {}
}

override val bitmapPool: BitmapPool = BitmapPool(0)

override val defaults: DefaultRequestOptions = DefaultRequestOptions()
override val memoryCache: MemoryCache
get() = TODO("Not yet implemented")

override fun enqueue(request: ImageRequest): Disposable {
// Always call onStart before onSuccess.
request.target?.onStart(drawable)
request.target?.onSuccess(drawable)
return disposable
}

override suspend fun execute(request: ImageRequest): ImageResult {
return SuccessResult(
drawable = drawable,
request = request,
metadata = ImageResult.Metadata(
memoryCacheKey = MemoryCache.Key(""),
isSampled = false,
dataSource = DataSource.MEMORY_CACHE,
isPlaceholderMemoryCacheKeyPresent = false
)
)
}

override fun shutdown() { }
}
Providers(AmbientImageLoader provides loader, children = content)
}
1 change: 1 addition & 0 deletions Owl/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="androidx.activity.ComponentActivity" />
</application>

</manifest>
37 changes: 37 additions & 0 deletions Owl/app/src/main/java/com/example/owl/ui/NavGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@
package com.example.owl.ui

import android.os.Bundle
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.navArgument
import androidx.navigation.compose.navigate
import androidx.navigation.compose.rememberNavController
import com.example.owl.ui.course.CourseDetails
import com.example.owl.ui.courses.Courses
import com.example.owl.ui.onboarding.Onboarding

open class Destination(open val route: String)
open class DestinationSingleArg(
Expand Down Expand Up @@ -57,3 +65,32 @@ class MainActions(navController: NavHostController) {
navController.popBackStack()
}
}

@Composable
fun NavGraph(startDestination: String = MainDestinations.Onboarding.route) {
val navController = rememberNavController()

val actions = remember(navController) { MainActions(navController) }
NavHost(
navController = navController,
startDestination = startDestination
) {
composable(MainDestinations.Onboarding.route) {
Onboarding(onboardingComplete = actions.onboardingComplete)
}
composable(MainDestinations.Courses.route) {
Courses(selectCourse = actions.selectCourse)
}
composable(
MainDestinations.CourseDetail.route,
arguments = MainDestinations.CourseDetail.args
) { backStackEntry ->
val arguments = requireNotNull(backStackEntry.arguments)
CourseDetails(
courseId = MainDestinations.CourseDetail.getArgFromBundle(arguments),
selectCourse = actions.selectCourse,
upPress = actions.upPress
)
}
}
}
33 changes: 1 addition & 32 deletions Owl/app/src/main/java/com/example/owl/ui/OwlApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ package com.example.owl.ui
import androidx.activity.OnBackPressedDispatcher
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Providers
import androidx.compose.runtime.remember
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.owl.ui.course.CourseDetails
import com.example.owl.ui.courses.Courses
import com.example.owl.ui.onboarding.Onboarding
import com.example.owl.ui.utils.AmbientBackDispatcher
import com.example.owl.ui.utils.ProvideDisplayInsets
import com.example.owl.ui.utils.ProvideImageLoader
Expand All @@ -36,31 +29,7 @@ fun OwlApp(backDispatcher: OnBackPressedDispatcher) {
Providers(AmbientBackDispatcher provides backDispatcher) {
ProvideDisplayInsets {
ProvideImageLoader {
val navController = rememberNavController()

val actions = remember(navController) { MainActions(navController) }
NavHost(
navController = navController,
startDestination = MainDestinations.Onboarding.route
) {
composable(MainDestinations.Onboarding.route) {
Onboarding(onboardingComplete = actions.onboardingComplete)
}
composable(MainDestinations.Courses.route) {
Courses(selectCourse = actions.selectCourse)
}
composable(
MainDestinations.CourseDetail.route,
arguments = MainDestinations.CourseDetail.args
) { backStackEntry ->
val arguments = requireNotNull(backStackEntry.arguments)
CourseDetails(
courseId = MainDestinations.CourseDetail.getArgFromBundle(arguments),
selectCourse = actions.selectCourse,
upPress = actions.upPress
)
}
}
NavGraph()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ fun FeaturedCourse(
shape = MaterialTheme.shapes.medium
) {
ConstraintLayout(
modifier = Modifier.clickable(
onClick = { selectCourse(course.id) }
)
modifier = Modifier
.clickable(
onClick = { selectCourse(course.id) }
)
) {
val (image, avatar, subject, name, steps, icon) = createRefs()
NetworkImage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.drawLayer
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.semantics.accessibilityLabel
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.ui.tooling.preview.Preview
Expand All @@ -80,9 +82,14 @@ fun Onboarding(onboardingComplete: () -> Unit) {
topBar = { AppBar() },
backgroundColor = MaterialTheme.colors.primarySurface,
floatingActionButton = {
val fabLabel = stringResource(id = R.string.continue_to_courses)
FloatingActionButton(
onClick = onboardingComplete,
modifier = Modifier.navigationBarsPadding()
modifier = Modifier
.navigationBarsPadding()
.semantics {
accessibilityLabel = fabLabel
}
) {
Icon(Icons.Rounded.Explore)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fun NetworkImage(
)
}

private val AmbientImageLoader = staticAmbientOf<ImageLoader> {
val AmbientImageLoader = staticAmbientOf<ImageLoader> {
error("No loader provided")
}

Expand Down
1 change: 1 addition & 0 deletions Owl/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<!-- Onboarding -->
<string name="choose_topics_that_interest_you">Choose topics that interest you</string>
<string name="continue_to_courses">Continue to courses</string>

<!-- Courses -->
<string name="my_courses">My Courses</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ object Libs {
const val snapshot = ""
const val version = "1.0.0-alpha06"

const val runtime = "androidx.compose.runtime:runtime:$version"
const val animation = "androidx.compose.animation:animation:$version"
const val foundation = "androidx.compose.foundation:foundation:$version"
const val layout = "androidx.compose.foundation:foundation-layout:$version"
const val ui = "androidx.compose.ui:ui:$version"
const val iconsExtended = "androidx.compose.material:material-icons-extended:$version"
const val material = "androidx.compose.material:material:$version"
const val animation = "androidx.compose.animation:animation:$version"
const val runtime = "androidx.compose.runtime:runtime:$version"
const val tooling = "androidx.ui:ui-tooling:$version"
const val iconsExtended = "androidx.compose.material:material-icons-extended:$version"
const val ui = "androidx.compose.ui:ui:$version"
const val uiTest = "androidx.ui:ui-test:$version"
}

object Test {
Expand Down