Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/media/mtransport/test/nrappkit_unittest.cpp
Line
Count
Source (jump to first uncovered line)
1
2
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3
/* vim: set ts=2 et sw=2 tw=80: */
4
/* This Source Code Form is subject to the terms of the Mozilla Public
5
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
6
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8
// Original author: ekr@rtfm.com
9
#include <iostream>
10
11
#include "nsThreadUtils.h"
12
#include "nsXPCOM.h"
13
14
// nrappkit includes
15
extern "C" {
16
#include "nr_api.h"
17
#include "async_timer.h"
18
}
19
20
#include "runnable_utils.h"
21
22
#define GTEST_HAS_RTTI 0
23
#include "gtest/gtest.h"
24
#include "gtest_utils.h"
25
26
using namespace mozilla;
27
28
namespace {
29
30
class TimerTest : public MtransportTest {
31
 public:
32
0
  TimerTest() : MtransportTest(), handle_(nullptr), fired_(false) {}
33
0
  virtual ~TimerTest() {}
34
35
0
  int ArmTimer(int timeout) {
36
0
    int ret;
37
0
38
0
    test_utils_->sts_target()->Dispatch(
39
0
        WrapRunnableRet(&ret, this, &TimerTest::ArmTimer_w, timeout),
40
0
        NS_DISPATCH_SYNC);
41
0
42
0
    return ret;
43
0
  }
44
45
0
  int ArmCancelTimer(int timeout) {
46
0
    int ret;
47
0
48
0
    test_utils_->sts_target()->Dispatch(
49
0
        WrapRunnableRet(&ret, this, &TimerTest::ArmCancelTimer_w, timeout),
50
0
        NS_DISPATCH_SYNC);
51
0
52
0
    return ret;
53
0
  }
54
55
0
  int ArmTimer_w(int timeout) {
56
0
    return NR_ASYNC_TIMER_SET(timeout, cb, this, &handle_);
57
0
  }
58
59
0
  int ArmCancelTimer_w(int timeout) {
60
0
    int r;
61
0
    r = ArmTimer_w(timeout);
62
0
    if (r)
63
0
      return r;
64
0
65
0
    return CancelTimer_w();
66
0
  }
67
68
0
  int CancelTimer() {
69
0
    int ret;
70
0
71
0
    test_utils_->sts_target()->Dispatch(
72
0
        WrapRunnableRet(&ret, this, &TimerTest::CancelTimer_w),
73
0
        NS_DISPATCH_SYNC);
74
0
75
0
    return ret;
76
0
  }
77
78
0
  int CancelTimer_w() {
79
0
    return NR_async_timer_cancel(handle_);
80
0
  }
81
82
0
  int Schedule() {
83
0
    int ret;
84
0
85
0
    test_utils_->sts_target()->Dispatch(
86
0
        WrapRunnableRet(&ret, this, &TimerTest::Schedule_w),
87
0
        NS_DISPATCH_SYNC);
88
0
89
0
    return ret;
90
0
  }
91
92
0
  int Schedule_w() {
93
0
    NR_ASYNC_SCHEDULE(cb, this);
94
0
95
0
    return 0;
96
0
  }
97
98
99
0
  static void cb(NR_SOCKET r, int how, void *arg) {
100
0
    std::cerr << "Timer fired " << std::endl;
101
0
102
0
    TimerTest *t = static_cast<TimerTest *>(arg);
103
0
104
0
    t->fired_ = true;
105
0
  }
106
107
 protected:
108
  void *handle_;
109
  bool fired_;
110
};
111
}
112
113
0
TEST_F(TimerTest, SimpleTimer) {
114
0
  ArmTimer(100);
115
0
  ASSERT_TRUE_WAIT(fired_, 1000);
116
0
}
117
118
0
TEST_F(TimerTest, CancelTimer) {
119
0
  ArmTimer(1000);
120
0
  CancelTimer();
121
0
  PR_Sleep(2000);
122
0
  ASSERT_FALSE(fired_);
123
0
}
124
125
0
TEST_F(TimerTest, CancelTimer0) {
126
0
  ArmCancelTimer(0);
127
0
  PR_Sleep(100);
128
0
  ASSERT_FALSE(fired_);
129
0
}
130
131
0
TEST_F(TimerTest, ScheduleTest) {
132
0
  Schedule();
133
0
  ASSERT_TRUE_WAIT(fired_, 1000);
134
0
}