Move MetricsLog into the Metrics component.
[chromium-blink-merge.git] / chrome / browser / metrics / metrics_service_browsertest.cc
blob328f99440d66b4594b5e8b005814196c22f37590
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
6 // what we expect.
8 #include "chrome/browser/metrics/metrics_service.h"
10 #include <string>
12 #include "base/command_line.h"
13 #include "base/files/file_path.h"
14 #include "base/path_service.h"
15 #include "base/prefs/pref_service.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/common/url_constants.h"
23 #include "chrome/test/base/in_process_browser_test.h"
24 #include "chrome/test/base/ui_test_utils.h"
25 #include "content/public/test/browser_test_utils.h"
26 #include "net/base/filename_util.h"
27 #include "ui/base/window_open_disposition.h"
28 #include "url/gurl.h"
30 class MetricsServiceBrowserTest : public InProcessBrowserTest {
31 public:
32 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
33 // Enable the metrics service for testing (in recording-only mode).
34 command_line->AppendSwitch(switches::kMetricsRecordingOnly);
37 // Open a couple of tabs of random content.
38 void OpenTabs() {
39 const int kBrowserTestFlags =
40 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB |
41 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION;
43 base::FilePath test_directory;
44 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_directory));
46 base::FilePath page1_path = test_directory.AppendASCII("title2.html");
47 ui_test_utils::NavigateToURLWithDisposition(
48 browser(),
49 net::FilePathToFileURL(page1_path),
50 NEW_FOREGROUND_TAB,
51 kBrowserTestFlags);
53 base::FilePath page2_path = test_directory.AppendASCII("iframe.html");
54 ui_test_utils::NavigateToURLWithDisposition(
55 browser(),
56 net::FilePathToFileURL(page2_path),
57 NEW_FOREGROUND_TAB,
58 kBrowserTestFlags);
62 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, CloseRenderersNormally) {
63 OpenTabs();
65 // Verify that the expected stability metrics were recorded.
66 const PrefService* prefs = g_browser_process->local_state();
67 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount));
68 EXPECT_EQ(3, prefs->GetInteger(prefs::kStabilityPageLoadCount));
69 EXPECT_EQ(0, prefs->GetInteger(prefs::kStabilityRendererCrashCount));
70 // TODO(isherman): We should also verify that prefs::kStabilityExitedCleanly
71 // is set to true, but this preference isn't set until the browser
72 // exits... it's not clear to me how to test that.
75 // Flaky on Linux. See http://crbug.com/131094
76 #if defined(OS_LINUX)
77 #define MAYBE_CrashRenderers DISABLED_CrashRenderers
78 #else
79 #define MAYBE_CrashRenderers CrashRenderers
80 #endif
81 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, MAYBE_CrashRenderers) {
82 OpenTabs();
84 // Kill the process for one of the tabs.
85 content::RenderProcessHostWatcher observer(
86 browser()->tab_strip_model()->GetActiveWebContents(),
87 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
88 ui_test_utils::NavigateToURL(browser(), GURL(content::kChromeUICrashURL));
89 observer.Wait();
91 // The MetricsService listens for the same notification, so the |observer|
92 // might finish waiting before the MetricsService has a chance to process the
93 // notification. To avoid racing here, we repeatedly run the message loop
94 // until the MetricsService catches up. This should happen "real soon now",
95 // since the notification is posted to all observers essentially
96 // simultaneously... so busy waiting here shouldn't be too bad.
97 const PrefService* prefs = g_browser_process->local_state();
98 while (!prefs->GetInteger(prefs::kStabilityRendererCrashCount)) {
99 content::RunAllPendingInMessageLoop();
102 // Verify that the expected stability metrics were recorded.
103 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount));
104 EXPECT_EQ(4, prefs->GetInteger(prefs::kStabilityPageLoadCount));
105 EXPECT_EQ(1, prefs->GetInteger(prefs::kStabilityRendererCrashCount));
106 // TODO(isherman): We should also verify that prefs::kStabilityExitedCleanly
107 // is set to true, but this preference isn't set until the browser
108 // exits... it's not clear to me how to test that.