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.
31 class TestCompletionCallbackBaseInternal
{
33 bool have_result() const { return have_result_
; }
36 TestCompletionCallbackBaseInternal();
37 virtual ~TestCompletionCallbackBaseInternal();
43 bool waiting_for_result_
;
46 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBaseInternal
);
50 class TestCompletionCallbackTemplate
51 : public TestCompletionCallbackBaseInternal
{
53 virtual ~TestCompletionCallbackTemplate() override
{}
56 TestCompletionCallbackBaseInternal::WaitForResult();
60 R
GetResult(R result
) {
61 if (net::ERR_IO_PENDING
!= result
)
63 return WaitForResult();
67 // Override this method to gain control as the callback is running.
68 virtual void SetResult(R result
) {
73 TestCompletionCallbackTemplate() : result_(R()) {}
77 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackTemplate
);
80 } // namespace internal
83 : public internal::TestCompletionCallbackBaseInternal
{
85 using internal::TestCompletionCallbackBaseInternal::WaitForResult
;
88 ~TestClosure() override
;
90 const base::Closure
& closure() const { return closure_
; }
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
{
107 TestCompletionCallback();
108 ~TestCompletionCallback() override
;
110 const CompletionCallback
& callback() const { return callback_
; }
113 const CompletionCallback callback_
;
115 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback
);
118 class TestInt64CompletionCallback
: public TestInt64CompletionCallbackBase
{
120 TestInt64CompletionCallback();
121 ~TestInt64CompletionCallback() override
;
123 const Int64CompletionCallback
& callback() const { return callback_
; }
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
{
134 explicit ReleaseBufferCompletionCallback(IOBuffer
* buffer
);
135 ~ReleaseBufferCompletionCallback() override
;
138 void SetResult(int result
) override
;
141 DISALLOW_COPY_AND_ASSIGN(ReleaseBufferCompletionCallback
);
146 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_