1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 #include "Log.h"
18 #include "Settings.h"
19 #include "StringUtil.h"
20 #include "Report.h"
21 #include "task/TaskCase.h"
22 
23 
24 Report* Report::mInstance = NULL;
25 
Instance(const char * dirName)26 Report* Report::Instance(const char* dirName)
27 {
28     if (mInstance == NULL) {
29         mInstance = new Report();
30         ASSERT(mInstance->init(dirName));
31     }
32     return mInstance;
33 }
Finalize()34 void Report::Finalize()
35 {
36     delete mInstance;
37     mInstance = NULL;
38 }
39 
40 
Report()41 Report::Report()
42 {
43 
44 }
45 
~Report()46 Report::~Report()
47 {
48     writeReport();
49     mFailedCases.clear();
50     mPassedCases.clear();
51 }
52 
init(const char * dirName)53 bool Report::init(const char* dirName)
54 {
55     if (dirName == NULL) {
56         return true;
57     }
58     android::String8 report;
59     if (report.appendFormat("%s/report.xml", dirName) != 0) {
60         return false;
61     }
62     Settings::Instance()->addSetting(Settings::EREPORT_FILE, report);
63     return FileUtil::init(report.string());
64 }
65 
printf(const char * fmt,...)66 void Report::printf(const char* fmt, ...)
67 {
68     va_list ap;
69     va_start(ap, fmt);
70     FileUtil::doVprintf(false, -1, fmt, ap);
71     va_end(ap);
72 }
73 
addCasePassed(const TaskCase * task)74 void Report::addCasePassed(const TaskCase* task)
75 {
76     android::String8 name(" ");
77     task->getCaseName(name);
78     StringPair pair(name, task->getDetails());
79     mPassedCases.push_back(pair);
80 }
81 
addCaseFailed(const TaskCase * task)82 void Report::addCaseFailed(const TaskCase* task)
83 {
84     android::String8 name(" ");
85     task->getCaseName(name);
86     StringPair pair(name, task->getDetails());
87     mFailedCases.push_back(pair);
88 }
89 
writeResult(std::list<StringPair>::const_iterator begin,std::list<StringPair>::const_iterator end,bool passed)90 void Report::writeResult(std::list<StringPair>::const_iterator begin,
91         std::list<StringPair>::const_iterator end, bool passed)
92 {
93     std::list<StringPair>::const_iterator it;
94     for (it = begin; it != end; it++) {
95         if (passed) {
96             printf("    <test title=\"%s\" result=\"pass\" >", it->first.string());
97         } else {
98             printf("    <test title=\"%s\" result=\"fail\" >", it->first.string());
99         }
100         printf("        <details>\n%s", it->second.string());
101         printf("        </details>");
102         printf("    </test>");
103     }
104 }
105 
writeReport()106 void Report::writeReport()
107 {
108     printf("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>");
109     printf("<audio-test-results-report report-version=\"1\" creation-time=\"%s\">",
110             Settings::Instance()->getSetting(Settings::EREPORT_TIME).string());
111     printf("  <verifier-info version-name=\"1\" version-code=\"1\" />");
112     printf("  <device-info>");
113     printf("    %s", Settings::Instance()->getSetting(Settings::EDEVICE_INFO).string());
114     printf("  </device-info>");
115     printf("  <audio-test-results xml=\"%s\">",
116             Settings::Instance()->getSetting(Settings::ETEST_XML).string());
117 
118     writeResult(mFailedCases.begin(), mFailedCases.end(), false);
119     writeResult(mPassedCases.begin(), mPassedCases.end(), true);
120 
121     printf("  </audio-test-results>");
122     printf("</audio-test-results-report>");
123 }
124