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"
23 class MockFileStream
: public FileStream
{
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
,
34 const CompletionCallback
& callback
) override
;
35 int Write(IOBuffer
* buf
,
37 const CompletionCallback
& callback
) override
;
38 int Flush(const CompletionCallback
& callback
) override
;
40 void set_forced_error_async(int error
) {
41 forced_error_
= error
;
44 void set_forced_error(int error
) {
45 forced_error_
= error
;
48 void clear_forced_error() {
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();
63 int ReturnError(int function_error
) {
64 if (forced_error_
!= OK
) {
65 int ret
= forced_error_
;
70 return function_error
;
73 int64_t ReturnError64(int64_t function_error
) {
74 if (forced_error_
!= OK
) {
75 int64_t ret
= forced_error_
;
80 return function_error
;
83 // Wrappers for callbacks to make them honor ThrottleCallbacks and
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
);
96 base::Closure throttled_task_
;
99 base::WeakPtrFactory
<MockFileStream
> weak_factory_
;
102 } // namespace testing
106 #endif // NET_BASE_MOCK_FILE_STREAM_H_