OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/mock_file_stream.h" | 5 #include "net/base/mock_file_stream.h" |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 | |
10 namespace net { | 7 namespace net { |
11 | 8 |
12 namespace testing { | 9 namespace testing { |
13 | 10 |
14 MockFileStream::MockFileStream(net::NetLog* net_log) | |
15 : net::FileStream(net_log), | |
16 forced_error_(net::OK), | |
17 async_error_(false), | |
18 throttled_(false), | |
19 weak_factory_(this) { | |
20 } | |
21 | |
22 MockFileStream::MockFileStream(base::PlatformFile file, | |
23 int flags, | |
24 net::NetLog* net_log) | |
25 : net::FileStream(file, flags, net_log), | |
26 forced_error_(net::OK), | |
27 async_error_(false), | |
28 throttled_(false), | |
29 weak_factory_(this) { | |
30 } | |
31 | |
32 MockFileStream::MockFileStream( | |
33 base::PlatformFile file, | |
34 int flags, | |
35 net::NetLog* net_log, | |
36 const scoped_refptr<base::TaskRunner>& task_runner) | |
37 : net::FileStream(file, flags, net_log, task_runner), | |
38 forced_error_(net::OK), | |
39 async_error_(false), | |
40 throttled_(false), | |
41 weak_factory_(this) { | |
42 } | |
43 | |
44 MockFileStream::~MockFileStream() { | |
45 } | |
46 | |
47 int MockFileStream::OpenSync(const base::FilePath& path, int open_flags) { | 11 int MockFileStream::OpenSync(const base::FilePath& path, int open_flags) { |
48 path_ = path; | 12 path_ = path; |
49 return ReturnError(FileStream::OpenSync(path, open_flags)); | 13 return ReturnError(FileStream::OpenSync(path, open_flags)); |
50 } | 14 } |
51 | 15 |
52 int MockFileStream::Seek(Whence whence, int64 offset, | 16 int MockFileStream::Seek(Whence whence, int64 offset, |
53 const Int64CompletionCallback& callback) { | 17 const Int64CompletionCallback& callback) { |
54 Int64CompletionCallback wrapped_callback = | 18 return ReturnError(FileStream::Seek(whence, offset, callback)); |
55 base::Bind(&MockFileStream::DoCallback64, | |
56 weak_factory_.GetWeakPtr(), callback); | |
57 if (forced_error_ == net::OK) | |
58 return FileStream::Seek(whence, offset, wrapped_callback); | |
59 return ErrorCallback64(wrapped_callback); | |
60 } | 19 } |
61 | 20 |
62 int64 MockFileStream::SeekSync(Whence whence, int64 offset) { | 21 int64 MockFileStream::SeekSync(Whence whence, int64 offset) { |
63 return ReturnError64(FileStream::SeekSync(whence, offset)); | 22 return ReturnError64(FileStream::SeekSync(whence, offset)); |
64 } | 23 } |
65 | 24 |
66 int64 MockFileStream::Available() { | 25 int64 MockFileStream::Available() { |
67 return ReturnError64(FileStream::Available()); | 26 return ReturnError64(FileStream::Available()); |
68 } | 27 } |
69 | 28 |
70 int MockFileStream::Read(IOBuffer* buf, | 29 int MockFileStream::Read(IOBuffer* buf, |
71 int buf_len, | 30 int buf_len, |
72 const CompletionCallback& callback) { | 31 const CompletionCallback& callback) { |
73 CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback, | 32 return ReturnError(FileStream::Read(buf, buf_len, callback)); |
74 weak_factory_.GetWeakPtr(), | |
75 callback); | |
76 if (forced_error_ == net::OK) | |
77 return FileStream::Read(buf, buf_len, wrapped_callback); | |
78 return ErrorCallback(wrapped_callback); | |
79 } | 33 } |
80 | 34 |
81 int MockFileStream::ReadSync(char* buf, int buf_len) { | 35 int MockFileStream::ReadSync(char* buf, int buf_len) { |
82 return ReturnError(FileStream::ReadSync(buf, buf_len)); | 36 return ReturnError(FileStream::ReadSync(buf, buf_len)); |
83 } | 37 } |
84 | 38 |
85 int MockFileStream::ReadUntilComplete(char *buf, int buf_len) { | 39 int MockFileStream::ReadUntilComplete(char *buf, int buf_len) { |
86 return ReturnError(FileStream::ReadUntilComplete(buf, buf_len)); | 40 return ReturnError(FileStream::ReadUntilComplete(buf, buf_len)); |
87 } | 41 } |
88 | 42 |
89 int MockFileStream::Write(IOBuffer* buf, | 43 int MockFileStream::Write(IOBuffer* buf, |
90 int buf_len, | 44 int buf_len, |
91 const CompletionCallback& callback) { | 45 const CompletionCallback& callback) { |
92 CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback, | 46 return ReturnError(FileStream::Write(buf, buf_len, callback)); |
93 weak_factory_.GetWeakPtr(), | |
94 callback); | |
95 if (forced_error_ == net::OK) | |
96 return FileStream::Write(buf, buf_len, wrapped_callback); | |
97 return ErrorCallback(wrapped_callback); | |
98 } | 47 } |
99 | 48 |
100 int MockFileStream::WriteSync(const char* buf, int buf_len) { | 49 int MockFileStream::WriteSync(const char* buf, int buf_len) { |
101 return ReturnError(FileStream::WriteSync(buf, buf_len)); | 50 return ReturnError(FileStream::WriteSync(buf, buf_len)); |
102 } | 51 } |
103 | 52 |
104 int64 MockFileStream::Truncate(int64 bytes) { | 53 int64 MockFileStream::Truncate(int64 bytes) { |
105 return ReturnError64(FileStream::Truncate(bytes)); | 54 return ReturnError64(FileStream::Truncate(bytes)); |
106 } | 55 } |
107 | 56 |
108 int MockFileStream::Flush(const CompletionCallback& callback) { | 57 int MockFileStream::Flush(const CompletionCallback& callback) { |
109 CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback, | 58 return ReturnError(FileStream::Flush(callback)); |
110 weak_factory_.GetWeakPtr(), | |
111 callback); | |
112 if (forced_error_ == net::OK) | |
113 return FileStream::Flush(wrapped_callback); | |
114 return ErrorCallback(wrapped_callback); | |
115 } | 59 } |
116 | 60 |
117 int MockFileStream::FlushSync() { | 61 int MockFileStream::FlushSync() { |
118 return ReturnError(FileStream::FlushSync()); | 62 return ReturnError(FileStream::FlushSync()); |
119 } | 63 } |
120 | 64 |
121 void MockFileStream::ThrottleCallbacks() { | |
122 CHECK(!throttled_); | |
123 throttled_ = true; | |
124 } | |
125 | |
126 void MockFileStream::ReleaseCallbacks() { | |
127 CHECK(throttled_); | |
128 throttled_ = false; | |
129 | |
130 if (!throttled_task_.is_null()) { | |
131 base::Closure throttled_task = throttled_task_; | |
132 throttled_task_.Reset(); | |
133 base::MessageLoop::current()->PostTask(FROM_HERE, throttled_task); | |
134 } | |
135 } | |
136 | |
137 void MockFileStream::DoCallback(const CompletionCallback& callback, | |
138 int result) { | |
139 if (!throttled_) { | |
140 callback.Run(result); | |
141 return; | |
142 } | |
143 CHECK(throttled_task_.is_null()); | |
144 throttled_task_ = base::Bind(callback, result); | |
145 } | |
146 | |
147 void MockFileStream::DoCallback64(const Int64CompletionCallback& callback, | |
148 int64 result) { | |
149 if (!throttled_) { | |
150 callback.Run(result); | |
151 return; | |
152 } | |
153 CHECK(throttled_task_.is_null()); | |
154 throttled_task_ = base::Bind(callback, result); | |
155 } | |
156 | |
157 int MockFileStream::ErrorCallback(const CompletionCallback& callback) { | |
158 CHECK_NE(net::OK, forced_error_); | |
159 if (async_error_) { | |
160 base::MessageLoop::current()->PostTask( | |
161 FROM_HERE, base::Bind(callback, forced_error_)); | |
162 clear_forced_error(); | |
163 return net::ERR_IO_PENDING; | |
164 } | |
165 int ret = forced_error_; | |
166 clear_forced_error(); | |
167 return ret; | |
168 } | |
169 | |
170 int64 MockFileStream::ErrorCallback64(const Int64CompletionCallback& callback) { | |
171 CHECK_NE(net::OK, forced_error_); | |
172 if (async_error_) { | |
173 base::MessageLoop::current()->PostTask( | |
174 FROM_HERE, base::Bind(callback, forced_error_)); | |
175 clear_forced_error(); | |
176 return net::ERR_IO_PENDING; | |
177 } | |
178 int64 ret = forced_error_; | |
179 clear_forced_error(); | |
180 return ret; | |
181 } | |
182 | |
183 } // namespace testing | 65 } // namespace testing |
184 | 66 |
185 } // namespace net | 67 } // namespace net |
OLD | NEW |