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)
46 TestResultsCollectorBase* HtmlCollector::Constructor()
48 return new HtmlCollector();
51 void HtmlCollector::CollectCurrentTestGroupName(const std::string& name)
53 fprintf(m_fp.Get(), "<b>Starting group %s", name.c_str());
54 m_currentGroup = name;
57 bool HtmlCollector::Configure()
59 m_fp.Reset(fopen(m_filename.c_str(), "w"));
61 LogPedantic("Could not open file " << m_filename << " for writing");
67 std::string HtmlCollector::CollectorSpecificHelp() const
69 return "--file=<filename> - name of file for output\n"
70 " default - index.html\n";
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);
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)
116 using namespace DPL::Colors::Html;
117 std::string tmp = "'" + id + "' ...";
119 fprintf(m_fp.Get(), "Running test case %-100s", tmp.c_str());
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));
128 if (performanceTime > performanceMaxTime)
129 fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME_TOO_LONG(
130 get_milliseconds(performanceTime),
131 get_milliseconds(performanceMaxTime)));
133 fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME_MAX(
134 get_milliseconds(performanceTime),
135 get_milliseconds(performanceMaxTime)));
139 fprintf(m_fp.Get(), GREEN_RESULT_OK);
141 case TestResultsCollectorBase::FailStatus::FAILED:
142 PrintfErrorMessage(" FAILED ", reason, true);
144 case TestResultsCollectorBase::FailStatus::IGNORED:
145 PrintfIgnoredMessage("Ignored ", reason, true);
147 case TestResultsCollectorBase::FailStatus::INTERNAL:
148 PrintfErrorMessage("INTERNAL", reason, true);
151 Assert(false && "Bad status");
153 m_groupsStats[m_currentGroup].AddTest(status);
154 m_stats.AddTest(status);
157 void HtmlCollector::PrintfErrorMessage(const char* type,
158 const std::string& message,
161 using namespace DPL::Colors::Html;
180 void HtmlCollector::PrintfIgnoredMessage(const char* type,
181 const std::string& message,
184 using namespace DPL::Colors::Html;
204 void HtmlCollector::PrintStats(const std::string& name, const Statistic& stats)
206 using namespace DPL::Colors::Html;
208 m_fp.Get(), "\n%sResults [%s]:%s\n", BOLD_GREEN_BEGIN,
209 name.c_str(), BOLD_GREEN_END);
211 m_fp.Get(), "%s%s%3zu%s\n", CYAN_BEGIN,
212 "Total tests: ", stats.GetTotal(), CYAN_END);
214 m_fp.Get(), " %s%s%3zu%s\n", CYAN_BEGIN,
215 "Succeeded: ", stats.GetPassed(), CYAN_END);
217 m_fp.Get(), " %s%s%3zu%s\n", CYAN_BEGIN,
218 "Failed: ", stats.GetFailed(), CYAN_END);
220 m_fp.Get(), " %s%s%3zu%s\n", CYAN_BEGIN,
221 "Ignored: ", stats.GetIgnored(), CYAN_END);