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 MOJO_SHELL_DOMAIN_SOCKET_TEST_COMPLETION_CALLBACK_H_ | |
6 #define MOJO_SHELL_DOMAIN_SOCKET_TEST_COMPLETION_CALLBACK_H_ | |
7 | |
8 #include "base/compiler_specific.h" | |
9 #include "base/tuple.h" | |
10 #include "mojo/shell/domain_socket/completion_callback.h" | |
11 #include "mojo/shell/domain_socket/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 mojo { | |
26 namespace shell { | |
27 | |
28 namespace internal { | |
29 | |
30 class TestCompletionCallbackBaseInternal { | |
31 public: | |
32 bool have_result() const { return have_result_; } | |
33 | |
34 protected: | |
35 TestCompletionCallbackBaseInternal(); | |
36 void DidSetResult(); | |
37 void WaitForResult(); | |
38 | |
39 bool have_result_; | |
40 bool waiting_for_result_; | |
41 | |
42 private: | |
43 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBaseInternal); | |
44 }; | |
45 | |
46 template <typename R> | |
47 class TestCompletionCallbackTemplate | |
48 : public TestCompletionCallbackBaseInternal { | |
49 public: | |
50 virtual ~TestCompletionCallbackTemplate() {} | |
51 | |
52 R WaitForResult() { | |
53 TestCompletionCallbackBaseInternal::WaitForResult(); | |
54 return result_; | |
55 } | |
56 | |
57 R GetResult(R result) { | |
58 if (net::ERR_IO_PENDING != result) | |
59 return result; | |
60 return WaitForResult(); | |
61 } | |
62 | |
63 protected: | |
64 // Override this method to gain control as the callback is running. | |
65 virtual void SetResult(R result) { | |
66 result_ = result; | |
67 DidSetResult(); | |
68 } | |
69 | |
70 TestCompletionCallbackTemplate() : result_(R()) {} | |
71 R result_; | |
72 | |
73 private: | |
74 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackTemplate); | |
75 }; | |
76 | |
77 } // namespace internal | |
78 | |
79 // Base class overridden by custom implementations of TestCompletionCallback. | |
80 typedef internal::TestCompletionCallbackTemplate<int> | |
81 TestCompletionCallbackBase; | |
82 | |
83 typedef internal::TestCompletionCallbackTemplate<int64> | |
84 TestInt64CompletionCallbackBase; | |
85 | |
86 class TestCompletionCallback : public TestCompletionCallbackBase { | |
87 public: | |
88 TestCompletionCallback(); | |
89 ~TestCompletionCallback() override; | |
90 | |
91 const CompletionCallback& callback() const { return callback_; } | |
92 | |
93 private: | |
94 const CompletionCallback callback_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback); | |
97 }; | |
98 | |
99 class TestInt64CompletionCallback : public TestInt64CompletionCallbackBase { | |
100 public: | |
101 TestInt64CompletionCallback(); | |
102 ~TestInt64CompletionCallback() override; | |
103 | |
104 const Int64CompletionCallback& callback() const { return callback_; } | |
105 | |
106 private: | |
107 const Int64CompletionCallback callback_; | |
108 | |
109 DISALLOW_COPY_AND_ASSIGN(TestInt64CompletionCallback); | |
110 }; | |
111 | |
112 } // namespace shell | |
113 } // namespace mojo | |
114 | |
115 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ | |
OLD | NEW |