2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file test_results_collector_html.cpp
18 * @author Lukasz Wrzosek (l.wrzosek@samsung.com)
19 * @author Marcin Niesluchowski (m.niesluchow@samsung.com)
21 * @brief Source file containing HtmlCollector class definition
26 #include <dpl/assert.h>
27 #include <dpl/colors.h>
28 #include <dpl/test/test_results_collector_commons.h>
30 #include "dpl/test/test_results_collector_html.h"
37 const char *DEFAULT_HTML_FILE_NAME = "index.html";
41 HtmlCollector::HtmlCollector()
42 : m_filename(DEFAULT_HTML_FILE_NAME),
47 TestResultsCollectorBase* HtmlCollector::Constructor()
49 return new HtmlCollector();
52 void HtmlCollector::CollectCurrentTestGroupName(const std::string& name)
54 fprintf(m_fp.Get(), "<b>Starting group %s", name.c_str());
55 m_currentGroup = name;
58 bool HtmlCollector::Configure()
60 m_fp.Reset(fopen(m_filename.c_str(), "w"));
62 LogPedantic("Could not open file " << m_filename << " for writing");
68 std::string HtmlCollector::CollectorSpecificHelp() const
70 return CollectorFileHelp(DEFAULT_HTML_FILE_NAME) + COLLECTOR_NO_VERBOSE_HELP;
73 void HtmlCollector::Start()
75 Assert(!!m_fp && "File handle must not be null");
77 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0"
79 "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\""
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");
89 void HtmlCollector::Finish()
91 using namespace DPL::Colors::Html;
93 for (auto &group : m_groupsStats) {
94 PrintStats(group.first, group.second);
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");
103 bool HtmlCollector::ParseCollectorSpecificArg(const std::string& arg)
105 return ParseCollectorFileArg(arg, m_filename) || ParseCollectorNoVerboseArg(arg, m_verbosity);
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)
115 using namespace DPL::Colors::Html;
116 std::string tmp = "'" + id + "' ...";
118 fprintf(m_fp.Get(), "Running test case %-100s", tmp.c_str());
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));
127 if (performanceTime > performanceMaxTime)
128 fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME_TOO_LONG(
129 get_milliseconds(performanceTime),
130 get_milliseconds(performanceMaxTime)));
132 fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME_MAX(
133 get_milliseconds(performanceTime),
134 get_milliseconds(performanceMaxTime)));
138 fprintf(m_fp.Get(), GREEN_RESULT_OK);
140 case FailStatus::FAILED:
141 PrintfErrorMessage(" FAILED ", reason);
143 case FailStatus::IGNORED:
144 PrintfIgnoredMessage("Ignored ", reason);
147 Assert(false && "Bad status");
149 m_groupsStats[m_currentGroup].AddTest(status);
150 m_stats.AddTest(status);
153 void HtmlCollector::PrintfErrorMessage(const char* type, const std::string& message)
155 using namespace DPL::Colors::Html;
168 fprintf(m_fp.Get(), "\n");
171 void HtmlCollector::PrintfIgnoredMessage(const char* type, const std::string& message)
173 using namespace DPL::Colors::Html;
187 fprintf(m_fp.Get(), "\n");
190 void HtmlCollector::PrintStats(const std::string& name, const Statistic& stats)
192 using namespace DPL::Colors::Html;
194 m_fp.Get(), "\n%sResults [%s]:%s\n", BOLD_GREEN_BEGIN,
195 name.c_str(), BOLD_GREEN_END);
197 m_fp.Get(), "%s%s%3zu%s\n", CYAN_BEGIN,
198 "Total tests: ", stats.GetTotal(), CYAN_END);
200 m_fp.Get(), " %s%s%3zu%s\n", CYAN_BEGIN,
201 "Succeeded: ", stats.GetPassed(), CYAN_END);
203 m_fp.Get(), " %s%s%3zu%s\n", CYAN_BEGIN,
204 "Failed: ", stats.GetFailed(), CYAN_END);
206 m_fp.Get(), " %s%s%3zu%s\n", CYAN_BEGIN,
207 "Ignored: ", stats.GetIgnored(), CYAN_END);