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 "android_webview/browser/input_stream.h" | 5 #include "android_webview/browser/input_stream.h" |
6 #include "android_webview/browser/net/android_stream_reader_url_request_job.h" | 6 #include "android_webview/browser/net/android_stream_reader_url_request_job.h" |
7 #include "android_webview/browser/net/aw_url_request_job_factory.h" | 7 #include "android_webview/browser/net/aw_url_request_job_factory.h" |
8 #include "android_webview/browser/net/input_stream_reader.h" | 8 #include "android_webview/browser/net/input_stream_reader.h" |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 using testing::_; | 40 using testing::_; |
41 | 41 |
42 // Some of the classes will DCHECK on a null InputStream (which is desirable). | 42 // Some of the classes will DCHECK on a null InputStream (which is desirable). |
43 // The workaround is to use this class. None of the methods need to be | 43 // The workaround is to use this class. None of the methods need to be |
44 // implemented as the mock InputStreamReader should never forward calls to the | 44 // implemented as the mock InputStreamReader should never forward calls to the |
45 // InputStream. | 45 // InputStream. |
46 class NotImplInputStream : public InputStream { | 46 class NotImplInputStream : public InputStream { |
47 public: | 47 public: |
48 NotImplInputStream() {} | 48 NotImplInputStream() {} |
49 virtual ~NotImplInputStream() {} | 49 virtual ~NotImplInputStream() {} |
50 virtual bool BytesAvailable(int* bytes_available) const { | 50 virtual bool BytesAvailable(int* bytes_available) const OVERRIDE { |
51 NOTIMPLEMENTED(); | 51 NOTIMPLEMENTED(); |
52 return false; | 52 return false; |
53 } | 53 } |
54 virtual bool Skip(int64_t n, int64_t* bytes_skipped) { | 54 virtual bool Skip(int64_t n, int64_t* bytes_skipped) OVERRIDE { |
55 NOTIMPLEMENTED(); | 55 NOTIMPLEMENTED(); |
56 return false; | 56 return false; |
57 } | 57 } |
58 virtual bool Read(net::IOBuffer* dest, int length, int* bytes_read) { | 58 virtual bool Read(net::IOBuffer* dest, int length, int* bytes_read) OVERRIDE { |
59 NOTIMPLEMENTED(); | 59 NOTIMPLEMENTED(); |
60 return false; | 60 return false; |
61 } | 61 } |
62 }; | 62 }; |
63 | 63 |
64 // Required in order to create an instance of AndroidStreamReaderURLRequestJob. | 64 // Required in order to create an instance of AndroidStreamReaderURLRequestJob. |
65 class StreamReaderDelegate : | 65 class StreamReaderDelegate : |
66 public AndroidStreamReaderURLRequestJob::Delegate { | 66 public AndroidStreamReaderURLRequestJob::Delegate { |
67 public: | 67 public: |
68 StreamReaderDelegate() {} | 68 StreamReaderDelegate() {} |
69 | 69 |
70 virtual scoped_ptr<InputStream> OpenInputStream( | 70 virtual scoped_ptr<InputStream> OpenInputStream( |
71 JNIEnv* env, | 71 JNIEnv* env, |
72 const GURL& url) { | 72 const GURL& url) OVERRIDE { |
73 return make_scoped_ptr<InputStream>(new NotImplInputStream()); | 73 return make_scoped_ptr<InputStream>(new NotImplInputStream()); |
74 } | 74 } |
75 | 75 |
76 virtual void OnInputStreamOpenFailed(net::URLRequest* request, | 76 virtual void OnInputStreamOpenFailed(net::URLRequest* request, |
77 bool* restart) { | 77 bool* restart) OVERRIDE { |
78 *restart = false; | 78 *restart = false; |
79 } | 79 } |
80 | 80 |
81 virtual bool GetMimeType( | 81 virtual bool GetMimeType(JNIEnv* env, |
82 JNIEnv* env, | 82 net::URLRequest* request, |
83 net::URLRequest* request, | 83 android_webview::InputStream* stream, |
84 android_webview::InputStream* stream, | 84 std::string* mime_type) OVERRIDE { |
85 std::string* mime_type) { | |
86 return false; | 85 return false; |
87 } | 86 } |
88 | 87 |
89 virtual bool GetCharset( | 88 virtual bool GetCharset(JNIEnv* env, |
90 JNIEnv* env, | 89 net::URLRequest* request, |
91 net::URLRequest* request, | 90 android_webview::InputStream* stream, |
92 android_webview::InputStream* stream, | 91 std::string* charset) OVERRIDE { |
93 std::string* charset) { | |
94 return false; | 92 return false; |
95 } | 93 } |
96 }; | 94 }; |
97 | 95 |
98 class NullStreamReaderDelegate : public StreamReaderDelegate { | 96 class NullStreamReaderDelegate : public StreamReaderDelegate { |
99 public: | 97 public: |
100 NullStreamReaderDelegate() {} | 98 NullStreamReaderDelegate() {} |
101 | 99 |
102 virtual scoped_ptr<InputStream> OpenInputStream( | 100 virtual scoped_ptr<InputStream> OpenInputStream( |
103 JNIEnv* env, | 101 JNIEnv* env, |
104 const GURL& url) { | 102 const GURL& url) OVERRIDE { |
105 return make_scoped_ptr<InputStream>(NULL); | 103 return make_scoped_ptr<InputStream>(NULL); |
106 } | 104 } |
107 }; | 105 }; |
108 | 106 |
109 class MockInputStreamReader : public InputStreamReader { | 107 class MockInputStreamReader : public InputStreamReader { |
110 public: | 108 public: |
111 MockInputStreamReader() : InputStreamReader(new NotImplInputStream()) {} | 109 MockInputStreamReader() : InputStreamReader(new NotImplInputStream()) {} |
112 ~MockInputStreamReader() {} | 110 ~MockInputStreamReader() {} |
113 | 111 |
114 MOCK_METHOD1(Seek, int(const net::HttpByteRange& byte_range)); | 112 MOCK_METHOD1(Seek, int(const net::HttpByteRange& byte_range)); |
115 MOCK_METHOD2(ReadRawData, int(net::IOBuffer* buffer, int buffer_size)); | 113 MOCK_METHOD2(ReadRawData, int(net::IOBuffer* buffer, int buffer_size)); |
116 }; | 114 }; |
117 | 115 |
118 | 116 |
119 class TestStreamReaderJob : public AndroidStreamReaderURLRequestJob { | 117 class TestStreamReaderJob : public AndroidStreamReaderURLRequestJob { |
120 public: | 118 public: |
121 TestStreamReaderJob( | 119 TestStreamReaderJob( |
122 net::URLRequest* request, | 120 net::URLRequest* request, |
123 net::NetworkDelegate* network_delegate, | 121 net::NetworkDelegate* network_delegate, |
124 scoped_ptr<Delegate> delegate, | 122 scoped_ptr<Delegate> delegate, |
125 scoped_ptr<InputStreamReader> stream_reader) | 123 scoped_ptr<InputStreamReader> stream_reader) |
126 : AndroidStreamReaderURLRequestJob(request, | 124 : AndroidStreamReaderURLRequestJob(request, |
127 network_delegate, | 125 network_delegate, |
128 delegate.Pass()), | 126 delegate.Pass()), |
129 stream_reader_(stream_reader.Pass()) { | 127 stream_reader_(stream_reader.Pass()) { |
130 message_loop_proxy_ = base::MessageLoopProxy::current(); | 128 message_loop_proxy_ = base::MessageLoopProxy::current(); |
131 } | 129 } |
132 | 130 |
133 virtual scoped_ptr<InputStreamReader> CreateStreamReader( | 131 virtual scoped_ptr<InputStreamReader> CreateStreamReader( |
134 InputStream* stream) { | 132 InputStream* stream) OVERRIDE { |
135 return stream_reader_.Pass(); | 133 return stream_reader_.Pass(); |
136 } | 134 } |
137 protected: | 135 protected: |
138 virtual ~TestStreamReaderJob() {} | 136 virtual ~TestStreamReaderJob() {} |
139 | 137 |
140 virtual base::TaskRunner* GetWorkerThreadRunner() { | 138 virtual base::TaskRunner* GetWorkerThreadRunner() OVERRIDE { |
141 return message_loop_proxy_.get(); | 139 return message_loop_proxy_.get(); |
142 } | 140 } |
143 | 141 |
144 scoped_ptr<InputStreamReader> stream_reader_; | 142 scoped_ptr<InputStreamReader> stream_reader_; |
145 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 143 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
146 }; | 144 }; |
147 | 145 |
148 class AndroidStreamReaderURLRequestJobTest : public Test { | 146 class AndroidStreamReaderURLRequestJobTest : public Test { |
149 public: | 147 public: |
150 AndroidStreamReaderURLRequestJobTest() : loop_(base::MessageLoop::TYPE_IO) {} | 148 AndroidStreamReaderURLRequestJobTest() : loop_(base::MessageLoop::TYPE_IO) {} |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 | 348 |
351 SetRange(req_.get(), offset, bytes_available); | 349 SetRange(req_.get(), offset, bytes_available); |
352 req_->Start(); | 350 req_->Start(); |
353 | 351 |
354 loop.Run(); | 352 loop.Run(); |
355 | 353 |
356 EXPECT_EQ(0, network_delegate_.completed_requests()); | 354 EXPECT_EQ(0, network_delegate_.completed_requests()); |
357 req_->Cancel(); | 355 req_->Cancel(); |
358 EXPECT_EQ(1, network_delegate_.completed_requests()); | 356 EXPECT_EQ(1, network_delegate_.completed_requests()); |
359 } | 357 } |
OLD | NEW |