upload tizen1.0 source
[framework/web/wrt-commons.git] / tests / 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     default:
51         break;
52     }
53 }
54
55 void DBusTest::quit()
56 {
57     DPL::Event::ControllerEventHandler<QuitEvent>::PostEvent(QuitEvent());
58 }
59
60 void DBusTest::setStatus(Status status)
61 {
62     m_status = status;
63 }
64
65 void DBusTest::setMessage(const std::string& message)
66 {
67     m_message = message;
68 }
69
70 void DBusTest::success()
71 {
72     m_status = Status::SUCCESS;
73 }
74
75 void DBusTest::fail(const std::string& message)
76 {
77     m_status = Status::FAILED;
78     m_message = message;
79 }
80
81 void DBusTest::OnEventReceived(const TimeoutEvent& /*event*/)
82 {
83     fail("Test timed out.");
84
85     // Saving one event dispatch since Quit and Timeout work on the same thread.
86     LoopControl::wrt_end_loop();
87 }
88
89 void DBusTest::OnEventReceived(const QuitEvent& /*event*/)
90 {
91     LoopControl::wrt_end_loop();
92 }
93
94 DBusTestManager& DBusTestManager::getInstance()
95 {
96     static DBusTestManager instance;
97     return instance;
98 }
99
100 DBusTestManager::DBusTestManager() : m_test(NULL) { }
101
102 DBusTest& DBusTestManager::getCurrentTest() const
103 {
104     Assert(NULL != m_test && "Test not set.");
105
106     return *m_test;
107 }
108
109 void DBusTestManager::setCurrentTest(DBusTest& test)
110 {
111     m_test = &test;
112 }
113
114 void DBusTestManager::clear()
115 {
116     m_test = NULL;
117 }