blob: 4cbb341ca237b4a196fa417206acc8ebc5de6356 [file] [log] [blame]
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "build/build_config.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/simple_message_box_internal.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test_utils.h"
namespace {
class ProfileErrorBrowserTest : public InProcessBrowserTest,
public testing::WithParamInterface<bool> {
// A fixture that allows testing histograms reporting when faced with a
// corrupted profile. The boolean parameter forces the creation of an empty or
// corrupted profile, allowing to test both the corruption case and that what
// it is testing indeed happens differently when not under corruption.
ProfileErrorBrowserTest() : do_corrupt_(GetParam()) {}
bool SetUpUserDataDirectory() override {
base::FilePath profile_dir;
if (!base::PathService::Get(chrome::DIR_USER_DATA, &profile_dir)) {
return false;
profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir);
if (!base::CreateDirectory(profile_dir)) {
return false;
const base::FilePath pref_file =
// Write either an empty or an invalid string to the user profile as
// determined by the boolean parameter.
const std::string kUserProfileData(do_corrupt_ ? "invalid json" : "{}");
if (base::WriteFile(pref_file, kUserProfileData.c_str(),
kUserProfileData.size()) !=
static_cast<int>(kUserProfileData.size())) {
return false;
return true;
void SetUpInProcessBrowserTestFixture() override {
// Skip showing the error message box in order to avoid freezing the main
// thread.
chrome::internal::g_should_skip_message_box_for_test = true;
// Histogram value verifier.
const base::HistogramTester histogram_tester_;
// Whether the test fixture and test should set up a corrupted profile and
// expect a reaction to one.
const bool do_corrupt_;
#if defined(OS_CHROMEOS)
// Disable the test on chromos since kernel controls the user profile thus we
// won't be able to corrupt it.
#define MAYBE_CorruptedProfile DISABLED_CorruptedProfile
#define MAYBE_CorruptedProfile DISABLED_CorruptedProfile
IN_PROC_BROWSER_TEST_P(ProfileErrorBrowserTest, MAYBE_CorruptedProfile) {
const char kPaintHistogram[] = "Startup.FirstWebContents.NonEmptyPaint2";
const char kLoadHistogram[] = "Startup.FirstWebContents.MainFrameLoad2";
// Navigate to a URL so the first non-empty paint is registered.
ui_test_utils::NavigateToURL(browser(), GURL(""));
content::WebContents* contents =
// Wait for the page to produce a frame, the first visually non-empty paint
// metric is not valid until then.
bool loaded = false;
"requestAnimationFrame(function() {"
" window.domAutomationController.send(true);"
if (do_corrupt_) {
histogram_tester_.ExpectTotalCount(kPaintHistogram, 0);
histogram_tester_.ExpectTotalCount(kLoadHistogram, 0);
} else {
histogram_tester_.ExpectTotalCount(kPaintHistogram, 1);
histogram_tester_.ExpectTotalCount(kLoadHistogram, 1);
} // namespace