screen: add ds_tizen_screen
[platform/core/uifw/libds-tizen.git] / tests / mockclient.cpp
1 /**************************************************************************
2  *
3  * Copyright 2022 Samsung Electronics co., Ltd. All Rights Reserved.
4  *
5  * Contact: SooChan Lim <sc1.lim@samsung.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
23  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27 **************************************************************************/
28
29 #include "mockclient.h"
30 #include <iostream>
31 #include <gtest/gtest.h>
32
33 MockClient::MockClient()
34 {
35     display = wl_display_connect("test-ds-tizen");
36     if (!display)
37         ds_err("wl_display_connect() filed.");
38
39     registry = wl_display_get_registry(display);
40     if (!registry)
41         ds_err("wl_display_get_registry() filed.");
42 }
43
44 MockClient::MockClient(const struct wl_registry_listener *registry_listener, void *data)
45 {
46     int ret;
47     display = wl_display_connect("test-ds-tizen");
48     if (!display)
49         ds_err("wl_display_connect() filed.");
50
51     registry = wl_display_get_registry(display);
52     if (!registry)
53         ds_err("wl_display_get_registry() filed.");
54
55     ret = wl_registry_add_listener(registry, registry_listener, data);
56     if (ret)
57         ds_err("wl_registry_add_listener() filed.");
58
59     wl_display_roundtrip(display);
60 }
61
62 MockClient::~MockClient()
63 {
64     wl_registry_destroy(registry);
65     wl_display_disconnect(display);
66 }
67
68 void MockClient::RoundTrip()
69 {
70     wl_display_roundtrip(display);
71 }
72
73 void MockClient::ExpectNoError()
74 {
75     const struct wl_interface *interface;
76     uint32_t errcode;
77
78     this->RoundTrip();
79
80     int err = wl_display_get_error(this->display);
81
82     if (err != 0) {
83         errcode = wl_display_get_protocol_error(this->display, &interface,
84                 NULL);
85         FAIL() << "Expected no error, but got error(" << err << ") interface("
86             << interface->name << ") errcode(" << errcode << ")";
87     }
88 }
89
90 void MockClient::ExpectProtocolError(const struct wl_interface *intf,
91         uint32_t code)
92 {
93     this->RoundTrip();
94
95     int err = wl_display_get_error(this->display);
96
97     ASSERT_NE(err, 0) << "Expected protocol error but nothing came";
98     ASSERT_EQ(err, EPROTO)
99         << "Expected protocol error but got local error";
100
101     const struct wl_interface *interface;
102     uint32_t errcode = wl_display_get_protocol_error(this->display,
103             &interface, NULL);
104
105     ASSERT_EQ(errcode, code)
106         << "Should get error code " << code << " but got " << errcode;
107
108     ASSERT_STREQ(intf->name, interface->name)
109         << "Should get interface '" << intf->name << "' but got '"
110         << interface->name << "'";
111 }
112
113 struct wl_display * MockClient::GetWlDisplay()
114 {
115     return display;
116 }