172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Use of this source code is governed by a BSD-style license that can be 3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// found in the LICENSE file. 4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 5c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Tests the MetricsService stat recording to make sure that the numbers are 6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// what we expect. 7c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <string> 9c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 10c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/file_path.h" 11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/file_util.h" 12c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/path_service.h" 1372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "base/test/test_timeouts.h" 143f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen#include "base/threading/platform_thread.h" 153345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include "chrome/browser/prefs/pref_service.h" 1621d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen#include "chrome/browser/prefs/pref_service_mock_builder.h" 173345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include "chrome/browser/prefs/pref_value_store.h" 18c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/common/chrome_constants.h" 19c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/common/chrome_paths.h" 20c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/common/json_pref_store.h" 21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/common/pref_names.h" 22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/common/url_constants.h" 23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/test/automation/browser_proxy.h" 2421d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen#include "chrome/test/automation/tab_proxy.h" 25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/test/ui/ui_test.h" 26c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "net/base/net_util.h" 27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 28c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass MetricsServiceTest : public UITest { 29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public: 30c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch MetricsServiceTest() : UITest() { 31c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // We need to show the window so web content type tabs load. 32c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch show_window_ = true; 33c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 35c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Open a few tabs of random content 36c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch void OpenTabs() { 37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch scoped_refptr<BrowserProxy> window = automation()->GetBrowserWindow(0); 38c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_TRUE(window.get()); 39c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 40c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch FilePath page1_path; 41c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &page1_path)); 42c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch page1_path = page1_path.AppendASCII("title2.html"); 43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_TRUE(window->AppendTab(net::FilePathToFileURL(page1_path))); 44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch FilePath page2_path; 46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &page2_path)); 47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch page2_path = page2_path.AppendASCII("iframe.html"); 48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_TRUE(window->AppendTab(net::FilePathToFileURL(page2_path))); 49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 50c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Get a PrefService whose contents correspond to the Local State file 52c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // that was saved by the app as it closed. The caller takes ownership of the 53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // returned PrefService object. 54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch PrefService* GetLocalState() { 5521d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen FilePath path = user_data_dir().Append(chrome::kLocalStateFilename); 5621d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen return PrefServiceMockBuilder().WithUserFilePrefs(path).Create(); 57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}; 59c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 60c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochTEST_F(MetricsServiceTest, CloseRenderersNormally) { 61c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch OpenTabs(); 62c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch QuitBrowser(); 63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch scoped_ptr<PrefService> local_state(GetLocalState()); 65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch local_state->RegisterBooleanPref(prefs::kStabilityExitedCleanly, true); 66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch local_state->RegisterIntegerPref(prefs::kStabilityLaunchCount, 0); 67c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch local_state->RegisterIntegerPref(prefs::kStabilityPageLoadCount, 0); 68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch local_state->RegisterIntegerPref(prefs::kStabilityRendererCrashCount, 0); 69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(local_state->GetBoolean(prefs::kStabilityExitedCleanly)); 70c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(1, local_state->GetInteger(prefs::kStabilityLaunchCount)); 71c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(3, local_state->GetInteger(prefs::kStabilityPageLoadCount)); 72c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(0, local_state->GetInteger(prefs::kStabilityRendererCrashCount)); 73c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 74c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 75dc0f95d653279beabeb9817299e2902918ba123eKristian MonsenTEST_F(MetricsServiceTest, DISABLED_CrashRenderers) { 76c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // This doesn't make sense to test in single process mode. 773f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen if (ProxyLauncher::in_process_renderer()) 78c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return; 79c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 80c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch OpenTabs(); 81c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 82c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch { 83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Limit the lifetime of various automation proxies used here. We must 84c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // destroy them before calling QuitBrowser. 85c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 86c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch scoped_refptr<BrowserProxy> window = automation()->GetBrowserWindow(0); 87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_TRUE(window.get()); 88c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 89c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Kill the process for one of the tabs. 90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch scoped_refptr<TabProxy> tab(window->GetTab(1)); 91c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_TRUE(tab.get()); 92c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 933345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // We can get crash dumps on Windows always, Linux when breakpad is 943345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick // enabled, and all platforms for official Google Chrome builds. 953345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#if defined(OS_WIN) || defined(USE_LINUX_BREAKPAD) || \ 963345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick defined(GOOGLE_CHROME_BUILD) 97c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch expected_crashes_ = 1; 98c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif 99c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ASSERT_TRUE(tab->NavigateToURLAsync(GURL(chrome::kAboutCrashURL))); 100c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 101c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 102c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Give the browser a chance to notice the crashed tab. 10372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms()); 104c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 105c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch QuitBrowser(); 106c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 107c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch scoped_ptr<PrefService> local_state(GetLocalState()); 108c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch local_state->RegisterBooleanPref(prefs::kStabilityExitedCleanly, true); 109c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch local_state->RegisterIntegerPref(prefs::kStabilityLaunchCount, 0); 110c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch local_state->RegisterIntegerPref(prefs::kStabilityPageLoadCount, 0); 111c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch local_state->RegisterIntegerPref(prefs::kStabilityRendererCrashCount, 0); 112c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(local_state->GetBoolean(prefs::kStabilityExitedCleanly)); 113c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(1, local_state->GetInteger(prefs::kStabilityLaunchCount)); 114c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(4, local_state->GetInteger(prefs::kStabilityPageLoadCount)); 115c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(1, local_state->GetInteger(prefs::kStabilityRendererCrashCount)); 116c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 117