diff --git a/generator/templates/art/methods.cpp b/generator/templates/art/methods.cpp index 1029be9767..de9919d81c 100644 --- a/generator/templates/art/methods.cpp +++ b/generator/templates/art/methods.cpp @@ -42,7 +42,9 @@ namespace dawn::kotlin_api { 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 0000000000..7466873702 --- /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) + } + } + } + } +}