Support symlinks in tools/vim/chromium.ycm_extra_conf.py
[chromium-blink-merge.git] / net / base / mock_file_stream.h
blob7b7dea83170ae5037d08b6db3ebbfa2f11d7cda1
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 // This file defines MockFileStream, a test class for FileStream.
7 #ifndef NET_BASE_MOCK_FILE_STREAM_H_
8 #define NET_BASE_MOCK_FILE_STREAM_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/files/file_path.h"
13 #include "base/memory/weak_ptr.h"
14 #include "net/base/file_stream.h"
15 #include "net/base/net_errors.h"
17 namespace net {
19 class IOBuffer;
21 namespace testing {
23 class MockFileStream : public net::FileStream {
24 public:
25 explicit MockFileStream(const scoped_refptr<base::TaskRunner>& task_runner);
26 MockFileStream(base::File file,
27 const scoped_refptr<base::TaskRunner>& task_runner);
28 ~MockFileStream() override;
30 // FileStream methods.
31 int Seek(base::File::Whence whence,
32 int64 offset,
33 const Int64CompletionCallback& callback) override;
34 int Read(IOBuffer* buf,
35 int buf_len,
36 const CompletionCallback& callback) override;
37 int Write(IOBuffer* buf,
38 int buf_len,
39 const CompletionCallback& callback) override;
40 int Flush(const CompletionCallback& callback) override;
42 void set_forced_error_async(int error) {
43 forced_error_ = error;
44 async_error_ = true;
46 void set_forced_error(int error) {
47 forced_error_ = error;
48 async_error_ = false;
50 void clear_forced_error() {
51 forced_error_ = net::OK;
52 async_error_ = false;
54 int forced_error() const { return forced_error_; }
55 const base::FilePath& get_path() const { return path_; }
57 // Throttles all asynchronous callbacks, including forced errors, until a
58 // matching ReleaseCallbacks call.
59 void ThrottleCallbacks();
61 // Resumes running asynchronous callbacks and runs any throttled callbacks.
62 void ReleaseCallbacks();
64 private:
65 int ReturnError(int function_error) {
66 if (forced_error_ != net::OK) {
67 int ret = forced_error_;
68 clear_forced_error();
69 return ret;
72 return function_error;
75 int64 ReturnError64(int64 function_error) {
76 if (forced_error_ != net::OK) {
77 int64 ret = forced_error_;
78 clear_forced_error();
79 return ret;
82 return function_error;
85 // Wrappers for callbacks to make them honor ThrottleCallbacks and
86 // ReleaseCallbacks.
87 void DoCallback(const CompletionCallback& callback, int result);
88 void DoCallback64(const Int64CompletionCallback& callback, int64 result);
90 // Depending on |async_error_|, either synchronously returns |forced_error_|
91 // asynchronously calls |callback| with |async_error_|.
92 int ErrorCallback(const CompletionCallback& callback);
93 int64 ErrorCallback64(const Int64CompletionCallback& callback);
95 int forced_error_;
96 bool async_error_;
97 bool throttled_;
98 base::Closure throttled_task_;
99 base::FilePath path_;
101 base::WeakPtrFactory<MockFileStream> weak_factory_;
104 } // namespace testing
106 } // namespace net
108 #endif // NET_BASE_MOCK_FILE_STREAM_H_