Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / base / mock_file_stream.h
blob55261b4393d8a4095f90a0214565b54b6a34f7a3
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 virtual ~MockFileStream();
30 // FileStream methods.
31 virtual int Seek(base::File::Whence whence, int64 offset,
32 const Int64CompletionCallback& callback) OVERRIDE;
33 virtual int Read(IOBuffer* buf,
34 int buf_len,
35 const CompletionCallback& callback) OVERRIDE;
36 virtual int Write(IOBuffer* buf,
37 int buf_len,
38 const CompletionCallback& callback) OVERRIDE;
39 virtual int Flush(const CompletionCallback& callback) OVERRIDE;
41 void set_forced_error_async(int error) {
42 forced_error_ = error;
43 async_error_ = true;
45 void set_forced_error(int error) {
46 forced_error_ = error;
47 async_error_ = false;
49 void clear_forced_error() {
50 forced_error_ = net::OK;
51 async_error_ = false;
53 int forced_error() const { return forced_error_; }
54 const base::FilePath& get_path() const { return path_; }
56 // Throttles all asynchronous callbacks, including forced errors, until a
57 // matching ReleaseCallbacks call.
58 void ThrottleCallbacks();
60 // Resumes running asynchronous callbacks and runs any throttled callbacks.
61 void ReleaseCallbacks();
63 private:
64 int ReturnError(int function_error) {
65 if (forced_error_ != net::OK) {
66 int ret = forced_error_;
67 clear_forced_error();
68 return ret;
71 return function_error;
74 int64 ReturnError64(int64 function_error) {
75 if (forced_error_ != net::OK) {
76 int64 ret = forced_error_;
77 clear_forced_error();
78 return ret;
81 return function_error;
84 // Wrappers for callbacks to make them honor ThrottleCallbacks and
85 // ReleaseCallbacks.
86 void DoCallback(const CompletionCallback& callback, int result);
87 void DoCallback64(const Int64CompletionCallback& callback, int64 result);
89 // Depending on |async_error_|, either synchronously returns |forced_error_|
90 // asynchronously calls |callback| with |async_error_|.
91 int ErrorCallback(const CompletionCallback& callback);
92 int64 ErrorCallback64(const Int64CompletionCallback& callback);
94 int forced_error_;
95 bool async_error_;
96 bool throttled_;
97 base::Closure throttled_task_;
98 base::FilePath path_;
100 base::WeakPtrFactory<MockFileStream> weak_factory_;
103 } // namespace testing
105 } // namespace net
107 #endif // NET_BASE_MOCK_FILE_STREAM_H_