blob: 9766c2c3a8b502aea10dd0165e4e2875baae4655 [file] [log] [blame]
henrike@webrtc.orgf7795df2014-05-13 18:00:26 +00001/*
2 * Copyright 2004 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#ifndef WEBRTC_BASE_GUNIT_H_
12#define WEBRTC_BASE_GUNIT_H_
13
14#include "webrtc/base/logging.h"
15#include "webrtc/base/thread.h"
16#if defined(WEBRTC_ANDROID) || defined(GTEST_RELATIVE_PATH)
17#include "gtest/gtest.h"
18#else
19#include "testing/base/public/gunit.h"
20#endif
21
22// forward declarations
23namespace rtc {
24class Pathname;
25}
26
27// Wait until "ex" is true, or "timeout" expires.
28#define WAIT(ex, timeout) \
29 for (uint32 start = rtc::Time(); \
30 !(ex) && rtc::Time() < start + timeout;) \
31 rtc::Thread::Current()->ProcessMessages(1);
32
33// This returns the result of the test in res, so that we don't re-evaluate
34// the expression in the XXXX_WAIT macros below, since that causes problems
35// when the expression is only true the first time you check it.
36#define WAIT_(ex, timeout, res) \
37 do { \
38 uint32 start = rtc::Time(); \
39 res = (ex); \
40 while (!res && rtc::Time() < start + timeout) { \
41 rtc::Thread::Current()->ProcessMessages(1); \
42 res = (ex); \
43 } \
44 } while (0);
45
46// The typical EXPECT_XXXX and ASSERT_XXXXs, but done until true or a timeout.
47#define EXPECT_TRUE_WAIT(ex, timeout) \
48 do { \
49 bool res; \
50 WAIT_(ex, timeout, res); \
51 if (!res) EXPECT_TRUE(ex); \
52 } while (0);
53
54#define EXPECT_EQ_WAIT(v1, v2, timeout) \
55 do { \
56 bool res; \
57 WAIT_(v1 == v2, timeout, res); \
58 if (!res) EXPECT_EQ(v1, v2); \
59 } while (0);
60
61#define ASSERT_TRUE_WAIT(ex, timeout) \
62 do { \
63 bool res; \
64 WAIT_(ex, timeout, res); \
65 if (!res) ASSERT_TRUE(ex); \
66 } while (0);
67
68#define ASSERT_EQ_WAIT(v1, v2, timeout) \
69 do { \
70 bool res; \
71 WAIT_(v1 == v2, timeout, res); \
72 if (!res) ASSERT_EQ(v1, v2); \
73 } while (0);
74
75// Version with a "soft" timeout and a margin. This logs if the timeout is
76// exceeded, but it only fails if the expression still isn't true after the
77// margin time passes.
78#define EXPECT_TRUE_WAIT_MARGIN(ex, timeout, margin) \
79 do { \
80 bool res; \
81 WAIT_(ex, timeout, res); \
82 if (res) { \
83 break; \
84 } \
85 LOG(LS_WARNING) << "Expression " << #ex << " still not true after " << \
86 timeout << "ms; waiting an additional " << margin << "ms"; \
87 WAIT_(ex, margin, res); \
88 if (!res) { \
89 EXPECT_TRUE(ex); \
90 } \
91 } while (0);
92
93rtc::Pathname GetTalkDirectory();
94
95#endif // WEBRTC_BASE_GUNIT_H_