aboutsummaryrefslogtreecommitdiff
path: root/webrtc/system_wrappers/source/unittest_utilities.h
blob: c00dd04331f74db2a15b91cd7d3ffb5caf314162 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_UNITTEST_UTILITIES_H_
#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_UNITTEST_UTILITIES_H_

// This file contains utilities that make it simpler to write unittests
// that are appropriate for the system_wrappers classes.

#include <stdio.h>
#include <string.h>

#include "webrtc/system_wrappers/interface/trace.h"

namespace webrtc {

class TestTraceCallback : public TraceCallback {
 public:
  virtual void Print(TraceLevel level, const char* msg, int length) {
    if (msg) {
      char* cmd_print = new char[length+1];
      memcpy(cmd_print, msg, length);
      cmd_print[length] = '\0';
      printf("%s\n", cmd_print);
      fflush(stdout);
      delete[] cmd_print;
    }
  }
};

// A class that turns on tracing to stdout at the beginning of the test,
// and turns it off once the test is finished.
// Intended usage:
// class SomeTest : public ::testing::Test {
//  protected:
//   SomeTest()
//     : trace_(false) {}  // Change to true to turn on tracing.
//  private:
//   ScopedTracing trace_;
// }
class ScopedTracing {
 public:
  explicit ScopedTracing(bool logOn) {
    logging_ = logOn;
    StartTrace();
  }

  ~ScopedTracing() {
    StopTrace();
  }

 private:
  void StartTrace() {
    if (logging_) {
      Trace::CreateTrace();
      Trace::SetLevelFilter(webrtc::kTraceAll);
      Trace::SetTraceCallback(&trace_);
    }
  }

  void StopTrace() {
    if (logging_) {
      Trace::SetTraceCallback(NULL);
      Trace::ReturnTrace();
    }
  }

 private:
  bool logging_;
  TestTraceCallback trace_;
};

}  // namespace webrtc

#endif  // WEBRTC_SYSTEM_WRAPPERS_SOURCE_UNITTEST_UTILITIES_H_