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