Kotlin: throw exception rather than returning null from getMappedRange()
In API review it was pointed out that Kotlin APIs should not use null
returns when a request is invalid.
Bug: 381392576
Test: BufferTest.*
Change-Id: Iaf9b15b1083d3c73ee6e26047339b4b0435fc357
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/218594
Commit-Queue: Alex Benton <bentonian@google.com>
Reviewed-by: Alex Benton <bentonian@google.com>
diff --git a/generator/templates/art/methods.cpp b/generator/templates/art/methods.cpp
index 1029be9..de9919d 100644
--- a/generator/templates/art/methods.cpp
+++ b/generator/templates/art/methods.cpp
@@ -42,7 +42,9 @@
jobject toByteBuffer(JNIEnv *env, const void* address, jlong size) {
if (!address) {
- return nullptr;
+ //* TODO(b/344805524): custom exception for Dawn.
+ env->ThrowNew(env->FindClass("java/lang/Error"), "Invalid byte buffer.");
+ return nullptr;
}
jclass byteBufferClass = env->FindClass("java/nio/ByteBuffer");
diff --git a/tools/android/webgpu/src/androidTest/java/android/dawn/BufferTest.kt b/tools/android/webgpu/src/androidTest/java/android/dawn/BufferTest.kt
new file mode 100644
index 0000000..7466873
--- /dev/null
+++ b/tools/android/webgpu/src/androidTest/java/android/dawn/BufferTest.kt
@@ -0,0 +1,44 @@
+package android.dawn
+
+import org.junit.Assert.assertThrows
+import org.junit.Test
+
+class BufferTest {
+ @Test
+ /**
+ * Test that calling getMappedRange() on a mapped buffer does not raise an exception.
+ */
+ fun bufferMapTest() {
+ dawnTestLauncher() { device ->
+ device.createBuffer(
+ BufferDescriptor(
+ usage = BufferUsage.Vertex,
+ size = 1024,
+ mappedAtCreation = true
+ )
+ ).apply {
+ getMappedRange(size = size)
+ }
+ }
+ }
+
+ @Test
+ /**
+ * Test that calling getMappedRange() on a non-mapped buffer raises an exception.
+ */
+ fun bufferMapFailureTest() {
+ dawnTestLauncher() { device ->
+ assertThrows(Error::class.java) {
+ device.createBuffer(
+ BufferDescriptor(
+ usage = BufferUsage.Vertex,
+ size = 1024,
+ mappedAtCreation = false
+ )
+ ).apply {
+ getMappedRange(size = size)
+ }
+ }
+ }
+ }
+}