Skip to content

Commit edf5410

Browse files
authored
Merge 78179a9 into 392c6c2
2 parents 392c6c2 + 78179a9 commit edf5410

3 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎navigation/navigation-common/src/androidTest/java/androidx/navigation/NavTypeTest.kt‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package androidx.navigation
1919
import android.content.pm.ActivityInfo
2020
import android.graphics.Bitmap
2121
import android.os.Bundle
22+
import androidx.navigation.common.test.R
2223
import androidx.test.filters.SmallTest
2324
import com.google.common.truth.Truth.assertThat
2425
import org.junit.Test
@@ -42,6 +43,8 @@ class NavTypeTest {
4243
private val booleans = booleanArrayOf(b, false)
4344
private val s = "a_string"
4445
private val strings = arrayOf("aa", "bb")
46+
private val reference = R.id.nav_id_reference
47+
private val referenceHex = "0x" + R.id.nav_id_reference.toString(16)
4548
private val parcelable = ActivityInfo()
4649
private val parcelables = arrayOf(parcelable)
4750
private val en = Bitmap.Config.ALPHA_8
@@ -78,6 +81,8 @@ class NavTypeTest {
7881
.isEqualTo(NavType.StringType)
7982
assertThat(NavType.fromArgType("string[]", null))
8083
.isEqualTo(NavType.StringArrayType)
84+
assertThat(NavType.fromArgType("reference", null))
85+
.isEqualTo(NavType.ReferenceType)
8186
assertThat(NavType.fromArgType("android.content.pm.ActivityInfo", null))
8287
.isEqualTo(parcelableNavType)
8388
assertThat(NavType.fromArgType("android.content.pm.ActivityInfo[]", null))
@@ -98,6 +103,8 @@ class NavTypeTest {
98103
.isEqualTo(NavType.StringType)
99104
assertThat(NavType.inferFromValue("123"))
100105
.isEqualTo(NavType.IntType)
106+
assertThat(NavType.inferFromValue("0xFF"))
107+
.isEqualTo(NavType.IntType)
101108
assertThat(NavType.inferFromValue("123L"))
102109
.isEqualTo(NavType.LongType)
103110
assertThat(NavType.inferFromValue("1.5"))
@@ -196,6 +203,11 @@ class NavTypeTest {
196203
.isEqualTo(strings)
197204
bundle.clear()
198205

206+
NavType.ReferenceType.put(bundle, key, reference)
207+
assertThat(NavType.ReferenceType.get(bundle, key))
208+
.isEqualTo(reference)
209+
bundle.clear()
210+
199211
parcelableNavType.put(bundle, key, parcelable)
200212
assertThat(parcelableNavType.get(bundle, key))
201213
.isEqualTo(parcelable)
@@ -221,4 +233,13 @@ class NavTypeTest {
221233
.isEqualTo(serializables)
222234
bundle.clear()
223235
}
236+
237+
@Test
238+
fun parseValueWithHex() {
239+
assertThat(NavType.IntType.parseValue(referenceHex))
240+
.isEqualTo(reference)
241+
242+
assertThat(NavType.ReferenceType.parseValue(referenceHex))
243+
.isEqualTo(reference)
244+
}
224245
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
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+
http://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+
<resources>
17+
<item type="id" name="nav_id_reference" />
18+
</resources>

‎navigation/navigation-common/src/main/java/androidx/navigation/NavType.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,11 @@ public Integer get(@NonNull Bundle bundle, @NonNull String key) {
317317
@NonNull
318318
@Override
319319
public Integer parseValue(@NonNull String value) {
320-
throw new UnsupportedOperationException(
321-
"References don't support parsing string values.");
320+
if (value.startsWith("0x")) {
321+
return Integer.parseInt(value.substring(2), 16);
322+
} else {
323+
return Integer.parseInt(value);
324+
}
322325
}
323326

324327
@NonNull

0 commit comments

Comments
 (0)