Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2128)

Side by Side Diff: content/browser/background_fetch/background_fetch_event_dispatcher_unittest.cc

Issue 2767093004: Implement the BackgroundFetch{Fail,ed} Service Worker events (Closed)
Patch Set: forward declare the data view Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "content/browser/background_fetch/background_fetch_event_dispatcher.h" 5 #include "content/browser/background_fetch/background_fetch_event_dispatcher.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <memory> 8 #include <memory>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/optional.h" 14 #include "base/optional.h"
15 #include "base/run_loop.h" 15 #include "base/run_loop.h"
16 #include "base/test/histogram_tester.h" 16 #include "base/test/histogram_tester.h"
17 #include "content/browser/service_worker/embedded_worker_test_helper.h" 17 #include "content/browser/service_worker/embedded_worker_test_helper.h"
18 #include "content/browser/service_worker/service_worker_context_wrapper.h" 18 #include "content/browser/service_worker/service_worker_context_wrapper.h"
19 #include "content/common/background_fetch/background_fetch_types.h"
19 #include "content/common/service_worker/service_worker_status_code.h" 20 #include "content/common/service_worker/service_worker_status_code.h"
20 #include "content/common/service_worker/service_worker_types.h" 21 #include "content/common/service_worker/service_worker_types.h"
21 #include "content/public/test/test_browser_thread_bundle.h" 22 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
23 24
24 namespace content { 25 namespace content {
25 namespace { 26 namespace {
26 27
27 const char kExampleOrigin[] = "https://example.com/"; 28 const char kExampleOrigin[] = "https://example.com/";
28 const char kExampleScriptUrl[] = "https://example.com/sw.js"; 29 const char kExampleScriptUrl[] = "https://example.com/sw.js";
29 const char kExampleTag[] = "my-tag"; 30 const char kExampleTag[] = "my-tag";
30 const char kExampleTag2[] = "my-second-tag"; 31 const char kExampleTag2[] = "my-second-tag";
31 32
32 // Extension of the EmbeddedWorkerTestHelper that enables instrumentation of the 33 // Extension of the EmbeddedWorkerTestHelper that enables instrumentation of the
33 // events related to the Background Fetch API. Storage for these tests will 34 // events related to the Background Fetch API. Storage for these tests will
34 // always be kept in memory, as data persistence is tested elsewhere. 35 // always be kept in memory, as data persistence is tested elsewhere.
35 class BackgroundFetchEmbeddedWorkerTestHelper 36 class BackgroundFetchEmbeddedWorkerTestHelper
36 : public EmbeddedWorkerTestHelper { 37 : public EmbeddedWorkerTestHelper {
37 public: 38 public:
38 BackgroundFetchEmbeddedWorkerTestHelper() 39 BackgroundFetchEmbeddedWorkerTestHelper()
39 : EmbeddedWorkerTestHelper(base::FilePath() /* in memory */) {} 40 : EmbeddedWorkerTestHelper(base::FilePath() /* in memory */) {}
40 ~BackgroundFetchEmbeddedWorkerTestHelper() override = default; 41 ~BackgroundFetchEmbeddedWorkerTestHelper() override = default;
41 42
42 void set_fail_abort_event(bool fail) { fail_abort_event_ = fail; } 43 void set_fail_abort_event(bool fail) { fail_abort_event_ = fail; }
43 44
44 void set_fail_click_event(bool fail) { fail_click_event_ = fail; } 45 void set_fail_click_event(bool fail) { fail_click_event_ = fail; }
45 46
47 void set_fail_fail_event(bool fail) { fail_fail_event_ = fail; }
48
49 void set_fail_fetched_event(bool fail) { fail_fetched_event_ = fail; }
50
46 const base::Optional<std::string>& last_tag() const { return last_tag_; } 51 const base::Optional<std::string>& last_tag() const { return last_tag_; }
47 const base::Optional<mojom::BackgroundFetchState>& last_state() const { 52 const base::Optional<mojom::BackgroundFetchState>& last_state() const {
48 return last_state_; 53 return last_state_;
49 } 54 }
55 const base::Optional<std::vector<BackgroundFetchSettledFetch>> last_fetches()
56 const {
57 return last_fetches_;
58 }
50 59
51 protected: 60 protected:
52 void OnBackgroundFetchAbortEvent( 61 void OnBackgroundFetchAbortEvent(
53 const std::string& tag, 62 const std::string& tag,
54 const mojom::ServiceWorkerEventDispatcher:: 63 const mojom::ServiceWorkerEventDispatcher::
55 DispatchBackgroundFetchAbortEventCallback& callback) override { 64 DispatchBackgroundFetchAbortEventCallback& callback) override {
56 last_tag_ = tag; 65 last_tag_ = tag;
57 66
58 if (fail_abort_event_) { 67 if (fail_abort_event_) {
59 callback.Run(SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, 68 callback.Run(SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED,
(...skipping 12 matching lines...) Expand all
72 last_state_ = state; 81 last_state_ = state;
73 82
74 if (fail_click_event_) { 83 if (fail_click_event_) {
75 callback.Run(SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, 84 callback.Run(SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED,
76 base::Time::Now()); 85 base::Time::Now());
77 } else { 86 } else {
78 callback.Run(SERVICE_WORKER_OK, base::Time::Now()); 87 callback.Run(SERVICE_WORKER_OK, base::Time::Now());
79 } 88 }
80 } 89 }
81 90
91 void OnBackgroundFetchFailEvent(
92 const std::string& tag,
93 const std::vector<BackgroundFetchSettledFetch>& fetches,
94 const mojom::ServiceWorkerEventDispatcher::
95 DispatchBackgroundFetchFailEventCallback& callback) override {
96 last_tag_ = tag;
97 last_fetches_ = fetches;
98
99 if (fail_fail_event_) {
100 callback.Run(SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED,
101 base::Time::Now());
102 } else {
103 callback.Run(SERVICE_WORKER_OK, base::Time::Now());
104 }
105 }
106
107 void OnBackgroundFetchedEvent(
108 const std::string& tag,
109 const std::vector<BackgroundFetchSettledFetch>& fetches,
110 const mojom::ServiceWorkerEventDispatcher::
111 DispatchBackgroundFetchedEventCallback& callback) override {
112 last_tag_ = tag;
113 last_fetches_ = fetches;
114
115 if (fail_fetched_event_) {
116 callback.Run(SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED,
117 base::Time::Now());
118 } else {
119 callback.Run(SERVICE_WORKER_OK, base::Time::Now());
120 }
121 }
122
82 private: 123 private:
83 bool fail_abort_event_ = false; 124 bool fail_abort_event_ = false;
84 bool fail_click_event_ = false; 125 bool fail_click_event_ = false;
126 bool fail_fail_event_ = false;
127 bool fail_fetched_event_ = false;
85 128
86 base::Optional<std::string> last_tag_; 129 base::Optional<std::string> last_tag_;
87 base::Optional<mojom::BackgroundFetchState> last_state_; 130 base::Optional<mojom::BackgroundFetchState> last_state_;
131 base::Optional<std::vector<BackgroundFetchSettledFetch>> last_fetches_;
88 132
89 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchEmbeddedWorkerTestHelper); 133 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchEmbeddedWorkerTestHelper);
90 }; 134 };
91 135
92 class BackgroundFetchEventDispatcherTest : public ::testing::Test { 136 class BackgroundFetchEventDispatcherTest : public ::testing::Test {
93 public: 137 public:
94 BackgroundFetchEventDispatcherTest() 138 BackgroundFetchEventDispatcherTest()
95 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), 139 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
96 event_dispatcher_(embedded_worker_test_helper_.context_wrapper()) {} 140 event_dispatcher_(embedded_worker_test_helper_.context_wrapper()) {}
97 141
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 "BackgroundFetch.EventDispatchResult.ClickEvent", 349 "BackgroundFetch.EventDispatchResult.ClickEvent",
306 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1); 350 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1);
307 histogram_tester()->ExpectBucketCount( 351 histogram_tester()->ExpectBucketCount(
308 "BackgroundFetch.EventDispatchResult.ClickEvent", 352 "BackgroundFetch.EventDispatchResult.ClickEvent",
309 BackgroundFetchEventDispatcher::DISPATCH_RESULT_CANNOT_DISPATCH_EVENT, 1); 353 BackgroundFetchEventDispatcher::DISPATCH_RESULT_CANNOT_DISPATCH_EVENT, 1);
310 histogram_tester()->ExpectUniqueSample( 354 histogram_tester()->ExpectUniqueSample(
311 "BackgroundFetch.EventDispatchFailure.Dispatch.ClickEvent", 355 "BackgroundFetch.EventDispatchFailure.Dispatch.ClickEvent",
312 SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, 1); 356 SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, 1);
313 } 357 }
314 358
359 TEST_F(BackgroundFetchEventDispatcherTest, DispatchFailEvent) {
360 auto service_worker_registration = RegisterServiceWorker();
361 ASSERT_TRUE(service_worker_registration);
362 ASSERT_TRUE(service_worker_registration->active_version());
363
364 GURL origin(kExampleOrigin);
365
366 std::vector<BackgroundFetchSettledFetch> fetches;
367 fetches.push_back(BackgroundFetchSettledFetch());
368
369 {
370 base::RunLoop run_loop;
371 dispatcher()->DispatchBackgroundFetchFailEvent(
372 service_worker_registration->id(), origin, kExampleTag, fetches,
373 run_loop.QuitClosure());
374
375 run_loop.Run();
376 }
377
378 ASSERT_TRUE(test_helpers()->last_tag().has_value());
379 EXPECT_EQ(kExampleTag, test_helpers()->last_tag().value());
380
381 ASSERT_TRUE(test_helpers()->last_fetches().has_value());
382 EXPECT_EQ(fetches.size(), test_helpers()->last_fetches()->size());
383
384 histogram_tester()->ExpectUniqueSample(
385 "BackgroundFetch.EventDispatchResult.FailEvent",
386 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1);
387
388 fetches.push_back(BackgroundFetchSettledFetch());
389
390 test_helpers()->set_fail_fail_event(true);
391
392 {
393 base::RunLoop run_loop;
394 dispatcher()->DispatchBackgroundFetchFailEvent(
395 service_worker_registration->id(), origin, kExampleTag2, fetches,
396 run_loop.QuitClosure());
397
398 run_loop.Run();
399 }
400
401 ASSERT_TRUE(test_helpers()->last_tag().has_value());
402 EXPECT_EQ(kExampleTag2, test_helpers()->last_tag().value());
403
404 ASSERT_TRUE(test_helpers()->last_fetches().has_value());
405 EXPECT_EQ(fetches.size(), test_helpers()->last_fetches()->size());
406
407 histogram_tester()->ExpectBucketCount(
408 "BackgroundFetch.EventDispatchResult.FailEvent",
409 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1);
410 histogram_tester()->ExpectBucketCount(
411 "BackgroundFetch.EventDispatchResult.FailEvent",
412 BackgroundFetchEventDispatcher::DISPATCH_RESULT_CANNOT_DISPATCH_EVENT, 1);
413 histogram_tester()->ExpectUniqueSample(
414 "BackgroundFetch.EventDispatchFailure.Dispatch.FailEvent",
415 SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, 1);
416 }
417
418 TEST_F(BackgroundFetchEventDispatcherTest, DispatchFetchedEvent) {
419 auto service_worker_registration = RegisterServiceWorker();
420 ASSERT_TRUE(service_worker_registration);
421 ASSERT_TRUE(service_worker_registration->active_version());
422
423 GURL origin(kExampleOrigin);
424
425 std::vector<BackgroundFetchSettledFetch> fetches;
426 fetches.push_back(BackgroundFetchSettledFetch());
427
428 {
429 base::RunLoop run_loop;
430 dispatcher()->DispatchBackgroundFetchedEvent(
431 service_worker_registration->id(), origin, kExampleTag, fetches,
432 run_loop.QuitClosure());
433
434 run_loop.Run();
435 }
436
437 ASSERT_TRUE(test_helpers()->last_tag().has_value());
438 EXPECT_EQ(kExampleTag, test_helpers()->last_tag().value());
439
440 ASSERT_TRUE(test_helpers()->last_fetches().has_value());
441 EXPECT_EQ(fetches.size(), test_helpers()->last_fetches()->size());
442
443 histogram_tester()->ExpectUniqueSample(
444 "BackgroundFetch.EventDispatchResult.FetchedEvent",
445 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1);
446
447 fetches.push_back(BackgroundFetchSettledFetch());
448
449 test_helpers()->set_fail_fetched_event(true);
450
451 {
452 base::RunLoop run_loop;
453 dispatcher()->DispatchBackgroundFetchedEvent(
454 service_worker_registration->id(), origin, kExampleTag2, fetches,
455 run_loop.QuitClosure());
456
457 run_loop.Run();
458 }
459
460 ASSERT_TRUE(test_helpers()->last_tag().has_value());
461 EXPECT_EQ(kExampleTag2, test_helpers()->last_tag().value());
462
463 ASSERT_TRUE(test_helpers()->last_fetches().has_value());
464 EXPECT_EQ(fetches.size(), test_helpers()->last_fetches()->size());
465
466 histogram_tester()->ExpectBucketCount(
467 "BackgroundFetch.EventDispatchResult.FetchedEvent",
468 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS, 1);
469 histogram_tester()->ExpectBucketCount(
470 "BackgroundFetch.EventDispatchResult.FetchedEvent",
471 BackgroundFetchEventDispatcher::DISPATCH_RESULT_CANNOT_DISPATCH_EVENT, 1);
472 histogram_tester()->ExpectUniqueSample(
473 "BackgroundFetch.EventDispatchFailure.Dispatch.FetchedEvent",
474 SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, 1);
475 }
476
315 } // namespace 477 } // namespace
316 } // namespace content 478 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698