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