Fix log fomatter according to boost version upgrade
[platform/upstream/csr-framework.git] / test / test-main.cpp
1 /*
2  *  Copyright (c) 2016 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       test-main.cpp
18  * @author     Kyungwook Tak(k.tak@samsung.com)
19  * @version    1.0
20  */
21 #define BOOST_TEST_MODULE CSR_API_TEST
22
23 #include <iostream>
24
25 #include <boost/test/unit_test.hpp>
26 #include <boost/test/unit_test_log.hpp>
27 #include <boost/test/results_reporter.hpp>
28 #include <colour_log_formatter.h>
29
30 #include <csr-engine-manager.h>
31
32 #include "test-common.h"
33
34 namespace {
35
36 csr_state_e setEngineState(csr_engine_id_e id, csr_state_e state)
37 {
38         csr_engine_h handle;
39         auto ret = csr_get_current_engine(id, &handle);
40         if (ret == CSR_ERROR_ENGINE_NOT_EXIST) {
41                 BOOST_MESSAGE("engine not exist! engine id: " << static_cast<int>(id));
42
43                 return CSR_STATE_DISABLE;
44         } else if (ret != CSR_ERROR_NONE) {
45                 BOOST_MESSAGE(
46                         "Failed to csr_get_current_engine with ret: " <<
47                         Test::capi_ec_to_string(ret));
48
49                 return CSR_STATE_DISABLE;
50         }
51
52         csr_state_e current = CSR_STATE_DISABLE;
53         ret = csr_engine_get_state(handle, &current);
54         if (ret == CSR_ERROR_ENGINE_NOT_EXIST) {
55                 BOOST_MESSAGE("Engine not exist! engine id: " << static_cast<int>(id));
56                 return CSR_STATE_DISABLE;
57         } else if (ret != CSR_ERROR_NONE) {
58                 BOOST_MESSAGE(
59                         "Failed to csr_engine_get_state with ret: " <<
60                         Test::capi_ec_to_string(ret));
61
62                 return CSR_STATE_DISABLE;
63         }
64
65         if (current == state)
66                 return current;
67
68         ret = csr_engine_set_state(handle, state);
69         BOOST_WARN_MESSAGE(ret != CSR_ERROR_NONE,
70                 "Failed to csr_engine_set_state with ret: " <<
71                 Test::capi_ec_to_string(ret));
72
73         return current;
74 }
75
76 }
77
78 struct TestConfig {
79         TestConfig()
80         {
81                 boost::unit_test::unit_test_log.set_threshold_level(
82                         boost::unit_test::log_test_units);
83                 boost::unit_test::results_reporter::set_level(boost::unit_test::SHORT_REPORT);
84                 boost::unit_test::unit_test_log.set_formatter(new Csr::Test::colour_log_formatter);
85         }
86 };
87
88 bool isEngineInitialized = false;
89 struct Initializer {
90         Initializer()
91         {
92                 m_oldCsState = setEngineState(CSR_ENGINE_CS, CSR_STATE_ENABLE);
93                 m_oldWpState = setEngineState(CSR_ENGINE_WP, CSR_STATE_ENABLE);
94         }
95
96         ~Initializer()
97         {
98                 setEngineState(CSR_ENGINE_CS, m_oldCsState);
99                 setEngineState(CSR_ENGINE_WP, m_oldWpState);
100         }
101
102         csr_state_e m_oldCsState;
103         csr_state_e m_oldWpState;
104 };
105
106 BOOST_GLOBAL_FIXTURE(TestConfig);
107 BOOST_GLOBAL_FIXTURE(Initializer);