sync
[platform/framework/web/wrt-commons.git] / tests / dbus / test_cases.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    TestCases.cpp
18  * @author  Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for test cases for DBus internal tests.
21  */
22
23 #include <string>
24 #include <dpl/test/test_runner.h>
25 #include <dpl/event/event_listener.h>
26 #include <dpl/dbus/exception.h>
27 #include <dpl/dbus/connection.h>
28 #include <dpl/dbus/interface.h>
29 #include "dbus_test.h"
30
31 namespace {
32 const std::string dbusServiceName = "org.freedesktop.DBus";
33 const std::string dbusObjectPath = "/";
34 const std::string dbusInterfaceName = "org.freedesktop.DBus";
35 const std::string dbusMethodGetId = "GetId";
36
37 const std::string serviceName = "org.tizen.DBusTestService";
38 const std::string objectPath = "/org/tizen/DBusTestService";
39 const std::string interfaceName = "org.tizen.DBusTestService";
40 const std::string methodNameEcho = "echo";
41 const std::string methodNameQuit = "quit";
42 const std::string nodeInfo =
43     "<?xml version='1.0'?>"
44     "<node>"
45     "  <interface name='" + interfaceName + "'>"
46                                             "    <method name='" +
47     methodNameEcho + "'>"
48                      "      <arg type='s' name='challenge' direction='in'/>"
49                      "      <arg type='s' name='response' direction='out'/>"
50                      "    </method>"
51                      "    <method name='"
52     + methodNameQuit + "'>"
53                        "    </method>"
54                        "  </interface>"
55                        "</node>";
56
57 const std::string challenge = "Hello world!";
58
59 const int DEFAULT_TIMEOUT = 2; // in seconds
60 }
61
62 RUNNER_TEST_GROUP_INIT(DPL)
63
64 /*
65 Name: AcquireSessionBus
66 Description: tests acquiring session bus
67 Expected: no exceptions
68 */
69 RUNNER_TEST(AcquireSessionBus)
70 {
71     try {
72         DPL::DBus::Connection::sessionBus();
73     } catch (const DPL::DBus::Exception& ex) {
74         RUNNER_ASSERT_MSG(false, ex.DumpToString());
75     }
76 }
77
78 /*
79 Name: AcquireSystemBus
80 Description: tests acquiring system bus
81 Expected: no exceptions
82 */
83 RUNNER_TEST(AcquireSystemBus)
84 {
85     try {
86         DPL::DBus::Connection::systemBus();
87     } catch (const DPL::DBus::Exception& ex) {
88         RUNNER_ASSERT_MSG(false, ex.DumpToString());
89     }
90 }
91
92 /*
93 Name: ParseNodeInfo
94 Description: creates dbus interface from xml string
95 Expected: interface should be created correctly
96 */
97 RUNNER_TEST(ParseNodeInfo)
98 {
99     try {
100         auto ifaces = DPL::DBus::Interface::fromXMLString(nodeInfo);
101         RUNNER_ASSERT(!ifaces.empty());
102
103         auto iface = ifaces.at(0);
104         RUNNER_ASSERT(NULL != iface->getVTable());
105         RUNNER_ASSERT(NULL != iface->getInfo());
106     } catch (const DPL::DBus::Exception& ex) {
107         RUNNER_ASSERT_MSG(false, ex.DumpToString());
108     }
109 }
110
111 /*
112 Name: InvokeRemoteMethod
113 Description: performs procedure call via dbus
114 Expected: call should return not empty id
115 */
116 RUNNER_TEST(InvokeRemoteMethod)
117 {
118     try {
119         auto connection = DPL::DBus::Connection::systemBus();
120         auto freedesktop = connection->createObjectProxy(dbusServiceName,
121                                                          dbusObjectPath);
122         auto getId = freedesktop->createMethodProxy<std::string>
123                 (dbusInterfaceName, dbusMethodGetId);
124         RUNNER_ASSERT(!getId().empty());
125     } catch (const DPL::DBus::Exception& ex) {
126         RUNNER_ASSERT_MSG(false, ex.DumpToString());
127     }
128 }
129
130 class RegisterServiceListener :
131     public DPL::Event::EventListener<DPL::DBus::ConnectionEvents::
132                                          ServiceNameAcquiredEvent>
133 {
134   public:
135     void OnEventReceived(
136         const DPL::DBus::ConnectionEvents::ServiceNameAcquiredEvent& event)
137     {
138         DBusTest& test = DBusTestManager::getInstance().getCurrentTest();
139
140         auto name = event.GetArg0();
141         if (serviceName == name) {
142             test.success();
143         } else {
144             test.fail("Acquired service name: " + name);
145         }
146         test.quit();
147     }
148 };
149
150 /*
151 Name: RegisterService
152 Description: tests event listener for AcquiredEvent in context of dbus
153 Expected: event should be received
154 */
155 DBUS_TEST(RegisterService)
156 {
157     try {
158         RegisterServiceListener listener;
159
160         auto connection = DPL::DBus::Connection::sessionBus();
161         connection->DPL::Event::EventSupport<DPL::DBus::ConnectionEvents::
162                                                  ServiceNameAcquiredEvent>::
163             AddListener(&listener);
164         connection->registerService(serviceName);
165
166         DBusTestManager::getInstance().getCurrentTest().run(DEFAULT_TIMEOUT);
167     } catch (const DPL::DBus::Exception& ex) {
168         RUNNER_ASSERT_MSG(false, ex.DumpToString());
169     }
170 }
171
172 /**
173  * This test checks:
174  * - object registration (done on the wrt-dbus-test-service side)
175  * - service registration (done on the wrt-dbus-test-service side)
176  * - dispatching method calls (done on the wrt-dbus-test-service side)
177  * - launching dbus service on demand
178  * - invoking remote method(s)
179  */
180 DBUS_TEST(InvokeTestService)
181 {
182     try {
183         auto connection = DPL::DBus::Connection::sessionBus();
184         auto testService = connection->createObjectProxy(serviceName,
185                                                          objectPath);
186         auto echo = testService->createMethodProxy<std::string, std::string>
187                 (interfaceName, methodNameEcho);
188         auto response = echo(challenge);
189
190         testService->createMethodProxy<void>(interfaceName, methodNameQuit) ();
191
192         RUNNER_ASSERT_MSG(response == challenge,
193                           "[challenge = " << challenge <<
194                           ", response = " << response << "]");
195     } catch (const DPL::DBus::Exception& ex) {
196         RUNNER_ASSERT_MSG(false, ex.DumpToString());
197     }
198 }