Standardize usage of virtual/override/final specifiers in net/.
[chromium-blink-merge.git] / net / base / test_completion_callback.h
blobb7f8985c31703ee8ac75178203894465ddda84b9
1 // Copyright (c) 2012 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.
5 #ifndef NET_BASE_TEST_COMPLETION_CALLBACK_H_
6 #define NET_BASE_TEST_COMPLETION_CALLBACK_H_
8 #include "base/callback.h"
9 #include "base/compiler_specific.h"
10 #include "net/base/completion_callback.h"
11 #include "net/base/net_errors.h"
13 //-----------------------------------------------------------------------------
14 // completion callback helper
16 // A helper class for completion callbacks, designed to make it easy to run
17 // tests involving asynchronous operations. Just call WaitForResult to wait
18 // for the asynchronous operation to complete.
20 // NOTE: Since this runs a message loop to wait for the completion callback,
21 // there could be other side-effects resulting from WaitForResult. For this
22 // reason, this class is probably not ideal for a general application.
25 namespace net {
27 class IOBuffer;
29 namespace internal {
31 class TestCompletionCallbackBaseInternal {
32 public:
33 bool have_result() const { return have_result_; }
35 protected:
36 TestCompletionCallbackBaseInternal();
37 virtual ~TestCompletionCallbackBaseInternal();
39 void DidSetResult();
40 void WaitForResult();
42 bool have_result_;
43 bool waiting_for_result_;
45 private:
46 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBaseInternal);
49 template <typename R>
50 class TestCompletionCallbackTemplate
51 : public TestCompletionCallbackBaseInternal {
52 public:
53 virtual ~TestCompletionCallbackTemplate() override {}
55 R WaitForResult() {
56 TestCompletionCallbackBaseInternal::WaitForResult();
57 return result_;
60 R GetResult(R result) {
61 if (net::ERR_IO_PENDING != result)
62 return result;
63 return WaitForResult();
66 protected:
67 // Override this method to gain control as the callback is running.
68 virtual void SetResult(R result) {
69 result_ = result;
70 DidSetResult();
73 TestCompletionCallbackTemplate() : result_(R()) {}
74 R result_;
76 private:
77 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackTemplate);
80 } // namespace internal
82 class TestClosure
83 : public internal::TestCompletionCallbackBaseInternal {
84 public:
85 using internal::TestCompletionCallbackBaseInternal::WaitForResult;
87 TestClosure();
88 ~TestClosure() override;
90 const base::Closure& closure() const { return closure_; }
92 private:
93 const base::Closure closure_;
95 DISALLOW_COPY_AND_ASSIGN(TestClosure);
98 // Base class overridden by custom implementations of TestCompletionCallback.
99 typedef internal::TestCompletionCallbackTemplate<int>
100 TestCompletionCallbackBase;
102 typedef internal::TestCompletionCallbackTemplate<int64>
103 TestInt64CompletionCallbackBase;
105 class TestCompletionCallback : public TestCompletionCallbackBase {
106 public:
107 TestCompletionCallback();
108 ~TestCompletionCallback() override;
110 const CompletionCallback& callback() const { return callback_; }
112 private:
113 const CompletionCallback callback_;
115 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback);
118 class TestInt64CompletionCallback : public TestInt64CompletionCallbackBase {
119 public:
120 TestInt64CompletionCallback();
121 ~TestInt64CompletionCallback() override;
123 const Int64CompletionCallback& callback() const { return callback_; }
125 private:
126 const Int64CompletionCallback callback_;
128 DISALLOW_COPY_AND_ASSIGN(TestInt64CompletionCallback);
131 // Makes sure that the buffer is not referenced when the callback runs.
132 class ReleaseBufferCompletionCallback: public TestCompletionCallback {
133 public:
134 explicit ReleaseBufferCompletionCallback(IOBuffer* buffer);
135 ~ReleaseBufferCompletionCallback() override;
137 private:
138 void SetResult(int result) override;
140 IOBuffer* buffer_;
141 DISALLOW_COPY_AND_ASSIGN(ReleaseBufferCompletionCallback);
144 } // namespace net
146 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_