Home | History | Annotate | Download | only in src
      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 "StringUtil.h"
     19 #include "Report.h"
     20 
     21 
     22 Report* Report::mInstance = NULL;
     23 
     24 Report* Report::Instance(const char* dirName)
     25 {
     26     if (mInstance == NULL) {
     27         mInstance = new Report();
     28         ASSERT(mInstance->init(dirName));
     29     }
     30     return mInstance;
     31 }
     32 void Report::Finalize()
     33 {
     34     delete mInstance;
     35     mInstance = NULL;
     36 }
     37 
     38 
     39 Report::Report()
     40 {
     41 
     42 }
     43 
     44 Report::~Report()
     45 {
     46     writeSummary();
     47 }
     48 
     49 bool Report::init(const char* dirName)
     50 {
     51     if (dirName == NULL) {
     52         return true;
     53     }
     54     android::String8 report;
     55     if (report.appendFormat("%s/report.txt", dirName) != 0) {
     56         return false;
     57     }
     58     return FileUtil::init(report.string());
     59 }
     60 
     61 void Report::printf(const char* fmt, ...)
     62 {
     63     va_list ap;
     64     va_start(ap, fmt);
     65     FileUtil::doVprintf(false, -1, fmt, ap);
     66     va_end(ap);
     67 }
     68 
     69 void Report::addCasePassed(const android::String8& name)
     70 {
     71     mPassedCases.push_back(name);
     72 }
     73 
     74 void Report::addCaseFailed(const android::String8& name)
     75 {
     76     mFailedCases.push_back(name);
     77 }
     78 
     79 void Report::writeSummary()
     80 {
     81     printf("= Test cases executed: %d, passed: %d, failed: %d =",
     82             mPassedCases.size() + mFailedCases.size(), mPassedCases.size(), mFailedCases.size());
     83     printf("= Failed cases =");
     84     std::list<android::String8>::iterator it;
     85     for (it = mFailedCases.begin(); it != mFailedCases.end(); it++) {
     86         printf("* %s", it->string());
     87     }
     88     printf("= Passed cases =");
     89     for (it = mPassedCases.begin(); it != mPassedCases.end(); it++) {
     90         printf("* %s", it->string());
     91     }
     92 }
     93