summaryrefslogtreecommitdiffstats
path: root/remoting/jingle_glue/jingle_thread_unittest.cc
blob: e73c8b2b067d47ffedf638b6b57e0d3743f907ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/message_loop.h"
#include "base/time.h"
#include "base/synchronization/waitable_event.h"
#include "remoting/jingle_glue/jingle_thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace remoting {

class MockCallback {
 public:
  MOCK_METHOD0(Run, void());
};

namespace {

// Delay used to test delayed tasks. Shouldn't be too big, so that we don't
// slow down the test, yet, should be big enough to be measurable.
int kDelayMs = 50;  // 0.05 s.
int kDelayTimeoutMs = 10000;  // 10 s.

}  // namespace

TEST(JingleThreadTest, PostTask) {
  JingleThread thread;
  MockCallback task;
  EXPECT_CALL(task, Run());

  thread.Start();
  thread.message_loop()->PostTask(
      FROM_HERE, base::Bind(&MockCallback::Run, base::Unretained(&task)));
  thread.Stop();
}

TEST(JingleThreadTest, PostNonNestableTask) {
  JingleThread thread;
  MockCallback task;
  EXPECT_CALL(task, Run());

  thread.Start();
  thread.message_loop()->PostNonNestableTask(
      FROM_HERE, base::Bind(&MockCallback::Run, base::Unretained(&task)));
  thread.Stop();
}

ACTION_P(SignalEvent, event) {
  event->Signal();
}

TEST(JingleThreadTest, PostDelayedTask) {
  JingleThread thread;
  MockCallback task;
  base::WaitableEvent event(true, false);
  EXPECT_CALL(task, Run()).WillOnce(SignalEvent(&event));

  thread.Start();
  base::Time start = base::Time::Now();
  thread.message_loop()->PostDelayedTask(
      FROM_HERE, base::Bind(&MockCallback::Run, base::Unretained(&task)),
      base::TimeDelta::FromMilliseconds(kDelayMs));
  event.TimedWait(base::TimeDelta::FromMilliseconds(kDelayTimeoutMs));
  base::Time end = base::Time::Now();
  thread.Stop();

  EXPECT_GE((end - start).InMillisecondsRoundedUp(), kDelayMs);
}

TEST(JingleThreadTest, PostNonNestableDelayedTask) {
  JingleThread thread;
  MockCallback task;
  base::WaitableEvent event(true, false);
  EXPECT_CALL(task, Run()).WillOnce(SignalEvent(&event));

  thread.Start();
  base::Time start = base::Time::Now();
  thread.message_loop()->PostNonNestableDelayedTask(
      FROM_HERE, base::Bind(&MockCallback::Run, base::Unretained(&task)),
      base::TimeDelta::FromMilliseconds(kDelayMs));
  event.TimedWait(base::TimeDelta::FromMilliseconds(kDelayTimeoutMs));
  base::Time end = base::Time::Now();
  thread.Stop();

  EXPECT_GE((end - start).InMillisecondsRoundedUp(), kDelayMs);
}

}  // namespace remoting