Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(556)

Side by Side Diff: remoting/host/ack_or_timeout_reporter_unittest.cc

Issue 719983002: Reporting of policy errors via host-offline-reason: part 3 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@hor-nohoststatussender
Patch Set: Rebasing... Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "remoting/host/ack_or_timeout_reporter.h"
6
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/sequenced_task_runner.h"
11 #include "base/time/time.h"
12 #include "remoting/host/mock_callback.h"
13
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace remoting {
18
19 class MockSequencedTaskRunner : public base::SequencedTaskRunner {
20 public:
21 MOCK_METHOD3(PostDelayedTask, bool(
22 const tracked_objects::Location&,
23 const base::Closure&,
24 base::TimeDelta));
25
26 MOCK_CONST_METHOD0(RunsTasksOnCurrentThread, bool());
27
28 MOCK_METHOD3(PostNonNestableDelayedTask, bool(
29 const tracked_objects::Location&,
30 const base::Closure&,
31 base::TimeDelta));
32
33 private:
34 ~MockSequencedTaskRunner() { }
35 };
36
37 const base::TimeDelta kTestTimeout = base::TimeDelta::FromSeconds(123);
38
39 static void CallReportAckOrTimeoutAndCaptureCallbacks(
40 const base::Callback<void(AckOrTimeout)>& ack_or_timeout_callback,
41 /* out */ base::Closure* captured_ack_callback,
42 /* out */ base::Closure* captured_timeout_callback) {
43 // Setup mocks.
44 MockCallback<void(const base::Closure&)> mock_function_that_acks;
45 scoped_refptr<MockSequencedTaskRunner> mock_task_runner(
46 new MockSequencedTaskRunner());
47
48 // Capture |captured_ack_callback| and |captured_timeout_callback|.
49 EXPECT_CALL(mock_function_that_acks, Run(testing::_))
50 .WillOnce(testing::SaveArg<0>(captured_ack_callback));
51 EXPECT_CALL(*mock_task_runner,
52 PostDelayedTask(testing::_, testing::_, kTestTimeout))
53 .WillOnce(testing::DoAll(
54 testing::SaveArg<1>(captured_timeout_callback),
55 testing::Return(true)));
56
57 // Call function-under-test.
58 ReportAckOrTimeout(
59 mock_function_that_acks.GetCallback(),
60 kTestTimeout,
61 mock_task_runner,
62 ack_or_timeout_callback);
63 }
64
65 TEST(AckOrTimeoutReporterTest, FirstAckCalledAsynchronously) {
66 MockCallback<void(AckOrTimeout)> mock_ack_or_timeout_callback;
67 base::Closure captured_ack_callback;
68 base::Closure captured_timeout_callback;
69
70 // |mock_ack_or_timeout_callback| should not be called until
71 // |captured_ack_callback| or |captured_timeout_callback| is called.
72 EXPECT_CALL(mock_ack_or_timeout_callback, Run(testing::_)).Times(0);
73
74 CallReportAckOrTimeoutAndCaptureCallbacks(
75 mock_ack_or_timeout_callback.GetCallback(),
76 &captured_ack_callback,
77 &captured_timeout_callback);
78
79 {
80 // |mock_ack_or_timeout_callback| should be called as soon as
81 // |captured_ack_callback| runs.
82 EXPECT_CALL(mock_ack_or_timeout_callback, Run(AckOrTimeout::Ack));
83 captured_ack_callback.Run();
84 }
85
86 // |ack_or_timeout_callback| should be dropped after being called once.
87 // This is expectation is depended on by |MinimumHeartbeatSupporter| to
88 // manage its lifetime (i.e. release references to self after getting an
89 // ack).
90 EXPECT_FALSE(mock_ack_or_timeout_callback.HasRemainingCallbacks());
91
92 // It should be safe to call the callbacks multiple times.
93 captured_ack_callback.Run();
94 captured_timeout_callback.Run();
95 }
96
97 TEST(AckOrTimeoutReporterTest, FirstTimeoutCalledAsynchronously) {
98 MockCallback<void(AckOrTimeout)> mock_ack_or_timeout_callback;
99 base::Closure captured_ack_callback;
100 base::Closure captured_timeout_callback;
101
102 // |mock_ack_or_timeout_callback| should not be called until
103 // |captured_ack_callback| or |captured_timeout_callback| is called.
104 EXPECT_CALL(mock_ack_or_timeout_callback, Run(testing::_)).Times(0);
105
106 CallReportAckOrTimeoutAndCaptureCallbacks(
107 mock_ack_or_timeout_callback.GetCallback(),
108 &captured_ack_callback,
109 &captured_timeout_callback);
110
111 {
112 // |mock_ack_or_timeout_callback| should be called as soon as
113 // |captured_ack_callback| runs.
114 EXPECT_CALL(mock_ack_or_timeout_callback, Run(AckOrTimeout::Timeout));
115 captured_timeout_callback.Run();
116 }
117
118 // |ack_or_timeout_callback| should be dropped after being called once.
119 // This is expectation is depended on by |MinimumHeartbeatSupporter| to
120 // manage its lifetime (i.e. release references to self after getting an
121 // ack).
122 EXPECT_FALSE(mock_ack_or_timeout_callback.HasRemainingCallbacks());
123
124 // It should be safe to call the callbacks multiple times.
125 captured_ack_callback.Run();
126 captured_timeout_callback.Run();
127 }
128
129 } // namespace remoting
130
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698