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. 4 5 // Tests the MetricsService stat recording to make sure that the numbers are 6 // what we expect. 7 8 #include <string> 9 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/common/chrome_paths.h" 17 #include "chrome/common/chrome_switches.h" 18 #include "chrome/common/pref_names.h" 19 #include "chrome/common/url_constants.h" 20 #include "chrome/test/base/in_process_browser_test.h" 21 #include "chrome/test/base/ui_test_utils.h" 22 #include "content/public/browser/notification_service.h" 23 #include "content/public/browser/notification_types.h" 24 #include "net/base/net_util.h" 25 #include "ui/base/window_open_disposition.h" 26 #include "url/gurl.h" 27 28 class MetricsServiceBrowserTest : public InProcessBrowserTest { 29 public: 30 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 31 // Enable the metrics service for testing (in recording-only mode). 32 command_line->AppendSwitch(switches::kMetricsRecordingOnly); 33 } 34 35 // Open a couple of tabs of random content. 36 void OpenTabs() { 37 const int kBrowserTestFlags = 38 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | 39 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION; 40 41 base::FilePath test_directory; 42 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_directory)); 43 44 base::FilePath page1_path = test_directory.AppendASCII("title2.html"); 45 ui_test_utils::NavigateToURLWithDisposition( 46 browser(), 47 net::FilePathToFileURL(page1_path), 48 NEW_FOREGROUND_TAB, 49 kBrowserTestFlags); 50 51 base::FilePath page2_path = test_directory.AppendASCII("iframe.html"); 52 ui_test_utils::NavigateToURLWithDisposition( 53 browser(), 54 net::FilePathToFileURL(page2_path), 55 NEW_FOREGROUND_TAB, 56 kBrowserTestFlags); 57 } 58 }; 59 60 class MetricsServiceReportingTest : public InProcessBrowserTest { 61 public: 62 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 63 // Enable the metrics service for testing (in the full mode). 64 command_line->AppendSwitch(switches::kEnableMetricsReportingForTesting); 65 } 66 }; 67 68 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, CloseRenderersNormally) { 69 OpenTabs(); 70 71 // Verify that the expected stability metrics were recorded. 72 const PrefService* prefs = g_browser_process->local_state(); 73 EXPECT_EQ(1, prefs->GetInteger(prefs::kStabilityLaunchCount)); 74 EXPECT_EQ(3, prefs->GetInteger(prefs::kStabilityPageLoadCount)); 75 EXPECT_EQ(0, prefs->GetInteger(prefs::kStabilityRendererCrashCount)); 76 // TODO(isherman): We should also verify that prefs::kStabilityExitedCleanly 77 // is set to true, but this preference isn't set until the browser 78 // exits... it's not clear to me how to test that. 79 } 80 81 // Flaky on Linux. See http://crbug.com/131094 82 #if defined(OS_LINUX) 83 #define MAYBE_CrashRenderers DISABLED_CrashRenderers 84 #else 85 #define MAYBE_CrashRenderers CrashRenderers 86 #endif 87 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, MAYBE_CrashRenderers) { 88 OpenTabs(); 89 90 // Kill the process for one of the tabs. 91 content::WindowedNotificationObserver observer( 92 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, 93 content::NotificationService::AllSources()); 94 ui_test_utils::NavigateToURL(browser(), GURL(content::kChromeUICrashURL)); 95 observer.Wait(); 96 97 // The MetricsService listens for the same notification, so the |observer| 98 // might finish waiting before the MetricsService has a chance to process the 99 // notification. To avoid racing here, we repeatedly run the message loop 100 // until the MetricsService catches up. This should happen "real soon now", 101 // since the notification is posted to all observers essentially 102 // simultaneously... so busy waiting here shouldn't be too bad. 103 const PrefService* prefs = g_browser_process->local_state(); 104 while (!prefs->GetInteger(prefs::kStabilityRendererCrashCount)) { 105 content::RunAllPendingInMessageLoop(); 106 } 107 108 // Verify that the expected stability metrics were recorded. 109 EXPECT_EQ(1, prefs->GetInteger(prefs::kStabilityLaunchCount)); 110 EXPECT_EQ(4, prefs->GetInteger(prefs::kStabilityPageLoadCount)); 111 EXPECT_EQ(1, prefs->GetInteger(prefs::kStabilityRendererCrashCount)); 112 // TODO(isherman): We should also verify that prefs::kStabilityExitedCleanly 113 // is set to true, but this preference isn't set until the browser 114 // exits... it's not clear to me how to test that. 115 } 116 117 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, CheckLowEntropySourceUsed) { 118 // Since MetricsService is only in recording mode, and is not reporting, 119 // check that the low entropy source is returned at some point. 120 ASSERT_TRUE(g_browser_process->metrics_service()); 121 EXPECT_EQ(MetricsService::LAST_ENTROPY_LOW, 122 g_browser_process->metrics_service()->entropy_source_returned()); 123 } 124 125 IN_PROC_BROWSER_TEST_F(MetricsServiceReportingTest, 126 CheckHighEntropySourceUsed) { 127 // Since the full metrics service runs in this test, we expect that 128 // MetricsService returns the full entropy source at some point during 129 // BrowserMain startup. 130 ASSERT_TRUE(g_browser_process->metrics_service()); 131 EXPECT_EQ(MetricsService::LAST_ENTROPY_HIGH, 132 g_browser_process->metrics_service()->entropy_source_returned()); 133 } 134