Skip to content

Commit f1777b2

Browse files
authored
[Jetsnack] Add tests (android#450)
1 parent f7522fe commit f1777b2

File tree

4 files changed

+108
-6
lines changed

4 files changed

+108
-6
lines changed

Jetsnack/app/build.gradle

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ android {
7373
composeOptions {
7474
kotlinCompilerExtensionVersion Libs.AndroidX.Compose.version
7575
}
76+
77+
packagingOptions {
78+
// Multiple dependency bring these files in. Exclude them to enable
79+
// our test APK to build (has no effect on our AARs)
80+
excludes += "/META-INF/AL2.0"
81+
excludes += "/META-INF/LGPL2.1"
82+
}
7683
}
7784

7885
dependencies {
@@ -98,4 +105,12 @@ dependencies {
98105

99106
implementation Libs.Accompanist.coil
100107
implementation Libs.Accompanist.insets
108+
109+
androidTestImplementation Libs.JUnit.junit
110+
androidTestImplementation Libs.AndroidX.Test.runner
111+
androidTestImplementation Libs.AndroidX.Test.espressoCore
112+
androidTestImplementation Libs.AndroidX.Test.rules
113+
androidTestImplementation Libs.AndroidX.Test.Ext.junit
114+
androidTestImplementation Libs.Coroutines.test
115+
androidTestImplementation Libs.AndroidX.Compose.uiTest
101116
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2021 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+
* https://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.jetsnack
18+
19+
import androidx.activity.OnBackPressedDispatcher
20+
import androidx.compose.ui.test.assertIsDisplayed
21+
import androidx.compose.ui.test.junit4.createAndroidComposeRule
22+
import androidx.compose.ui.test.onNodeWithText
23+
import androidx.compose.ui.test.performClick
24+
import com.example.jetsnack.ui.JetsnackApp
25+
import com.example.jetsnack.ui.MainActivity
26+
import org.junit.Before
27+
import org.junit.Rule
28+
import org.junit.Test
29+
30+
class AppTest {
31+
32+
@get:Rule
33+
val composeTestRule = createAndroidComposeRule<MainActivity>()
34+
35+
@Before
36+
fun setUp() {
37+
composeTestRule.setContent {
38+
JetsnackApp(OnBackPressedDispatcher())
39+
}
40+
}
41+
42+
@Test
43+
fun app_launches() {
44+
// Check app launches at the correct destination
45+
composeTestRule.onNodeWithText("HOME").assertIsDisplayed()
46+
composeTestRule.onNodeWithText("Android's picks").assertIsDisplayed()
47+
}
48+
49+
@Test
50+
fun app_canNavigateToAllScreens() {
51+
// Check app launches at HOME
52+
composeTestRule.onNodeWithText("HOME").assertIsDisplayed()
53+
composeTestRule.onNodeWithText("Android's picks").assertIsDisplayed()
54+
55+
// Navigate to Search
56+
composeTestRule.onNodeWithText("SEARCH").performClick().assertIsDisplayed()
57+
composeTestRule.onNodeWithText("Categories").assertIsDisplayed()
58+
59+
// Navigate to Cart
60+
composeTestRule.onNodeWithText("MY CART").performClick().assertIsDisplayed()
61+
composeTestRule.onNodeWithText("Order (3 items)").assertIsDisplayed()
62+
63+
// Navigate to Profile
64+
composeTestRule.onNodeWithText("PROFILE").performClick().assertIsDisplayed()
65+
composeTestRule.onNodeWithText("Profile").assertIsDisplayed()
66+
}
67+
}

Jetsnack/app/src/main/java/com/example/jetsnack/ui/home/Home.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import com.example.jetsnack.ui.components.JetsnackSurface
7373
import com.example.jetsnack.ui.home.cart.Cart
7474
import com.example.jetsnack.ui.home.search.Search
7575
import com.example.jetsnack.ui.theme.JetsnackTheme
76+
import com.google.accompanist.insets.ProvideWindowInsets
7677
import com.google.accompanist.insets.navigationBarsPadding
7778

7879
@Composable
@@ -364,11 +365,13 @@ private enum class HomeSections(
364365
@Preview
365366
@Composable
366367
private fun JsetsnackBottomNavPreview() {
367-
JetsnackTheme {
368-
JetsnackBottomNav(
369-
currentSection = HomeSections.Feed,
370-
onSectionSelected = { },
371-
items = HomeSections.values().toList()
372-
)
368+
ProvideWindowInsets {
369+
JetsnackTheme {
370+
JetsnackBottomNav(
371+
currentSection = HomeSections.Feed,
372+
onSectionSelected = { },
373+
items = HomeSections.values().toList()
374+
)
375+
}
373376
}
374377
}

Jetsnack/buildSrc/src/main/java/com/example/jetsnack/buildsrc/Dependencies.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ object Libs {
5959
const val animation = "androidx.compose.animation:animation:${version}"
6060
const val tooling = "androidx.compose.ui:ui-tooling:${version}"
6161
const val iconsExtended = "androidx.compose.material:material-icons-extended:$version"
62+
const val uiTest = "androidx.compose.ui:ui-test-junit4:$version"
6263
}
6364

6465
object Activity {
@@ -74,5 +75,21 @@ object Libs {
7475
const val constraintLayoutCompose =
7576
"androidx.constraintlayout:constraintlayout-compose:1.0.0-alpha03"
7677
}
78+
79+
object Test {
80+
private const val version = "1.2.0"
81+
const val runner = "androidx.test:runner:$version"
82+
const val rules = "androidx.test:rules:$version"
83+
object Ext {
84+
private const val version = "1.1.2-rc01"
85+
const val junit = "androidx.test.ext:junit-ktx:$version"
86+
}
87+
const val espressoCore = "androidx.test.espresso:espresso-core:3.2.0"
88+
}
89+
}
90+
91+
object JUnit {
92+
private const val version = "4.13"
93+
const val junit = "junit:junit:$version"
7794
}
7895
}

0 commit comments

Comments
 (0)