Repository navigation
Expand file tree
/
Copy pathNavGraphAndroidTest.kt
More file actions
363 lines (323 loc) · 13.5 KB
/
Copy pathNavGraphAndroidTest.kt
File metadata and controls
363 lines (323 loc) · 13.5 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* 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 androidx.navigation
import android.net.Uri
import androidx.navigation.test.nullableStringArgument
import androidx.test.filters.SmallTest
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.Truth.assertWithMessage
import org.junit.Test
@SmallTest
class NavGraphAndroidTest {
companion object {
const val DESTINATION_ID = 1
const val DESTINATION_ROUTE = "destination_route"
const val DESTINATION_LABEL = "test_label"
const val GRAPH_ID = 2
const val GRAPH_ROUTE = "graph_route"
const val GRAPH_LABEL = "graph_label"
}
@Test
fun matchDeepLink() {
val navigatorProvider = NavigatorProvider().apply {
addNavigator(NavGraphNavigator(this))
}
val graph = navigatorProvider.getNavigator(NavGraphNavigator::class.java)
.createDestination()
val idArgument = NavArgument.Builder()
.setType(NavType.IntType)
.build()
graph.addArgument("id", idArgument)
graph.addDeepLink("www.example.com/users/{id}")
val match = graph.matchDeepLink(
Uri.parse("https://www.example.com/users/43")
)
assertWithMessage("Deep link should match")
.that(match)
.isNotNull()
assertWithMessage("Deep link should extract id argument correctly")
.that(match?.matchingArgs?.getInt("id"))
.isEqualTo(43)
}
@Test
fun matchDeepLinkBestMatchExact() {
val navigatorProvider = NavigatorProvider().apply {
addNavigator(NavGraphNavigator(this))
}
val graph = navigatorProvider.getNavigator(NavGraphNavigator::class.java)
.createDestination()
graph.addDeepLink("www.example.com/users/index.html")
graph.addArgument("id", nullableStringArgument(null))
graph.addDeepLink("www.example.com/users/{id}")
val match = graph.matchDeepLink(
Uri.parse("https://www.example.com/users/index.html")
)
assertWithMessage("Deep link should match")
.that(match)
.isNotNull()
assertWithMessage("Deep link should pick the exact match")
.that(match?.matchingArgs?.size())
.isEqualTo(0)
}
@Test
fun matchDotStar() {
val navigatorProvider = NavigatorProvider().apply {
addNavigator(NavGraphNavigator(this))
}
val graph = navigatorProvider.getNavigator(NavGraphNavigator::class.java)
.createDestination()
graph.addDeepLink("www.example.com/.*")
graph.addDeepLink("www.example.com/{name}")
val match = graph.matchDeepLink(Uri.parse("https://www.example.com/foo"))
assertWithMessage("Deep link should match")
.that(match)
.isNotNull()
assertWithMessage("Deep link should pick name over .*")
.that(match?.matchingArgs?.size())
.isEqualTo(1)
}
@Test
fun matchDeepLinkBestMatch() {
val navigatorProvider = NavigatorProvider().apply {
addNavigator(NavGraphNavigator(this))
}
val graph = navigatorProvider.getNavigator(NavGraphNavigator::class.java)
.createDestination()
val idArgument = NavArgument.Builder()
.setType(NavType.IntType)
.build()
graph.addArgument("id", idArgument)
graph.addDeepLink("www.example.com/users/{id}")
val postIdArgument = NavArgument.Builder()
.setType(NavType.IntType)
.build()
graph.addArgument("postId", postIdArgument)
graph.addDeepLink("www.example.com/users/{id}/posts/{postId}")
val match = graph.matchDeepLink(
Uri.parse("https://www.example.com/users/43/posts/99")
)
assertWithMessage("Deep link should match")
.that(match)
.isNotNull()
assertWithMessage("Deep link should pick the argument with more matching arguments")
.that(match?.matchingArgs?.size())
.isEqualTo(2)
assertWithMessage("Deep link should extract id argument correctly")
.that(match?.matchingArgs?.getInt("id"))
.isEqualTo(43)
assertWithMessage("Deep link should extract postId argument correctly")
.that(match?.matchingArgs?.getInt("postId"))
.isEqualTo(99)
}
@Test
fun matchDeepLinkBestMatchPathAndQuery() {
val navigatorProvider = NavigatorProvider().apply {
addNavigator(NavGraphNavigator(this))
}
val graph = navigatorProvider.getNavigator(NavGraphNavigator::class.java)
.createDestination()
graph.addArgument("code", nullableStringArgument(null))
graph.addDeepLink("www.example.com/users?code={code}")
graph.addArgument("id", nullableStringArgument(null))
graph.addDeepLink("www.example.com/users?id={id}")
val match = graph.matchDeepLink(
Uri.parse("https://www.example.com/users?id=1234")
)
assertWithMessage("Deep link should match")
.that(match)
.isNotNull()
assertWithMessage("Deep link should pick the argument with given values")
.that(match?.matchingArgs?.size())
.isEqualTo(1)
assertWithMessage("Deep link should extract id argument correctly")
.that(match?.matchingArgs?.getString("id"))
.isEqualTo("1234")
}
@Test
fun matchDeepLinkBestMatchChildren() {
val navigatorProvider = NavigatorProvider().apply {
addNavigator(NavGraphNavigator(this))
addNavigator(NoOpNavigator())
}
val graph = navigatorProvider.getNavigator(NavGraphNavigator::class.java)
.createDestination()
val userDestination = navigatorProvider.getNavigator(NoOpNavigator::class.java)
.createDestination()
userDestination.id = 1
val idArgument = NavArgument.Builder()
.setType(NavType.IntType)
.build()
userDestination.addArgument("id", idArgument)
userDestination.addDeepLink("www.example.com/users/{id}")
graph.addDestination(userDestination)
val postDestination = navigatorProvider.getNavigator(NoOpNavigator::class.java)
.createDestination()
postDestination.id = 2
val postIdArgument = NavArgument.Builder()
.setType(NavType.IntType)
.build()
postDestination.addArgument("id", idArgument)
postDestination.addArgument("postId", postIdArgument)
postDestination.addDeepLink("www.example.com/users/{id}/posts/{postId}")
graph.addDestination(postDestination)
val match = graph.matchDeepLink(
Uri.parse("https://www.example.com/users/43/posts/99")
)
assertWithMessage("Deep link should match")
.that(match)
.isNotNull()
assertWithMessage("Deep link should point to correct destination")
.that(match?.destination)
.isSameInstanceAs(postDestination)
assertWithMessage("Deep link should extract id argument correctly")
.that(match?.matchingArgs?.getInt("id"))
.isEqualTo(43)
assertWithMessage("Deep link should extract postId argument correctly")
.that(match?.matchingArgs?.getInt("postId"))
.isEqualTo(99)
}
@Test
fun toStringStartDestIdOnly() {
val navigatorProvider = NavigatorProvider().apply {
addNavigator(NavGraphNavigator(this))
addNavigator(NoOpNavigator())
}
val graph = navigatorProvider.getNavigator(NavGraphNavigator::class.java)
.createDestination().apply {
id = GRAPH_ID
label = GRAPH_LABEL
setStartDestination(DESTINATION_ID)
}
val expected = "NavGraph(0x${GRAPH_ID.toString(16)}) label=$GRAPH_LABEL " +
"startDestination=0x${DESTINATION_ID.toString(16)}"
assertThat(graph.toString()).isEqualTo(expected)
}
@Test
fun toStringStartDestRouteOnly() {
val navigatorProvider = NavigatorProvider().apply {
addNavigator(NavGraphNavigator(this))
addNavigator(NoOpNavigator())
}
val graph = navigatorProvider.getNavigator(NavGraphNavigator::class.java)
.createDestination().apply {
route = GRAPH_ROUTE
id = GRAPH_ID
label = GRAPH_LABEL
setStartDestination(DESTINATION_ROUTE)
}
val expected = "NavGraph(0x${GRAPH_ID.toString(16)}) route=$GRAPH_ROUTE " +
"label=$GRAPH_LABEL startDestination=$DESTINATION_ROUTE"
assertThat(graph.toString()).isEqualTo(expected)
}
@Test
fun toStringStartDestInNodes() {
val navigatorProvider = NavigatorProvider().apply {
addNavigator(NavGraphNavigator(this))
addNavigator(NoOpNavigator())
}
val destination = navigatorProvider.getNavigator(NoOpNavigator::class.java)
.createDestination().apply {
id = DESTINATION_ID
label = DESTINATION_LABEL
}
val graph = navigatorProvider.getNavigator(NavGraphNavigator::class.java)
.createDestination().apply {
id = GRAPH_ID
label = GRAPH_LABEL
setStartDestination(DESTINATION_ID)
addDestination(destination)
}
val expected = "NavGraph(0x${GRAPH_ID.toString(16)}) label=$GRAPH_LABEL " +
"startDestination={NavDestination(0x${DESTINATION_ID.toString(16)}) " +
"label=$DESTINATION_LABEL}"
assertThat(graph.toString()).isEqualTo(expected)
}
@Test
fun toStringStartDestInNodesRoute() {
val navigatorProvider = NavigatorProvider().apply {
addNavigator(NavGraphNavigator(this))
addNavigator(NoOpNavigator())
}
val destination = navigatorProvider.getNavigator(NoOpNavigator::class.java)
.createDestination().apply {
route = DESTINATION_ROUTE
id = DESTINATION_ID
label = DESTINATION_LABEL
}
val graph = navigatorProvider.getNavigator(NavGraphNavigator::class.java)
.createDestination().apply {
route = GRAPH_ROUTE
id = GRAPH_ID
label = GRAPH_LABEL
setStartDestination(DESTINATION_ROUTE)
setStartDestination(DESTINATION_ID)
addDestination(destination)
}
val expected = "NavGraph(0x${GRAPH_ID.toString(16)}) route=$GRAPH_ROUTE " +
"label=$GRAPH_LABEL " +
"startDestination={NavDestination(0x${DESTINATION_ID.toString(16)}) " +
"route=$DESTINATION_ROUTE label=$DESTINATION_LABEL}"
assertThat(graph.toString()).isEqualTo(expected)
}
@Test
fun toStringStartDestInNodesRouteWithStartDestID() {
val navigatorProvider = NavigatorProvider().apply {
addNavigator(NavGraphNavigator(this))
addNavigator(NoOpNavigator())
}
val destination = navigatorProvider.getNavigator(NoOpNavigator::class.java)
.createDestination().apply {
route = DESTINATION_ROUTE
label = DESTINATION_LABEL
}
val graph = navigatorProvider.getNavigator(NavGraphNavigator::class.java)
.createDestination().apply {
route = GRAPH_ROUTE
id = GRAPH_ID
label = GRAPH_LABEL
setStartDestination(DESTINATION_ROUTE)
setStartDestination(DESTINATION_ID)
addDestination(destination)
}
val expected = "NavGraph(0x${GRAPH_ID.toString(16)}) route=$GRAPH_ROUTE " +
"label=$GRAPH_LABEL startDestination=0x${DESTINATION_ID.toString(16)}"
assertThat(graph.toString()).isEqualTo(expected)
}
@Test
fun toStringStartDestInNodesRouteWithID() {
val navigatorProvider = NavigatorProvider().apply {
addNavigator(NavGraphNavigator(this))
addNavigator(NoOpNavigator())
}
val destination = navigatorProvider.getNavigator(NoOpNavigator::class.java)
.createDestination().apply {
route = DESTINATION_ROUTE
id = DESTINATION_ID
label = DESTINATION_LABEL
}
val graph = navigatorProvider.getNavigator(NavGraphNavigator::class.java)
.createDestination().apply {
route = GRAPH_ROUTE
id = GRAPH_ID
label = GRAPH_LABEL
setStartDestination(DESTINATION_ROUTE)
addDestination(destination)
}
val expected = "NavGraph(0x${GRAPH_ID.toString(16)}) route=$GRAPH_ROUTE " +
"label=$GRAPH_LABEL startDestination=$DESTINATION_ROUTE"
assertThat(graph.toString()).isEqualTo(expected)
}
}