Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: chrome/browser/ui/profile_error_browsertest.cc

Issue 1169503002: Do not record startup metrics when non-browser UI was displayed (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added macro to completely disable the test on chromeos Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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.
4
5 #include <string>
6
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/metrics/histogram_base.h"
10 #include "base/metrics/histogram_samples.h"
11 #include "base/metrics/statistics_recorder.h"
12 #include "base/path_service.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/strings/string_util.h"
15 #include "base/values.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/simple_message_box_internal.h"
19 #include "chrome/common/chrome_constants.h"
20 #include "chrome/common/chrome_paths.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/test/base/in_process_browser_test.h"
23 #include "chrome/test/base/testing_profile.h"
24 #include "chrome/test/base/ui_test_utils.h"
25
26 namespace {
27
28 // Returns the number of times |histogram_name| was reported so far;
29 int GetTrackedStartupHistogramCount(const char* histogram_name) {
gab 2015/06/09 15:52:28 Given you test only cares whether queried histogra
30 const base::HistogramBase* histogram =
31 base::StatisticsRecorder::FindHistogram(histogram_name);
32 if (!histogram)
33 return 0;
34
35 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
36 return samples->TotalCount();
37 }
38
39 #define PROFILE_ERROR_BROWSER_TEST(fixture, test_name) \
40 IN_PROC_BROWSER_TEST_P(fixture, PRE_##test_name) { SetupProfile(); } \
41 IN_PROC_BROWSER_TEST_P(fixture, test_name) { VerifyReactionToProfAttack(); } \
42 INSTANTIATE_TEST_CASE_P(fixture##Instance, fixture, testing::Bool());
gab 2015/06/09 15:52:28 Inline these below (i.e. get rid of the PROFILE_ER
43
44 // A base fixture that instantiates tests via the PREF_HASH_BROWSER_TEST
45 // macro above.
gab 2015/06/09 15:52:28 Remove this comment.
46 class ProfileErrorBrowserTest : public InProcessBrowserTest,
47 public testing::WithParamInterface<bool> {
48 public:
49 ProfileErrorBrowserTest() : do_attack_(GetParam()) {}
50
51 // Called from the PRE_ test's body, sets up the profile so the main test
52 // can attack it.
53 void SetupProfile() {
54 browser()->profile()->GetPrefs()->SetString(prefs::kHomePage,
55 "http://example.com");
gab 2015/06/09 15:52:28 I'd say get rid of this (and have the PRE_ test be
56 }
57
58 bool SetUpUserDataDirectory() override {
59 // Setup normally in the PRE test and attack user profile in the main test.
60 if (IsPRETest())
61 return InProcessBrowserTest::SetUpUserDataDirectory();
62
63 AttackProfileOnDisk();
64 return true;
65 }
66
67 void SetUpInProcessBrowserTestFixture() override {
68 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
gab 2015/06/09 15:52:28 Remove this override (i.e. if you don't override t
69 }
70
71 protected:
72 void AttackProfileOnDisk() {
gab 2015/06/09 15:52:28 Rename "Attack" to "Corrupt" here an elsewhere (th
73 if (do_attack_) {
gab 2015/06/09 15:52:28 Instead of having the boolean in here, have the ca
74 base::FilePath profile_dir;
75 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &profile_dir));
76 profile_dir =
77 profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir);
78
79 // Read the preferences from disk.
gab 2015/06/09 15:52:28 Remove this comment.
80 const base::FilePath unprotected_pref_file =
gab 2015/06/09 15:52:28 s/unprotected_pref_file/pref_file/
81 profile_dir.Append(chrome::kPreferencesFilename);
82 EXPECT_TRUE(base::PathExists(unprotected_pref_file));
83
84 // Corrupt the user profile.
85 std::string junk("junk");
86 EXPECT_TRUE(
87 base::AppendToFile(unprotected_pref_file, junk.c_str(), junk.size()));
88
89 // Emulate but do not show the message box so the test can terminate
90 chrome::internal::g_should_skip_message_box_for_test = true;
gab 2015/06/09 15:52:28 I don't think this belongs in the CorruptProfileOn
91 }
92 }
93
94 // Called from the body of the main test. Verifies that the browser had the
95 // desired reaction facing the attack orchestrated in AttackProfileOnDisk().
96 void VerifyReactionToProfAttack() {
gab 2015/06/09 15:52:28 I think this should simply move to the body of the
97 // Navigate to a URL so the first non-empty paint is registered.
98 ui_test_utils::NavigateToURL(browser(), GURL("http://www.google.com/"));
99 if (do_attack_) {
100 EXPECT_EQ(GetTrackedStartupHistogramCount(
101 "Startup.FirstWebContents.NonEmptyPaint"),
102 0);
103 EXPECT_EQ(GetTrackedStartupHistogramCount(
104 "Startup.FirstWebContents.MainFrameLoad"),
105 0);
106 } else {
107 EXPECT_GT(GetTrackedStartupHistogramCount(
108 "Startup.FirstWebContents.NonEmptyPaint"),
109 0);
110 EXPECT_GT(GetTrackedStartupHistogramCount(
111 "Startup.FirstWebContents.MainFrameLoad"),
112 0);
113 }
114 }
115
116 bool do_attack_;
117
118 private:
119 // Returns true if this is the PRE_ phase of the test.
120 bool IsPRETest() {
121 return StartsWithASCII(
122 testing::UnitTest::GetInstance()->current_test_info()->name(), "PRE_",
123 true /* case_sensitive */);
124 }
125 };
126
127 } // namespace
128
129 #if defined(OS_CHROMEOS)
130 #define MAYBE_CorruptedProfile DISABLED_CorruptedProfile
131 #define PRE_MAYBE_CorruptedProfile PRE_DISABLED_CorruptedProfile
132 #else
133 #define MAYBE_CorruptedProfile CorruptedProfile
134 #define PRE_MAYBE_CorruptedProfile PRE_CorruptedProfile
135 #endif
136 // Disable the test on chromos since kernel controls the user profile we
137 // won't be able to attack.
gab 2015/06/09 15:52:28 This comment should go on line 130 between the if
138 PROFILE_ERROR_BROWSER_TEST(ProfileErrorBrowserTest, MAYBE_CorruptedProfile);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698