metrics_service_uitest.cc revision 3f50c38dc070f4bb515c1b64450dae14f316474e
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/threading/platform_thread.h"
14#include "chrome/browser/prefs/pref_service.h"
15#include "chrome/browser/prefs/pref_service_mock_builder.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/browser_proxy.h"
23#include "chrome/test/automation/tab_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 path = user_data_dir().Append(chrome::kLocalStateFilename);
55    return PrefServiceMockBuilder().WithUserFilePrefs(path).Create();
56  }
57};
58
59TEST_F(MetricsServiceTest, CloseRenderersNormally) {
60  OpenTabs();
61  QuitBrowser();
62
63  scoped_ptr<PrefService> local_state(GetLocalState());
64  local_state->RegisterBooleanPref(prefs::kStabilityExitedCleanly, true);
65  local_state->RegisterIntegerPref(prefs::kStabilityLaunchCount, 0);
66  local_state->RegisterIntegerPref(prefs::kStabilityPageLoadCount, 0);
67  local_state->RegisterIntegerPref(prefs::kStabilityRendererCrashCount, 0);
68  EXPECT_TRUE(local_state->GetBoolean(prefs::kStabilityExitedCleanly));
69  EXPECT_EQ(1, local_state->GetInteger(prefs::kStabilityLaunchCount));
70  EXPECT_EQ(3, local_state->GetInteger(prefs::kStabilityPageLoadCount));
71  EXPECT_EQ(0, local_state->GetInteger(prefs::kStabilityRendererCrashCount));
72}
73
74TEST_F(MetricsServiceTest, CrashRenderers) {
75  // This doesn't make sense to test in single process mode.
76  if (ProxyLauncher::in_process_renderer())
77    return;
78
79  OpenTabs();
80
81  {
82    // Limit the lifetime of various automation proxies used here. We must
83    // destroy them before calling QuitBrowser.
84
85    scoped_refptr<BrowserProxy> window = automation()->GetBrowserWindow(0);
86    ASSERT_TRUE(window.get());
87
88    // Kill the process for one of the tabs.
89    scoped_refptr<TabProxy> tab(window->GetTab(1));
90    ASSERT_TRUE(tab.get());
91
92    // We can get crash dumps on Windows always, Linux when breakpad is
93    // enabled, and all platforms for official Google Chrome builds.
94#if defined(OS_WIN) || defined(USE_LINUX_BREAKPAD) || \
95    defined(GOOGLE_CHROME_BUILD)
96    expected_crashes_ = 1;
97#endif
98    ASSERT_TRUE(tab->NavigateToURLAsync(GURL(chrome::kAboutCrashURL)));
99  }
100
101  // Give the browser a chance to notice the crashed tab.
102  base::PlatformThread::Sleep(sleep_timeout_ms());
103
104  QuitBrowser();
105
106  scoped_ptr<PrefService> local_state(GetLocalState());
107  local_state->RegisterBooleanPref(prefs::kStabilityExitedCleanly, true);
108  local_state->RegisterIntegerPref(prefs::kStabilityLaunchCount, 0);
109  local_state->RegisterIntegerPref(prefs::kStabilityPageLoadCount, 0);
110  local_state->RegisterIntegerPref(prefs::kStabilityRendererCrashCount, 0);
111  EXPECT_TRUE(local_state->GetBoolean(prefs::kStabilityExitedCleanly));
112  EXPECT_EQ(1, local_state->GetInteger(prefs::kStabilityLaunchCount));
113  EXPECT_EQ(4, local_state->GetInteger(prefs::kStabilityPageLoadCount));
114  EXPECT_EQ(1, local_state->GetInteger(prefs::kStabilityRendererCrashCount));
115}
116