1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Tests the MetricsService stat recording to make sure that the numbers are
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/path_service.h"
13 #include "base/prefs/pref_service.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/metrics/metrics_service.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/common/url_constants.h"
22 #include "chrome/test/base/in_process_browser_test.h"
23 #include "chrome/test/base/ui_test_utils.h"
24 #include "content/public/test/browser_test_utils.h"
25 #include "net/base/net_util.h"
26 #include "ui/base/window_open_disposition.h"
29 class MetricsServiceBrowserTest
: public InProcessBrowserTest
{
31 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
32 // Enable the metrics service for testing (in recording-only mode).
33 command_line
->AppendSwitch(switches::kMetricsRecordingOnly
);
36 // Open a couple of tabs of random content.
38 const int kBrowserTestFlags
=
39 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
40 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
;
42 base::FilePath test_directory
;
43 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_directory
));
45 base::FilePath page1_path
= test_directory
.AppendASCII("title2.html");
46 ui_test_utils::NavigateToURLWithDisposition(
48 net::FilePathToFileURL(page1_path
),
52 base::FilePath page2_path
= test_directory
.AppendASCII("iframe.html");
53 ui_test_utils::NavigateToURLWithDisposition(
55 net::FilePathToFileURL(page2_path
),
61 class MetricsServiceReportingTest
: public InProcessBrowserTest
{
63 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
64 // Enable the metrics service for testing (in the full mode).
65 command_line
->AppendSwitch(switches::kEnableMetricsReportingForTesting
);
69 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest
, CloseRenderersNormally
) {
72 // Verify that the expected stability metrics were recorded.
73 const PrefService
* prefs
= g_browser_process
->local_state();
74 EXPECT_EQ(1, prefs
->GetInteger(prefs::kStabilityLaunchCount
));
75 EXPECT_EQ(3, prefs
->GetInteger(prefs::kStabilityPageLoadCount
));
76 EXPECT_EQ(0, prefs
->GetInteger(prefs::kStabilityRendererCrashCount
));
77 // TODO(isherman): We should also verify that prefs::kStabilityExitedCleanly
78 // is set to true, but this preference isn't set until the browser
79 // exits... it's not clear to me how to test that.
82 // Flaky on Linux. See http://crbug.com/131094
84 #define MAYBE_CrashRenderers DISABLED_CrashRenderers
86 #define MAYBE_CrashRenderers CrashRenderers
88 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest
, MAYBE_CrashRenderers
) {
91 // Kill the process for one of the tabs.
92 content::RenderProcessHostWatcher
observer(
93 browser()->tab_strip_model()->GetActiveWebContents(),
94 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT
);
95 ui_test_utils::NavigateToURL(browser(), GURL(content::kChromeUICrashURL
));
98 // The MetricsService listens for the same notification, so the |observer|
99 // might finish waiting before the MetricsService has a chance to process the
100 // notification. To avoid racing here, we repeatedly run the message loop
101 // until the MetricsService catches up. This should happen "real soon now",
102 // since the notification is posted to all observers essentially
103 // simultaneously... so busy waiting here shouldn't be too bad.
104 const PrefService
* prefs
= g_browser_process
->local_state();
105 while (!prefs
->GetInteger(prefs::kStabilityRendererCrashCount
)) {
106 content::RunAllPendingInMessageLoop();
109 // Verify that the expected stability metrics were recorded.
110 EXPECT_EQ(1, prefs
->GetInteger(prefs::kStabilityLaunchCount
));
111 EXPECT_EQ(4, prefs
->GetInteger(prefs::kStabilityPageLoadCount
));
112 EXPECT_EQ(1, prefs
->GetInteger(prefs::kStabilityRendererCrashCount
));
113 // TODO(isherman): We should also verify that prefs::kStabilityExitedCleanly
114 // is set to true, but this preference isn't set until the browser
115 // exits... it's not clear to me how to test that.
118 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest
, CheckLowEntropySourceUsed
) {
119 // Since MetricsService is only in recording mode, and is not reporting,
120 // check that the low entropy source is returned at some point.
121 ASSERT_TRUE(g_browser_process
->metrics_service());
122 EXPECT_EQ(MetricsService::LAST_ENTROPY_LOW
,
123 g_browser_process
->metrics_service()->entropy_source_returned());
126 IN_PROC_BROWSER_TEST_F(MetricsServiceReportingTest
,
127 CheckHighEntropySourceUsed
) {
128 // Since the full metrics service runs in this test, we expect that
129 // MetricsService returns the full entropy source at some point during
130 // BrowserMain startup.
131 ASSERT_TRUE(g_browser_process
->metrics_service());
132 EXPECT_EQ(MetricsService::LAST_ENTROPY_HIGH
,
133 g_browser_process
->metrics_service()->entropy_source_returned());