2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
20 * @brief Implementation file for test cases for DBus internal tests.
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"
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";
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'?>"
45 " <interface name='" + interfaceName + "'>"
48 " <arg type='s' name='challenge' direction='in'/>"
49 " <arg type='s' name='response' direction='out'/>"
52 + methodNameQuit + "'>"
57 const std::string challenge = "Hello world!";
59 const int DEFAULT_TIMEOUT = 2; // in seconds
62 RUNNER_TEST_GROUP_INIT(DPL)
65 Name: AcquireSessionBus
66 Description: tests acquiring session bus
67 Expected: no exceptions
69 RUNNER_TEST(AcquireSessionBus)
72 DPL::DBus::Connection::sessionBus();
73 } catch (const DPL::DBus::Exception& ex) {
74 RUNNER_ASSERT_MSG(false, ex.DumpToString());
79 Name: AcquireSystemBus
80 Description: tests acquiring system bus
81 Expected: no exceptions
83 RUNNER_TEST(AcquireSystemBus)
86 DPL::DBus::Connection::systemBus();
87 } catch (const DPL::DBus::Exception& ex) {
88 RUNNER_ASSERT_MSG(false, ex.DumpToString());
94 Description: creates dbus interface from xml string
95 Expected: interface should be created correctly
97 RUNNER_TEST(ParseNodeInfo)
100 auto ifaces = DPL::DBus::Interface::fromXMLString(nodeInfo);
101 RUNNER_ASSERT(!ifaces.empty());
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());
112 Name: InvokeRemoteMethod
113 Description: performs procedure call via dbus
114 Expected: call should return not empty id
116 RUNNER_TEST(InvokeRemoteMethod)
119 auto connection = DPL::DBus::Connection::systemBus();
120 auto freedesktop = connection->createObjectProxy(dbusServiceName,
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());
130 class RegisterServiceListener :
131 public DPL::Event::EventListener<DPL::DBus::ConnectionEvents::
132 ServiceNameAcquiredEvent>
135 void OnEventReceived(
136 const DPL::DBus::ConnectionEvents::ServiceNameAcquiredEvent& event)
138 DBusTest& test = DBusTestManager::getInstance().getCurrentTest();
140 auto name = event.GetArg0();
141 if (serviceName == name) {
144 test.fail("Acquired service name: " + name);
151 Name: RegisterService
152 Description: tests event listener for AcquiredEvent in context of dbus
153 Expected: event should be received
155 DBUS_TEST(RegisterService)
158 RegisterServiceListener listener;
160 auto connection = DPL::DBus::Connection::sessionBus();
161 connection->DPL::Event::EventSupport<DPL::DBus::ConnectionEvents::
162 ServiceNameAcquiredEvent>::
163 AddListener(&listener);
164 connection->registerService(serviceName);
166 DBusTestManager::getInstance().getCurrentTest().run(DEFAULT_TIMEOUT);
167 } catch (const DPL::DBus::Exception& ex) {
168 RUNNER_ASSERT_MSG(false, ex.DumpToString());
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)
180 DBUS_TEST(InvokeTestService)
183 auto connection = DPL::DBus::Connection::sessionBus();
184 auto testService = connection->createObjectProxy(serviceName,
186 auto echo = testService->createMethodProxy<std::string, std::string>
187 (interfaceName, methodNameEcho);
188 auto response = echo(challenge);
190 testService->createMethodProxy<void>(interfaceName, methodNameQuit) ();
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());