Home | History | Annotate | Download | only in metrics
      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 "components/metrics/metrics_service.h"
      9 
     10 #include <string>
     11 
     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"
     29 
     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);
     35   }
     36 
     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;
     42 
     43     base::FilePath test_directory;
     44     ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_directory));
     45 
     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);
     52 
     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);
     59   }
     60 };
     61 
     62 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, CloseRenderersNormally) {
     63   OpenTabs();
     64 
     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.
     74 }
     75 
     76 // Flaky on Linux. See http://crbug.com/131094
     77 #if defined(OS_LINUX)
     78 #define MAYBE_CrashRenderers DISABLED_CrashRenderers
     79 #else
     80 #define MAYBE_CrashRenderers CrashRenderers
     81 #endif
     82 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, MAYBE_CrashRenderers) {
     83   OpenTabs();
     84 
     85   // Kill the process for one of the tabs.
     86   content::RenderProcessHostWatcher observer(
     87       browser()->tab_strip_model()->GetActiveWebContents(),
     88       content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
     89   ui_test_utils::NavigateToURL(browser(), GURL(content::kChromeUICrashURL));
     90   observer.Wait();
     91 
     92   // The MetricsService listens for the same notification, so the |observer|
     93   // might finish waiting before the MetricsService has a chance to process the
     94   // notification.  To avoid racing here, we repeatedly run the message loop
     95   // until the MetricsService catches up.  This should happen "real soon now",
     96   // since the notification is posted to all observers essentially
     97   // simultaneously... so busy waiting here shouldn't be too bad.
     98   const PrefService* prefs = g_browser_process->local_state();
     99   while (!prefs->GetInteger(prefs::kStabilityRendererCrashCount)) {
    100     content::RunAllPendingInMessageLoop();
    101   }
    102 
    103   // Verify that the expected stability metrics were recorded.
    104   EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount));
    105   EXPECT_EQ(4, prefs->GetInteger(prefs::kStabilityPageLoadCount));
    106   EXPECT_EQ(1, prefs->GetInteger(prefs::kStabilityRendererCrashCount));
    107   // TODO(isherman): We should also verify that
    108   // metrics::prefs::kStabilityExitedCleanly
    109   // is set to true, but this preference isn't set until the browser
    110   // exits... it's not clear to me how to test that.
    111 }
    112 
    113