Skip to content

Commit

Permalink
Add IDL tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian-Nordic committed Jun 7, 2022
1 parent c72a670 commit ca15f4e
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
3 changes: 3 additions & 0 deletions scripts/idl/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ pw_python_package("idl") {
"tests/available_tests.yaml",
"tests/inputs/cluster_struct_attribute.matter",
"tests/inputs/global_struct_attribute.matter",
"tests/inputs/optional_argument.matter",
"tests/inputs/several_clusters.matter",
"tests/inputs/simple_attribute.matter",
"tests/outputs/cluster_struct_attribute/jni/DemoClusterClient-ReadImpl.cpp",
"tests/outputs/cluster_struct_attribute/jni/DemoClusterClient-InvokeSubscribeImpl.cpp",
"tests/outputs/global_struct_attribute/jni/DemoClusterClient-ReadImpl.cpp",
"tests/outputs/global_struct_attribute/jni/DemoClusterClient-InvokeSubscribeImpl.cpp",
"tests/outputs/optional_argument/jni/MyClusterClient-ReadImpl.cpp",
"tests/outputs/optional_argument/jni/MyClusterClient-InvokeSubscribeImpl.cpp",
"tests/outputs/several_clusters/jni/FirstClient-ReadImpl.cpp",
"tests/outputs/several_clusters/jni/SecondClient-ReadImpl.cpp",
"tests/outputs/several_clusters/jni/ThirdClient-ReadImpl.cpp",
Expand Down
1 change: 1 addition & 0 deletions scripts/idl/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def write_new_data(self, relative_path: str, content: str):
# to write any new data

# This will display actual diffs in the output files
self.checker.maxDiff = 10000
self.checker.assertEqual(
self.get_existing_data(relative_path), content)

Expand Down
4 changes: 4 additions & 0 deletions scripts/idl/tests/available_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ java:
jni/FirstClient-InvokeSubscribeImpl.cpp: outputs/several_clusters/jni/FirstClient-InvokeSubscribeImpl.cpp
jni/SecondClient-InvokeSubscribeImpl.cpp: outputs/several_clusters/jni/SecondClient-InvokeSubscribeImpl.cpp
jni/ThirdClient-InvokeSubscribeImpl.cpp: outputs/several_clusters/jni/ThirdClient-InvokeSubscribeImpl.cpp

inputs/optional_argument.matter:
jni/MyClusterClient-ReadImpl.cpp: outputs/optional_argument/jni/MyClusterClient-ReadImpl.cpp
jni/MyClusterClient-InvokeSubscribeImpl.cpp: outputs/optional_argument/jni/MyClusterClient-InvokeSubscribeImpl.cpp
7 changes: 7 additions & 0 deletions scripts/idl/tests/inputs/optional_argument.matter
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
client cluster MyCluster = 1 {
request struct FooRequest {
optional OCTET_STRING argument = 0;
}

command Foo(FooRequest): DefaultSuccess = 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <controller/java/zap-generated/CHIPCallbackTypes.h>
#include <controller/java/zap-generated/CHIPInvokeCallbacks.h>
#include <controller/java/zap-generated/CHIPReadCallbacks.h>

#include <app-common/zap-generated/cluster-objects.h>
#include <zap-generated/CHIPClientCallbacks.h>
#include <zap-generated/CHIPClusters.h>

#include <controller/java/AndroidCallbacks.h>
#include <controller/java/AndroidClusterExceptions.h>
#include <controller/java/CHIPDefaultCallbacks.h>
#include <jni.h>
#include <lib/support/CHIPListUtils.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>
#include <lib/support/Span.h>
#include <platform/PlatformManager.h>
#include <vector>

#define JNI_METHOD(RETURN, CLASS_NAME, METHOD_NAME) \
extern "C" JNIEXPORT RETURN JNICALL Java_chip_devicecontroller_ChipClusters_00024##CLASS_NAME##_##METHOD_NAME

using namespace chip;
using namespace chip::Controller;

JNI_METHOD(jlong, MyClusterCluster, initWithDevice)(JNIEnv * env, jobject self, jlong devicePtr, jint endpointId)
{
chip::DeviceLayer::StackLock lock;
MyClusterCluster * cppCluster = new MyClusterCluster();

cppCluster->Associate(reinterpret_cast<DeviceProxy *>(devicePtr), endpointId);
return reinterpret_cast<jlong>(cppCluster);
}

JNI_METHOD(void, MyClusterCluster,
foo)(JNIEnv * env, jobject self, jlong clusterPtr, jobject callback,jobject argument,jobject timedInvokeTimeoutMs)
{
chip::DeviceLayer::StackLock lock;
CHIP_ERROR err = CHIP_NO_ERROR;
MyClusterCluster * cppCluster;

ListFreer listFreer;
chip::app::Clusters::MyCluster::Commands::Foo::Type request;

std::vector<Platform::UniquePtr<JniByteArray>> cleanupByteArrays;
std::vector<Platform::UniquePtr<JniUtfString>> cleanupStrings;{
jobject optionalValue_0 = nullptr;
chip::JniReferences::GetInstance().GetOptionalValue(argument, optionalValue_0);
if (optionalValue_0) {
auto & definedValue_0 = request.argument.Emplace();
cleanupByteArrays.push_back(chip::Platform::MakeUnique<chip::JniByteArray>(env, static_cast<jbyteArray>(optionalValue_0)));
definedValue_0 = cleanupByteArrays.back()->byteSpan();
}
}


std::unique_ptr<CHIPDefaultSuccessCallback, void (*)(CHIPDefaultSuccessCallback *)> onSuccess(
Platform::New<CHIPDefaultSuccessCallback>(callback), Platform::Delete<CHIPDefaultSuccessCallback>);
std::unique_ptr<CHIPDefaultFailureCallback, void (*)(CHIPDefaultFailureCallback *)> onFailure(Platform::New<CHIPDefaultFailureCallback>(callback), Platform::Delete<CHIPDefaultFailureCallback>);
VerifyOrReturn(onSuccess.get() != nullptr, AndroidClusterExceptions::GetInstance().ReturnIllegalStateException(env, callback, "Error creating native callback", CHIP_ERROR_NO_MEMORY));
VerifyOrReturn(onFailure.get() != nullptr, AndroidClusterExceptions::GetInstance().ReturnIllegalStateException(env, callback, "Error creating native callback", CHIP_ERROR_NO_MEMORY));

cppCluster = reinterpret_cast<MyClusterCluster *>(clusterPtr);
VerifyOrReturn(cppCluster != nullptr, AndroidClusterExceptions::GetInstance().ReturnIllegalStateException(env, callback, "Error getting native cluster", CHIP_ERROR_INCORRECT_STATE));

auto successFn = chip::Callback::Callback<CHIPDefaultSuccessCallbackType>::FromCancelable(onSuccess->Cancel());
auto failureFn = chip::Callback::Callback<CHIPDefaultFailureCallbackType>::FromCancelable(onFailure->Cancel());

if (timedInvokeTimeoutMs == nullptr) {
err = cppCluster->InvokeCommand(request, onSuccess->mContext, successFn->mCall, failureFn->mCall);
} else {
err = cppCluster->InvokeCommand(request, onSuccess->mContext, successFn->mCall, failureFn->mCall, chip::JniReferences::GetInstance().IntegerToPrimitive(timedInvokeTimeoutMs));
}
VerifyOrReturn(err == CHIP_NO_ERROR, AndroidClusterExceptions::GetInstance().ReturnIllegalStateException(env, callback, "Error invoking command", CHIP_ERROR_INCORRECT_STATE));

onSuccess.release();
onFailure.release();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

#include <controller/java/zap-generated/CHIPReadCallbacks.h>

#include <app-common/zap-generated/cluster-objects.h>
#include <zap-generated/CHIPClusters.h>

#include <controller/java/AndroidClusterExceptions.h>
#include <controller/java/CHIPDefaultCallbacks.h>
#include <jni.h>
#include <lib/support/CodeUtils.h>
#include <platform/PlatformManager.h>

#define JNI_METHOD(RETURN, CLASS_NAME, METHOD_NAME) \
extern "C" JNIEXPORT RETURN JNICALL Java_chip_devicecontroller_ChipClusters_00024##CLASS_NAME##_##METHOD_NAME

0 comments on commit ca15f4e

Please sign in to comment.