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

Side by Side Diff: content/browser/service_worker/service_worker_version_unittest.cc

Issue 2045153003: Speculatively launch Service Workers on mouse/touch events. [4/5] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add test Created 4 years, 4 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/service_worker/service_worker_version.h" 5 #include "content/browser/service_worker/service_worker_version.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <tuple> 8 #include <tuple>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/test/histogram_tester.h"
13 #include "content/browser/service_worker/embedded_worker_registry.h" 14 #include "content/browser/service_worker/embedded_worker_registry.h"
14 #include "content/browser/service_worker/embedded_worker_status.h" 15 #include "content/browser/service_worker/embedded_worker_status.h"
15 #include "content/browser/service_worker/embedded_worker_test_helper.h" 16 #include "content/browser/service_worker/embedded_worker_test_helper.h"
16 #include "content/browser/service_worker/service_worker_context_core.h" 17 #include "content/browser/service_worker/service_worker_context_core.h"
17 #include "content/browser/service_worker/service_worker_registration.h" 18 #include "content/browser/service_worker/service_worker_registration.h"
18 #include "content/browser/service_worker/service_worker_test_utils.h" 19 #include "content/browser/service_worker/service_worker_test_utils.h"
19 #include "content/common/service_worker/service_worker_utils.h" 20 #include "content/common/service_worker/service_worker_utils.h"
20 #include "content/public/test/mock_render_process_host.h" 21 #include "content/public/test/mock_render_process_host.h"
21 #include "content/public/test/test_browser_thread_bundle.h" 22 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "content/public/test/test_mojo_service.mojom.h" 23 #include "content/public/test/test_mojo_service.mojom.h"
(...skipping 1297 matching lines...) Expand 10 before | Expand all | Expand 10 after
1320 version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN, 1321 version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN,
1321 CreateReceiverOnCurrentThread(&status)); 1322 CreateReceiverOnCurrentThread(&status));
1322 base::RunLoop().RunUntilIdle(); 1323 base::RunLoop().RunUntilIdle();
1323 EXPECT_EQ(SERVICE_WORKER_OK, status); 1324 EXPECT_EQ(SERVICE_WORKER_OK, status);
1324 EXPECT_EQ(helper_->mock_render_process_id(), 1325 EXPECT_EQ(helper_->mock_render_process_id(),
1325 version_->embedded_worker()->process_id()); 1326 version_->embedded_worker()->process_id());
1326 version_->StopWorker(CreateReceiverOnCurrentThread(&status)); 1327 version_->StopWorker(CreateReceiverOnCurrentThread(&status));
1327 base::RunLoop().RunUntilIdle(); 1328 base::RunLoop().RunUntilIdle();
1328 } 1329 }
1329 1330
1331 TEST_F(ServiceWorkerFailToStartTest, RestartStalledWorker) {
1332 ServiceWorkerStatusCode status1 = SERVICE_WORKER_ERROR_FAILED;
1333 ServiceWorkerStatusCode status2 = SERVICE_WORKER_ERROR_FAILED;
1334 version_->StartWorker(ServiceWorkerMetrics::EventType::FETCH_MAIN_FRAME,
1335 CreateReceiverOnCurrentThread(&status1));
1336 base::RunLoop().RunUntilIdle();
1337 // The default start mode is StartMode::STALL. So the callback of StartWorker
1338 // is not called yet.
1339 EXPECT_EQ(SERVICE_WORKER_ERROR_FAILED, status1);
1340
1341 // Set StartMode::SUCCEED. So the next start worker will be successful.
1342 set_start_mode(MessageReceiverDisallowStart::StartMode::SUCCEED);
1343
1344 // StartWorker message will be sent again because OnStopped is called before
1345 // OnStarted.
1346 version_->StopWorker(CreateReceiverOnCurrentThread(&status2));
1347 base::RunLoop().RunUntilIdle();
1348
1349 EXPECT_EQ(SERVICE_WORKER_OK, status1);
1350 EXPECT_EQ(SERVICE_WORKER_OK, status2);
1351 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, version_->running_status());
1352 }
1353
1330 TEST_F(ServiceWorkerVersionTest, DispatchConcurrentEvent) { 1354 TEST_F(ServiceWorkerVersionTest, DispatchConcurrentEvent) {
1331 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value 1355 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value
1332 1356
1333 // Activate and start worker. 1357 // Activate and start worker.
1334 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); 1358 version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
1335 version_->StartWorker(ServiceWorkerMetrics::EventType::SYNC, 1359 version_->StartWorker(ServiceWorkerMetrics::EventType::SYNC,
1336 CreateReceiverOnCurrentThread(&status)); 1360 CreateReceiverOnCurrentThread(&status));
1337 base::RunLoop().RunUntilIdle(); 1361 base::RunLoop().RunUntilIdle();
1338 EXPECT_EQ(SERVICE_WORKER_OK, status); 1362 EXPECT_EQ(SERVICE_WORKER_OK, status);
1339 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, version_->running_status()); 1363 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, version_->running_status());
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
1538 EXPECT_EQ(request_id2, received_request_id2); 1562 EXPECT_EQ(request_id2, received_request_id2);
1539 EXPECT_EQ(reply2, received_data2); 1563 EXPECT_EQ(reply2, received_data2);
1540 1564
1541 // Should not have timed out, so error callback should not have been 1565 // Should not have timed out, so error callback should not have been
1542 // called and FinishRequest should return true. 1566 // called and FinishRequest should return true.
1543 EXPECT_EQ(SERVICE_WORKER_OK, status); 1567 EXPECT_EQ(SERVICE_WORKER_OK, status);
1544 EXPECT_TRUE(version_->FinishRequest(request_id1, true)); 1568 EXPECT_TRUE(version_->FinishRequest(request_id1, true));
1545 EXPECT_TRUE(version_->FinishRequest(request_id2, true)); 1569 EXPECT_TRUE(version_->FinishRequest(request_id2, true));
1546 } 1570 }
1547 1571
1572 class ServiceWorkerNavigationHintUMATest : public ServiceWorkerVersionTest {
1573 protected:
1574 ServiceWorkerNavigationHintUMATest() : ServiceWorkerVersionTest() {}
1575
1576 void StartWorker(ServiceWorkerMetrics::EventType purpose) {
1577 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
1578 version_->StartWorker(purpose, CreateReceiverOnCurrentThread(&status));
1579 base::RunLoop().RunUntilIdle();
1580 EXPECT_EQ(SERVICE_WORKER_OK, status);
1581 }
1582
1583 void StopWorker() {
1584 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
1585 version_->StopWorker(CreateReceiverOnCurrentThread(&status));
1586 base::RunLoop().RunUntilIdle();
1587 EXPECT_EQ(SERVICE_WORKER_OK, status);
1588 }
1589
1590 void SimpleNavigationHintTest(
1591 ServiceWorkerMetrics::EventType purpose,
1592 const char* changed_historam_name,
1593 const std::vector<const char*>& unchanged_historam_names) {
1594 StartWorker(purpose);
1595 StopWorker();
1596 histogram_tester_.ExpectBucketCount(kNavigationHintPrecision, true, 0);
1597 histogram_tester_.ExpectBucketCount(kNavigationHintPrecision, false, 1);
1598 histogram_tester_.ExpectBucketCount(changed_historam_name, true, 0);
1599 histogram_tester_.ExpectBucketCount(changed_historam_name, false, 1);
1600 for (const auto& unchanged_historam_name : unchanged_historam_names) {
1601 histogram_tester_.ExpectTotalCount(unchanged_historam_name, 0);
1602 }
1603
1604 StartWorker(purpose);
1605 SimulateDispatchEvent(ServiceWorkerMetrics::EventType::MESSAGE);
1606 StopWorker();
1607 histogram_tester_.ExpectBucketCount(kNavigationHintPrecision, true, 1);
1608 histogram_tester_.ExpectBucketCount(kNavigationHintPrecision, false, 1);
1609 histogram_tester_.ExpectBucketCount(changed_historam_name, true, 1);
1610 histogram_tester_.ExpectBucketCount(changed_historam_name, false, 1);
1611 for (const auto& unchanged_historam_name : unchanged_historam_names) {
1612 histogram_tester_.ExpectTotalCount(unchanged_historam_name, 0);
1613 }
1614 }
1615
1616 static const char kNavigationHintPrecision[];
1617 static const char kLinkMouseDown[];
1618 static const char kLinkTapUnconfirmed[];
1619 static const char kLinkTapDown[];
1620
1621 base::HistogramTester histogram_tester_;
1622
1623 private:
1624 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerNavigationHintUMATest);
1625 };
1626
1627 const char ServiceWorkerNavigationHintUMATest::kNavigationHintPrecision[] =
1628 "ServiceWorker.NavigationHintPrecision";
1629 const char ServiceWorkerNavigationHintUMATest::kLinkMouseDown[] =
1630 "ServiceWorker.NavigationHintPrecision.LINK_MOUSE_DOWN";
1631 const char ServiceWorkerNavigationHintUMATest::kLinkTapUnconfirmed[] =
1632 "ServiceWorker.NavigationHintPrecision.LINK_TAP_UNCONFIRMED";
1633 const char ServiceWorkerNavigationHintUMATest::kLinkTapDown[] =
1634 "ServiceWorker.NavigationHintPrecision.LINK_TAP_DOWN";
1635
1636 TEST_F(ServiceWorkerNavigationHintUMATest, LinkMouseDown) {
1637 SimpleNavigationHintTest(
1638 ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_MOUSE_DOWN,
1639 kLinkMouseDown, {kLinkTapUnconfirmed, kLinkTapDown});
1640 }
1641
1642 TEST_F(ServiceWorkerNavigationHintUMATest, LinkTapUnconfirmed) {
1643 SimpleNavigationHintTest(
1644 ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_TAP_UNCONFIRMED,
1645 kLinkTapUnconfirmed, {kLinkMouseDown, kLinkTapDown});
1646 }
1647
1648 TEST_F(ServiceWorkerNavigationHintUMATest, LinkTapDown) {
1649 SimpleNavigationHintTest(
1650 ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_TAP_DOWN,
1651 kLinkTapDown, {kLinkMouseDown, kLinkTapUnconfirmed});
1652 }
1653
1654 TEST_F(ServiceWorkerNavigationHintUMATest, ConcurrentStart) {
1655 ServiceWorkerStatusCode status1 = SERVICE_WORKER_ERROR_FAILED;
1656 ServiceWorkerStatusCode status2 = SERVICE_WORKER_ERROR_FAILED;
1657 version_->StartWorker(ServiceWorkerMetrics::EventType::FETCH_MAIN_FRAME,
1658 CreateReceiverOnCurrentThread(&status1));
1659 version_->StartWorker(
1660 ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_MOUSE_DOWN,
1661 CreateReceiverOnCurrentThread(&status2));
1662 base::RunLoop().RunUntilIdle();
1663 EXPECT_EQ(SERVICE_WORKER_OK, status1);
1664 EXPECT_EQ(SERVICE_WORKER_OK, status2);
1665 StopWorker();
1666 // The first purpose of starting worker was not a navigation hint.
1667 histogram_tester_.ExpectTotalCount(kNavigationHintPrecision, 0);
1668
1669 status1 = SERVICE_WORKER_ERROR_FAILED;
1670 status2 = SERVICE_WORKER_ERROR_FAILED;
1671 version_->StartWorker(
1672 ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_MOUSE_DOWN,
1673 CreateReceiverOnCurrentThread(&status2));
1674 version_->StartWorker(ServiceWorkerMetrics::EventType::FETCH_MAIN_FRAME,
1675 CreateReceiverOnCurrentThread(&status1));
1676 base::RunLoop().RunUntilIdle();
1677 EXPECT_EQ(SERVICE_WORKER_OK, status1);
1678 EXPECT_EQ(SERVICE_WORKER_OK, status2);
1679 SimulateDispatchEvent(ServiceWorkerMetrics::EventType::MESSAGE);
1680 StopWorker();
1681 // The first purpose of starting worker was a navigation hint.
1682 histogram_tester_.ExpectTotalCount(kNavigationHintPrecision, 1);
1683 histogram_tester_.ExpectBucketCount(kNavigationHintPrecision, true, 1);
1684 histogram_tester_.ExpectBucketCount(kNavigationHintPrecision, false, 0);
1685 }
1686
1687 TEST_F(ServiceWorkerNavigationHintUMATest, StartWhileStopping) {
1688 StartWorker(ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_MOUSE_DOWN);
1689 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
1690 version_->StopWorker(CreateReceiverOnCurrentThread(&status));
1691 EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, version_->running_status());
1692 histogram_tester_.ExpectTotalCount(kLinkMouseDown, 0);
1693
1694 StartWorker(ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_TAP_DOWN);
1695 // The UMA for kLinkMouseDown must be recorded while restarting.
1696 histogram_tester_.ExpectTotalCount(kLinkMouseDown, 1);
1697 histogram_tester_.ExpectTotalCount(kLinkTapDown, 0);
1698 EXPECT_EQ(SERVICE_WORKER_OK, status);
1699 StopWorker();
1700 // The UMA for kLinkMouseDown must be recorded when the worker stopped.
1701 histogram_tester_.ExpectTotalCount(kLinkMouseDown, 1);
1702 histogram_tester_.ExpectTotalCount(kLinkTapDown, 1);
1703 }
1704
1548 } // namespace content 1705 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698