Infobar material design refresh: layout
[chromium-blink-merge.git] / chrome / browser / ui / profile_error_browsertest.cc
blob0c04c39d6e71518aa3c21442dac1162d46132c77
1 // Copyright 2015 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.
5 #include <string>
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/path_service.h"
10 #include "base/strings/string_util.h"
11 #include "base/test/histogram_tester.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/simple_message_box_internal.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/common/chrome_constants.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "content/public/test/browser_test_utils.h"
22 namespace {
24 class ProfileErrorBrowserTest : public InProcessBrowserTest,
25 public testing::WithParamInterface<bool> {
26 // A fixture that allows testing histograms reporting when faced with a
27 // corrupted profile. The boolean parameter forces the creation of an empty or
28 // corrupted profile, allowing to test both the corruption case and that what
29 // it is testing indeed happens differently when not under corruption.
30 public:
31 ProfileErrorBrowserTest() : do_corrupt_(GetParam()) {}
33 bool SetUpUserDataDirectory() override {
34 base::FilePath profile_dir;
35 if (!PathService::Get(chrome::DIR_USER_DATA, &profile_dir)) {
36 ADD_FAILURE();
37 return false;
39 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir);
40 if (!base::CreateDirectory(profile_dir)) {
41 ADD_FAILURE();
42 return false;
44 const base::FilePath pref_file =
45 profile_dir.Append(chrome::kPreferencesFilename);
47 // Write either an empty or an invalid string to the user profile as
48 // determined by the boolean parameter.
49 const std::string kUserProfileData(do_corrupt_ ? "invalid json" : "{}");
50 if (!base::WriteFile(pref_file, kUserProfileData.c_str(),
51 kUserProfileData.size())) {
52 ADD_FAILURE();
53 return false;
55 return true;
58 void SetUpInProcessBrowserTestFixture() override {
59 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
61 // Skip showing the error message box in order to avoid freezing the main
62 // thread.
63 chrome::internal::g_should_skip_message_box_for_test = true;
66 protected:
67 // Histogram value verifier.
68 const base::HistogramTester histogram_tester_;
70 // Whether the test fixture and test should set up a corrupted profile and
71 // expect a reaction to one.
72 const bool do_corrupt_;
75 #if defined(OS_CHROMEOS)
76 // Disable the test on chromos since kernel controls the user profile thus we
77 // won't be able to corrupt it.
78 #define MAYBE_CorruptedProfile DISABLED_CorruptedProfile
79 #else
80 // http://crbug.com/527145
81 #define MAYBE_CorruptedProfile DISABLED_CorruptedProfile
82 #endif
84 IN_PROC_BROWSER_TEST_P(ProfileErrorBrowserTest, MAYBE_CorruptedProfile) {
85 const char kPaintHistogram[] = "Startup.FirstWebContents.NonEmptyPaint";
86 const char kLoadHistogram[] = "Startup.FirstWebContents.MainFrameLoad";
88 // Navigate to a URL so the first non-empty paint is registered.
89 ui_test_utils::NavigateToURL(browser(), GURL("http://www.example.com/"));
91 content::WebContents* contents =
92 browser()->tab_strip_model()->GetActiveWebContents();
94 // Wait for the page to produce a frame, the first visually non-empty paint
95 // metric is not valid until then.
96 bool loaded = false;
97 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
98 contents,
99 "requestAnimationFrame(function() {"
100 " window.domAutomationController.send(true);"
101 "});",
102 &loaded));
103 ASSERT_TRUE(loaded);
105 if (do_corrupt_) {
106 histogram_tester_.ExpectTotalCount(kPaintHistogram, 0);
107 histogram_tester_.ExpectTotalCount(kLoadHistogram, 0);
108 } else {
109 histogram_tester_.ExpectTotalCount(kPaintHistogram, 1);
110 histogram_tester_.ExpectTotalCount(kLoadHistogram, 1);
114 INSTANTIATE_TEST_CASE_P(ProfileErrorBrowserTestInstance,
115 ProfileErrorBrowserTest,
116 testing::Bool());
118 } // namespace