1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/mtransport/test/nrappkit_unittest.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,122 @@ 1.4 + 1.5 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.6 +/* vim: set ts=2 et sw=2 tw=80: */ 1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.9 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.10 + 1.11 +// Original author: ekr@rtfm.com 1.12 +#include <iostream> 1.13 + 1.14 +#include "nsThreadUtils.h" 1.15 +#include "nsXPCOM.h" 1.16 + 1.17 +// nrappkit includes 1.18 +extern "C" { 1.19 +#include "nr_api.h" 1.20 +#include "async_timer.h" 1.21 +} 1.22 + 1.23 +#include "mtransport_test_utils.h" 1.24 +#include "runnable_utils.h" 1.25 + 1.26 +#define GTEST_HAS_RTTI 0 1.27 +#include "gtest/gtest.h" 1.28 +#include "gtest_utils.h" 1.29 + 1.30 +using namespace mozilla; 1.31 + 1.32 +MtransportTestUtils *test_utils; 1.33 + 1.34 +namespace { 1.35 + 1.36 +class TimerTest : public ::testing::Test { 1.37 + public: 1.38 + TimerTest() : handle_(nullptr), fired_(false) {} 1.39 + 1.40 + int ArmTimer(int timeout) { 1.41 + int ret; 1.42 + 1.43 + test_utils->sts_target()->Dispatch( 1.44 + WrapRunnableRet(this, &TimerTest::ArmTimer_w, timeout, &ret), 1.45 + NS_DISPATCH_SYNC); 1.46 + 1.47 + return ret; 1.48 + } 1.49 + 1.50 + int ArmTimer_w(int timeout) { 1.51 + return NR_ASYNC_TIMER_SET(timeout, cb, this, &handle_); 1.52 + } 1.53 + 1.54 + int CancelTimer() { 1.55 + int ret; 1.56 + 1.57 + test_utils->sts_target()->Dispatch( 1.58 + WrapRunnableRet(this, &TimerTest::CancelTimer_w, &ret), 1.59 + NS_DISPATCH_SYNC); 1.60 + 1.61 + return ret; 1.62 + } 1.63 + 1.64 + int CancelTimer_w() { 1.65 + return NR_async_timer_cancel(handle_); 1.66 + } 1.67 + 1.68 + int Schedule() { 1.69 + int ret; 1.70 + 1.71 + test_utils->sts_target()->Dispatch( 1.72 + WrapRunnableRet(this, &TimerTest::Schedule_w, &ret), 1.73 + NS_DISPATCH_SYNC); 1.74 + 1.75 + return ret; 1.76 + } 1.77 + 1.78 + int Schedule_w() { 1.79 + NR_ASYNC_SCHEDULE(cb, this); 1.80 + 1.81 + return 0; 1.82 + } 1.83 + 1.84 + 1.85 + static void cb(NR_SOCKET r, int how, void *arg) { 1.86 + std::cerr << "Timer fired " << std::endl; 1.87 + 1.88 + TimerTest *t = static_cast<TimerTest *>(arg); 1.89 + 1.90 + t->fired_ = true; 1.91 + } 1.92 + 1.93 + protected: 1.94 + void *handle_; 1.95 + bool fired_; 1.96 +}; 1.97 +} 1.98 + 1.99 +TEST_F(TimerTest, SimpleTimer) { 1.100 + ArmTimer(100); 1.101 + ASSERT_TRUE_WAIT(fired_, 1000); 1.102 +} 1.103 + 1.104 +TEST_F(TimerTest, CancelTimer) { 1.105 + ArmTimer(1000); 1.106 + CancelTimer(); 1.107 + PR_Sleep(2000); 1.108 + ASSERT_FALSE(fired_); 1.109 +} 1.110 + 1.111 +TEST_F(TimerTest, ScheduleTest) { 1.112 + Schedule(); 1.113 + ASSERT_TRUE_WAIT(fired_, 1000); 1.114 +} 1.115 + 1.116 +int main(int argc, char **argv) { 1.117 + test_utils = new MtransportTestUtils(); 1.118 + 1.119 + // Start the tests 1.120 + ::testing::InitGoogleTest(&argc, argv); 1.121 + 1.122 + int rv = RUN_ALL_TESTS(); 1.123 + delete test_utils; 1.124 + return rv; 1.125 +}