0ca047515086f35bb5811ac36e726f22a0866e4d
[platform/core/test/security-tests.git] / src / 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 CollectorFileHelp(DEFAULT_HTML_FILE_NAME);
70 }
71
72 void HtmlCollector::Start()
73 {
74     Assert(!!m_fp && "File handle must not be null");
75     fprintf(m_fp.Get(),
76             "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0"
77             "Transitional//EN\" "
78             "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\""
79             ">\n");
80     fprintf(m_fp.Get(),
81             "<html xmlns=\"http://www.w3.org/1999/xhtml\" "
82             "lang=\"en\" dir=\"ltr\">\n");
83     fprintf(m_fp.Get(), "<body style=\"background-color: black;\">\n");
84     fprintf(m_fp.Get(), "<pre>\n");
85     fprintf(m_fp.Get(), "<font color=\"white\">\n");
86 }
87
88 void HtmlCollector::Finish()
89 {
90     using namespace DPL::Colors::Html;
91     // Show result
92     for (auto &group : m_groupsStats) {
93         PrintStats(group.first, group.second);
94     }
95     PrintStats("All tests together", m_stats);
96     fprintf(m_fp.Get(), "</font>\n");
97     fprintf(m_fp.Get(), "</pre>\n");
98     fprintf(m_fp.Get(), "</body>\n");
99     fprintf(m_fp.Get(), "</html>\n");
100 }
101
102 bool HtmlCollector::ParseCollectorSpecificArg(const std::string& arg)
103 {
104     return ParseCollectorFileArg(arg, m_filename);
105 }
106
107 void HtmlCollector::CollectResult(const std::string& id,
108                                   const FailStatus status,
109                                   const std::string& reason,
110                                   const bool& isPerformanceTest,
111                                   const std::chrono::system_clock::duration& performanceTime,
112                                   const std::chrono::system_clock::duration& performanceMaxTime)
113 {
114     using namespace DPL::Colors::Html;
115     std::string tmp = "'" + id + "' ...";
116
117     fprintf(m_fp.Get(), "Running test case %-100s", tmp.c_str());
118     switch (status) {
119     case FailStatus::NONE:
120         if (isPerformanceTest) {
121             if (performanceMaxTime <= std::chrono::microseconds::zero()) {
122                 fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME,
123                         get_milliseconds(performanceTime));
124                 break;
125             } else {
126                 if (performanceTime > performanceMaxTime)
127                     fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME_TOO_LONG(
128                             get_milliseconds(performanceTime),
129                             get_milliseconds(performanceMaxTime)));
130                 else
131                     fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME_MAX(
132                             get_milliseconds(performanceTime),
133                             get_milliseconds(performanceMaxTime)));
134                 break;
135             }
136         }
137         fprintf(m_fp.Get(), GREEN_RESULT_OK);
138         break;
139     case FailStatus::FAILED:
140         PrintfErrorMessage(" FAILED ", reason, true);
141         break;
142     case FailStatus::IGNORED:
143         PrintfIgnoredMessage("Ignored ", reason, true);
144         break;
145     default:
146         Assert(false && "Bad status");
147     }
148     m_groupsStats[m_currentGroup].AddTest(status);
149     m_stats.AddTest(status);
150 }
151
152 void HtmlCollector::PrintfErrorMessage(const char* type,
153                                        const std::string& message,
154                                        bool verbosity)
155 {
156     using namespace DPL::Colors::Html;
157     if (verbosity) {
158         fprintf(m_fp.Get(),
159                 "[%s%s%s] %s%s%s\n",
160                 BOLD_RED_BEGIN,
161                 type,
162                 BOLD_RED_END,
163                 BOLD_YELLOW_BEGIN,
164                 message.c_str(),
165                 BOLD_YELLOW_END);
166     } else {
167         fprintf(m_fp.Get(),
168                 "[%s%s%s]\n",
169                 BOLD_RED_BEGIN,
170                 type,
171                 BOLD_RED_END);
172     }
173 }
174
175 void HtmlCollector::PrintfIgnoredMessage(const char* type,
176                                          const std::string& message,
177                                          bool verbosity)
178 {
179     using namespace DPL::Colors::Html;
180
181     if (verbosity) {
182         fprintf(m_fp.Get(),
183                 "[%s%s%s] %s%s%s\n",
184                 CYAN_BEGIN,
185                 type,
186                 CYAN_END,
187                 BOLD_GOLD_BEGIN,
188                 message.c_str(),
189                 BOLD_GOLD_END);
190     } else {
191         fprintf(m_fp.Get(),
192                 "[%s%s%s]\n",
193                 CYAN_BEGIN,
194                 type,
195                 CYAN_END);
196     }
197 }
198
199 void HtmlCollector::PrintStats(const std::string& name, const Statistic& stats)
200 {
201     using namespace DPL::Colors::Html;
202     fprintf(
203         m_fp.Get(), "\n%sResults [%s]:%s\n", BOLD_GREEN_BEGIN,
204         name.c_str(), BOLD_GREEN_END);
205     fprintf(
206         m_fp.Get(), "%s%s%3zu%s\n", CYAN_BEGIN,
207         "Total tests:            ", stats.GetTotal(), CYAN_END);
208     fprintf(
209         m_fp.Get(), "  %s%s%3zu%s\n", CYAN_BEGIN,
210         "Succeeded:            ", stats.GetPassed(), CYAN_END);
211     fprintf(
212         m_fp.Get(), "  %s%s%3zu%s\n", CYAN_BEGIN,
213         "Failed:               ", stats.GetFailed(), CYAN_END);
214     fprintf(
215         m_fp.Get(), "  %s%s%3zu%s\n", CYAN_BEGIN,
216         "Ignored:              ", stats.GetIgnored(), CYAN_END);
217 }
218
219 } // namespace Test
220 } // namespace DPL