1 /*
2  * libjingle
3  * Copyright 2004--2008, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
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.
15  *
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.
26  */
27 
28 #ifndef TALK_BASE_GUNIT_H_
29 #define TALK_BASE_GUNIT_H_
30 
31 #include "talk/base/logging.h"
32 #include "talk/base/thread.h"
33 #ifdef ANDROID
34 #include <gtest/gtest.h>
35 #elif LIBJINGLE_UNITTEST
36 #include "third_party/gtest/include/gtest/gtest.h"
37 #else
38 #include "testing/base/public/gunit.h"
39 #endif
40 
41 // forward declarations
42 namespace talk_base {
43 class Pathname;
44 }
45 
46 // Wait until "ex" is true, or "timeout" expires.
47 #define WAIT(ex, timeout) \
48   for (uint32 start = talk_base::Time(); \
49       !(ex) && talk_base::Time() < start + timeout;) \
50     talk_base::Thread::Current()->ProcessMessages(1);
51 
52 // This returns the result of the test in res, so that we don't re-evaluate
53 // the expression in the XXXX_WAIT macros below, since that causes problems
54 // when the expression is only true the first time you check it.
55 #define WAIT_(ex, timeout, res) \
56   do { \
57     uint32 start = talk_base::Time(); \
58     res = (ex); \
59     while (!res && talk_base::Time() < start + timeout) { \
60       talk_base::Thread::Current()->ProcessMessages(1); \
61       res = (ex); \
62     } \
63   } while (0);
64 
65 // The typical EXPECT_XXXX and ASSERT_XXXXs, but done until true or a timeout.
66 #define EXPECT_TRUE_WAIT(ex, timeout) \
67   do { \
68     bool res; \
69     WAIT_(ex, timeout, res); \
70     if (!res) EXPECT_TRUE(ex); \
71   } while (0);
72 
73 #define EXPECT_EQ_WAIT(v1, v2, timeout) \
74   do { \
75     bool res; \
76     WAIT_(v1 == v2, timeout, res); \
77     if (!res) EXPECT_EQ(v1, v2); \
78   } while (0);
79 
80 #define ASSERT_TRUE_WAIT(ex, timeout) \
81   do { \
82     bool res; \
83     WAIT_(ex, timeout, res); \
84     if (!res) ASSERT_TRUE(ex); \
85   } while (0);
86 
87 #define ASSERT_EQ_WAIT(v1, v2, timeout) \
88   do { \
89     bool res; \
90     WAIT_(v1 == v2, timeout, res); \
91     if (!res) ASSERT_EQ(v1, v2); \
92   } while (0);
93 
94 // Version with a "soft" timeout and a margin. This logs if the timeout is
95 // exceeded, but it only fails if the expression still isn't true after the
96 // margin time passes.
97 #define EXPECT_TRUE_WAIT_MARGIN(ex, timeout, margin) \
98   do { \
99     bool res; \
100     WAIT_(ex, timeout, res); \
101     if (res) { \
102       break; \
103     } \
104     LOG(LS_WARNING) << "Expression " << #ex << " still not true after " << \
105         timeout << "ms; waiting an additional " << margin << "ms"; \
106     WAIT_(ex, margin, res); \
107     if (!res) { \
108       EXPECT_TRUE(ex); \
109     } \
110   } while (0);
111 
112 talk_base::Pathname GetTalkDirectory();
113 
114 #endif  // TALK_BASE_GUNIT_H_
115