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 "base/tuple.h"
11 #include "net/base/completion_callback.h"
12 #include "net/base/net_errors.h"
14 //-----------------------------------------------------------------------------
15 // completion callback helper
17 // A helper class for completion callbacks, designed to make it easy to run
18 // tests involving asynchronous operations. Just call WaitForResult to wait
19 // for the asynchronous operation to complete.
21 // NOTE: Since this runs a message loop to wait for the completion callback,
22 // there could be other side-effects resulting from WaitForResult. For this
23 // reason, this class is probably not ideal for a general application.
32 class TestCompletionCallbackBaseInternal {
34 bool have_result() const { return have_result_; }
37 TestCompletionCallbackBaseInternal();
38 virtual ~TestCompletionCallbackBaseInternal();
44 bool waiting_for_result_;
47 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBaseInternal);
51 class TestCompletionCallbackTemplate
52 : public TestCompletionCallbackBaseInternal {
54 virtual ~TestCompletionCallbackTemplate() override {}
57 TestCompletionCallbackBaseInternal::WaitForResult();
61 R GetResult(R result) {
62 if (net::ERR_IO_PENDING != result)
64 return WaitForResult();
68 // Override this method to gain control as the callback is running.
69 virtual void SetResult(R result) {
74 TestCompletionCallbackTemplate() : result_(R()) {}
78 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackTemplate);
81 } // namespace internal
84 : public internal::TestCompletionCallbackBaseInternal {
86 using internal::TestCompletionCallbackBaseInternal::WaitForResult;
89 virtual ~TestClosure() override;
91 const base::Closure& closure() const { return closure_; }
94 const base::Closure closure_;
96 DISALLOW_COPY_AND_ASSIGN(TestClosure);
99 // Base class overridden by custom implementations of TestCompletionCallback.
100 typedef internal::TestCompletionCallbackTemplate<int>
101 TestCompletionCallbackBase;
103 typedef internal::TestCompletionCallbackTemplate<int64>
104 TestInt64CompletionCallbackBase;
106 class TestCompletionCallback : public TestCompletionCallbackBase {
108 TestCompletionCallback();
109 virtual ~TestCompletionCallback() override;
111 const CompletionCallback& callback() const { return callback_; }
114 const CompletionCallback callback_;
116 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback);
119 class TestInt64CompletionCallback : public TestInt64CompletionCallbackBase {
121 TestInt64CompletionCallback();
122 virtual ~TestInt64CompletionCallback() override;
124 const Int64CompletionCallback& callback() const { return callback_; }
127 const Int64CompletionCallback callback_;
129 DISALLOW_COPY_AND_ASSIGN(TestInt64CompletionCallback);
132 // Makes sure that the buffer is not referenced when the callback runs.
133 class ReleaseBufferCompletionCallback: public TestCompletionCallback {
135 explicit ReleaseBufferCompletionCallback(IOBuffer* buffer);
136 virtual ~ReleaseBufferCompletionCallback() override;
139 void SetResult(int result) override;
142 DISALLOW_COPY_AND_ASSIGN(ReleaseBufferCompletionCallback);
147 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_