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 CollectorFileHelp(DEFAULT_HTML_FILE_NAME);
72 void HtmlCollector::Start()
74 Assert(!!m_fp && "File handle must not be null");
76 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0"
78 "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\""
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");
88 void HtmlCollector::Finish()
90 using namespace DPL::Colors::Html;
92 for (auto &group : m_groupsStats) {
93 PrintStats(group.first, group.second);
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");
102 bool HtmlCollector::ParseCollectorSpecificArg(const std::string& arg)
104 return ParseCollectorFileArg(arg, m_filename);
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)
114 using namespace DPL::Colors::Html;
115 std::string tmp = "'" + id + "' ...";
117 fprintf(m_fp.Get(), "Running test case %-100s", tmp.c_str());
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));
126 if (performanceTime > performanceMaxTime)
127 fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME_TOO_LONG(
128 get_milliseconds(performanceTime),
129 get_milliseconds(performanceMaxTime)));
131 fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME_MAX(
132 get_milliseconds(performanceTime),
133 get_milliseconds(performanceMaxTime)));
137 fprintf(m_fp.Get(), GREEN_RESULT_OK);
139 case FailStatus::FAILED:
140 PrintfErrorMessage(" FAILED ", reason, true);
142 case FailStatus::IGNORED:
143 PrintfIgnoredMessage("Ignored ", reason, true);
146 Assert(false && "Bad status");
148 m_groupsStats[m_currentGroup].AddTest(status);
149 m_stats.AddTest(status);
152 void HtmlCollector::PrintfErrorMessage(const char* type,
153 const std::string& message,
156 using namespace DPL::Colors::Html;
175 void HtmlCollector::PrintfIgnoredMessage(const char* type,
176 const std::string& message,
179 using namespace DPL::Colors::Html;
199 void HtmlCollector::PrintStats(const std::string& name, const Statistic& stats)
201 using namespace DPL::Colors::Html;
203 m_fp.Get(), "\n%sResults [%s]:%s\n", BOLD_GREEN_BEGIN,
204 name.c_str(), BOLD_GREEN_END);
206 m_fp.Get(), "%s%s%3zu%s\n", CYAN_BEGIN,
207 "Total tests: ", stats.GetTotal(), CYAN_END);
209 m_fp.Get(), " %s%s%3zu%s\n", CYAN_BEGIN,
210 "Succeeded: ", stats.GetPassed(), CYAN_END);
212 m_fp.Get(), " %s%s%3zu%s\n", CYAN_BEGIN,
213 "Failed: ", stats.GetFailed(), CYAN_END);
215 m_fp.Get(), " %s%s%3zu%s\n", CYAN_BEGIN,
216 "Ignored: ", stats.GetIgnored(), CYAN_END);