From c5c277d1f548822620cd1dbbf038a41f0dc6f6aa Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Tue, 22 Mar 2022 14:37:45 +0900 Subject: [PATCH] Add core test case for setting device type Change-Id: Ic041a9ad82c8e5bcc909f4ddf775e55367c2cd6d Signed-off-by: hyunuk.tak --- tests/unittest/mocks/thread-mock-dbus.cpp | 6 ++++-- tests/unittest/mocks/thread-mock-dummy.cpp | 33 ++++++++++++++++++++++-------- tests/unittest/mocks/thread-mock-dummy.h | 3 ++- tests/unittest/thread-unittest-core.cpp | 24 ++++++++++++++++++++++ 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/tests/unittest/mocks/thread-mock-dbus.cpp b/tests/unittest/mocks/thread-mock-dbus.cpp index 06256b4..3342ffb 100644 --- a/tests/unittest/mocks/thread-mock-dbus.cpp +++ b/tests/unittest/mocks/thread-mock-dbus.cpp @@ -70,13 +70,15 @@ GVariant *g_dbus_proxy_call_sync(GDBusProxy *proxy, GCancellable *cancellable, GError **error) { - retv_if(proxy == NULL, NULL); + retv_if(proxy == nullptr, nullptr); if (proxy == GINT_TO_POINTER(0x1234)) return _handle_sync_method(method_name, parameters); if (proxy == GINT_TO_POINTER(0x5678)) - return _handle_get_property(parameters); + return _handle_property(method_name, parameters); + + return nullptr; } void g_object_unref(gpointer object) diff --git a/tests/unittest/mocks/thread-mock-dummy.cpp b/tests/unittest/mocks/thread-mock-dummy.cpp index b09a2a0..93d1c0e 100644 --- a/tests/unittest/mocks/thread-mock-dummy.cpp +++ b/tests/unittest/mocks/thread-mock-dummy.cpp @@ -35,7 +35,12 @@ static GVariant *__method_factoryreset(GVariant *parameters) static GVariant *__property_device_role() { - return g_variant_new("(v)", g_variant_new("s", "router")); + return g_variant_new("(v)", g_variant_new("s", "child")); +} + +static GVariant *__property_link_mode() +{ + return g_variant_new("(i)", THREAD_ERROR_NONE); } struct { @@ -80,14 +85,21 @@ struct { }; struct { + const gchar *method_name; const gchar *property_name; GVariant *(*handler)(void); } thread_gdbus_property_list[] = { { + "Get", THREAD_DBUS_PROPERTY_DEVICE_ROLE, __property_device_role, }, { + "Set", + THREAD_DBUS_PROPERTY_LINK_MODE, + __property_link_mode, + }, + { NULL, NULL, } @@ -104,9 +116,10 @@ static bool __is_same_method(int i, const char *method_name) return (g_strcmp0(thread_gdbus_method_list[i].method_name, method_name) == 0); } -static bool __is_same_property(int i, const char *property_name) +static bool __is_same_property(int i, const char *method_name, const char *property_name) { - return (g_strcmp0(thread_gdbus_property_list[i].property_name, property_name) == 0); + return (g_strcmp0(thread_gdbus_property_list[i].method_name, method_name) == 0 + && g_strcmp0(thread_gdbus_property_list[i].property_name, property_name) == 0); } guint _subscribe_signal(const char *interface_name, @@ -136,16 +149,20 @@ GVariant *_handle_sync_method(const gchar *method_name, } } -GVariant *_handle_get_property(GVariant *parameters) +GVariant *_handle_property(const gchar *method_name, + GVariant *parameters) { - const char *property_name; - g_variant_get(parameters, "(ss)", nullptr, &property_name); + const char *property_name = nullptr; + if (g_strcmp0(method_name, "Get") == 0) + g_variant_get(parameters, "(ss)", nullptr, &property_name); + if (g_strcmp0(method_name, "Set") == 0) + g_variant_get(parameters, "(ssv)", nullptr, &property_name, nullptr); for (int i = 0; ; ++i) { - if (thread_gdbus_property_list[i].property_name == NULL) + if (thread_gdbus_property_list[i].method_name == NULL) return nullptr; - if (__is_same_property(i, property_name)) + if (__is_same_property(i, method_name, property_name)) return thread_gdbus_property_list[i].handler(); } } \ No newline at end of file diff --git a/tests/unittest/mocks/thread-mock-dummy.h b/tests/unittest/mocks/thread-mock-dummy.h index 0f71083..491a53e 100644 --- a/tests/unittest/mocks/thread-mock-dummy.h +++ b/tests/unittest/mocks/thread-mock-dummy.h @@ -32,4 +32,5 @@ guint _subscribe_signal(const char *interface_name, GVariant *_handle_sync_method(const gchar *method_name, GVariant *parameters); -GVariant *_handle_get_property(GVariant *parameters); \ No newline at end of file +GVariant *_handle_property(const gchar *method_name, + GVariant *parameters); \ No newline at end of file diff --git a/tests/unittest/thread-unittest-core.cpp b/tests/unittest/thread-unittest-core.cpp index 4665dec..4c438f6 100644 --- a/tests/unittest/thread-unittest-core.cpp +++ b/tests/unittest/thread-unittest-core.cpp @@ -175,4 +175,28 @@ TEST_F(ThreadCoreTest, SetDeviceRoleChangedCbErrorNone) EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance)); EXPECT_EQ(THREAD_ERROR_NONE, thread_set_device_role_changed_cb(instance, deviceRoleCb, nullptr)); +} + +TEST_F(ThreadCoreTest, SetDeviceTypeNotInitialized) +{ + EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize()); + EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED, + thread_set_device_type(instance, THREAD_DEVICE_TYPE_ROUTER)); +} + +TEST_F(ThreadCoreTest, SetDeviceTypeInvalidParameter) +{ + EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER, + thread_set_device_type(instance, THREAD_DEVICE_TYPE_ROUTER)); +} + +TEST_F(ThreadCoreTest, SetDeviceTypeErrorNone) +{ + EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance)); + EXPECT_EQ(THREAD_ERROR_NONE, + thread_set_device_type(instance, THREAD_DEVICE_TYPE_MINIMAL_END_DEVICE)); + EXPECT_EQ(THREAD_ERROR_NONE, + thread_set_device_type(instance, THREAD_DEVICE_TYPE_SLEEPY_END_DEVICE)); + EXPECT_EQ(THREAD_ERROR_NONE, + thread_set_device_type(instance, THREAD_DEVICE_TYPE_ROUTER)); } \ No newline at end of file -- 2.7.4