Add --no-verbose option to HTML collector
[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       m_verbosity(true)
44 {
45 }
46
47 TestResultsCollectorBase* HtmlCollector::Constructor()
48 {
49     return new HtmlCollector();
50 }
51
52 void HtmlCollector::CollectCurrentTestGroupName(const std::string& name)
53 {
54     fprintf(m_fp.Get(), "<b>Starting group %s", name.c_str());
55     m_currentGroup = name;
56 }
57
58 bool HtmlCollector::Configure()
59 {
60     m_fp.Reset(fopen(m_filename.c_str(), "w"));
61     if (!m_fp) {
62         LogPedantic("Could not open file " << m_filename << " for writing");
63         return false;
64     }
65     return true;
66 }
67
68 std::string HtmlCollector::CollectorSpecificHelp() const
69 {
70     return CollectorFileHelp(DEFAULT_HTML_FILE_NAME) + COLLECTOR_NO_VERBOSE_HELP;
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) || ParseCollectorNoVerboseArg(arg, m_verbosity);
106 }
107
108 void HtmlCollector::CollectResult(const std::string& id,
109                                   const FailStatus status,
110                                   const std::string& reason,
111                                   const bool& isPerformanceTest,
112                                   const std::chrono::system_clock::duration& performanceTime,
113                                   const std::chrono::system_clock::duration& performanceMaxTime)
114 {
115     using namespace DPL::Colors::Html;
116     std::string tmp = "'" + id + "' ...";
117
118     fprintf(m_fp.Get(), "Running test case %-100s", tmp.c_str());
119     switch (status) {
120     case FailStatus::NONE:
121         if (isPerformanceTest) {
122             if (performanceMaxTime <= std::chrono::microseconds::zero()) {
123                 fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME,
124                         get_milliseconds(performanceTime));
125                 break;
126             } else {
127                 if (performanceTime > performanceMaxTime)
128                     fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME_TOO_LONG(
129                             get_milliseconds(performanceTime),
130                             get_milliseconds(performanceMaxTime)));
131                 else
132                     fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME_MAX(
133                             get_milliseconds(performanceTime),
134                             get_milliseconds(performanceMaxTime)));
135                 break;
136             }
137         }
138         fprintf(m_fp.Get(), GREEN_RESULT_OK);
139         break;
140     case FailStatus::FAILED:
141         PrintfErrorMessage(" FAILED ", reason);
142         break;
143     case FailStatus::IGNORED:
144         PrintfIgnoredMessage("Ignored ", reason);
145         break;
146     default:
147         Assert(false && "Bad status");
148     }
149     m_groupsStats[m_currentGroup].AddTest(status);
150     m_stats.AddTest(status);
151 }
152
153 void HtmlCollector::PrintfErrorMessage(const char* type, const std::string& message)
154 {
155     using namespace DPL::Colors::Html;
156     fprintf(m_fp.Get(),
157             "[%s%s%s]",
158             BOLD_RED_BEGIN,
159             type,
160             BOLD_RED_END);
161     if (m_verbosity) {
162         fprintf(m_fp.Get(),
163                 " %s%s%s",
164                 BOLD_YELLOW_BEGIN,
165                 message.c_str(),
166                 BOLD_YELLOW_END);
167     }
168     fprintf(m_fp.Get(), "\n");
169 }
170
171 void HtmlCollector::PrintfIgnoredMessage(const char* type, const std::string& message)
172 {
173     using namespace DPL::Colors::Html;
174
175     fprintf(m_fp.Get(),
176             "[%s%s%s]",
177             CYAN_BEGIN,
178             type,
179             CYAN_END);
180     if (m_verbosity) {
181         fprintf(m_fp.Get(),
182                 " %s%s%s",
183                 BOLD_GOLD_BEGIN,
184                 message.c_str(),
185                 BOLD_GOLD_END);
186     }
187     fprintf(m_fp.Get(), "\n");
188 }
189
190 void HtmlCollector::PrintStats(const std::string& name, const Statistic& stats)
191 {
192     using namespace DPL::Colors::Html;
193     fprintf(
194         m_fp.Get(), "\n%sResults [%s]:%s\n", BOLD_GREEN_BEGIN,
195         name.c_str(), BOLD_GREEN_END);
196     fprintf(
197         m_fp.Get(), "%s%s%3zu%s\n", CYAN_BEGIN,
198         "Total tests:            ", stats.GetTotal(), CYAN_END);
199     fprintf(
200         m_fp.Get(), "  %s%s%3zu%s\n", CYAN_BEGIN,
201         "Succeeded:            ", stats.GetPassed(), CYAN_END);
202     fprintf(
203         m_fp.Get(), "  %s%s%3zu%s\n", CYAN_BEGIN,
204         "Failed:               ", stats.GetFailed(), CYAN_END);
205     fprintf(
206         m_fp.Get(), "  %s%s%3zu%s\n", CYAN_BEGIN,
207         "Ignored:              ", stats.GetIgnored(), CYAN_END);
208 }
209
210 } // namespace Test
211 } // namespace DPL