b93805d650292d02d4fc1853ccb966c2d45394b1
[platform/core/api/context.git] / testsuite / src / shared.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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 #include "shared.h"
18
19 static GMainLoop* __mainloop = NULL;
20
21 void test::start_mainloop()
22 {
23         if (!__mainloop) {
24                 __mainloop = g_main_loop_new(NULL, FALSE);
25                 g_main_loop_run(__mainloop);
26         }
27 }
28
29 void test::stop_mainloop()
30 {
31         if (__mainloop) {
32                 g_main_loop_quit(__mainloop);
33                 g_main_loop_unref(__mainloop);
34                 __mainloop = NULL;
35         }
36 }
37
38 bool test::is_mainloop_running()
39 {
40         return (__mainloop != NULL);
41 }
42
43 int test::read_int()
44 {
45         return read_int("");
46 }
47
48 int test::read_int(const std::string& msg)
49 {
50         std::cout << msg;
51         std::string line;
52         std::getline(std::cin, line);
53
54         if (line.empty())
55                 return INT_MIN;
56
57         return atoi(line.c_str());
58 }
59
60 std::string test::read_str()
61 {
62         return read_str("");
63 }
64
65 std::string test::read_str(const std::string& msg)
66 {
67         std::cout << msg;
68         std::string line;
69         std::getline(std::cin, line);
70
71         return line;
72 }
73
74 void test::run_test_case(const char *tcName, bool (*tcFunc)(void))
75 {
76         IF_FAIL_VOID(tcName && tcFunc);
77
78         g_print(CYAN("%s") ": ", tcName);
79                 g_print("\n");
80
81         bool result = tcFunc();
82         if (result)
83                 g_print("\n>> " GREEN("PASS") "\n");
84 }