| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef NET_BASE_TEST_COMPLETION_CALLBACK_H_ | |
| 6 #define NET_BASE_TEST_COMPLETION_CALLBACK_H_ | |
| 7 | |
| 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" | |
| 12 | |
| 13 //----------------------------------------------------------------------------- | |
| 14 // completion callback helper | |
| 15 | |
| 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. | |
| 19 // | |
| 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. | |
| 23 // | |
| 24 | |
| 25 namespace net { | |
| 26 | |
| 27 class IOBuffer; | |
| 28 | |
| 29 namespace internal { | |
| 30 | |
| 31 class TestCompletionCallbackBaseInternal { | |
| 32 public: | |
| 33 bool have_result() const { return have_result_; } | |
| 34 | |
| 35 protected: | |
| 36 TestCompletionCallbackBaseInternal(); | |
| 37 virtual ~TestCompletionCallbackBaseInternal(); | |
| 38 | |
| 39 void DidSetResult(); | |
| 40 void WaitForResult(); | |
| 41 | |
| 42 bool have_result_; | |
| 43 bool waiting_for_result_; | |
| 44 | |
| 45 private: | |
| 46 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBaseInternal); | |
| 47 }; | |
| 48 | |
| 49 template <typename R> | |
| 50 class TestCompletionCallbackTemplate | |
| 51 : public TestCompletionCallbackBaseInternal { | |
| 52 public: | |
| 53 virtual ~TestCompletionCallbackTemplate() override {} | |
| 54 | |
| 55 R WaitForResult() { | |
| 56 TestCompletionCallbackBaseInternal::WaitForResult(); | |
| 57 return result_; | |
| 58 } | |
| 59 | |
| 60 R GetResult(R result) { | |
| 61 if (net::ERR_IO_PENDING != result) | |
| 62 return result; | |
| 63 return WaitForResult(); | |
| 64 } | |
| 65 | |
| 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(); | |
| 71 } | |
| 72 | |
| 73 TestCompletionCallbackTemplate() : result_(R()) {} | |
| 74 R result_; | |
| 75 | |
| 76 private: | |
| 77 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackTemplate); | |
| 78 }; | |
| 79 | |
| 80 } // namespace internal | |
| 81 | |
| 82 class TestClosure | |
| 83 : public internal::TestCompletionCallbackBaseInternal { | |
| 84 public: | |
| 85 using internal::TestCompletionCallbackBaseInternal::WaitForResult; | |
| 86 | |
| 87 TestClosure(); | |
| 88 ~TestClosure() override; | |
| 89 | |
| 90 const base::Closure& closure() const { return closure_; } | |
| 91 | |
| 92 private: | |
| 93 const base::Closure closure_; | |
| 94 | |
| 95 DISALLOW_COPY_AND_ASSIGN(TestClosure); | |
| 96 }; | |
| 97 | |
| 98 // Base class overridden by custom implementations of TestCompletionCallback. | |
| 99 typedef internal::TestCompletionCallbackTemplate<int> | |
| 100 TestCompletionCallbackBase; | |
| 101 | |
| 102 typedef internal::TestCompletionCallbackTemplate<int64> | |
| 103 TestInt64CompletionCallbackBase; | |
| 104 | |
| 105 class TestCompletionCallback : public TestCompletionCallbackBase { | |
| 106 public: | |
| 107 TestCompletionCallback(); | |
| 108 ~TestCompletionCallback() override; | |
| 109 | |
| 110 const CompletionCallback& callback() const { return callback_; } | |
| 111 | |
| 112 private: | |
| 113 const CompletionCallback callback_; | |
| 114 | |
| 115 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback); | |
| 116 }; | |
| 117 | |
| 118 class TestInt64CompletionCallback : public TestInt64CompletionCallbackBase { | |
| 119 public: | |
| 120 TestInt64CompletionCallback(); | |
| 121 ~TestInt64CompletionCallback() override; | |
| 122 | |
| 123 const Int64CompletionCallback& callback() const { return callback_; } | |
| 124 | |
| 125 private: | |
| 126 const Int64CompletionCallback callback_; | |
| 127 | |
| 128 DISALLOW_COPY_AND_ASSIGN(TestInt64CompletionCallback); | |
| 129 }; | |
| 130 | |
| 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; | |
| 136 | |
| 137 private: | |
| 138 void SetResult(int result) override; | |
| 139 | |
| 140 IOBuffer* buffer_; | |
| 141 DISALLOW_COPY_AND_ASSIGN(ReleaseBufferCompletionCallback); | |
| 142 }; | |
| 143 | |
| 144 } // namespace net | |
| 145 | |
| 146 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ | |
| OLD | NEW |