2ef9a1d6e4e8ecbcd2e4b295d7785435ce573a3c
[framework/web/wrt-commons.git] / tests / dpl / dbus / dbus_test.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    dbus_test.cpp
18  * @author  Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  * @brief   Implementation file for DBusTest and DBusTestManager.
20  */
21
22 #include <dpl/test/test_runner.h>
23 #include "loop_control.h"
24 #include "dbus_test.h"
25
26 DBusTest::DBusTest(const std::string& name)
27     : m_name(name),
28       m_status(Status::NONE)
29 {
30 }
31
32 void DBusTest::run(unsigned int timeout)
33 {
34     DPL::Event::ControllerEventHandler<TimeoutEvent>::Touch();
35     DPL::Event::ControllerEventHandler<QuitEvent>::Touch();
36
37     DPL::Event::ControllerEventHandler<TimeoutEvent>::PostTimedEvent(
38         TimeoutEvent(), timeout);
39
40     LoopControl::wrt_start_loop();
41
42     switch (m_status)
43     {
44     case Status::FAILED:
45         throw DPL::Test::TestRunner::TestFailed(m_name.c_str(),
46                                                 __FILE__,
47                                                 __LINE__,
48                                                 m_message);
49
50     case Status::SUCCESS:
51     case Status::NONE:
52     default:
53         break;
54     }
55 }
56
57 void DBusTest::quit()
58 {
59     DPL::Event::ControllerEventHandler<QuitEvent>::PostEvent(QuitEvent());
60 }
61
62 void DBusTest::setStatus(Status status)
63 {
64     m_status = status;
65 }
66
67 void DBusTest::setMessage(const std::string& message)
68 {
69     m_message = message;
70 }
71
72 void DBusTest::success()
73 {
74     m_status = Status::SUCCESS;
75 }
76
77 void DBusTest::fail(const std::string& message)
78 {
79     m_status = Status::FAILED;
80     m_message = message;
81 }
82
83 void DBusTest::OnEventReceived(const TimeoutEvent& /*event*/)
84 {
85     fail("Test timed out.");
86
87     // Saving one event dispatch since Quit and Timeout work on the same thread.
88     LoopControl::wrt_end_loop();
89 }
90
91 void DBusTest::OnEventReceived(const QuitEvent& /*event*/)
92 {
93     LoopControl::wrt_end_loop();
94 }
95
96 DBusTestManager& DBusTestManager::getInstance()
97 {
98     static DBusTestManager instance;
99     return instance;
100 }
101
102 DBusTestManager::DBusTestManager() : m_test(NULL) { }
103
104 DBusTest& DBusTestManager::getCurrentTest() const
105 {
106     Assert(NULL != m_test && "Test not set.");
107
108     return *m_test;
109 }
110
111 void DBusTestManager::setCurrentTest(DBusTest& test)
112 {
113     m_test = &test;
114 }
115
116 void DBusTestManager::clear()
117 {
118     m_test = NULL;
119 }