From: SooChan Lim Date: Thu, 21 Jul 2022 23:45:13 +0000 (+0900) Subject: add test cases for ds_tizen_display_policy X-Git-Tag: accepted/tizen/unified/20220808.135412~10 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F29%2F278629%2F1;p=platform%2Fcore%2Fuifw%2Flibds-tizen.git add test cases for ds_tizen_display_policy Change-Id: I8a140320dbe5547b65c214f44095dae2bf0d1283 --- diff --git a/packaging/libds-tizen.spec b/packaging/libds-tizen.spec index 372f0c3..5ec3a14 100644 --- a/packaging/libds-tizen.spec +++ b/packaging/libds-tizen.spec @@ -354,3 +354,4 @@ ninja -C builddir install %{_includedir}/libds-tizen/display_policy.h %{_libdir}/pkgconfig/libds-tizen-display-policy.pc %{_libdir}/libds-tizen-display-policy.so +%{_bindir}/libds-tizen-display-policy-tests diff --git a/tests/meson.build b/tests/meson.build index 4b4686d..4d63f58 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -84,3 +84,23 @@ executable('libds-tizen-launch-appinfo-tests', install_dir: libds_tizen_bindir, install : true ) + +## display-policy tests +tc_display_policy_files = [ + 'tc_main.cpp', + 'tc_display_policy.cpp', +] + +executable('libds-tizen-display-policy-tests', + [ + tc_mock_files, + tc_display_policy_files + ], + dependencies: [ + deps_test_common, + deps_libds_tizen_display_policy, + dependency('libdrm', required: true), + ], + install_dir: libds_tizen_bindir, + install : true +) diff --git a/tests/tc_display_policy.cpp b/tests/tc_display_policy.cpp new file mode 100644 index 0000000..fe165dc --- /dev/null +++ b/tests/tc_display_policy.cpp @@ -0,0 +1,397 @@ +#include "tc_main.h" +#include "mockclient.h" +#include "mockcompositor.h" +#include +#include + +#define TIZEN_DISPLAY_POLICY_VERSION 1 + +class MockDisplayPolicyCompositor : public MockCompositor +{ +public: + MockDisplayPolicyCompositor() + : MockCompositor(&MockDisplayPolicyCompositor::TestSetup, this) + { + ds_inf("%s : this(%p)", __func__, this); + + // initialize the flags to check + bSurfaceDestroyed = false; + + bDestroyed = false; + bSetBrightness = false; + mBrightnessValue = -1; + bDestroyDisplayPolicyInfo = false; + } + + ~MockDisplayPolicyCompositor() + { + ds_inf("%s : this(%p)", __func__, this); + } + + static void TestSetup(void *data) + { + MockDisplayPolicyCompositor *mockComp = + static_cast(data); + Compositor *comp = mockComp->compositor; + + ds_inf("%s: mockComp(%p)", __func__, mockComp); + + // new surface listener + mockComp->mNewSurfaceListener.notify = + MockDisplayPolicyCompositor::NewSurfaceCallback; + mockComp->mNewSurfaceListener.parent = mockComp; + ds_compositor_add_new_surface_listener(comp->compositor, + &mockComp->mNewSurfaceListener); + + mockComp->mDisplayPolicy = + ds_tizen_display_policy_create(comp->display); + + // destroy listener + mockComp->mDestroyListener.notify = + MockDisplayPolicyCompositor::DestroyCallback; + mockComp->mDestroyListener.parent = mockComp; + ds_tizen_display_policy_add_destroy_listener(mockComp->mDisplayPolicy, + &mockComp->mDestroyListener); + + // set_display_policy listener + mockComp->mSetDisplayPolicyListener.notify = + MockDisplayPolicyCompositor::SetBrightnessCallback; + mockComp->mSetDisplayPolicyListener.parent = mockComp; + ds_tizen_display_policy_add_set_brightness_info_listener( + mockComp->mDisplayPolicy, + &mockComp->mSetDisplayPolicyListener); + } + + static void NewSurfaceCallback(struct wl_listener *listener, void *data) + { + MockDisplayPolicyCompositor *mockComp = + reinterpret_cast(listener)->parent; + struct ds_surface *surface = static_cast(data); + + ds_inf("%s: mockComp(%p), surface(%p)", __func__, mockComp, surface); + + mockComp->mSurface = surface; + + // del surface listener + mockComp->mDelSurfaceListener.notify = + MockDisplayPolicyCompositor::DelSurfaceCallback; + mockComp->mDelSurfaceListener.parent = mockComp; + ds_surface_add_destroy_listener(surface, + &mockComp->mDelSurfaceListener); + } + + static void DelSurfaceCallback(struct wl_listener *listener, void *data) + { + MockDisplayPolicyCompositor *mockComp = + reinterpret_cast(listener)->parent; + struct ds_surface *surface = static_cast(data); + + ds_inf("%s: mockComp(%p), surface(%p)", __func__, mockComp, surface); + + if (ds_surface_get_wl_resource(mockComp->mSurface) == + ds_surface_get_wl_resource(surface)) { + ds_inf("%s: surface is deleted.", __func__); + mockComp->bSurfaceDestroyed = true; + } + } + + static void DestroyCallback(struct wl_listener *listener, void *data) + { + ds_inf("%s", __func__); + + MockDisplayPolicyCompositor *mockComp = + reinterpret_cast(listener)->parent; + + mockComp->bDestroyed = true; + } + + static void SetBrightnessCallback(struct wl_listener *listener, + void *data) + { + ds_inf("%s", __func__); + + MockDisplayPolicyCompositor *mockComp = + reinterpret_cast(listener)->parent; + struct ds_tizen_display_policy_info *info = + static_cast(data); + + ds_inf("%s: mockComp(%p), info(%p)", __func__, mockComp, info); + + mockComp->bSetBrightness = true; + + mockComp->mInfo = info; + mockComp->mSurface = + ds_tizen_display_policy_info_get_surface(mockComp->mInfo); + mockComp->mBrightnessValue = + ds_tizen_display_policy_info_get_brightness_value(mockComp->mInfo); + + // info destroy listener + mockComp->mDisplayPolicyInfoDestroyListener.notify = + MockDisplayPolicyCompositor::DisplayPolicyInfoDestroyCallback; + mockComp->mDisplayPolicyInfoDestroyListener.parent = mockComp; + ds_tizen_display_policy_info_add_destroy_listener(mockComp->mInfo, + &mockComp->mDisplayPolicyInfoDestroyListener); + } + + static void DisplayPolicyInfoDestroyCallback(struct wl_listener *listener, + void *data) + { + ds_inf("%s", __func__); + + MockDisplayPolicyCompositor *mockComp = + reinterpret_cast(listener)->parent; + struct ds_tizen_display_policy_info *info = + static_cast(data); + + ds_inf("%s: mockComp(%p), info(%p)", __func__, mockComp, info); + + if (mockComp->mInfo == info) { + ds_inf("%s: info is deleted.", __func__); + mockComp->bDestroyDisplayPolicyInfo = true; + } + } + + void SendDisplayPolicyDone(int32_t error_state) + { + ds_inf("%s", __func__); + + ds_tizen_display_policy_info_send_brightness_done( + mInfo, DS_TIZEN_DISPLAY_POLICY_ERROR_STATE_PERMISSION_DENIED); + } + +public: + bool bSurfaceDestroyed; + bool bDestroyed; + bool bSetBrightness; + int32_t mBrightnessValue; + bool bDestroyDisplayPolicyInfo; + +private: + struct ds_tizen_display_policy_info *mInfo; + struct ds_surface *mSurface; + struct NewSurfaceListener : ::wl_listener { + MockDisplayPolicyCompositor *parent; + }; + NewSurfaceListener mNewSurfaceListener; + struct DelSurfaceListener : ::wl_listener { + MockDisplayPolicyCompositor *parent; + }; + DelSurfaceListener mDelSurfaceListener; + + struct ds_tizen_display_policy *mDisplayPolicy; + struct DestroyListener : ::wl_listener { + MockDisplayPolicyCompositor *parent; + }; + DestroyListener mDestroyListener; + struct SetDisplayPolicyListener : ::wl_listener { + MockDisplayPolicyCompositor *parent; + }; + SetDisplayPolicyListener mSetDisplayPolicyListener; + struct DisplayPolicyInfoDestroyListener : ::wl_listener { + MockDisplayPolicyCompositor *parent; + }; + DisplayPolicyInfoDestroyListener mDisplayPolicyInfoDestroyListener; +}; + +class MockDisplayPolicyClient : public MockClient +{ +public: + MockDisplayPolicyClient() + : bBrightnessDoneEvent(false), + mBrightnessValue(-1), + mErrorState(0), + compositor_res(nullptr), + tizen_display_policy(nullptr) + {} + MockDisplayPolicyClient(const struct wl_registry_listener *listener) + : MockClient(listener, this) + { + ds_inf("%s", __func__); + + bBrightnessDoneEvent = false; + mBrightnessValue = -1; + mErrorState = 0; + } + ~MockDisplayPolicyClient() + { + ds_inf("%s", __func__); + } + + void SetWlCompositor(struct wl_compositor *global_res) + { + ds_inf("%s", __func__); + + compositor_res = global_res; + } + + struct wl_compositor *GetWlCompositor() + { + ds_inf("%s", __func__); + + return compositor_res; + } + + void SetTizenDisplayPolicy(struct tizen_display_policy *global_res) + { + ds_inf("%s", __func__); + + tizen_display_policy = global_res; + } + + struct tizen_display_policy *GetTizenDisplayPolicy() + { + ds_inf("%s", __func__); + + return tizen_display_policy; + } + +public: + bool bBrightnessDoneEvent; + int32_t mBrightnessValue; + uint32_t mErrorState; + +private: + struct wl_compositor *compositor_res; + struct tizen_display_policy *tizen_display_policy; +}; + +static void +client_tizen_display_policy_cb_window_brightness_done(void *data, + struct tizen_display_policy *display_policy, struct wl_surface *surface, + int32_t brightness_value, uint32_t error_state) +{ + ds_inf("%s", __func__); + + MockDisplayPolicyClient *client = static_cast(data); + + client->bBrightnessDoneEvent = true; + client->mBrightnessValue = brightness_value; + client->mErrorState = error_state; +} + +static const struct tizen_display_policy_listener display_policy_cb_listener = { + .window_brightness_done = client_tizen_display_policy_cb_window_brightness_done, +}; + +static void +client_registry_cb_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version) +{ + ds_inf("%s", __func__); + + MockDisplayPolicyClient *client = static_cast(data); + struct wl_compositor *compositor_res; + struct tizen_display_policy *tizen_display_policy; + + if (!strcmp(interface, "wl_compositor")) { + compositor_res = (struct wl_compositor *)wl_registry_bind(registry, + name, &wl_compositor_interface, 1); + if (compositor_res == nullptr) { + ds_err("wl_registry_bind() failed. wl_compositor resource."); + return; + } + client->SetWlCompositor(compositor_res); + } else if (!strcmp(interface, "tizen_display_policy")) { + tizen_display_policy = (struct tizen_display_policy *)wl_registry_bind(registry, + name, &tizen_display_policy_interface, TIZEN_DISPLAY_POLICY_VERSION); + if (tizen_display_policy == nullptr) { + ds_err("wl_registry_bind() failed. tizen_display_policy resource."); + return; + } + client->SetTizenDisplayPolicy(tizen_display_policy); + + tizen_display_policy_add_listener(tizen_display_policy, + &display_policy_cb_listener, client); + } +} + +static void +client_registry_cb_global_remove(void *data, struct wl_registry *registry, + uint32_t name) +{ + ds_inf("%s", __func__); + + MockDisplayPolicyClient *client = static_cast(data); + struct wl_compositor *compositor_res = client->GetWlCompositor(); + struct tizen_display_policy *display_policy_res = client->GetTizenDisplayPolicy(); + + tizen_display_policy_destroy(display_policy_res); + wl_compositor_destroy(compositor_res); +} + +static const struct wl_registry_listener registry_listener = { + .global = client_registry_cb_global, + .global_remove = client_registry_cb_global_remove +}; + +class DisplayPolicyTest : public ::testing::Test +{ +public: + void SetUp(void) override; + void TearDown(void) override; + + MockDisplayPolicyCompositor *comp; + MockDisplayPolicyClient *client; + struct wl_compositor *compositor_res; + struct tizen_display_policy *display_policy_res; + struct wl_surface *surface_res; +}; + +void +DisplayPolicyTest::SetUp(void) +{ + //ds_log_init(DS_DBG, NULL); + + ds_inf("%s", __func__); + + comp = new MockDisplayPolicyCompositor(); + client = new MockDisplayPolicyClient(®istry_listener); + compositor_res = client->GetWlCompositor(); + display_policy_res = client->GetTizenDisplayPolicy(); + surface_res = wl_compositor_create_surface(compositor_res); + + client->RoundTrip(); +} + +void +DisplayPolicyTest::TearDown(void) +{ + ds_inf("%s", __func__); + + wl_surface_destroy(surface_res); + client->RoundTrip(); + EXPECT_TRUE(comp->bSurfaceDestroyed); + + delete client; + delete comp; +} + +TEST_F(DisplayPolicyTest, Create_P) +{ + EXPECT_TRUE(true); +} + +TEST_F(DisplayPolicyTest, Req_TizenDisplayPolicySetDisplayPolicy) +{ + tizen_display_policy_set_window_brightness(display_policy_res, surface_res, 10); + client->RoundTrip(); + EXPECT_TRUE(comp->bSetBrightness); + EXPECT_TRUE(comp->mBrightnessValue == 10); +} + +TEST_F(DisplayPolicyTest, Ev_TizenDisplayPolicyWindowDisplayPolicyDone) +{ + tizen_display_policy_set_window_brightness(display_policy_res, surface_res, 10); + client->RoundTrip(); + EXPECT_TRUE(comp->bSetBrightness); + EXPECT_TRUE(comp->mBrightnessValue == 10); + + comp->SendDisplayPolicyDone(DS_TIZEN_DISPLAY_POLICY_ERROR_STATE_PERMISSION_DENIED); + comp->Process(); + + client->RoundTrip(); + EXPECT_TRUE(client->bBrightnessDoneEvent); + EXPECT_TRUE(client->mBrightnessValue == 10); + EXPECT_TRUE(client->mErrorState == + DS_TIZEN_DISPLAY_POLICY_ERROR_STATE_PERMISSION_DENIED); +}