Changes for start_with_ext Telemetry test to work on Windows.
[chromium-blink-merge.git] / chrome / browser / metrics / metrics_service_browsertest.cc
blobf77221c3554d14768e26fce63beb38c68933311a
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 "components/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 void SetUpCommandLine(base::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
71 // metrics::prefs::kStabilityExitedCleanly
72 // is set to true, but this preference isn't set until the browser
73 // exits... it's not clear to me how to test that.
76 // Flaky on Linux. See http://crbug.com/131094
77 // Child crashes fail the process on ASan (see crbug.com/411251,
78 // crbug.com/368525).
79 #if defined(OS_LINUX) || defined(ADDRESS_SANITIZER)
80 #define MAYBE_CrashRenderers DISABLED_CrashRenderers
81 #else
82 #define MAYBE_CrashRenderers CrashRenderers
83 #endif
84 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, MAYBE_CrashRenderers) {
85 OpenTabs();
87 // Kill the process for one of the tabs.
88 content::RenderProcessHostWatcher observer(
89 browser()->tab_strip_model()->GetActiveWebContents(),
90 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
91 ui_test_utils::NavigateToURL(browser(), GURL(content::kChromeUICrashURL));
92 observer.Wait();
94 // The MetricsService listens for the same notification, so the |observer|
95 // might finish waiting before the MetricsService has a chance to process the
96 // notification. To avoid racing here, we repeatedly run the message loop
97 // until the MetricsService catches up. This should happen "real soon now",
98 // since the notification is posted to all observers essentially
99 // simultaneously... so busy waiting here shouldn't be too bad.
100 const PrefService* prefs = g_browser_process->local_state();
101 while (!prefs->GetInteger(prefs::kStabilityRendererCrashCount)) {
102 content::RunAllPendingInMessageLoop();
105 // Verify that the expected stability metrics were recorded.
106 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount));
107 EXPECT_EQ(4, prefs->GetInteger(prefs::kStabilityPageLoadCount));
108 EXPECT_EQ(1, prefs->GetInteger(prefs::kStabilityRendererCrashCount));
109 // TODO(isherman): We should also verify that
110 // metrics::prefs::kStabilityExitedCleanly
111 // is set to true, but this preference isn't set until the browser
112 // exits... it's not clear to me how to test that.