[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/base/mock_file_stream.h" |
| 6 | |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/message_loop/message_loop.h" |
| 9 | |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 10 | namespace net { |
| 11 | |
| 12 | namespace testing { |
| 13 | |
[email protected] | 671e95fd | 2014-04-30 11:21:36 | [diff] [blame] | 14 | MockFileStream::MockFileStream( |
| 15 | const scoped_refptr<base::TaskRunner>& task_runner) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 16 | : FileStream(task_runner), |
| 17 | forced_error_(OK), |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 18 | async_error_(false), |
| 19 | throttled_(false), |
| 20 | weak_factory_(this) { |
| 21 | } |
| 22 | |
| 23 | MockFileStream::MockFileStream( |
[email protected] | be6fb60b | 2014-04-03 18:33:58 | [diff] [blame] | 24 | base::File file, |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 25 | const scoped_refptr<base::TaskRunner>& task_runner) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 26 | : FileStream(file.Pass(), task_runner), |
| 27 | forced_error_(OK), |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 28 | async_error_(false), |
| 29 | throttled_(false), |
| 30 | weak_factory_(this) { |
| 31 | } |
| 32 | |
| 33 | MockFileStream::~MockFileStream() { |
| 34 | } |
| 35 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 36 | int MockFileStream::Seek(base::File::Whence whence, int64 offset, |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 37 | const Int64CompletionCallback& callback) { |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 38 | Int64CompletionCallback wrapped_callback = |
| 39 | base::Bind(&MockFileStream::DoCallback64, |
| 40 | weak_factory_.GetWeakPtr(), callback); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 41 | if (forced_error_ == OK) |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 42 | return FileStream::Seek(whence, offset, wrapped_callback); |
| 43 | return ErrorCallback64(wrapped_callback); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 44 | } |
| 45 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 46 | int MockFileStream::Read(IOBuffer* buf, |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 47 | int buf_len, |
| 48 | const CompletionCallback& callback) { |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 49 | CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback, |
| 50 | weak_factory_.GetWeakPtr(), |
| 51 | callback); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 52 | if (forced_error_ == OK) |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 53 | return FileStream::Read(buf, buf_len, wrapped_callback); |
| 54 | return ErrorCallback(wrapped_callback); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 57 | int MockFileStream::Write(IOBuffer* buf, |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 58 | int buf_len, |
| 59 | const CompletionCallback& callback) { |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 60 | CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback, |
| 61 | weak_factory_.GetWeakPtr(), |
| 62 | callback); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 63 | if (forced_error_ == OK) |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 64 | return FileStream::Write(buf, buf_len, wrapped_callback); |
| 65 | return ErrorCallback(wrapped_callback); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 66 | } |
| 67 | |
[email protected] | a95ce076 | 2012-10-01 05:54:26 | [diff] [blame] | 68 | int MockFileStream::Flush(const CompletionCallback& callback) { |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 69 | CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback, |
| 70 | weak_factory_.GetWeakPtr(), |
| 71 | callback); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 72 | if (forced_error_ == OK) |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 73 | return FileStream::Flush(wrapped_callback); |
| 74 | return ErrorCallback(wrapped_callback); |
[email protected] | a95ce076 | 2012-10-01 05:54:26 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 77 | void MockFileStream::ThrottleCallbacks() { |
| 78 | CHECK(!throttled_); |
| 79 | throttled_ = true; |
| 80 | } |
| 81 | |
| 82 | void MockFileStream::ReleaseCallbacks() { |
| 83 | CHECK(throttled_); |
| 84 | throttled_ = false; |
| 85 | |
| 86 | if (!throttled_task_.is_null()) { |
| 87 | base::Closure throttled_task = throttled_task_; |
| 88 | throttled_task_.Reset(); |
| 89 | base::MessageLoop::current()->PostTask(FROM_HERE, throttled_task); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void MockFileStream::DoCallback(const CompletionCallback& callback, |
| 94 | int result) { |
| 95 | if (!throttled_) { |
| 96 | callback.Run(result); |
| 97 | return; |
| 98 | } |
| 99 | CHECK(throttled_task_.is_null()); |
| 100 | throttled_task_ = base::Bind(callback, result); |
| 101 | } |
| 102 | |
| 103 | void MockFileStream::DoCallback64(const Int64CompletionCallback& callback, |
| 104 | int64 result) { |
| 105 | if (!throttled_) { |
| 106 | callback.Run(result); |
| 107 | return; |
| 108 | } |
| 109 | CHECK(throttled_task_.is_null()); |
| 110 | throttled_task_ = base::Bind(callback, result); |
| 111 | } |
| 112 | |
| 113 | int MockFileStream::ErrorCallback(const CompletionCallback& callback) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 114 | CHECK_NE(OK, forced_error_); |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 115 | if (async_error_) { |
| 116 | base::MessageLoop::current()->PostTask( |
| 117 | FROM_HERE, base::Bind(callback, forced_error_)); |
| 118 | clear_forced_error(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 119 | return ERR_IO_PENDING; |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 120 | } |
| 121 | int ret = forced_error_; |
| 122 | clear_forced_error(); |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | int64 MockFileStream::ErrorCallback64(const Int64CompletionCallback& callback) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 127 | CHECK_NE(OK, forced_error_); |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 128 | if (async_error_) { |
| 129 | base::MessageLoop::current()->PostTask( |
| 130 | FROM_HERE, base::Bind(callback, forced_error_)); |
| 131 | clear_forced_error(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame^] | 132 | return ERR_IO_PENDING; |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 133 | } |
| 134 | int64 ret = forced_error_; |
| 135 | clear_forced_error(); |
| 136 | return ret; |
| 137 | } |
| 138 | |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 139 | } // namespace testing |
| 140 | |
| 141 | } // namespace net |