Skip to content

Commit

Permalink
Kotlin: throw exception rather than returning null from getMappedRange()
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Alex Benton <[email protected]>
  • Loading branch information
jimblacklercorp authored and Dawn LUCI CQ committed Dec 6, 2024
1 parent de761f3 commit 6261813
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
4 changes: 3 additions & 1 deletion generator/templates/art/methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}
}

0 comments on commit 6261813

Please sign in to comment.