15976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org/*
25976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * libjingle
35976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Copyright 2004--2008, Google Inc.
45976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
55976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Redistribution and use in source and binary forms, with or without
65976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * modification, are permitted provided that the following conditions are met:
75976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
85976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
95976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     this list of conditions and the following disclaimer.
105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     this list of conditions and the following disclaimer in the documentation
125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     and/or other materials provided with the distribution.
135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  3. The name of the author may not be used to endorse or promote products
145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     derived from this software without specific prior written permission.
155976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
165976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
175976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
185976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
195976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
205976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
215976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
225976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
235976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
245976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
255976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
265976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org */
275976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
285976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#ifndef TALK_BASE_GUNIT_H_
295976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define TALK_BASE_GUNIT_H_
305976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
315976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/logging.h"
325976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/thread.h"
335976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#if defined(ANDROID) || defined(GTEST_RELATIVE_PATH)
345976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "gtest/gtest.h"
355976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#else
365976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "testing/base/public/gunit.h"
375976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#endif
385976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
395976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// forward declarations
405976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgnamespace talk_base {
415976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgclass Pathname;
425976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
435976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
445976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Wait until "ex" is true, or "timeout" expires.
455976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define WAIT(ex, timeout) \
465976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  for (uint32 start = talk_base::Time(); \
475976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      !(ex) && talk_base::Time() < start + timeout;) \
485976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    talk_base::Thread::Current()->ProcessMessages(1);
495976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
505976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// This returns the result of the test in res, so that we don't re-evaluate
515976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// the expression in the XXXX_WAIT macros below, since that causes problems
525976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// when the expression is only true the first time you check it.
535976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define WAIT_(ex, timeout, res) \
545976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  do { \
555976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    uint32 start = talk_base::Time(); \
565976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    res = (ex); \
575976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    while (!res && talk_base::Time() < start + timeout) { \
585976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      talk_base::Thread::Current()->ProcessMessages(1); \
595976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      res = (ex); \
605976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    } \
615976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  } while (0);
625976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
635976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// The typical EXPECT_XXXX and ASSERT_XXXXs, but done until true or a timeout.
645976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define EXPECT_TRUE_WAIT(ex, timeout) \
655976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  do { \
665976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    bool res; \
675976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    WAIT_(ex, timeout, res); \
685976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    if (!res) EXPECT_TRUE(ex); \
695976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  } while (0);
705976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
715976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define EXPECT_EQ_WAIT(v1, v2, timeout) \
725976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  do { \
735976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    bool res; \
745976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    WAIT_(v1 == v2, timeout, res); \
755976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    if (!res) EXPECT_EQ(v1, v2); \
765976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  } while (0);
775976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
785976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define ASSERT_TRUE_WAIT(ex, timeout) \
795976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  do { \
805976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    bool res; \
815976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    WAIT_(ex, timeout, res); \
825976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    if (!res) ASSERT_TRUE(ex); \
835976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  } while (0);
845976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
855976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define ASSERT_EQ_WAIT(v1, v2, timeout) \
865976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  do { \
875976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    bool res; \
885976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    WAIT_(v1 == v2, timeout, res); \
895976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    if (!res) ASSERT_EQ(v1, v2); \
905976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  } while (0);
915976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
925976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Version with a "soft" timeout and a margin. This logs if the timeout is
935976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// exceeded, but it only fails if the expression still isn't true after the
945976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// margin time passes.
955976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define EXPECT_TRUE_WAIT_MARGIN(ex, timeout, margin) \
965976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  do { \
975976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    bool res; \
985976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    WAIT_(ex, timeout, res); \
995976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    if (res) { \
1005976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      break; \
1015976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    } \
1025976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    LOG(LS_WARNING) << "Expression " << #ex << " still not true after " << \
1035976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org        timeout << "ms; waiting an additional " << margin << "ms"; \
1045976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    WAIT_(ex, margin, res); \
1055976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    if (!res) { \
1065976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      EXPECT_TRUE(ex); \
1075976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    } \
1085976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  } while (0);
1095976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgtalk_base::Pathname GetTalkDirectory();
1115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#endif  // TALK_BASE_GUNIT_H_
113