OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "base/test/mock_callback.h" | 5 #include "base/test/mock_callback.h" |
6 #include "components/offline_pages/core/prefetch/generate_page_bundle_request.h" | 6 #include "components/offline_pages/core/prefetch/generate_page_bundle_request.h" |
7 #include "components/offline_pages/core/prefetch/get_operation_request.h" | 7 #include "components/offline_pages/core/prefetch/get_operation_request.h" |
8 #include "components/offline_pages/core/prefetch/prefetch_proto_utils.h" | 8 #include "components/offline_pages/core/prefetch/prefetch_proto_utils.h" |
9 #include "components/offline_pages/core/prefetch/prefetch_request_test_base.h" | 9 #include "components/offline_pages/core/prefetch/prefetch_request_test_base.h" |
10 #include "components/offline_pages/core/prefetch/prefetch_types.h" | 10 #include "components/offline_pages/core/prefetch/prefetch_types.h" |
11 #include "components/offline_pages/core/prefetch/proto/offline_pages.pb.h" | 11 #include "components/offline_pages/core/prefetch/proto/offline_pages.pb.h" |
12 #include "components/offline_pages/core/prefetch/proto/operation.pb.h" | 12 #include "components/offline_pages/core/prefetch/proto/operation.pb.h" |
13 #include "net/http/http_status_code.h" | 13 #include "net/http/http_status_code.h" |
14 #include "net/url_request/url_request_status.h" | 14 #include "net/url_request/url_request_status.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
17 | 17 |
18 using testing::_; | 18 using testing::_; |
19 using testing::DoAll; | 19 using testing::DoAll; |
20 using testing::Eq; | 20 using testing::Eq; |
21 using testing::SaveArg; | 21 using testing::SaveArg; |
22 | 22 |
23 namespace offline_pages { | 23 namespace offline_pages { |
24 | 24 |
25 namespace { | 25 namespace { |
26 const version_info::Channel kTestChannel = version_info::Channel::UNKNOWN; | 26 const version_info::Channel kTestChannel = version_info::Channel::UNKNOWN; |
| 27 const char kTestOperationName[] = "operation/test123"; |
27 const char kTestURL[] = "http://example.com"; | 28 const char kTestURL[] = "http://example.com"; |
28 const char kTestURL2[] = "http://example.com/2"; | 29 const char kTestURL2[] = "http://example.com/2"; |
29 const char kTestURL3[] = "http://example.com/3"; | 30 const char kTestURL3[] = "http://example.com/3"; |
30 const char kTestURL4[] = "http://example.com/4"; | 31 const char kTestURL4[] = "http://example.com/4"; |
31 const char kTestUserAgent[] = "Test User Agent"; | 32 const char kTestUserAgent[] = "Test User Agent"; |
32 const char kTestGCMID[] = "Test GCM ID"; | 33 const char kTestGCMID[] = "Test GCM ID"; |
33 const int kTestMaxBundleSize = 100000; | 34 const int kTestMaxBundleSize = 100000; |
34 const char kTestBodyName[] = "body_name"; | 35 const char kTestBodyName[] = "body_name"; |
35 const int64_t kTestBodyLength = 12345678LL; | 36 const int64_t kTestBodyLength = 12345678LL; |
36 const char kTestMethodName[] = "Test name"; | 37 const char kTestMethodName[] = "Test name"; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 | 95 |
95 protected: | 96 protected: |
96 // Helper function to build the operation based on |is_done| that controls | 97 // Helper function to build the operation based on |is_done| that controls |
97 // where Any value goes. | 98 // where Any value goes. |
98 std::string BuildOperation(bool is_done, | 99 std::string BuildOperation(bool is_done, |
99 int error_code, | 100 int error_code, |
100 const std::string& error_message, | 101 const std::string& error_message, |
101 const std::string& any_type_url, | 102 const std::string& any_type_url, |
102 const std::string& any_value) { | 103 const std::string& any_value) { |
103 proto::Operation operation; | 104 proto::Operation operation; |
| 105 operation.set_name(kTestOperationName); |
104 operation.set_done(is_done); | 106 operation.set_done(is_done); |
105 if (error_code != proto::OK) { | 107 if (error_code != proto::OK) { |
106 operation.mutable_error()->set_code(error_code); | 108 operation.mutable_error()->set_code(error_code); |
107 operation.mutable_error()->set_message(error_message); | 109 operation.mutable_error()->set_message(error_message); |
108 } | 110 } |
109 if (!any_type_url.empty()) { | 111 if (!any_type_url.empty()) { |
110 proto::Any* any = | 112 proto::Any* any = |
111 is_done ? operation.mutable_response() : operation.mutable_metadata(); | 113 is_done ? operation.mutable_response() : operation.mutable_metadata(); |
112 any->set_type_url(any_type_url); | 114 any->set_type_url(any_type_url); |
113 any->set_value(any_value); | 115 any->set_value(any_value); |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 } | 229 } |
228 | 230 |
229 PrefetchRequestStatus SendWithPageBundleResponse( | 231 PrefetchRequestStatus SendWithPageBundleResponse( |
230 const proto::PageBundle& bundle) { | 232 const proto::PageBundle& bundle) { |
231 std::string bundle_data; | 233 std::string bundle_data; |
232 EXPECT_TRUE(bundle.SerializeToString(&bundle_data)); | 234 EXPECT_TRUE(bundle.SerializeToString(&bundle_data)); |
233 return SendWithResponse( | 235 return SendWithResponse( |
234 builder_.BuildFromAny(kPageBundleTypeURL, bundle_data)); | 236 builder_.BuildFromAny(kPageBundleTypeURL, bundle_data)); |
235 } | 237 } |
236 | 238 |
| 239 const std::string& operation_name() const { return operation_name_; } |
237 const std::vector<RenderPageInfo>& pages() const { return pages_; } | 240 const std::vector<RenderPageInfo>& pages() const { return pages_; } |
238 | 241 |
239 private: | 242 private: |
240 PrefetchRequestStatus SendWithResponse(const std::string& response_data) { | 243 PrefetchRequestStatus SendWithResponse(const std::string& response_data) { |
241 base::MockCallback<PrefetchRequestFinishedCallback> callback; | 244 base::MockCallback<PrefetchRequestFinishedCallback> callback; |
242 builder_.CreateRequest(request_context(), callback.Get()); | 245 builder_.CreateRequest(request_context(), callback.Get()); |
243 | 246 |
244 PrefetchRequestStatus status; | 247 PrefetchRequestStatus status; |
| 248 operation_name_.clear(); |
245 pages_.clear(); | 249 pages_.clear(); |
246 EXPECT_CALL(callback, Run(_, _)) | 250 EXPECT_CALL(callback, Run(_, _, _)) |
247 .WillOnce(DoAll(SaveArg<0>(&status), SaveArg<1>(&pages_))); | 251 .WillOnce(DoAll(SaveArg<0>(&status), SaveArg<1>(&operation_name_), |
| 252 SaveArg<2>(&pages_))); |
248 RespondWithData(response_data); | 253 RespondWithData(response_data); |
249 return status; | 254 return status; |
250 } | 255 } |
251 | 256 |
252 T builder_; | 257 T builder_; |
| 258 std::string operation_name_; |
253 std::vector<RenderPageInfo> pages_; | 259 std::vector<RenderPageInfo> pages_; |
254 }; | 260 }; |
255 | 261 |
256 // Lists all scenarios we want to test. | 262 // Lists all scenarios we want to test. |
257 typedef testing::Types<GeneratePageBundleRequestDoneOperationBuilder, | 263 typedef testing::Types<GeneratePageBundleRequestDoneOperationBuilder, |
258 GeneratePageBundleRequestPendingOperationBuilder, | 264 GeneratePageBundleRequestPendingOperationBuilder, |
259 GetOperationRequestDoneOperationBuilder, | 265 GetOperationRequestDoneOperationBuilder, |
260 GetOperationRequestPendingOperationBuilder> | 266 GetOperationRequestPendingOperationBuilder> |
261 MyTypes; | 267 MyTypes; |
262 TYPED_TEST_CASE(PrefetchRequestOperationResponseTest, MyTypes); | 268 TYPED_TEST_CASE(PrefetchRequestOperationResponseTest, MyTypes); |
263 | 269 |
264 TYPED_TEST(PrefetchRequestOperationResponseTest, EmptyOperation) { | 270 TYPED_TEST(PrefetchRequestOperationResponseTest, EmptyOperation) { |
265 EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, | 271 EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, |
266 // No error is set for OK. Thus this will cause the operation | 272 // No error is set for OK. Thus this will cause the operation |
267 // being filled with only done flag. | 273 // being filled with only done flag. |
268 this->SendWithErrorResponse(proto::OK, "")); | 274 this->SendWithErrorResponse(proto::OK, "")); |
| 275 EXPECT_TRUE(this->operation_name().empty()); |
269 EXPECT_TRUE(this->pages().empty()); | 276 EXPECT_TRUE(this->pages().empty()); |
270 } | 277 } |
271 | 278 |
272 TYPED_TEST(PrefetchRequestOperationResponseTest, ErrorValue) { | 279 TYPED_TEST(PrefetchRequestOperationResponseTest, ErrorValue) { |
273 EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, | 280 EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, |
274 this->SendWithErrorResponse(proto::UNKNOWN, kErrorMessage)); | 281 this->SendWithErrorResponse(proto::UNKNOWN, kErrorMessage)); |
| 282 EXPECT_TRUE(this->operation_name().empty()); |
275 EXPECT_TRUE(this->pages().empty()); | 283 EXPECT_TRUE(this->pages().empty()); |
276 } | 284 } |
277 | 285 |
278 TYPED_TEST(PrefetchRequestOperationResponseTest, InvalidTypeUrl) { | 286 TYPED_TEST(PrefetchRequestOperationResponseTest, InvalidTypeUrl) { |
279 EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, | 287 EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, |
280 this->SendWithAnyResponse("foo", "")); | 288 this->SendWithAnyResponse("foo", "")); |
| 289 EXPECT_TRUE(this->operation_name().empty()); |
281 EXPECT_TRUE(this->pages().empty()); | 290 EXPECT_TRUE(this->pages().empty()); |
282 } | 291 } |
283 | 292 |
284 TYPED_TEST(PrefetchRequestOperationResponseTest, InvalidValue) { | 293 TYPED_TEST(PrefetchRequestOperationResponseTest, InvalidValue) { |
285 EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, | 294 EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, |
286 this->SendWithAnyResponse(kPageBundleTypeURL, "foo")); | 295 this->SendWithAnyResponse(kPageBundleTypeURL, "foo")); |
| 296 EXPECT_TRUE(this->operation_name().empty()); |
287 EXPECT_TRUE(this->pages().empty()); | 297 EXPECT_TRUE(this->pages().empty()); |
288 } | 298 } |
289 | 299 |
290 TYPED_TEST(PrefetchRequestOperationResponseTest, EmptyPageBundle) { | 300 TYPED_TEST(PrefetchRequestOperationResponseTest, EmptyPageBundle) { |
291 proto::PageBundle bundle; | 301 proto::PageBundle bundle; |
292 EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, | 302 EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, |
293 this->SendWithPageBundleResponse(bundle)); | 303 this->SendWithPageBundleResponse(bundle)); |
| 304 EXPECT_TRUE(this->operation_name().empty()); |
294 EXPECT_TRUE(this->pages().empty()); | 305 EXPECT_TRUE(this->pages().empty()); |
295 } | 306 } |
296 | 307 |
297 TYPED_TEST(PrefetchRequestOperationResponseTest, EmptyArchive) { | 308 TYPED_TEST(PrefetchRequestOperationResponseTest, EmptyArchive) { |
298 proto::PageBundle bundle; | 309 proto::PageBundle bundle; |
299 bundle.add_archives(); | 310 bundle.add_archives(); |
300 EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, | 311 EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, |
301 this->SendWithPageBundleResponse(bundle)); | 312 this->SendWithPageBundleResponse(bundle)); |
| 313 EXPECT_TRUE(this->operation_name().empty()); |
302 EXPECT_TRUE(this->pages().empty()); | 314 EXPECT_TRUE(this->pages().empty()); |
303 } | 315 } |
304 | 316 |
305 TYPED_TEST(PrefetchRequestOperationResponseTest, NoPageInfo) { | 317 TYPED_TEST(PrefetchRequestOperationResponseTest, NoPageInfo) { |
306 proto::PageBundle bundle; | 318 proto::PageBundle bundle; |
307 proto::Archive* archive = bundle.add_archives(); | 319 proto::Archive* archive = bundle.add_archives(); |
308 archive->set_body_name(kTestBodyName); | 320 archive->set_body_name(kTestBodyName); |
309 archive->set_body_length(kTestBodyLength); | 321 archive->set_body_length(kTestBodyLength); |
310 EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, | 322 EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, |
311 this->SendWithPageBundleResponse(bundle)); | 323 this->SendWithPageBundleResponse(bundle)); |
| 324 EXPECT_TRUE(this->operation_name().empty()); |
312 EXPECT_TRUE(this->pages().empty()); | 325 EXPECT_TRUE(this->pages().empty()); |
313 } | 326 } |
314 | 327 |
315 TYPED_TEST(PrefetchRequestOperationResponseTest, MissingPageInfoUrl) { | 328 TYPED_TEST(PrefetchRequestOperationResponseTest, MissingPageInfoUrl) { |
316 proto::PageBundle bundle; | 329 proto::PageBundle bundle; |
317 proto::Archive* archive = bundle.add_archives(); | 330 proto::Archive* archive = bundle.add_archives(); |
318 proto::PageInfo* page_info = archive->add_page_infos(); | 331 proto::PageInfo* page_info = archive->add_page_infos(); |
319 page_info->set_redirect_url(kTestURL); | 332 page_info->set_redirect_url(kTestURL); |
320 EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, | 333 EXPECT_EQ(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, |
321 this->SendWithPageBundleResponse(bundle)); | 334 this->SendWithPageBundleResponse(bundle)); |
| 335 EXPECT_TRUE(this->operation_name().empty()); |
322 EXPECT_TRUE(this->pages().empty()); | 336 EXPECT_TRUE(this->pages().empty()); |
323 } | 337 } |
324 | 338 |
325 TYPED_TEST(PrefetchRequestOperationResponseTest, SinglePage) { | 339 TYPED_TEST(PrefetchRequestOperationResponseTest, SinglePage) { |
326 proto::PageBundle bundle; | 340 proto::PageBundle bundle; |
327 proto::Archive* archive = bundle.add_archives(); | 341 proto::Archive* archive = bundle.add_archives(); |
328 archive->set_body_name(kTestBodyName); | 342 archive->set_body_name(kTestBodyName); |
329 archive->set_body_length(kTestBodyLength); | 343 archive->set_body_length(kTestBodyLength); |
330 proto::PageInfo* page_info = archive->add_page_infos(); | 344 proto::PageInfo* page_info = archive->add_page_infos(); |
331 page_info->set_url(kTestURL); | 345 page_info->set_url(kTestURL); |
332 page_info->set_redirect_url(kTestURL2); | 346 page_info->set_redirect_url(kTestURL2); |
333 page_info->mutable_status()->set_code(proto::OK); | 347 page_info->mutable_status()->set_code(proto::OK); |
334 page_info->set_transformation(proto::NO_TRANSFORMATION); | 348 page_info->set_transformation(proto::NO_TRANSFORMATION); |
335 int64_t ms_since_epoch = base::Time::Now().ToJavaTime(); | 349 int64_t ms_since_epoch = base::Time::Now().ToJavaTime(); |
336 page_info->mutable_render_time()->set_seconds(ms_since_epoch / 1000); | 350 page_info->mutable_render_time()->set_seconds(ms_since_epoch / 1000); |
337 page_info->mutable_render_time()->set_nanos((ms_since_epoch % 1000) * | 351 page_info->mutable_render_time()->set_nanos((ms_since_epoch % 1000) * |
338 1000000); | 352 1000000); |
339 EXPECT_EQ(PrefetchRequestStatus::SUCCESS, | 353 EXPECT_EQ(PrefetchRequestStatus::SUCCESS, |
340 this->SendWithPageBundleResponse(bundle)); | 354 this->SendWithPageBundleResponse(bundle)); |
| 355 EXPECT_EQ(kTestOperationName, this->operation_name()); |
341 ASSERT_EQ(1u, this->pages().size()); | 356 ASSERT_EQ(1u, this->pages().size()); |
342 EXPECT_EQ(kTestURL, this->pages().back().url); | 357 EXPECT_EQ(kTestURL, this->pages().back().url); |
343 EXPECT_EQ(kTestURL2, this->pages().back().redirect_url); | 358 EXPECT_EQ(kTestURL2, this->pages().back().redirect_url); |
344 EXPECT_EQ(RenderStatus::RENDERED, this->pages().back().status); | 359 EXPECT_EQ(RenderStatus::RENDERED, this->pages().back().status); |
345 EXPECT_EQ(kTestBodyName, this->pages().back().body_name); | 360 EXPECT_EQ(kTestBodyName, this->pages().back().body_name); |
346 EXPECT_EQ(kTestBodyLength, this->pages().back().body_length); | 361 EXPECT_EQ(kTestBodyLength, this->pages().back().body_length); |
347 EXPECT_EQ(ms_since_epoch, this->pages().back().render_time.ToJavaTime()); | 362 EXPECT_EQ(ms_since_epoch, this->pages().back().render_time.ToJavaTime()); |
348 } | 363 } |
349 | 364 |
350 TYPED_TEST(PrefetchRequestOperationResponseTest, MultiplePages) { | 365 TYPED_TEST(PrefetchRequestOperationResponseTest, MultiplePages) { |
(...skipping 25 matching lines...) Expand all Loading... |
376 page_info->set_url(kTestURL4); | 391 page_info->set_url(kTestURL4); |
377 page_info->mutable_status()->set_code(proto::OK); | 392 page_info->mutable_status()->set_code(proto::OK); |
378 page_info->set_transformation(proto::NO_TRANSFORMATION); | 393 page_info->set_transformation(proto::NO_TRANSFORMATION); |
379 int64_t ms_since_epoch = base::Time::Now().ToJavaTime(); | 394 int64_t ms_since_epoch = base::Time::Now().ToJavaTime(); |
380 page_info->mutable_render_time()->set_seconds(ms_since_epoch / 1000); | 395 page_info->mutable_render_time()->set_seconds(ms_since_epoch / 1000); |
381 page_info->mutable_render_time()->set_nanos((ms_since_epoch % 1000) * | 396 page_info->mutable_render_time()->set_nanos((ms_since_epoch % 1000) * |
382 1000000); | 397 1000000); |
383 | 398 |
384 EXPECT_EQ(PrefetchRequestStatus::SUCCESS, | 399 EXPECT_EQ(PrefetchRequestStatus::SUCCESS, |
385 this->SendWithPageBundleResponse(bundle)); | 400 this->SendWithPageBundleResponse(bundle)); |
| 401 EXPECT_EQ(kTestOperationName, this->operation_name()); |
386 ASSERT_EQ(4u, this->pages().size()); | 402 ASSERT_EQ(4u, this->pages().size()); |
387 EXPECT_EQ(kTestURL, this->pages().at(0).url); | 403 EXPECT_EQ(kTestURL, this->pages().at(0).url); |
388 EXPECT_EQ(RenderStatus::PENDING, this->pages().at(0).status); | 404 EXPECT_EQ(RenderStatus::PENDING, this->pages().at(0).status); |
389 EXPECT_EQ(kTestURL2, this->pages().at(1).url); | 405 EXPECT_EQ(kTestURL2, this->pages().at(1).url); |
390 EXPECT_EQ(RenderStatus::EXCEEDED_LIMIT, this->pages().at(1).status); | 406 EXPECT_EQ(RenderStatus::EXCEEDED_LIMIT, this->pages().at(1).status); |
391 EXPECT_EQ(kTestURL3, this->pages().at(2).url); | 407 EXPECT_EQ(kTestURL3, this->pages().at(2).url); |
392 EXPECT_EQ(RenderStatus::FAILED, this->pages().at(2).status); | 408 EXPECT_EQ(RenderStatus::FAILED, this->pages().at(2).status); |
393 EXPECT_EQ(kTestURL4, this->pages().at(3).url); | 409 EXPECT_EQ(kTestURL4, this->pages().at(3).url); |
394 EXPECT_EQ(RenderStatus::RENDERED, this->pages().at(3).status); | 410 EXPECT_EQ(RenderStatus::RENDERED, this->pages().at(3).status); |
395 EXPECT_EQ(kTestBodyName, this->pages().at(3).body_name); | 411 EXPECT_EQ(kTestBodyName, this->pages().at(3).body_name); |
396 EXPECT_EQ(kTestBodyLength, this->pages().at(3).body_length); | 412 EXPECT_EQ(kTestBodyLength, this->pages().at(3).body_length); |
397 EXPECT_EQ(ms_since_epoch, this->pages().at(3).render_time.ToJavaTime()); | 413 EXPECT_EQ(ms_since_epoch, this->pages().at(3).render_time.ToJavaTime()); |
398 } | 414 } |
399 | 415 |
400 } // namespace offline_pages | 416 } // namespace offline_pages |
OLD | NEW |