Tizen 2.0 Release
[framework/web/wrt-commons.git] / tests / dpl / dbus / dbus_test.h
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    dbus_test.h
18  * @author  Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  * @brief   Header file for DBusTest and DBusTestManager.
20  */
21
22 #ifndef WRT_TESTS_DBUS_TESTS_DBUS_TEST_H
23 #define WRT_TESTS_DBUS_TESTS_DBUS_TEST_H
24
25 #include <string>
26 #include <dpl/event/controller.h>
27 #include <dpl/generic_event.h>
28
29 DECLARE_GENERIC_EVENT_0(QuitEvent)
30 DECLARE_GENERIC_EVENT_0(TimeoutEvent)
31
32 class DBusTest :
33     private DPL::Event::Controller<DPL::TypeListDecl<QuitEvent, TimeoutEvent>::Type>
34 {
35 public:
36     enum class Status
37     {
38         NONE,
39         SUCCESS,
40         FAILED
41     };
42
43     explicit DBusTest(const std::string& name);
44
45     void run(unsigned int timeout);
46     void quit();
47
48     void setStatus(Status status);
49     void setMessage(const std::string& message);
50
51     void success();
52     void fail(const std::string& message = std::string());
53
54 private:
55     void OnEventReceived(const TimeoutEvent& event);
56     void OnEventReceived(const QuitEvent& event);
57
58     std::string m_name;
59     Status m_status;
60     std::string m_message;
61 };
62
63 class DBusTestManager : private DPL::Noncopyable
64 {
65 public:
66     static DBusTestManager& getInstance();
67
68     DBusTest& getCurrentTest() const;
69     void setCurrentTest(DBusTest& test);
70
71     void clear();
72
73 private:
74     DBusTestManager();
75
76     DBusTest* m_test;
77 };
78
79 #define DBUS_TEST(TestProc)                                                    \
80     void DBus##TestProc();                                                     \
81     RUNNER_TEST(TestProc)                                                      \
82     {                                                                          \
83         DBusTest test(#TestProc);                                              \
84         DBusTestManager::getInstance().setCurrentTest(test);                   \
85         DBus##TestProc();                                                      \
86         DBusTestManager::getInstance().clear();                                \
87     }                                                                          \
88     void DBus##TestProc()
89
90 #endif