dfb3c7e8084e03a2cee527d5dadba38aec61c48a
[platform/framework/web/wrt-commons.git] / tests / dpl / 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='" + methodNameEcho + "'>"
47         "      <arg type='s' name='challenge' direction='in'/>"
48         "      <arg type='s' name='response' direction='out'/>"
49         "    </method>"
50         "    <method name='" + methodNameQuit + "'>"
51         "    </method>"
52         "  </interface>"
53         "</node>";
54
55 const std::string challenge = "Hello world!";
56
57 const int DEFAULT_TIMEOUT = 2; // in seconds
58 }
59
60 RUNNER_TEST_GROUP_INIT(DPL)
61
62 RUNNER_TEST(AcquireSessionBus)
63 {
64     try
65     {
66         DPL::DBus::Connection::sessionBus();
67     }
68     catch (const DPL::DBus::Exception& ex)
69     {
70         RUNNER_ASSERT_MSG(false, ex.DumpToString());
71     }
72 }
73
74 RUNNER_TEST(AcquireSystemBus)
75 {
76     try
77     {
78         DPL::DBus::Connection::systemBus();
79     }
80     catch (const DPL::DBus::Exception& ex)
81     {
82         RUNNER_ASSERT_MSG(false, ex.DumpToString());
83     }
84 }
85
86 RUNNER_TEST(ParseNodeInfo)
87 {
88     try
89     {
90         auto ifaces = DPL::DBus::Interface::fromXMLString(nodeInfo);
91         RUNNER_ASSERT(!ifaces.empty());
92
93         auto iface = ifaces.at(0);
94         RUNNER_ASSERT(NULL != iface->getVTable());
95         RUNNER_ASSERT(NULL != iface->getInfo());
96     }
97     catch (const DPL::DBus::Exception& ex)
98     {
99         RUNNER_ASSERT_MSG(false, ex.DumpToString());
100     }
101 }
102
103 RUNNER_TEST(InvokeRemoteMethod)
104 {
105     try
106     {
107         auto connection = DPL::DBus::Connection::systemBus();
108         auto freedesktop = connection->createObjectProxy(dbusServiceName,
109                                                    dbusObjectPath);
110         auto getId = freedesktop->createMethodProxy<std::string>
111                 (dbusInterfaceName, dbusMethodGetId);
112         RUNNER_ASSERT(!getId().empty());
113     }
114     catch (const DPL::DBus::Exception& ex)
115     {
116         RUNNER_ASSERT_MSG(false, ex.DumpToString());
117     }
118 }
119
120 class RegisterServiceListener :
121     public DPL::Event::EventListener<DPL::DBus::ConnectionEvents::ServiceNameAcquiredEvent>
122 {
123 public:
124     void OnEventReceived(
125             const DPL::DBus::ConnectionEvents::ServiceNameAcquiredEvent& event)
126     {
127         DBusTest& test = DBusTestManager::getInstance().getCurrentTest();
128
129         auto name = event.GetArg0();
130         if (serviceName == name)
131         {
132             test.success();
133         }
134         else
135         {
136             test.fail("Acquired service name: " + name);
137         }
138         test.quit();
139     }
140 };
141
142 DBUS_TEST(RegisterService)
143 {
144     try
145     {
146         RegisterServiceListener listener;
147
148         auto connection = DPL::DBus::Connection::sessionBus();
149         connection->DPL::Event::EventSupport<DPL::DBus::ConnectionEvents::
150                 ServiceNameAcquiredEvent>::AddListener(&listener);
151         connection->registerService(serviceName);
152
153         DBusTestManager::getInstance().getCurrentTest().run(DEFAULT_TIMEOUT);
154     }
155     catch (const DPL::DBus::Exception& ex)
156     {
157         RUNNER_ASSERT_MSG(false, ex.DumpToString());
158     }
159 }
160
161 /**
162  * This test checks:
163  * - object registration (done on the wrt-dbus-test-service side)
164  * - service registration (done on the wrt-dbus-test-service side)
165  * - dispatching method calls (done on the wrt-dbus-test-service side)
166  * - launching dbus service on demand
167  * - invoking remote method(s)
168  */
169 DBUS_TEST(InvokeTestService)
170 {
171     try
172     {
173         auto connection = DPL::DBus::Connection::sessionBus();
174         auto testService = connection->createObjectProxy(serviceName,
175                                                          objectPath);
176         auto echo = testService->createMethodProxy<std::string, std::string>
177                 (interfaceName, methodNameEcho);
178         auto response = echo(challenge);
179
180         testService->createMethodProxy<void>(interfaceName, methodNameQuit)();
181
182         RUNNER_ASSERT_MSG(response == challenge,
183                           "[challenge = " << challenge <<
184                           ", response = " << response << "]");
185     }
186     catch (const DPL::DBus::Exception& ex)
187     {
188         RUNNER_ASSERT_MSG(false, ex.DumpToString());
189     }
190 }