Temporarily make lastchange use only the git hash.
[chromium-blink-merge.git] / net / base / test_completion_callback.h
blob4a0afe13359416f363f6ac1b709853c16caf625b
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/compiler_specific.h"
9 #include "base/tuple.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 void DidSetResult();
38 void WaitForResult();
40 bool have_result_;
41 bool waiting_for_result_;
43 private:
44 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBaseInternal);
47 template <typename R>
48 class TestCompletionCallbackTemplate
49 : public TestCompletionCallbackBaseInternal {
50 public:
51 virtual ~TestCompletionCallbackTemplate() {}
53 R WaitForResult() {
54 TestCompletionCallbackBaseInternal::WaitForResult();
55 return result_;
58 R GetResult(R result) {
59 if (net::ERR_IO_PENDING != result)
60 return result;
61 return WaitForResult();
64 protected:
65 // Override this method to gain control as the callback is running.
66 virtual void SetResult(R result) {
67 result_ = result;
68 DidSetResult();
71 TestCompletionCallbackTemplate() : result_(R()) {}
72 R result_;
74 private:
75 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackTemplate);
78 } // namespace internal
80 // Base class overridden by custom implementations of TestCompletionCallback.
81 typedef internal::TestCompletionCallbackTemplate<int>
82 TestCompletionCallbackBase;
84 typedef internal::TestCompletionCallbackTemplate<int64>
85 TestInt64CompletionCallbackBase;
87 class TestCompletionCallback : public TestCompletionCallbackBase {
88 public:
89 TestCompletionCallback();
90 virtual ~TestCompletionCallback();
92 const CompletionCallback& callback() const { return callback_; }
94 private:
95 const CompletionCallback callback_;
97 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback);
100 class TestInt64CompletionCallback : public TestInt64CompletionCallbackBase {
101 public:
102 TestInt64CompletionCallback();
103 virtual ~TestInt64CompletionCallback();
105 const Int64CompletionCallback& callback() const { return callback_; }
107 private:
108 const Int64CompletionCallback callback_;
110 DISALLOW_COPY_AND_ASSIGN(TestInt64CompletionCallback);
113 // Makes sure that the buffer is not referenced when the callback runs.
114 class ReleaseBufferCompletionCallback: public TestCompletionCallback {
115 public:
116 explicit ReleaseBufferCompletionCallback(IOBuffer* buffer);
117 virtual ~ReleaseBufferCompletionCallback();
119 private:
120 virtual void SetResult(int result) OVERRIDE;
122 IOBuffer* buffer_;
123 DISALLOW_COPY_AND_ASSIGN(ReleaseBufferCompletionCallback);
126 } // namespace net
128 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_