OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" |
5 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
6 #include "base/time.h" | 8 #include "base/time.h" |
7 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
8 #include "remoting/jingle_glue/jingle_thread.h" | 10 #include "remoting/jingle_glue/jingle_thread.h" |
9 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 | 13 |
12 namespace remoting { | 14 namespace remoting { |
13 | 15 |
14 class MockTask : public Task { | 16 class MockCallback { |
15 public: | 17 public: |
16 MOCK_METHOD0(Run, void()); | 18 MOCK_METHOD0(Run, void()); |
17 }; | 19 }; |
18 | 20 |
19 namespace { | 21 namespace { |
| 22 |
20 // Delay used to test delayed tasks. Shouldn't be too big, so that we don't | 23 // Delay used to test delayed tasks. Shouldn't be too big, so that we don't |
21 // slow down the test, yet, should be big enough to be measurable. | 24 // slow down the test, yet, should be big enough to be measurable. |
22 int kDelayMs = 50; // 0.05 s. | 25 int kDelayMs = 50; // 0.05 s. |
23 int kDelayTimeoutMs = 10000; // 10 s. | 26 int kDelayTimeoutMs = 10000; // 10 s. |
| 27 |
24 } // namespace | 28 } // namespace |
25 | 29 |
26 TEST(JingleThreadTest, PostTask) { | 30 TEST(JingleThreadTest, PostTask) { |
27 JingleThread thread; | 31 JingleThread thread; |
28 MockTask* task = new MockTask(); | 32 MockCallback task; |
29 EXPECT_CALL(*task, Run()); | 33 EXPECT_CALL(task, Run()); |
30 | 34 |
31 thread.Start(); | 35 thread.Start(); |
32 thread.message_loop()->PostTask(FROM_HERE, task); | 36 thread.message_loop()->PostTask( |
| 37 FROM_HERE, base::Bind(&MockCallback::Run, base::Unretained(&task))); |
33 thread.Stop(); | 38 thread.Stop(); |
34 } | 39 } |
35 | 40 |
36 TEST(JingleThreadTest, PostNonNestableTask) { | 41 TEST(JingleThreadTest, PostNonNestableTask) { |
37 JingleThread thread; | 42 JingleThread thread; |
38 MockTask* task = new MockTask(); | 43 MockCallback task; |
39 EXPECT_CALL(*task, Run()); | 44 EXPECT_CALL(task, Run()); |
40 | 45 |
41 thread.Start(); | 46 thread.Start(); |
42 thread.message_loop()->PostNonNestableTask(FROM_HERE, task); | 47 thread.message_loop()->PostNonNestableTask( |
| 48 FROM_HERE, base::Bind(&MockCallback::Run, base::Unretained(&task))); |
43 thread.Stop(); | 49 thread.Stop(); |
44 } | 50 } |
45 | 51 |
46 ACTION_P(SignalEvent, event) { | 52 ACTION_P(SignalEvent, event) { |
47 event->Signal(); | 53 event->Signal(); |
48 } | 54 } |
49 | 55 |
50 TEST(JingleThreadTest, PostDelayedTask) { | 56 TEST(JingleThreadTest, PostDelayedTask) { |
51 JingleThread thread; | 57 JingleThread thread; |
52 MockTask* task = new MockTask(); | 58 MockCallback task; |
53 base::WaitableEvent event(true, false); | 59 base::WaitableEvent event(true, false); |
54 EXPECT_CALL(*task, Run()).WillOnce(SignalEvent(&event)); | 60 EXPECT_CALL(task, Run()).WillOnce(SignalEvent(&event)); |
55 | 61 |
56 thread.Start(); | 62 thread.Start(); |
57 base::Time start = base::Time::Now(); | 63 base::Time start = base::Time::Now(); |
58 thread.message_loop()->PostDelayedTask(FROM_HERE, task, kDelayMs); | 64 thread.message_loop()->PostDelayedTask( |
| 65 FROM_HERE, base::Bind(&MockCallback::Run, base::Unretained(&task)), |
| 66 kDelayMs); |
59 event.TimedWait(base::TimeDelta::FromMilliseconds(kDelayTimeoutMs)); | 67 event.TimedWait(base::TimeDelta::FromMilliseconds(kDelayTimeoutMs)); |
60 base::Time end = base::Time::Now(); | 68 base::Time end = base::Time::Now(); |
61 thread.Stop(); | 69 thread.Stop(); |
62 | 70 |
63 EXPECT_GE((end - start).InMillisecondsRoundedUp(), kDelayMs); | 71 EXPECT_GE((end - start).InMillisecondsRoundedUp(), kDelayMs); |
64 } | 72 } |
65 | 73 |
66 TEST(JingleThreadTest, PostNonNestableDelayedTask) { | 74 TEST(JingleThreadTest, PostNonNestableDelayedTask) { |
67 JingleThread thread; | 75 JingleThread thread; |
68 MockTask* task = new MockTask(); | 76 MockCallback task; |
69 base::WaitableEvent event(true, false); | 77 base::WaitableEvent event(true, false); |
70 EXPECT_CALL(*task, Run()).WillOnce(SignalEvent(&event)); | 78 EXPECT_CALL(task, Run()).WillOnce(SignalEvent(&event)); |
71 | 79 |
72 thread.Start(); | 80 thread.Start(); |
73 base::Time start = base::Time::Now(); | 81 base::Time start = base::Time::Now(); |
74 thread.message_loop()->PostNonNestableDelayedTask(FROM_HERE, task, kDelayMs); | 82 thread.message_loop()->PostNonNestableDelayedTask( |
| 83 FROM_HERE, base::Bind(&MockCallback::Run, base::Unretained(&task)), |
| 84 kDelayMs); |
75 event.TimedWait(base::TimeDelta::FromMilliseconds(kDelayTimeoutMs)); | 85 event.TimedWait(base::TimeDelta::FromMilliseconds(kDelayTimeoutMs)); |
76 base::Time end = base::Time::Now(); | 86 base::Time end = base::Time::Now(); |
77 thread.Stop(); | 87 thread.Stop(); |
78 | 88 |
79 EXPECT_GE((end - start).InMillisecondsRoundedUp(), kDelayMs); | 89 EXPECT_GE((end - start).InMillisecondsRoundedUp(), kDelayMs); |
80 } | 90 } |
81 | 91 |
82 } // namespace remoting | 92 } // namespace remoting |
OLD | NEW |