1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11//
12// vie_autotest_defines.h
13//
14
15
16#ifndef WEBRTC_VIDEO_ENGINE_MAIN_TEST_AUTOTEST_INTERFACE_VIE_AUTOTEST_DEFINES_H_
17#define WEBRTC_VIDEO_ENGINE_MAIN_TEST_AUTOTEST_INTERFACE_VIE_AUTOTEST_DEFINES_H_
18
19#include <assert.h>
20#include <stdarg.h>
21#include <stdio.h>
22
23#include <string>
24
25#include "testing/gtest/include/gtest/gtest.h"
26#include "webrtc/engine_configurations.h"
27#include "webrtc/system_wrappers/interface/sleep.h"
28
29#if defined(_WIN32)
30#include <windows.h>
31#elif defined (WEBRTC_ANDROID)
32#include <android/log.h>
33#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
34#include <stdlib.h>
35#include <string.h>
36#include <sys/time.h>
37#include <time.h>
38#endif
39
40// Choose how to log
41//#define VIE_LOG_TO_FILE
42#define VIE_LOG_TO_STDOUT
43
44// Choose one way to test error
45#define VIE_ASSERT_ERROR
46
47#define VIE_LOG_FILE_NAME "ViEAutotestLog.txt"
48
49#undef RGB
50#define RGB(r,g,b) r|g<<8|b<<16
51
52enum { kAutoTestSleepTimeMs = 5000 };
53enum { kAutoTestFullStackSleepTimeMs = 20000 };
54
55struct AutoTestSize {
56  unsigned int width;
57  unsigned int height;
58  AutoTestSize() :
59    width(0), height(0) {
60  }
61  AutoTestSize(unsigned int iWidth, unsigned int iHeight) :
62    width(iWidth), height(iHeight) {
63  }
64};
65
66struct AutoTestOrigin {
67  unsigned int x;
68  unsigned int y;
69  AutoTestOrigin() :
70    x(0), y(0) {
71  }
72  AutoTestOrigin(unsigned int iX, unsigned int iY) :
73    x(iX), y(iY) {
74  }
75};
76
77struct AutoTestRect {
78  AutoTestSize size;
79  AutoTestOrigin origin;
80  AutoTestRect() :
81    size(), origin() {
82  }
83
84  AutoTestRect(unsigned int iX, unsigned int iY, unsigned int iWidth, unsigned int iHeight) :
85    size(iX, iY), origin(iWidth, iHeight) {
86  }
87
88  void Copy(AutoTestRect iRect) {
89    origin.x = iRect.origin.x;
90    origin.y = iRect.origin.y;
91    size.width = iRect.size.width;
92    size.height = iRect.size.height;
93  }
94};
95
96// ============================================
97
98class ViETest {
99 public:
100  static int Init() {
101#ifdef VIE_LOG_TO_FILE
102    log_file_ = fopen(VIE_LOG_FILE_NAME, "w+t");
103#else
104    log_file_ = NULL;
105#endif
106    log_str_ = new char[kMaxLogSize];
107    memset(log_str_, 0, kMaxLogSize);
108    return 0;
109  }
110
111  static int Terminate() {
112    if (log_file_) {
113      fclose(log_file_);
114      log_file_ = NULL;
115    }
116    if (log_str_) {
117      delete[] log_str_;
118      log_str_ = NULL;
119    }
120    return 0;
121  }
122
123  static void Log(const char* fmt, ...) {
124    va_list va;
125    va_start(va, fmt);
126    memset(log_str_, 0, kMaxLogSize);
127    vsprintf(log_str_, fmt, va);
128    va_end(va);
129
130    WriteToSuitableOutput(log_str_);
131  }
132
133  // Writes to a suitable output, depending on platform and log mode.
134  static void WriteToSuitableOutput(const char* message) {
135#ifdef VIE_LOG_TO_FILE
136    if (log_file_)
137    {
138      fwrite(log_str_, 1, strlen(log_str_), log_file_);
139      fwrite("\n", 1, 1, log_file_);
140      fflush(log_file_);
141    }
142#endif
143#ifdef VIE_LOG_TO_STDOUT
144#if WEBRTC_ANDROID
145    __android_log_write(ANDROID_LOG_DEBUG, "*WebRTCN*", log_str_);
146#else
147    printf("%s\n", log_str_);
148#endif
149#endif
150  }
151
152  // Deprecated(phoglund): Prefer to use googletest macros in all cases
153  // except the custom call case.
154  static int TestError(bool expr, const char* fmt, ...) {
155    if (!expr) {
156      va_list va;
157      va_start(va, fmt);
158      memset(log_str_, 0, kMaxLogSize);
159      vsprintf(log_str_, fmt, va);
160#ifdef WEBRTC_ANDROID
161      __android_log_write(ANDROID_LOG_ERROR, "*WebRTCN*", log_str_);
162#endif
163      WriteToSuitableOutput(log_str_);
164      va_end(va);
165
166      AssertError(log_str_);
167      return 1;
168    }
169    return 0;
170  }
171
172  // Returns a suitable path to write trace and result files to.
173  // You should always use this when you want to write output files.
174  // The returned path is guaranteed to end with a path separator.
175  // This function may be run at any time during the program's execution.
176  // Implemented in vie_autotest.cc
177  static std::string GetResultOutputPath();
178
179private:
180  static void AssertError(const char* message) {
181#ifdef VIE_ASSERT_ERROR
182    assert(false);
183#endif
184  }
185
186  static FILE* log_file_;
187  enum {
188    kMaxLogSize = 512
189  };
190  static char* log_str_;
191};
192
193#define AutoTestSleep webrtc::SleepMs
194
195#endif  // WEBRTC_VIDEO_ENGINE_MAIN_TEST_AUTOTEST_INTERFACE_VIE_AUTOTEST_DEFINES_H_
196