324eb9760e135d594feb201dca7c74d2d2f520dc
[platform/core/test/security-tests.git] / tests / framework / src / test_results_collector_html.cpp
1 /*
2  * Copyright (c) 2014 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_results_collector_html.cpp
18  * @author      Lukasz Wrzosek (l.wrzosek@samsung.com)
19  * @author      Marcin Niesluchowski (m.niesluchow@samsung.com)
20  * @version     1.0
21  * @brief       Source file containing HtmlCollector class definition
22  */
23
24 #include <cstdio>
25
26 #include <dpl/assert.h>
27 #include <dpl/colors.h>
28 #include <dpl/test/test_results_collector_commons.h>
29
30 #include "dpl/test/test_results_collector_html.h"
31
32 namespace DPL {
33 namespace Test {
34
35 namespace {
36
37 const char *DEFAULT_HTML_FILE_NAME = "index.html";
38
39 }
40
41 HtmlCollector::HtmlCollector()
42     : m_filename(DEFAULT_HTML_FILE_NAME)
43 {
44 }
45
46 TestResultsCollectorBase* HtmlCollector::Constructor()
47 {
48     return new HtmlCollector();
49 }
50
51 void HtmlCollector::CollectCurrentTestGroupName(const std::string& name)
52 {
53     fprintf(m_fp.Get(), "<b>Starting group %s", name.c_str());
54     m_currentGroup = name;
55 }
56
57 bool HtmlCollector::Configure()
58 {
59     m_fp.Reset(fopen(m_filename.c_str(), "w"));
60     if (!m_fp) {
61         LogPedantic("Could not open file " << m_filename << " for writing");
62         return false;
63     }
64     return true;
65 }
66
67 std::string HtmlCollector::CollectorSpecificHelp() const
68 {
69     return "--file=<filename> - name of file for output\n"
70            "                    default - index.html\n";
71 }
72
73 void HtmlCollector::Start()
74 {
75     Assert(!!m_fp && "File handle must not be null");
76     fprintf(m_fp.Get(),
77             "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0"
78             "Transitional//EN\" "
79             "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\""
80             ">\n");
81     fprintf(m_fp.Get(),
82             "<html xmlns=\"http://www.w3.org/1999/xhtml\" "
83             "lang=\"en\" dir=\"ltr\">\n");
84     fprintf(m_fp.Get(), "<body style=\"background-color: black;\">\n");
85     fprintf(m_fp.Get(), "<pre>\n");
86     fprintf(m_fp.Get(), "<font color=\"white\">\n");
87 }
88
89 void HtmlCollector::Finish()
90 {
91     using namespace DPL::Colors::Html;
92     // Show result
93     for (auto &group : m_groupsStats) {
94         PrintStats(group.first, group.second);
95     }
96     PrintStats("All tests together", m_stats);
97     fprintf(m_fp.Get(), "</font>\n");
98     fprintf(m_fp.Get(), "</pre>\n");
99     fprintf(m_fp.Get(), "</body>\n");
100     fprintf(m_fp.Get(), "</html>\n");
101 }
102
103 bool HtmlCollector::ParseCollectorSpecificArg(const std::string& arg)
104 {
105     return ParseCollectorFileArg(arg, m_filename);
106 }
107
108 void HtmlCollector::CollectResult(const std::string& id,
109                                   const std::string& /*description*/,
110                                   const FailStatus::Type status,
111                                   const std::string& reason,
112                                   const bool& isPerformanceTest,
113                                   const std::chrono::system_clock::duration& performanceTime,
114                                   const std::chrono::system_clock::duration& performanceMaxTime)
115 {
116     using namespace DPL::Colors::Html;
117     std::string tmp = "'" + id + "' ...";
118
119     fprintf(m_fp.Get(), "Running test case %-100s", tmp.c_str());
120     switch (status) {
121     case TestResultsCollectorBase::FailStatus::NONE:
122         if (isPerformanceTest) {
123             if (performanceMaxTime <= std::chrono::microseconds::zero()) {
124                 fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME,
125                         get_milliseconds(performanceTime));
126                 break;
127             } else {
128                 if (performanceTime > performanceMaxTime)
129                     fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME_TOO_LONG(
130                             get_milliseconds(performanceTime),
131                             get_milliseconds(performanceMaxTime)));
132                 else
133                     fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME_MAX(
134                             get_milliseconds(performanceTime),
135                             get_milliseconds(performanceMaxTime)));
136                 break;
137             }
138         }
139         fprintf(m_fp.Get(), GREEN_RESULT_OK);
140         break;
141     case TestResultsCollectorBase::FailStatus::FAILED:
142         PrintfErrorMessage(" FAILED ", reason, true);
143         break;
144     case TestResultsCollectorBase::FailStatus::IGNORED:
145         PrintfIgnoredMessage("Ignored ", reason, true);
146         break;
147     case TestResultsCollectorBase::FailStatus::INTERNAL:
148         PrintfErrorMessage("INTERNAL", reason, true);
149         break;
150     default:
151         Assert(false && "Bad status");
152     }
153     m_groupsStats[m_currentGroup].AddTest(status);
154     m_stats.AddTest(status);
155 }
156
157 void HtmlCollector::PrintfErrorMessage(const char* type,
158                                        const std::string& message,
159                                        bool verbosity)
160 {
161     using namespace DPL::Colors::Html;
162     if (verbosity) {
163         fprintf(m_fp.Get(),
164                 "[%s%s%s] %s%s%s\n",
165                 BOLD_RED_BEGIN,
166                 type,
167                 BOLD_RED_END,
168                 BOLD_YELLOW_BEGIN,
169                 message.c_str(),
170                 BOLD_YELLOW_END);
171     } else {
172         fprintf(m_fp.Get(),
173                 "[%s%s%s]\n",
174                 BOLD_RED_BEGIN,
175                 type,
176                 BOLD_RED_END);
177     }
178 }
179
180 void HtmlCollector::PrintfIgnoredMessage(const char* type,
181                                          const std::string& message,
182                                          bool verbosity)
183 {
184     using namespace DPL::Colors::Html;
185
186     if (verbosity) {
187         fprintf(m_fp.Get(),
188                 "[%s%s%s] %s%s%s\n",
189                 CYAN_BEGIN,
190                 type,
191                 CYAN_END,
192                 BOLD_GOLD_BEGIN,
193                 message.c_str(),
194                 BOLD_GOLD_END);
195     } else {
196         fprintf(m_fp.Get(),
197                 "[%s%s%s]\n",
198                 CYAN_BEGIN,
199                 type,
200                 CYAN_END);
201     }
202 }
203
204 void HtmlCollector::PrintStats(const std::string& name, const Statistic& stats)
205 {
206     using namespace DPL::Colors::Html;
207     fprintf(
208         m_fp.Get(), "\n%sResults [%s]:%s\n", BOLD_GREEN_BEGIN,
209         name.c_str(), BOLD_GREEN_END);
210     fprintf(
211         m_fp.Get(), "%s%s%3zu%s\n", CYAN_BEGIN,
212         "Total tests:            ", stats.GetTotal(), CYAN_END);
213     fprintf(
214         m_fp.Get(), "  %s%s%3zu%s\n", CYAN_BEGIN,
215         "Succeeded:            ", stats.GetPassed(), CYAN_END);
216     fprintf(
217         m_fp.Get(), "  %s%s%3zu%s\n", CYAN_BEGIN,
218         "Failed:               ", stats.GetFailed(), CYAN_END);
219     fprintf(
220         m_fp.Get(), "  %s%s%3zu%s\n", CYAN_BEGIN,
221         "Ignored:              ", stats.GetIgnored(), CYAN_END);
222 }
223
224 } // namespace Test
225 } // namespace DPL