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