[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / net / base / test_completion_callback.h
blob236c9b3f758520d3415c639a0a6f97eec6086db5
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.
5 #ifndef NET_BASE_TEST_COMPLETION_CALLBACK_H_
6 #define NET_BASE_TEST_COMPLETION_CALLBACK_H_
8 #include "base/callback.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "net/base/completion_callback.h"
12 #include "net/base/net_errors.h"
14 //-----------------------------------------------------------------------------
15 // completion callback helper
17 // A helper class for completion callbacks, designed to make it easy to run
18 // tests involving asynchronous operations. Just call WaitForResult to wait
19 // for the asynchronous operation to complete. Uses a RunLoop to spin the
20 // current MessageLoop while waiting. The callback must be invoked on the same
21 // thread WaitForResult is called on.
23 // NOTE: Since this runs a message loop to wait for the completion callback,
24 // there could be other side-effects resulting from WaitForResult. For this
25 // reason, this class is probably not ideal for a general application.
27 namespace base {
28 class RunLoop;
31 namespace net {
33 class IOBuffer;
35 namespace internal {
37 class TestCompletionCallbackBaseInternal {
38 public:
39 bool have_result() const { return have_result_; }
41 protected:
42 TestCompletionCallbackBaseInternal();
43 virtual ~TestCompletionCallbackBaseInternal();
45 void DidSetResult();
46 void WaitForResult();
48 private:
49 // RunLoop. Only non-NULL during the call to WaitForResult, so the class is
50 // reusable.
51 scoped_ptr<base::RunLoop> run_loop_;
52 bool have_result_;
54 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBaseInternal);
57 template <typename R>
58 class TestCompletionCallbackTemplate
59 : public TestCompletionCallbackBaseInternal {
60 public:
61 virtual ~TestCompletionCallbackTemplate() override {}
63 R WaitForResult() {
64 TestCompletionCallbackBaseInternal::WaitForResult();
65 return result_;
68 R GetResult(R result) {
69 if (ERR_IO_PENDING != result)
70 return result;
71 return WaitForResult();
74 protected:
75 TestCompletionCallbackTemplate() : result_(R()) {}
77 // Override this method to gain control as the callback is running.
78 virtual void SetResult(R result) {
79 result_ = result;
80 DidSetResult();
83 private:
84 R result_;
86 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackTemplate);
89 } // namespace internal
91 class TestClosure : public internal::TestCompletionCallbackBaseInternal {
92 public:
93 using internal::TestCompletionCallbackBaseInternal::WaitForResult;
95 TestClosure();
96 ~TestClosure() override;
98 const base::Closure& closure() const { return closure_; }
100 private:
101 const base::Closure closure_;
103 DISALLOW_COPY_AND_ASSIGN(TestClosure);
106 // Base class overridden by custom implementations of TestCompletionCallback.
107 typedef internal::TestCompletionCallbackTemplate<int>
108 TestCompletionCallbackBase;
110 typedef internal::TestCompletionCallbackTemplate<int64_t>
111 TestInt64CompletionCallbackBase;
113 class TestCompletionCallback : public TestCompletionCallbackBase {
114 public:
115 TestCompletionCallback();
116 ~TestCompletionCallback() override;
118 const CompletionCallback& callback() const { return callback_; }
120 private:
121 const CompletionCallback callback_;
123 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback);
126 class TestInt64CompletionCallback : public TestInt64CompletionCallbackBase {
127 public:
128 TestInt64CompletionCallback();
129 ~TestInt64CompletionCallback() override;
131 const Int64CompletionCallback& callback() const { return callback_; }
133 private:
134 const Int64CompletionCallback callback_;
136 DISALLOW_COPY_AND_ASSIGN(TestInt64CompletionCallback);
139 // Makes sure that the buffer is not referenced when the callback runs.
140 class ReleaseBufferCompletionCallback: public TestCompletionCallback {
141 public:
142 explicit ReleaseBufferCompletionCallback(IOBuffer* buffer);
143 ~ReleaseBufferCompletionCallback() override;
145 private:
146 void SetResult(int result) override;
148 IOBuffer* buffer_;
149 DISALLOW_COPY_AND_ASSIGN(ReleaseBufferCompletionCallback);
152 } // namespace net
154 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_