1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/base/filters.h"
#include "media/base/mock_callback.h"
#include "media/base/mock_filter_host.h"
#include "media/base/mock_filters.h"
#include "net/base/net_errors.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLError.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoader.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h"
#include "webkit/glue/media/simple_data_source.h"
#include "webkit/mocks/mock_webframe.h"
#include "webkit/mocks/mock_weburlloader.h"
using ::testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::NiceMock;
using ::testing::NotNull;
using ::testing::Return;
using ::testing::SetArgumentPointee;
using ::testing::StrictMock;
using ::testing::WithArgs;
using WebKit::WebURLError;
using WebKit::WebURLLoader;
using WebKit::WebURLRequest;
using WebKit::WebURLResponse;
namespace {
const int kDataSize = 1024;
const char kHttpUrl[] = "http://test";
const char kHttpsUrl[] = "https://test";
const char kFileUrl[] = "file://test";
const char kDataUrl[] =
"data:text/plain;base64,YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoK";
const char kDataUrlDecoded[] = "abcdefghijklmnopqrstuvwxyz";
const char kInvalidUrl[] = "whatever://test";
const char kHttpRedirectToSameDomainUrl1[] = "http://test/ing";
const char kHttpRedirectToSameDomainUrl2[] = "http://test/ing2";
const char kHttpRedirectToDifferentDomainUrl1[] = "http://test2";
const char kHttpRedirectToDifferentDomainUrl2[] = "http://test2/ing";
} // namespace
namespace webkit_glue {
class SimpleDataSourceTest : public testing::Test {
public:
SimpleDataSourceTest() {
for (int i = 0; i < kDataSize; ++i) {
data_[i] = i;
}
}
virtual ~SimpleDataSourceTest() {
}
void InitializeDataSource(const char* url,
media::MockCallback* callback) {
gurl_ = GURL(url);
frame_.reset(new NiceMock<MockWebFrame>());
url_loader_ = new NiceMock<MockWebURLLoader>();
data_source_ = new SimpleDataSource(MessageLoop::current(),
frame_.get());
// There is no need to provide a message loop to data source.
data_source_->set_host(&host_);
data_source_->SetURLLoaderForTest(url_loader_);
data_source_->Initialize(url, callback);
MessageLoop::current()->RunAllPending();
}
void RequestSucceeded(bool is_loaded) {
WebURLResponse response(gurl_);
response.setExpectedContentLength(kDataSize);
data_source_->didReceiveResponse(NULL, response);
int64 size;
EXPECT_TRUE(data_source_->GetSize(&size));
EXPECT_EQ(kDataSize, size);
for (int i = 0; i < kDataSize; ++i) {
data_source_->didReceiveData(NULL, data_ + i, 1);
}
EXPECT_CALL(host_, SetLoaded(is_loaded));
InSequence s;
EXPECT_CALL(host_, SetTotalBytes(kDataSize));
EXPECT_CALL(host_, SetBufferedBytes(kDataSize));
data_source_->didFinishLoading(NULL, 0);
// Let the tasks to be executed.
MessageLoop::current()->RunAllPending();
}
void RequestFailed() {
InSequence s;
EXPECT_CALL(host_, SetError(media::PIPELINE_ERROR_NETWORK));
WebURLError error;
error.reason = net::ERR_FAILED;
data_source_->didFail(NULL, error);
// Let the tasks to be executed.
MessageLoop::current()->RunAllPending();
}
void Redirect(const char* url) {
GURL redirectUrl(url);
WebKit::WebURLRequest newRequest(redirectUrl);
WebKit::WebURLResponse redirectResponse(gurl_);
data_source_->willSendRequest(url_loader_, newRequest, redirectResponse);
MessageLoop::current()->RunAllPending();
}
void DestroyDataSource() {
data_source_->Stop(media::NewExpectedCallback());
MessageLoop::current()->RunAllPending();
data_source_ = NULL;
}
void AsyncRead() {
for (int i = 0; i < kDataSize; ++i) {
uint8 buffer[1];
EXPECT_CALL(*this, ReadCallback(1));
data_source_->Read(
i, 1, buffer,
NewCallback(this, &SimpleDataSourceTest::ReadCallback));
EXPECT_EQ(static_cast<uint8>(data_[i]), buffer[0]);
}
}
MOCK_METHOD1(ReadCallback, void(size_t size));
protected:
GURL gurl_;
scoped_ptr<MessageLoop> message_loop_;
NiceMock<MockWebURLLoader>* url_loader_;
scoped_refptr<SimpleDataSource> data_source_;
StrictMock<media::MockFilterHost> host_;
scoped_ptr<NiceMock<MockWebFrame> > frame_;
char data_[kDataSize];
DISALLOW_COPY_AND_ASSIGN(SimpleDataSourceTest);
};
TEST_F(SimpleDataSourceTest, InitializeHTTP) {
InitializeDataSource(kHttpUrl, media::NewExpectedCallback());
RequestSucceeded(false);
DestroyDataSource();
}
TEST_F(SimpleDataSourceTest, InitializeHTTPS) {
InitializeDataSource(kHttpsUrl, media::NewExpectedCallback());
RequestSucceeded(false);
DestroyDataSource();
}
TEST_F(SimpleDataSourceTest, InitializeFile) {
InitializeDataSource(kFileUrl, media::NewExpectedCallback());
RequestSucceeded(true);
DestroyDataSource();
}
TEST_F(SimpleDataSourceTest, InitializeData) {
frame_.reset(new NiceMock<MockWebFrame>());
url_loader_ = new NiceMock<MockWebURLLoader>();
data_source_ = new SimpleDataSource(MessageLoop::current(),
frame_.get());
EXPECT_TRUE(data_source_->IsUrlSupported(kDataUrl));
// There is no need to provide a message loop to data source.
data_source_->set_host(&host_);
data_source_->SetURLLoaderForTest(url_loader_);
EXPECT_CALL(host_, SetLoaded(true));
EXPECT_CALL(host_, SetTotalBytes(sizeof(kDataUrlDecoded)));
EXPECT_CALL(host_, SetBufferedBytes(sizeof(kDataUrlDecoded)));
data_source_->Initialize(kDataUrl, media::NewExpectedCallback());
MessageLoop::current()->RunAllPending();
DestroyDataSource();
}
TEST_F(SimpleDataSourceTest, RequestFailed) {
InitializeDataSource(kHttpUrl, media::NewExpectedCallback());
RequestFailed();
DestroyDataSource();
}
TEST_F(SimpleDataSourceTest, StopWhenDownloading) {
// The callback should be deleted, but not executed.
// TODO(scherkus): should this really be the behaviour? Seems strange...
StrictMock<media::MockCallback>* callback =
new StrictMock<media::MockCallback>();
EXPECT_CALL(*callback, Destructor());
InitializeDataSource(kHttpUrl, callback);
EXPECT_CALL(*url_loader_, cancel());
DestroyDataSource();
}
TEST_F(SimpleDataSourceTest, AsyncRead) {
InitializeDataSource(kFileUrl, media::NewExpectedCallback());
RequestSucceeded(true);
AsyncRead();
DestroyDataSource();
}
// NOTE: This test will need to be reworked a little once
// http://code.google.com/p/chromium/issues/detail?id=72578
// is fixed.
TEST_F(SimpleDataSourceTest, HasSingleOrigin) {
// Make sure no redirect case works as expected.
InitializeDataSource(kHttpUrl, media::NewExpectedCallback());
RequestSucceeded(false);
EXPECT_TRUE(data_source_->HasSingleOrigin());
DestroyDataSource();
// Test redirect to the same domain.
InitializeDataSource(kHttpUrl, media::NewExpectedCallback());
Redirect(kHttpRedirectToSameDomainUrl1);
RequestSucceeded(false);
EXPECT_TRUE(data_source_->HasSingleOrigin());
DestroyDataSource();
// Test redirect twice to the same domain.
InitializeDataSource(kHttpUrl, media::NewExpectedCallback());
Redirect(kHttpRedirectToSameDomainUrl1);
Redirect(kHttpRedirectToSameDomainUrl2);
RequestSucceeded(false);
EXPECT_TRUE(data_source_->HasSingleOrigin());
DestroyDataSource();
// Test redirect to a different domain.
InitializeDataSource(kHttpUrl, media::NewExpectedCallback());
Redirect(kHttpRedirectToDifferentDomainUrl1);
RequestSucceeded(false);
EXPECT_FALSE(data_source_->HasSingleOrigin());
DestroyDataSource();
// Test redirect twice to a different domain.
InitializeDataSource(kHttpUrl, media::NewExpectedCallback());
Redirect(kHttpRedirectToDifferentDomainUrl1);
Redirect(kHttpRedirectToDifferentDomainUrl2);
RequestSucceeded(false);
EXPECT_FALSE(data_source_->HasSingleOrigin());
DestroyDataSource();
// Test to a different domain and then back to the same domain.
// NOTE: A different origin was encountered at least once so that
// makes HasSingleOrigin() become false.
InitializeDataSource(kHttpUrl, media::NewExpectedCallback());
Redirect(kHttpRedirectToDifferentDomainUrl1);
Redirect(kHttpRedirectToSameDomainUrl1);
RequestSucceeded(false);
EXPECT_FALSE(data_source_->HasSingleOrigin());
DestroyDataSource();
}
} // namespace webkit_glue
|