Home | History | Annotate | Download | only in metrics
      1 // Copyright (c) 2011 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/file_path.h"
     11 #include "base/file_util.h"
     12 #include "base/path_service.h"
     13 #include "base/test/test_timeouts.h"
     14 #include "base/threading/platform_thread.h"
     15 #include "chrome/browser/prefs/pref_service.h"
     16 #include "chrome/browser/prefs/pref_service_mock_builder.h"
     17 #include "chrome/browser/prefs/pref_value_store.h"
     18 #include "chrome/common/chrome_constants.h"
     19 #include "chrome/common/chrome_paths.h"
     20 #include "chrome/common/json_pref_store.h"
     21 #include "chrome/common/pref_names.h"
     22 #include "chrome/common/url_constants.h"
     23 #include "chrome/test/automation/browser_proxy.h"
     24 #include "chrome/test/automation/tab_proxy.h"
     25 #include "chrome/test/ui/ui_test.h"
     26 #include "net/base/net_util.h"
     27 
     28 class MetricsServiceTest : public UITest {
     29  public:
     30   MetricsServiceTest() : UITest() {
     31     // We need to show the window so web content type tabs load.
     32     show_window_ = true;
     33   }
     34 
     35   // Open a few tabs of random content
     36   void OpenTabs() {
     37     scoped_refptr<BrowserProxy> window = automation()->GetBrowserWindow(0);
     38     ASSERT_TRUE(window.get());
     39 
     40     FilePath page1_path;
     41     ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &page1_path));
     42     page1_path = page1_path.AppendASCII("title2.html");
     43     ASSERT_TRUE(window->AppendTab(net::FilePathToFileURL(page1_path)));
     44 
     45     FilePath page2_path;
     46     ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &page2_path));
     47     page2_path = page2_path.AppendASCII("iframe.html");
     48     ASSERT_TRUE(window->AppendTab(net::FilePathToFileURL(page2_path)));
     49   }
     50 
     51   // Get a PrefService whose contents correspond to the Local State file
     52   // that was saved by the app as it closed.  The caller takes ownership of the
     53   // returned PrefService object.
     54   PrefService* GetLocalState() {
     55     FilePath path = user_data_dir().Append(chrome::kLocalStateFilename);
     56     return PrefServiceMockBuilder().WithUserFilePrefs(path).Create();
     57   }
     58 };
     59 
     60 TEST_F(MetricsServiceTest, CloseRenderersNormally) {
     61   OpenTabs();
     62   QuitBrowser();
     63 
     64   scoped_ptr<PrefService> local_state(GetLocalState());
     65   local_state->RegisterBooleanPref(prefs::kStabilityExitedCleanly, true);
     66   local_state->RegisterIntegerPref(prefs::kStabilityLaunchCount, 0);
     67   local_state->RegisterIntegerPref(prefs::kStabilityPageLoadCount, 0);
     68   local_state->RegisterIntegerPref(prefs::kStabilityRendererCrashCount, 0);
     69   EXPECT_TRUE(local_state->GetBoolean(prefs::kStabilityExitedCleanly));
     70   EXPECT_EQ(1, local_state->GetInteger(prefs::kStabilityLaunchCount));
     71   EXPECT_EQ(3, local_state->GetInteger(prefs::kStabilityPageLoadCount));
     72   EXPECT_EQ(0, local_state->GetInteger(prefs::kStabilityRendererCrashCount));
     73 }
     74 
     75 TEST_F(MetricsServiceTest, DISABLED_CrashRenderers) {
     76   // This doesn't make sense to test in single process mode.
     77   if (ProxyLauncher::in_process_renderer())
     78     return;
     79 
     80   OpenTabs();
     81 
     82   {
     83     // Limit the lifetime of various automation proxies used here. We must
     84     // destroy them before calling QuitBrowser.
     85 
     86     scoped_refptr<BrowserProxy> window = automation()->GetBrowserWindow(0);
     87     ASSERT_TRUE(window.get());
     88 
     89     // Kill the process for one of the tabs.
     90     scoped_refptr<TabProxy> tab(window->GetTab(1));
     91     ASSERT_TRUE(tab.get());
     92 
     93     // We can get crash dumps on Windows always, Linux when breakpad is
     94     // enabled, and all platforms for official Google Chrome builds.
     95 #if defined(OS_WIN) || defined(USE_LINUX_BREAKPAD) || \
     96     defined(GOOGLE_CHROME_BUILD)
     97     expected_crashes_ = 1;
     98 #endif
     99     ASSERT_TRUE(tab->NavigateToURLAsync(GURL(chrome::kAboutCrashURL)));
    100   }
    101 
    102   // Give the browser a chance to notice the crashed tab.
    103   base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms());
    104 
    105   QuitBrowser();
    106 
    107   scoped_ptr<PrefService> local_state(GetLocalState());
    108   local_state->RegisterBooleanPref(prefs::kStabilityExitedCleanly, true);
    109   local_state->RegisterIntegerPref(prefs::kStabilityLaunchCount, 0);
    110   local_state->RegisterIntegerPref(prefs::kStabilityPageLoadCount, 0);
    111   local_state->RegisterIntegerPref(prefs::kStabilityRendererCrashCount, 0);
    112   EXPECT_TRUE(local_state->GetBoolean(prefs::kStabilityExitedCleanly));
    113   EXPECT_EQ(1, local_state->GetInteger(prefs::kStabilityLaunchCount));
    114   EXPECT_EQ(4, local_state->GetInteger(prefs::kStabilityPageLoadCount));
    115   EXPECT_EQ(1, local_state->GetInteger(prefs::kStabilityRendererCrashCount));
    116 }
    117