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

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: incorporated nhiroki's comment 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_MAX_VALUE;
1333 ServiceWorkerStatusCode status2 = SERVICE_WORKER_ERROR_MAX_VALUE;
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_MAX_VALUE, 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_MAX_VALUE;
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_MAX_VALUE;
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 version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
1595 StartWorker(purpose);
1596 StopWorker();
1597 histogram_tester_.ExpectBucketCount(kNavigationHintPrecision, true, 0);
1598 histogram_tester_.ExpectBucketCount(kNavigationHintPrecision, false, 1);
1599 histogram_tester_.ExpectBucketCount(changed_historam_name, true, 0);
1600 histogram_tester_.ExpectBucketCount(changed_historam_name, false, 1);
1601 for (const char* unchanged_historam_name : unchanged_historam_names)
1602 histogram_tester_.ExpectTotalCount(unchanged_historam_name, 0);
1603
1604 StartWorker(purpose);
1605 SimulateDispatchEvent(ServiceWorkerMetrics::EventType::MESSAGE);
1606 StopWorker();
1607 histogram_tester_.ExpectBucketCount(kNavigationHintPrecision, true, 0);
1608 histogram_tester_.ExpectBucketCount(kNavigationHintPrecision, false, 2);
1609 histogram_tester_.ExpectBucketCount(changed_historam_name, true, 0);
1610 histogram_tester_.ExpectBucketCount(changed_historam_name, false, 2);
1611 for (const char* unchanged_historam_name : unchanged_historam_names)
1612 histogram_tester_.ExpectTotalCount(unchanged_historam_name, 0);
1613
1614 StartWorker(purpose);
1615 SimulateDispatchEvent(ServiceWorkerMetrics::EventType::FETCH_MAIN_FRAME);
1616 StopWorker();
1617 histogram_tester_.ExpectBucketCount(kNavigationHintPrecision, true, 1);
1618 histogram_tester_.ExpectBucketCount(kNavigationHintPrecision, false, 2);
1619 histogram_tester_.ExpectBucketCount(changed_historam_name, true, 1);
1620 histogram_tester_.ExpectBucketCount(changed_historam_name, false, 2);
1621 for (const char* unchanged_historam_name : unchanged_historam_names)
1622 histogram_tester_.ExpectTotalCount(unchanged_historam_name, 0);
1623
1624 StartWorker(purpose);
1625 SimulateDispatchEvent(ServiceWorkerMetrics::EventType::FETCH_SUB_FRAME);
1626 StopWorker();
1627 histogram_tester_.ExpectBucketCount(kNavigationHintPrecision, true, 2);
1628 histogram_tester_.ExpectBucketCount(kNavigationHintPrecision, false, 2);
1629 histogram_tester_.ExpectBucketCount(changed_historam_name, true, 2);
1630 histogram_tester_.ExpectBucketCount(changed_historam_name, false, 2);
1631 for (const char* unchanged_historam_name : unchanged_historam_names)
1632 histogram_tester_.ExpectTotalCount(unchanged_historam_name, 0);
1633 }
1634
1635 static const char kNavigationHintPrecision[];
1636 static const char kLinkMouseDown[];
1637 static const char kLinkTapUnconfirmed[];
1638 static const char kLinkTapDown[];
1639
1640 base::HistogramTester histogram_tester_;
1641
1642 private:
1643 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerNavigationHintUMATest);
1644 };
1645
1646 const char ServiceWorkerNavigationHintUMATest::kNavigationHintPrecision[] =
1647 "ServiceWorker.NavigationHintPrecision";
1648 const char ServiceWorkerNavigationHintUMATest::kLinkMouseDown[] =
1649 "ServiceWorker.NavigationHintPrecision.LINK_MOUSE_DOWN";
1650 const char ServiceWorkerNavigationHintUMATest::kLinkTapUnconfirmed[] =
1651 "ServiceWorker.NavigationHintPrecision.LINK_TAP_UNCONFIRMED";
1652 const char ServiceWorkerNavigationHintUMATest::kLinkTapDown[] =
1653 "ServiceWorker.NavigationHintPrecision.LINK_TAP_DOWN";
1654
1655 TEST_F(ServiceWorkerNavigationHintUMATest, LinkMouseDown) {
1656 SimpleNavigationHintTest(
1657 ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_MOUSE_DOWN,
1658 kLinkMouseDown, {kLinkTapUnconfirmed, kLinkTapDown});
1659 }
1660
1661 TEST_F(ServiceWorkerNavigationHintUMATest, LinkTapUnconfirmed) {
1662 SimpleNavigationHintTest(
1663 ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_TAP_UNCONFIRMED,
1664 kLinkTapUnconfirmed, {kLinkMouseDown, kLinkTapDown});
1665 }
1666
1667 TEST_F(ServiceWorkerNavigationHintUMATest, LinkTapDown) {
1668 SimpleNavigationHintTest(
1669 ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_TAP_DOWN,
1670 kLinkTapDown, {kLinkMouseDown, kLinkTapUnconfirmed});
1671 }
1672
1673 TEST_F(ServiceWorkerNavigationHintUMATest, ConcurrentStart) {
1674 version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
1675 ServiceWorkerStatusCode status1 = SERVICE_WORKER_ERROR_MAX_VALUE;
1676 ServiceWorkerStatusCode status2 = SERVICE_WORKER_ERROR_MAX_VALUE;
1677 version_->StartWorker(ServiceWorkerMetrics::EventType::FETCH_MAIN_FRAME,
1678 CreateReceiverOnCurrentThread(&status1));
1679 version_->StartWorker(
1680 ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_MOUSE_DOWN,
1681 CreateReceiverOnCurrentThread(&status2));
1682 base::RunLoop().RunUntilIdle();
1683 EXPECT_EQ(SERVICE_WORKER_OK, status1);
1684 EXPECT_EQ(SERVICE_WORKER_OK, status2);
1685 StopWorker();
1686 // The first purpose of starting worker was not a navigation hint.
1687 histogram_tester_.ExpectTotalCount(kNavigationHintPrecision, 0);
1688
1689 status1 = SERVICE_WORKER_ERROR_MAX_VALUE;
1690 status2 = SERVICE_WORKER_ERROR_MAX_VALUE;
1691 version_->StartWorker(
1692 ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_MOUSE_DOWN,
1693 CreateReceiverOnCurrentThread(&status2));
1694 version_->StartWorker(ServiceWorkerMetrics::EventType::FETCH_MAIN_FRAME,
1695 CreateReceiverOnCurrentThread(&status1));
1696 base::RunLoop().RunUntilIdle();
1697 EXPECT_EQ(SERVICE_WORKER_OK, status1);
1698 EXPECT_EQ(SERVICE_WORKER_OK, status2);
1699 SimulateDispatchEvent(ServiceWorkerMetrics::EventType::FETCH_MAIN_FRAME);
1700 StopWorker();
1701 // The first purpose of starting worker was a navigation hint.
1702 histogram_tester_.ExpectTotalCount(kNavigationHintPrecision, 1);
1703 histogram_tester_.ExpectBucketCount(kNavigationHintPrecision, true, 1);
1704 histogram_tester_.ExpectBucketCount(kNavigationHintPrecision, false, 0);
1705 }
1706
1707 TEST_F(ServiceWorkerNavigationHintUMATest, StartWhileStopping) {
1708 StartWorker(ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_MOUSE_DOWN);
1709 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE;
1710 version_->StopWorker(CreateReceiverOnCurrentThread(&status));
1711 EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, version_->running_status());
1712 histogram_tester_.ExpectTotalCount(kLinkMouseDown, 0);
1713
1714 StartWorker(ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_TAP_DOWN);
1715 // The UMA for kLinkMouseDown must be recorded while restarting.
1716 histogram_tester_.ExpectTotalCount(kLinkMouseDown, 1);
1717 histogram_tester_.ExpectTotalCount(kLinkTapDown, 0);
1718 EXPECT_EQ(SERVICE_WORKER_OK, status);
1719 StopWorker();
1720 // The UMA for kLinkMouseDown must be recorded when the worker stopped.
1721 histogram_tester_.ExpectTotalCount(kLinkMouseDown, 1);
1722 histogram_tester_.ExpectTotalCount(kLinkTapDown, 1);
1723 }
1724
1548 } // namespace content 1725 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/service_worker/service_worker_version.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698