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> > { | 25 |
26 // Base class overridden by custom implementations of TestCompletionCallback. | |
27 class TestCompletionCallbackBase { | |
26 public: | 28 public: |
27 TestOldCompletionCallback(); | 29 void SetResult(int result); |
28 virtual ~TestOldCompletionCallback(); | 30 int WaitForResult(); |
31 int GetResult(int result); | |
32 bool have_result() const { return have_result_; } | |
29 | 33 |
30 int WaitForResult(); | 34 protected: |
35 TestCompletionCallbackBase(); | |
31 | 36 |
32 int GetResult(int result); | 37 int result_; |
38 bool have_result_; | |
39 bool waiting_for_result_; | |
33 | 40 |
34 bool have_result() const { return have_result_; } | 41 private: |
42 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBase); | |
43 }; | |
csilv
2011/12/06 00:56:08
This is a pretty cool approach, although when we r
James Hawkins
2011/12/06 17:25:12
I don't think so; see the use of this base class t
| |
44 | |
45 class TestOldCompletionCallback : public TestCompletionCallbackBase, | |
46 public CallbackRunner< Tuple1<int> > { | |
47 public: | |
48 TestOldCompletionCallback() {}; | |
49 virtual ~TestOldCompletionCallback() {} | |
35 | 50 |
36 virtual void RunWithParams(const Tuple1<int>& params) OVERRIDE; | 51 virtual void RunWithParams(const Tuple1<int>& params) OVERRIDE; |
37 | 52 |
38 private: | 53 private: |
39 int result_; | 54 DISALLOW_COPY_AND_ASSIGN(TestOldCompletionCallback); |
40 bool have_result_; | |
41 bool waiting_for_result_; | |
42 }; | 55 }; |
43 | 56 |
44 namespace net { | 57 namespace net { |
45 | 58 |
46 class TestCompletionCallback { | 59 class TestCompletionCallback : public TestCompletionCallbackBase { |
47 public: | 60 public: |
48 TestCompletionCallback(); | 61 TestCompletionCallback(); |
49 ~TestCompletionCallback(); | 62 ~TestCompletionCallback(); |
50 | 63 |
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 | |
57 const CompletionCallback& callback() const { return callback_; } | 64 const CompletionCallback& callback() const { return callback_; } |
58 | 65 |
59 private: | 66 private: |
60 void OnComplete(int result); | 67 const CompletionCallback callback_; |
61 | 68 |
62 const CompletionCallback callback_; | 69 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback); |
63 TestOldCompletionCallback old_callback_impl_; | |
64 }; | 70 }; |
65 | 71 |
66 } // namespace net | 72 } // namespace net |
67 | 73 |
68 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ | 74 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ |
OLD | NEW |