Home | History | Annotate | Download | only in launcher
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef BASE_TEST_LAUNCHER_TEST_RESULTS_TRACKER_H_
      6 #define BASE_TEST_LAUNCHER_TEST_RESULTS_TRACKER_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/callback.h"
     14 #include "base/test/launcher/test_result.h"
     15 #include "base/threading/thread_checker.h"
     16 
     17 class CommandLine;
     18 
     19 namespace base {
     20 
     21 class FilePath;
     22 
     23 // A helper class to output results.
     24 // Note: as currently XML is the only supported format by gtest, we don't
     25 // check output format (e.g. "xml:" prefix) here and output an XML file
     26 // unconditionally.
     27 // Note: we don't output per-test-case or total summary info like
     28 // total failed_test_count, disabled_test_count, elapsed_time and so on.
     29 // Only each test (testcase element in the XML) will have the correct
     30 // failed/disabled/elapsed_time information. Each test won't include
     31 // detailed failure messages either.
     32 class TestResultsTracker {
     33  public:
     34   TestResultsTracker();
     35   ~TestResultsTracker();
     36 
     37   // Initialize the result tracker. Must be called exactly once before
     38   // calling any other methods. Returns true on success.
     39   bool Init(const CommandLine& command_line) WARN_UNUSED_RESULT;
     40 
     41   // Called when a test iteration is starting.
     42   void OnTestIterationStarting();
     43 
     44   // Adds |result| to the stored test results.
     45   void AddTestResult(const TestResult& result);
     46 
     47   // Prints a summary of current test iteration to stdout.
     48   void PrintSummaryOfCurrentIteration() const;
     49 
     50   // Prints a summary of all test iterations (not just the last one) to stdout.
     51   void PrintSummaryOfAllIterations() const;
     52 
     53   // Adds a string tag to the JSON summary. This is intended to indicate
     54   // conditions that affect the entire test run, as opposed to individual tests.
     55   void AddGlobalTag(const std::string& tag);
     56 
     57   // Saves a JSON summary of all test iterations results to |path|. Returns
     58   // true on success.
     59   bool SaveSummaryAsJSON(const FilePath& path) const WARN_UNUSED_RESULT;
     60 
     61  private:
     62   struct AggregateTestResult {
     63     AggregateTestResult();
     64     ~AggregateTestResult();
     65 
     66     std::vector<TestResult> test_results;
     67   };
     68 
     69   struct PerIterationData {
     70     PerIterationData();
     71     ~PerIterationData();
     72 
     73     // Aggregate test results grouped by full test name.
     74     typedef std::map<std::string, AggregateTestResult> ResultsMap;
     75     ResultsMap results;
     76   };
     77 
     78   ThreadChecker thread_checker_;
     79 
     80   // Set of global tags, i.e. strings indicating conditions that apply to
     81   // the entire test run.
     82   std::set<std::string> global_tags_;
     83 
     84   // Store test results for each iteration.
     85   std::vector<PerIterationData> per_iteration_data_;
     86 
     87   // Index of current iteration (starting from 0). -1 before the first
     88   // iteration.
     89   int iteration_;
     90 
     91   // File handle of output file (can be NULL if no file).
     92   FILE* out_;
     93 
     94   DISALLOW_COPY_AND_ASSIGN(TestResultsTracker);
     95 };
     96 
     97 }  // namespace base
     98 
     99 #endif  // BASE_TEST_LAUNCHER_TEST_RESULTS_TRACKER_H_
    100