summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/profile_error_browsertest.cc
blob: 0c04c39d6e71518aa3c21442dac1162d46132c77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// 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/histogram_tester.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.
 public:
  ProfileErrorBrowserTest() : do_corrupt_(GetParam()) {}

  bool SetUpUserDataDirectory() override {
    base::FilePath profile_dir;
    if (!PathService::Get(chrome::DIR_USER_DATA, &profile_dir)) {
      ADD_FAILURE();
      return false;
    }
    profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir);
    if (!base::CreateDirectory(profile_dir)) {
      ADD_FAILURE();
      return false;
    }
    const base::FilePath pref_file =
        profile_dir.Append(chrome::kPreferencesFilename);

    // 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())) {
      ADD_FAILURE();
      return false;
    }
    return true;
  }

  void SetUpInProcessBrowserTestFixture() override {
    InProcessBrowserTest::SetUpInProcessBrowserTestFixture();

    // Skip showing the error message box in order to avoid freezing the main
    // thread.
    chrome::internal::g_should_skip_message_box_for_test = true;
  }

 protected:
  // 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
#else
// http://crbug.com/527145
#define MAYBE_CorruptedProfile DISABLED_CorruptedProfile
#endif

IN_PROC_BROWSER_TEST_P(ProfileErrorBrowserTest, MAYBE_CorruptedProfile) {
  const char kPaintHistogram[] = "Startup.FirstWebContents.NonEmptyPaint";
  const char kLoadHistogram[] = "Startup.FirstWebContents.MainFrameLoad";

  // Navigate to a URL so the first non-empty paint is registered.
  ui_test_utils::NavigateToURL(browser(), GURL("http://www.example.com/"));

  content::WebContents* contents =
      browser()->tab_strip_model()->GetActiveWebContents();

  // Wait for the page to produce a frame, the first visually non-empty paint
  // metric is not valid until then.
  bool loaded = false;
  ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
      contents,
      "requestAnimationFrame(function() {"
      "  window.domAutomationController.send(true);"
      "});",
      &loaded));
  ASSERT_TRUE(loaded);

  if (do_corrupt_) {
    histogram_tester_.ExpectTotalCount(kPaintHistogram, 0);
    histogram_tester_.ExpectTotalCount(kLoadHistogram, 0);
  } else {
    histogram_tester_.ExpectTotalCount(kPaintHistogram, 1);
    histogram_tester_.ExpectTotalCount(kLoadHistogram, 1);
  }
}

INSTANTIATE_TEST_CASE_P(ProfileErrorBrowserTestInstance,
                        ProfileErrorBrowserTest,
                        testing::Bool());

}  // namespace