3 * Copyright 2004--2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef TALK_BASE_GUNIT_H_
29 #define TALK_BASE_GUNIT_H_
31 #include "talk/base/logging.h"
32 #include "talk/base/thread.h"
33 #if defined(ANDROID) || defined(GTEST_RELATIVE_PATH)
34 #include "gtest/gtest.h"
36 #include "testing/base/public/gunit.h"
39 // forward declarations
44 // Wait until "ex" is true, or "timeout" expires.
45 #define WAIT(ex, timeout) \
46 for (uint32 start = talk_base::Time(); \
47 !(ex) && talk_base::Time() < start + timeout;) \
48 talk_base::Thread::Current()->ProcessMessages(1);
50 // This returns the result of the test in res, so that we don't re-evaluate
51 // the expression in the XXXX_WAIT macros below, since that causes problems
52 // when the expression is only true the first time you check it.
53 #define WAIT_(ex, timeout, res) \
55 uint32 start = talk_base::Time(); \
57 while (!res && talk_base::Time() < start + timeout) { \
58 talk_base::Thread::Current()->ProcessMessages(1); \
63 // The typical EXPECT_XXXX and ASSERT_XXXXs, but done until true or a timeout.
64 #define EXPECT_TRUE_WAIT(ex, timeout) \
67 WAIT_(ex, timeout, res); \
68 if (!res) EXPECT_TRUE(ex); \
71 #define EXPECT_EQ_WAIT(v1, v2, timeout) \
74 WAIT_(v1 == v2, timeout, res); \
75 if (!res) EXPECT_EQ(v1, v2); \
78 #define ASSERT_TRUE_WAIT(ex, timeout) \
81 WAIT_(ex, timeout, res); \
82 if (!res) ASSERT_TRUE(ex); \
85 #define ASSERT_EQ_WAIT(v1, v2, timeout) \
88 WAIT_(v1 == v2, timeout, res); \
89 if (!res) ASSERT_EQ(v1, v2); \
92 // Version with a "soft" timeout and a margin. This logs if the timeout is
93 // exceeded, but it only fails if the expression still isn't true after the
94 // margin time passes.
95 #define EXPECT_TRUE_WAIT_MARGIN(ex, timeout, margin) \
98 WAIT_(ex, timeout, res); \
102 LOG(LS_WARNING) << "Expression " << #ex << " still not true after " << \
103 timeout << "ms; waiting an additional " << margin << "ms"; \
104 WAIT_(ex, margin, res); \
110 talk_base::Pathname GetTalkDirectory();
112 #endif // TALK_BASE_GUNIT_H_