| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_BASE_TEST_COMPLETION_CALLBACK_H_ | 5 #ifndef NET_BASE_TEST_COMPLETION_CALLBACK_H_ |
| 6 #define NET_BASE_TEST_COMPLETION_CALLBACK_H_ | 6 #define NET_BASE_TEST_COMPLETION_CALLBACK_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/tuple.h" | 10 #include "base/tuple.h" |
| 11 #include "net/base/completion_callback.h" | 11 #include "net/base/completion_callback.h" |
| 12 #include "net/base/net_errors.h" |
| 12 | 13 |
| 13 //----------------------------------------------------------------------------- | 14 //----------------------------------------------------------------------------- |
| 14 // completion callback helper | 15 // completion callback helper |
| 15 | 16 |
| 16 // A helper class for completion callbacks, designed to make it easy to run | 17 // A helper class for completion callbacks, designed to make it easy to run |
| 17 // tests involving asynchronous operations. Just call WaitForResult to wait | 18 // tests involving asynchronous operations. Just call WaitForResult to wait |
| 18 // for the asynchronous operation to complete. | 19 // for the asynchronous operation to complete. |
| 19 // | 20 // |
| 20 // NOTE: Since this runs a message loop to wait for the completion callback, | 21 // 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 // there could be other side-effects resulting from WaitForResult. For this |
| 22 // reason, this class is probably not ideal for a general application. | 23 // reason, this class is probably not ideal for a general application. |
| 23 // | 24 // |
| 24 | 25 |
| 25 // Base class overridden by custom implementations of TestCompletionCallback. | 26 namespace net { |
| 26 class TestCompletionCallbackBase { | 27 |
| 28 namespace internal { |
| 29 |
| 30 class TestCompletionCallbackBaseInternal { |
| 27 public: | 31 public: |
| 28 void SetResult(int result); | |
| 29 int WaitForResult(); | |
| 30 int GetResult(int result); | |
| 31 bool have_result() const { return have_result_; } | 32 bool have_result() const { return have_result_; } |
| 32 | 33 |
| 33 protected: | 34 protected: |
| 34 TestCompletionCallbackBase(); | 35 TestCompletionCallbackBaseInternal(); |
| 36 void DidSetResult(); |
| 37 void WaitForResult(); |
| 35 | 38 |
| 36 int result_; | |
| 37 bool have_result_; | 39 bool have_result_; |
| 38 bool waiting_for_result_; | 40 bool waiting_for_result_; |
| 39 | 41 |
| 40 private: | 42 private: |
| 41 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBase); | 43 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBaseInternal); |
| 42 }; | 44 }; |
| 43 | 45 |
| 44 namespace net { | 46 template <typename R> |
| 47 class TestCompletionCallbackTemplate |
| 48 : public TestCompletionCallbackBaseInternal { |
| 49 public: |
| 50 void SetResult(R result) { |
| 51 result_ = result; |
| 52 DidSetResult(); |
| 53 } |
| 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 TestCompletionCallbackTemplate() : result_(R()) {} |
| 68 R result_; |
| 69 |
| 70 private: |
| 71 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackTemplate); |
| 72 }; |
| 73 |
| 74 } // namespace internal |
| 75 |
| 76 // Base class overridden by custom implementations of TestCompletionCallback. |
| 77 typedef internal::TestCompletionCallbackTemplate<int> |
| 78 TestCompletionCallbackBase; |
| 79 |
| 80 typedef internal::TestCompletionCallbackTemplate<int64> |
| 81 TestInt64CompletionCallbackBase; |
| 45 | 82 |
| 46 class TestCompletionCallback : public TestCompletionCallbackBase { | 83 class TestCompletionCallback : public TestCompletionCallbackBase { |
| 47 public: | 84 public: |
| 48 TestCompletionCallback(); | 85 TestCompletionCallback(); |
| 49 ~TestCompletionCallback(); | 86 ~TestCompletionCallback(); |
| 50 | 87 |
| 51 const CompletionCallback& callback() const { return callback_; } | 88 const CompletionCallback& callback() const { return callback_; } |
| 52 | 89 |
| 53 private: | 90 private: |
| 54 const CompletionCallback callback_; | 91 const CompletionCallback callback_; |
| 55 | 92 |
| 56 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback); | 93 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback); |
| 57 }; | 94 }; |
| 58 | 95 |
| 96 class TestInt64CompletionCallback : public TestInt64CompletionCallbackBase { |
| 97 public: |
| 98 TestInt64CompletionCallback(); |
| 99 ~TestInt64CompletionCallback(); |
| 100 |
| 101 const Int64CompletionCallback& callback() const { return callback_; } |
| 102 |
| 103 private: |
| 104 const Int64CompletionCallback callback_; |
| 105 |
| 106 DISALLOW_COPY_AND_ASSIGN(TestInt64CompletionCallback); |
| 107 }; |
| 108 |
| 59 } // namespace net | 109 } // namespace net |
| 60 | 110 |
| 61 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ | 111 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ |
| OLD | NEW |