[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / net / base / mock_file_stream.h
blob0d188849178948bb56dfe28fe89e28a77590c1f5
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 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(int64_t offset, const Int64CompletionCallback& callback) override;
32 int Read(IOBuffer* buf,
33 int buf_len,
34 const CompletionCallback& callback) override;
35 int Write(IOBuffer* buf,
36 int buf_len,
37 const CompletionCallback& callback) override;
38 int Flush(const CompletionCallback& callback) override;
40 void set_forced_error_async(int error) {
41 forced_error_ = error;
42 async_error_ = true;
44 void set_forced_error(int error) {
45 forced_error_ = error;
46 async_error_ = false;
48 void clear_forced_error() {
49 forced_error_ = OK;
50 async_error_ = false;
52 int forced_error() const { return forced_error_; }
53 const base::FilePath& get_path() const { return path_; }
55 // Throttles all asynchronous callbacks, including forced errors, until a
56 // matching ReleaseCallbacks call.
57 void ThrottleCallbacks();
59 // Resumes running asynchronous callbacks and runs any throttled callbacks.
60 void ReleaseCallbacks();
62 private:
63 int ReturnError(int function_error) {
64 if (forced_error_ != OK) {
65 int ret = forced_error_;
66 clear_forced_error();
67 return ret;
70 return function_error;
73 int64_t ReturnError64(int64_t function_error) {
74 if (forced_error_ != OK) {
75 int64_t ret = forced_error_;
76 clear_forced_error();
77 return ret;
80 return function_error;
83 // Wrappers for callbacks to make them honor ThrottleCallbacks and
84 // ReleaseCallbacks.
85 void DoCallback(const CompletionCallback& callback, int result);
86 void DoCallback64(const Int64CompletionCallback& callback, int64_t result);
88 // Depending on |async_error_|, either synchronously returns |forced_error_|
89 // asynchronously calls |callback| with |async_error_|.
90 int ErrorCallback(const CompletionCallback& callback);
91 int64_t ErrorCallback64(const Int64CompletionCallback& callback);
93 int forced_error_;
94 bool async_error_;
95 bool throttled_;
96 base::Closure throttled_task_;
97 base::FilePath path_;
99 base::WeakPtrFactory<MockFileStream> weak_factory_;
102 } // namespace testing
104 } // namespace net
106 #endif // NET_BASE_MOCK_FILE_STREAM_H_