OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/callback_old.h" | 9 #include "base/callback_old.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/tuple.h" | 11 #include "base/tuple.h" |
12 #include "net/base/completion_callback.h" | 12 #include "net/base/completion_callback.h" |
13 | 13 |
14 //----------------------------------------------------------------------------- | 14 //----------------------------------------------------------------------------- |
15 // completion callback helper | 15 // completion callback helper |
16 | 16 |
17 // 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 |
18 // tests involving asynchronous operations. Just call WaitForResult to wait | 18 // tests involving asynchronous operations. Just call WaitForResult to wait |
19 // for the asynchronous operation to complete. | 19 // for the asynchronous operation to complete. |
20 // | 20 // |
21 // 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, |
22 // there could be other side-effects resulting from WaitForResult. For this | 22 // there could be other side-effects resulting from WaitForResult. For this |
23 // reason, this class is probably not ideal for a general application. | 23 // reason, this class is probably not ideal for a general application. |
24 // | 24 // |
| 25 class TestOldCompletionCallback : public CallbackRunner< Tuple1<int> > { |
| 26 public: |
| 27 TestOldCompletionCallback(); |
| 28 virtual ~TestOldCompletionCallback(); |
25 | 29 |
26 // Base class overridden by custom implementations of TestCompletionCallback. | |
27 class TestCompletionCallbackBase { | |
28 public: | |
29 void SetResult(int result); | |
30 int WaitForResult(); | 30 int WaitForResult(); |
| 31 |
31 int GetResult(int result); | 32 int GetResult(int result); |
| 33 |
32 bool have_result() const { return have_result_; } | 34 bool have_result() const { return have_result_; } |
33 | 35 |
34 protected: | |
35 TestCompletionCallbackBase(); | |
36 | |
37 int result_; | |
38 bool have_result_; | |
39 bool waiting_for_result_; | |
40 | |
41 private: | |
42 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBase); | |
43 }; | |
44 | |
45 class TestOldCompletionCallback : public TestCompletionCallbackBase, | |
46 public CallbackRunner< Tuple1<int> > { | |
47 public: | |
48 TestOldCompletionCallback() {}; | |
49 virtual ~TestOldCompletionCallback() {} | |
50 | |
51 virtual void RunWithParams(const Tuple1<int>& params) OVERRIDE; | 36 virtual void RunWithParams(const Tuple1<int>& params) OVERRIDE; |
52 | 37 |
53 private: | 38 private: |
54 DISALLOW_COPY_AND_ASSIGN(TestOldCompletionCallback); | 39 int result_; |
| 40 bool have_result_; |
| 41 bool waiting_for_result_; |
55 }; | 42 }; |
56 | 43 |
57 namespace net { | 44 namespace net { |
58 | 45 |
59 class TestCompletionCallback : public TestCompletionCallbackBase { | 46 class TestCompletionCallback { |
60 public: | 47 public: |
61 TestCompletionCallback(); | 48 TestCompletionCallback(); |
62 ~TestCompletionCallback(); | 49 ~TestCompletionCallback(); |
63 | 50 |
| 51 int WaitForResult() { return old_callback_impl_.WaitForResult(); } |
| 52 |
| 53 int GetResult(int result) { return old_callback_impl_.GetResult(result); } |
| 54 |
| 55 bool have_result() const { return old_callback_impl_.have_result(); } |
| 56 |
64 const CompletionCallback& callback() const { return callback_; } | 57 const CompletionCallback& callback() const { return callback_; } |
65 | 58 |
66 private: | 59 private: |
| 60 void OnComplete(int result); |
| 61 |
67 const CompletionCallback callback_; | 62 const CompletionCallback callback_; |
68 | 63 TestOldCompletionCallback old_callback_impl_; |
69 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback); | |
70 }; | 64 }; |
71 | 65 |
72 } // namespace net | 66 } // namespace net |
73 | 67 |
74 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ | 68 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ |
OLD | NEW |