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

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: Trying to see how things look without mock_callback.h 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"
Lambros 2014/12/05 22:19:48 Don't need here, already in ack_or_timeout_reporte
Łukasz Anforowicz 2014/12/05 23:59:28 Done.
11 #include "base/time/time.h"
12
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace remoting {
17
18 class MockSequencedTaskRunner : public base::SequencedTaskRunner {
19 public:
20 MOCK_METHOD3(PostDelayedTask,
21 bool(const tracked_objects::Location&,
22 const base::Closure&,
23 base::TimeDelta));
24
25 MOCK_CONST_METHOD0(RunsTasksOnCurrentThread, bool());
26
27 MOCK_METHOD3(PostNonNestableDelayedTask,
28 bool(const tracked_objects::Location&,
29 const base::Closure&,
30 base::TimeDelta));
31
32 private:
33 ~MockSequencedTaskRunner() {}
34 };
35
36 class MockFunctionThatAcks {
37 public:
38 MOCK_CONST_METHOD1(Run, void(const base::Closure&));
39 };
40
41 class MockAckOrTimeoutCallback {
42 public:
43 MOCK_CONST_METHOD1(Run, void(AckOrTimeout));
44 };
45
46 const base::TimeDelta kTestTimeout = base::TimeDelta::FromSeconds(123);
47
48 static void CallReportAckOrTimeoutAndCaptureCallbacks(
49 const base::Callback<void(AckOrTimeout)>& ack_or_timeout_callback,
50 /* out */ base::Closure* captured_ack_callback,
51 /* out */ base::Closure* captured_timeout_callback) {
52 // Setup mocks.
53 MockFunctionThatAcks mock_function_that_acks;
54 scoped_refptr<MockSequencedTaskRunner> mock_task_runner(
55 new MockSequencedTaskRunner());
56
57 // Capture |captured_ack_callback| and |captured_timeout_callback|.
58 EXPECT_CALL(mock_function_that_acks, Run(testing::_))
Lambros 2014/12/05 22:19:48 nit: We normally add using... declarations for all
Łukasz Anforowicz 2014/12/05 23:59:28 Done.
59 .WillOnce(testing::SaveArg<0>(captured_ack_callback));
60 EXPECT_CALL(*mock_task_runner,
61 PostDelayedTask(testing::_, testing::_, kTestTimeout))
62 .WillOnce(testing::DoAll(testing::SaveArg<1>(captured_timeout_callback),
63 testing::Return(true)));
64
65 // Call function-under-test.
66 ReportAckOrTimeout(base::Bind(&MockFunctionThatAcks::Run,
67 base::Unretained(&mock_function_that_acks)),
68 kTestTimeout, mock_task_runner, ack_or_timeout_callback);
69 }
70
71 TEST(AckOrTimeoutReporterTest, FirstAckCalledAsynchronously) {
72 MockAckOrTimeoutCallback mock_ack_or_timeout_callback;
73 base::WeakPtrFactory<MockAckOrTimeoutCallback> weak_factory(
74 &mock_ack_or_timeout_callback);
75
76 base::Closure captured_ack_callback;
77 base::Closure captured_timeout_callback;
78
79 // |mock_ack_or_timeout_callback| should not be called until
80 // |captured_ack_callback| or |captured_timeout_callback| is called.
81 EXPECT_CALL(mock_ack_or_timeout_callback, Run(testing::_)).Times(0);
82
83 CallReportAckOrTimeoutAndCaptureCallbacks(
84 base::Bind(&MockAckOrTimeoutCallback::Run, weak_factory.GetWeakPtr()),
85 &captured_ack_callback, &captured_timeout_callback);
86
87 {
88 // |mock_ack_or_timeout_callback| should be called as soon as
89 // |captured_ack_callback| runs.
90 EXPECT_CALL(mock_ack_or_timeout_callback, Run(AckOrTimeout::Ack));
91 captured_ack_callback.Run();
92 }
93
94 // |ack_or_timeout_callback| should be dropped after being called once.
95 // This is expectation is depended on by |MinimumHeartbeatSupporter| to
Lambros 2014/12/05 22:19:48 nit: This expectation...
Łukasz Anforowicz 2014/12/05 23:59:28 Done.
96 // manage its lifetime (i.e. release references to self after getting an
97 // ack).
98 EXPECT_FALSE(weak_factory.HasWeakPtrs());
99
100 // It should be safe to call the callbacks multiple times.
Lambros 2014/12/05 22:19:48 Is this testing anything - there are no expectatio
Łukasz Anforowicz 2014/12/05 23:59:28 I'll add a comment. This tests 1) that ack_or_tim
101 captured_ack_callback.Run();
102 captured_timeout_callback.Run();
103 }
104
105 TEST(AckOrTimeoutReporterTest, FirstTimeoutCalledAsynchronously) {
106 MockAckOrTimeoutCallback mock_ack_or_timeout_callback;
107 base::WeakPtrFactory<MockAckOrTimeoutCallback> weak_factory(
108 &mock_ack_or_timeout_callback);
109
110 base::Closure captured_ack_callback;
111 base::Closure captured_timeout_callback;
112
113 // |mock_ack_or_timeout_callback| should not be called until
114 // |captured_ack_callback| or |captured_timeout_callback| is called.
115 EXPECT_CALL(mock_ack_or_timeout_callback, Run(testing::_)).Times(0);
116
117 CallReportAckOrTimeoutAndCaptureCallbacks(
118 base::Bind(&MockAckOrTimeoutCallback::Run, weak_factory.GetWeakPtr()),
119 &captured_ack_callback, &captured_timeout_callback);
120
121 {
122 // |mock_ack_or_timeout_callback| should be called as soon as
123 // |captured_ack_callback| runs.
124 EXPECT_CALL(mock_ack_or_timeout_callback, Run(AckOrTimeout::Timeout));
125 captured_timeout_callback.Run();
126 }
127
128 // |ack_or_timeout_callback| should be dropped after being called once.
Lambros 2014/12/05 22:19:48 I think you can remove this? It's just a duplicate
Łukasz Anforowicz 2014/12/05 23:59:28 I don't see how this is a duplicate :-/
Lambros 2014/12/06 01:25:05 This is the same comment as on line 99 (in patch s
129 // This is expectation is depended on by |MinimumHeartbeatSupporter| to
130 // manage its lifetime (i.e. release references to self after getting an
131 // ack).
132 EXPECT_FALSE(weak_factory.HasWeakPtrs());
133
134 // It should be safe to call the callbacks multiple times.
135 captured_ack_callback.Run();
136 captured_timeout_callback.Run();
137 }
138
139 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698