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 #include "net/base/mock_file_stream.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
16 MockFileStream::MockFileStream(
17 const scoped_refptr
<base::TaskRunner
>& task_runner
)
18 : FileStream(task_runner
),
25 MockFileStream::MockFileStream(
27 const scoped_refptr
<base::TaskRunner
>& task_runner
)
28 : FileStream(file
.Pass(), task_runner
),
35 MockFileStream::~MockFileStream() {
38 int MockFileStream::Seek(int64_t offset
,
39 const Int64CompletionCallback
& callback
) {
40 Int64CompletionCallback wrapped_callback
=
41 base::Bind(&MockFileStream::DoCallback64
,
42 weak_factory_
.GetWeakPtr(), callback
);
43 if (forced_error_
== OK
)
44 return FileStream::Seek(offset
, wrapped_callback
);
45 return ErrorCallback64(wrapped_callback
);
48 int MockFileStream::Read(IOBuffer
* buf
,
50 const CompletionCallback
& callback
) {
51 CompletionCallback wrapped_callback
= base::Bind(&MockFileStream::DoCallback
,
52 weak_factory_
.GetWeakPtr(),
54 if (forced_error_
== OK
)
55 return FileStream::Read(buf
, buf_len
, wrapped_callback
);
56 return ErrorCallback(wrapped_callback
);
59 int MockFileStream::Write(IOBuffer
* buf
,
61 const CompletionCallback
& callback
) {
62 CompletionCallback wrapped_callback
= base::Bind(&MockFileStream::DoCallback
,
63 weak_factory_
.GetWeakPtr(),
65 if (forced_error_
== OK
)
66 return FileStream::Write(buf
, buf_len
, wrapped_callback
);
67 return ErrorCallback(wrapped_callback
);
70 int MockFileStream::Flush(const CompletionCallback
& callback
) {
71 CompletionCallback wrapped_callback
= base::Bind(&MockFileStream::DoCallback
,
72 weak_factory_
.GetWeakPtr(),
74 if (forced_error_
== OK
)
75 return FileStream::Flush(wrapped_callback
);
76 return ErrorCallback(wrapped_callback
);
79 void MockFileStream::ThrottleCallbacks() {
84 void MockFileStream::ReleaseCallbacks() {
88 if (!throttled_task_
.is_null()) {
89 base::Closure throttled_task
= throttled_task_
;
90 throttled_task_
.Reset();
91 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, throttled_task
);
95 void MockFileStream::DoCallback(const CompletionCallback
& callback
,
101 CHECK(throttled_task_
.is_null());
102 throttled_task_
= base::Bind(callback
, result
);
105 void MockFileStream::DoCallback64(const Int64CompletionCallback
& callback
,
108 callback
.Run(result
);
111 CHECK(throttled_task_
.is_null());
112 throttled_task_
= base::Bind(callback
, result
);
115 int MockFileStream::ErrorCallback(const CompletionCallback
& callback
) {
116 CHECK_NE(OK
, forced_error_
);
118 base::ThreadTaskRunnerHandle::Get()->PostTask(
119 FROM_HERE
, base::Bind(callback
, forced_error_
));
120 clear_forced_error();
121 return ERR_IO_PENDING
;
123 int ret
= forced_error_
;
124 clear_forced_error();
128 int64_t MockFileStream::ErrorCallback64(
129 const Int64CompletionCallback
& callback
) {
130 CHECK_NE(OK
, forced_error_
);
132 base::ThreadTaskRunnerHandle::Get()->PostTask(
133 FROM_HERE
, base::Bind(callback
, forced_error_
));
134 clear_forced_error();
135 return ERR_IO_PENDING
;
137 int64_t ret
= forced_error_
;
138 clear_forced_error();
142 } // namespace testing