This repository was archived by the owner on Jan 10, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathBottomNavigationTest.kt
More file actions
149 lines (112 loc) · 4.29 KB
/
Copy pathBottomNavigationTest.kt
File metadata and controls
149 lines (112 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright 2019, 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
*
* http://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.android.navigationadvancedsample
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.Espresso.pressBack
import androidx.test.espresso.NoActivityResumedException
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withContentDescription
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.rule.ActivityTestRule
import org.hamcrest.CoreMatchers.allOf
import org.junit.Assert.fail
import org.junit.Rule
import org.junit.Test
class BottomNavigationTest {
@get:Rule var instantTaskExecutorRule = InstantTaskExecutorRule()
@get:Rule var activityTestRule = ActivityTestRule(MainActivity::class.java)
@Test
fun bottomNavView_clickOnAllItems() {
// All screens open at their first destinations
assertFirstScreen()
openThirdScreen()
assertThirdScreen()
openSecondScreen()
assertSecondScreen()
openFirstScreen()
assertFirstScreen()
}
@Test
fun bottomNavView_backGoesToFirstItem() {
// From the 2nd or 3rd screens, back takes you to the 1st.
openThirdScreen()
pressBack()
assertFirstScreen()
}
@Test(expected = NoActivityResumedException::class)
fun bottomNavView_backfromFirstItemExits() {
// From the first screen, back finishes the activity
assertFirstScreen()
pressBack() // This should throw NoActivityResumedException
fail() // If it doesn't throw
}
@Test
fun bottomNavView_backstackMaintained() {
// The back stack of any screen is maintained when returning to it
openThirdScreen()
onView(withContentDescription(R.string.sign_up))
.perform(click())
assertDeeperThirdScreen()
openFirstScreen()
// Return to 3rd
openThirdScreen()
// Assert it maintained the back stack
assertDeeperThirdScreen()
}
@Test
fun bottomNavView_registerBackRegister() {
openThirdScreen()
pressBack() // This is handled in a especial way in code.
openThirdScreen()
onView(withContentDescription(R.string.sign_up))
.perform(click())
// Assert it maintained the back stack
assertDeeperThirdScreen()
}
private fun assertSecondScreen() {
onView(allOf(withText(R.string.title_list), isDescendantOfA(withId(R.id.action_bar))))
.check(matches(isDisplayed()))
}
private fun openSecondScreen() {
onView(allOf(withContentDescription(R.string.title_list), isDisplayed()))
.perform(click())
}
private fun assertDeeperThirdScreen() {
onView(withText(R.string.done))
.check(matches(isDisplayed()))
}
private fun openFirstScreen() {
onView(allOf(withContentDescription(R.string.title_home), isDisplayed()))
.perform(click())
}
private fun assertFirstScreen() {
onView(withText(R.string.welcome))
.check(matches(isDisplayed()))
}
private fun openThirdScreen() {
onView(allOf(withContentDescription(R.string.title_register), isDisplayed()))
.perform(click())
}
private fun assertThirdScreen() {
onView(withText(R.string.select_an_avatar))
.check(matches(isDisplayed()))
}
}