Chromium Code Reviews| Index: chrome/browser/ui/profile_error_browsertest.cc |
| diff --git a/chrome/browser/ui/profile_error_browsertest.cc b/chrome/browser/ui/profile_error_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f1bf1e6789594ef1ee024604c2bba19b4aeb07d5 |
| --- /dev/null |
| +++ b/chrome/browser/ui/profile_error_browsertest.cc |
| @@ -0,0 +1,117 @@ |
| +// 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" |
|
huangs
2015/06/16 17:50:52
Remove unused #includes. I don't see the followin
|
| +#include "base/files/file_util.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/metrics/histogram_base.h" |
| +#include "base/metrics/histogram_samples.h" |
| +#include "base/metrics/statistics_recorder.h" |
| +#include "base/path_service.h" |
| +#include "base/prefs/pref_service.h" |
| +#include "base/strings/string_util.h" |
| +#include "base/test/histogram_tester.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/simple_message_box_internal.h" |
| +#include "chrome/common/chrome_constants.h" |
| +#include "chrome/common/chrome_paths.h" |
| +#include "chrome/common/pref_names.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" |
| + |
| +namespace { |
| + |
| +class ProfileErrorBrowserTest : public InProcessBrowserTest, |
| + public testing::WithParamInterface<bool> { |
| + public: |
| + ProfileErrorBrowserTest() : do_corrupt_(GetParam()) {} |
| + |
| + void SetUp() override { |
| + // Imitate the behavior but do not show the message box in the main test so |
| + // it can terminate |
| + if (!IsPRETest()) |
| + chrome::internal::g_should_skip_message_box_for_test = true; |
| + InProcessBrowserTest::SetUp(); |
| + } |
| + |
| + bool SetUpUserDataDirectory() override { |
| + // Setup normally in the PRE test and corrupt user profile in the main test. |
| + if (IsPRETest()) |
| + return InProcessBrowserTest::SetUpUserDataDirectory(); |
| + |
| + if (do_corrupt_) |
| + CorruptProfileOnDisk(); |
| + return true; |
| + } |
| + |
| + protected: |
| + void CorruptProfileOnDisk() { |
| + base::FilePath profile_dir; |
| + EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &profile_dir)); |
| + profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir); |
| + |
| + const base::FilePath pref_file = |
| + profile_dir.Append(chrome::kPreferencesFilename); |
| + EXPECT_TRUE(base::PathExists(pref_file)); |
| + |
| + // Corrupt the user profile. |
| + std::string junk("junk"); |
| + EXPECT_TRUE(base::AppendToFile(pref_file, junk.c_str(), junk.size())); |
| + } |
| + |
| + base::HistogramTester& histograms() { return histograms_tester_; } |
| + |
| + // Decide to corrupt or not depending on the params passed into the test |
| + const bool do_corrupt_; |
| + |
| + private: |
| + // Returns true if this is the PRE_ phase of the test. |
| + bool IsPRETest() { |
| + return base::StartsWithASCII( |
| + testing::UnitTest::GetInstance()->current_test_info()->name(), "PRE_", |
| + true /* case_sensitive */); |
| + } |
| + |
| + // Histogram value verifier. |
| + base::HistogramTester histograms_tester_; |
| +}; |
| + |
| +#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(test) DISABLED_##test |
| +#else |
| +#define MAYBE(test) test |
| +#endif |
| + |
| +IN_PROC_BROWSER_TEST_P(ProfileErrorBrowserTest, MAYBE(PRE_CorruptedProfile)) { |
| + // Nothing to do, the purpose of this PRE test is only to bring up a default |
| + // User Data directory. |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_P(ProfileErrorBrowserTest, MAYBE(CorruptedProfile)) { |
| + const char* kPaintHistogram = "Startup.FirstWebContents.NonEmptyPaint"; |
| + const char* kFrameHistogram = "Startup.FirstWebContents.MainFrameLoad"; |
| + |
| + // Navigate to a URL so the first non-empty paint is registered. |
| + ui_test_utils::NavigateToURL(browser(), GURL("http://www.google.com/")); |
| + if (do_corrupt_) { |
| + histograms().ExpectTotalCount(kPaintHistogram, 0); |
| + histograms().ExpectTotalCount(kFrameHistogram, 0); |
| + } else { |
| + histograms().ExpectTotalCount(kPaintHistogram, 1); |
| + histograms().ExpectTotalCount(kFrameHistogram, 1); |
| + } |
| +} |
| + |
| +INSTANTIATE_TEST_CASE_P(ProfileErrorBrowserTestInstance, |
| + ProfileErrorBrowserTest, |
| + testing::Bool()); |
| + |
| +} // namespace |