OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <set> | 5 #include <set> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/macros.h" |
10 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/metrics/histogram_base.h" |
| 13 #include "base/metrics/histogram_samples.h" |
| 14 #include "base/metrics/statistics_recorder.h" |
11 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
12 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
13 #include "base/values.h" | 17 #include "base/values.h" |
14 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 19 #include "chrome/browser/prefs/pref_hash_store.h" |
15 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/profiles/profile_impl.h" | 21 #include "chrome/browser/profiles/profile_impl.h" |
17 #include "chrome/browser/profiles/profile_info_cache.h" | 22 #include "chrome/browser/profiles/profile_info_cache.h" |
18 #include "chrome/browser/profiles/profile_manager.h" | 23 #include "chrome/browser/profiles/profile_manager.h" |
19 #include "chrome/browser/profiles/profiles_state.h" | 24 #include "chrome/browser/profiles/profiles_state.h" |
20 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
21 #include "chrome/test/base/in_process_browser_test.h" | 26 #include "chrome/test/base/in_process_browser_test.h" |
22 #include "content/public/test/test_utils.h" | 27 #include "content/public/test/test_utils.h" |
23 | 28 |
24 namespace { | 29 namespace { |
(...skipping 26 matching lines...) Expand all Loading... |
51 std::set<base::FilePath> profile_paths; | 56 std::set<base::FilePath> profile_paths; |
52 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) | 57 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) |
53 profile_paths.insert(cache.GetPathOfProfileAtIndex(i)); | 58 profile_paths.insert(cache.GetPathOfProfileAtIndex(i)); |
54 for (size_t i = 0; i < loaded_profiles.size(); ++i) | 59 for (size_t i = 0; i < loaded_profiles.size(); ++i) |
55 EXPECT_EQ(1U, profile_paths.erase(loaded_profiles[i]->GetPath())); | 60 EXPECT_EQ(1U, profile_paths.erase(loaded_profiles[i]->GetPath())); |
56 if (profile_paths.size()) | 61 if (profile_paths.size()) |
57 return *profile_paths.begin(); | 62 return *profile_paths.begin(); |
58 return base::FilePath(); | 63 return base::FilePath(); |
59 } | 64 } |
60 | 65 |
| 66 // Returns the number of times |histogram_name| was reported so far; adding the |
| 67 // results of the first 100 buckets (there are only ~14 reporting IDs as of this |
| 68 // writting; varies depending on the platform). If |expect_zero| is true, this |
| 69 // method will explicitly report IDs that are non-zero for ease of diagnosis. |
| 70 int GetTrackedPrefHistogramCount(const char* histogram_name, bool expect_zero) { |
| 71 const base::HistogramBase* histogram = |
| 72 base::StatisticsRecorder::FindHistogram(histogram_name); |
| 73 if (!histogram) |
| 74 return 0; |
| 75 |
| 76 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); |
| 77 int sum = 0; |
| 78 for (int i = 0; i < 100; ++i) { |
| 79 int count_for_id = samples->GetCount(i); |
| 80 sum += count_for_id; |
| 81 |
| 82 if (expect_zero) |
| 83 EXPECT_EQ(0, count_for_id) << "Faulty reporting_id: " << i; |
| 84 } |
| 85 return sum; |
| 86 } |
| 87 |
61 } // namespace | 88 } // namespace |
62 | 89 |
63 typedef InProcessBrowserTest PrefHashBrowserTest; | 90 typedef InProcessBrowserTest PrefHashBrowserTest; |
64 | 91 |
65 IN_PROC_BROWSER_TEST_F(PrefHashBrowserTest, | 92 IN_PROC_BROWSER_TEST_F(PrefHashBrowserTest, |
66 PRE_PRE_InitializeUnloadedProfiles) { | 93 PRE_PRE_InitializeUnloadedProfiles) { |
67 if (!profiles::IsMultipleProfilesEnabled()) | 94 if (!profiles::IsMultipleProfilesEnabled()) |
68 return; | 95 return; |
69 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 96 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
70 | 97 |
71 // Create an additional profile. | 98 // Create an additional profile. |
72 const base::FilePath new_path = | 99 const base::FilePath new_path = |
73 profile_manager->GenerateNextProfileDirectoryPath(); | 100 profile_manager->GenerateNextProfileDirectoryPath(); |
74 const scoped_refptr<content::MessageLoopRunner> runner( | 101 const scoped_refptr<content::MessageLoopRunner> runner( |
75 new content::MessageLoopRunner); | 102 new content::MessageLoopRunner); |
76 profile_manager->CreateProfileAsync( | 103 profile_manager->CreateProfileAsync( |
77 new_path, | 104 new_path, |
78 base::Bind(&OnUnblockOnProfileCreation, runner->QuitClosure()), | 105 base::Bind(&OnUnblockOnProfileCreation, runner->QuitClosure()), |
79 base::string16(), | 106 base::string16(), |
80 base::string16(), | 107 base::string16(), |
81 std::string()); | 108 std::string()); |
82 | 109 |
83 // Spin to allow profile creation to take place, loop is terminated | 110 // Spin to allow profile creation to take place, loop is terminated |
84 // by OnUnblockOnProfileCreation when the profile is created. | 111 // by OnUnblockOnProfileCreation when the profile is created. |
85 runner->Run(); | 112 runner->Run(); |
| 113 |
| 114 // No profile should have gone through the unloaded profile initialization in |
| 115 // this phase as both profiles should have been loaded normally. |
| 116 EXPECT_EQ( |
| 117 0, GetTrackedPrefHistogramCount( |
| 118 "Settings.TrackedPreferencesInitializedForUnloadedProfile", |
| 119 true)); |
86 } | 120 } |
87 | 121 |
88 IN_PROC_BROWSER_TEST_F(PrefHashBrowserTest, | 122 IN_PROC_BROWSER_TEST_F(PrefHashBrowserTest, |
89 PRE_InitializeUnloadedProfiles) { | 123 PRE_InitializeUnloadedProfiles) { |
90 if (!profiles::IsMultipleProfilesEnabled()) | 124 if (!profiles::IsMultipleProfilesEnabled()) |
91 return; | 125 return; |
92 | 126 |
93 // Creating the profile would have initialized its hash store. Also, we don't | 127 // Creating the profile would have initialized its hash store. Also, we don't |
94 // know whether the newly created or original profile will be launched (does | 128 // know whether the newly created or original profile will be launched (does |
95 // creating a profile cause it to be the most recently used?). | 129 // creating a profile cause it to be the most recently used?). |
96 | 130 |
97 // So we will find the profile that isn't loaded, reset its hash store, and | 131 // So we will find the profile that isn't loaded, reset its hash store, and |
98 // then verify in the _next_ launch that it is, indeed, restored despite not | 132 // then verify in the _next_ launch that it is, indeed, restored despite not |
99 // having been loaded. | 133 // having been loaded. |
100 | 134 |
101 const base::DictionaryValue* hashes = | 135 const base::DictionaryValue* hashes = |
102 g_browser_process->local_state()->GetDictionary( | 136 g_browser_process->local_state()->GetDictionary( |
103 prefs::kProfilePreferenceHashes); | 137 prefs::kProfilePreferenceHashes); |
104 | 138 |
105 // 3 is for hash_of_hashes, default profile, and new profile. | 139 // 3 is for hash_of_hashes, default profile, and new profile. |
106 ASSERT_EQ(3U, hashes->size()); | 140 ASSERT_EQ(3U, hashes->size()); |
107 | 141 |
108 // One of the two profiles should not have been loaded. Reset its hash store. | 142 // One of the two profiles should not have been loaded. Reset its hash store. |
109 const base::FilePath unloaded_profile_path = GetUnloadedProfilePath(); | 143 const base::FilePath unloaded_profile_path = GetUnloadedProfilePath(); |
110 ProfileImpl::ResetPrefHashStore(unloaded_profile_path); | 144 ProfileImpl::ResetPrefHashStore(unloaded_profile_path); |
111 | 145 |
112 // One of the profile hash collections should be gone. | 146 // One of the profile hash collections should be gone. |
113 ASSERT_EQ(2U, hashes->size()); | 147 ASSERT_EQ(2U, hashes->size()); |
| 148 |
| 149 // No profile should have gone through the unloaded profile initialization in |
| 150 // this phase as both profiles were already initialized at the beginning of |
| 151 // this phase (resetting the unloaded profile's PrefHashStore should only |
| 152 // force initialization in the next phase's startup). |
| 153 EXPECT_EQ( |
| 154 0, GetTrackedPrefHistogramCount( |
| 155 "Settings.TrackedPreferencesInitializedForUnloadedProfile", |
| 156 true)); |
114 } | 157 } |
115 | 158 |
116 IN_PROC_BROWSER_TEST_F(PrefHashBrowserTest, | 159 IN_PROC_BROWSER_TEST_F(PrefHashBrowserTest, |
117 InitializeUnloadedProfiles) { | 160 InitializeUnloadedProfiles) { |
118 if (!profiles::IsMultipleProfilesEnabled()) | 161 if (!profiles::IsMultipleProfilesEnabled()) |
119 return; | 162 return; |
120 | 163 |
121 const base::DictionaryValue* hashes = | 164 const base::DictionaryValue* hashes = |
122 g_browser_process->local_state()->GetDictionary( | 165 g_browser_process->local_state()->GetDictionary( |
123 prefs::kProfilePreferenceHashes); | 166 prefs::kProfilePreferenceHashes); |
124 | 167 |
125 // The deleted hash collection should be restored. | 168 // The deleted hash collection should be restored. |
126 ASSERT_EQ(3U, hashes->size()); | 169 ASSERT_EQ(3U, hashes->size()); |
127 | 170 |
| 171 // Verify that the initialization truly did occur in this phase's startup; |
| 172 // rather than in the previous phase's shutdown. |
| 173 EXPECT_EQ( |
| 174 1, GetTrackedPrefHistogramCount( |
| 175 "Settings.TrackedPreferencesInitializedForUnloadedProfile", |
| 176 false)); |
| 177 |
128 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 178 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
129 const std::vector<Profile*> loaded_profiles = | |
130 profile_manager->GetLoadedProfiles(); | |
131 | 179 |
132 // Verify that only one profile was loaded. We assume that the unloaded | 180 // Verify that only one profile was loaded. We assume that the unloaded |
133 // profile is the same one that wasn't loaded in the last launch (i.e., it's | 181 // profile is the same one that wasn't loaded in the last launch (i.e., it's |
134 // the one whose hash store we reset, and the fact that it is now restored is | 182 // the one whose hash store we reset, and the fact that it is now restored is |
135 // evidence that we restored the hashes of an unloaded profile.). | 183 // evidence that we restored the hashes of an unloaded profile.). |
136 ASSERT_EQ(1U, loaded_profiles.size()); | 184 ASSERT_EQ(1U, profile_manager->GetLoadedProfiles().size()); |
| 185 |
| 186 // Loading the first profile should only have produced unchanged reports. |
| 187 EXPECT_EQ( |
| 188 0, GetTrackedPrefHistogramCount( |
| 189 "Settings.TrackedPreferenceChanged", true)); |
| 190 EXPECT_EQ( |
| 191 0, GetTrackedPrefHistogramCount( |
| 192 "Settings.TrackedPreferenceCleared", true)); |
| 193 EXPECT_EQ( |
| 194 0, GetTrackedPrefHistogramCount( |
| 195 "Settings.TrackedPreferenceInitialized", true)); |
| 196 EXPECT_EQ( |
| 197 0, GetTrackedPrefHistogramCount( |
| 198 "Settings.TrackedPreferenceTrustedInitialized", true)); |
| 199 EXPECT_EQ( |
| 200 0, GetTrackedPrefHistogramCount( |
| 201 "Settings.TrackedPreferenceMigrated", true)); |
| 202 int initial_unchanged_count = |
| 203 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceUnchanged", |
| 204 false); |
| 205 EXPECT_GT(initial_unchanged_count, 0); |
| 206 |
| 207 // Explicitly load the unloaded profile. |
| 208 profile_manager->GetProfile(GetUnloadedProfilePath()); |
| 209 ASSERT_EQ(2U, profile_manager->GetLoadedProfiles().size()); |
| 210 |
| 211 // Loading the unexpected profile should only generate unchanged pings; and |
| 212 // should have produced as many of them as loading the first profile. |
| 213 EXPECT_EQ( |
| 214 0, GetTrackedPrefHistogramCount( |
| 215 "Settings.TrackedPreferenceChanged", true)); |
| 216 EXPECT_EQ( |
| 217 0, GetTrackedPrefHistogramCount( |
| 218 "Settings.TrackedPreferenceCleared", true)); |
| 219 EXPECT_EQ( |
| 220 0, GetTrackedPrefHistogramCount( |
| 221 "Settings.TrackedPreferenceInitialized", true)); |
| 222 EXPECT_EQ( |
| 223 0, GetTrackedPrefHistogramCount( |
| 224 "Settings.TrackedPreferenceTrustedInitialized", true)); |
| 225 EXPECT_EQ( |
| 226 0, GetTrackedPrefHistogramCount( |
| 227 "Settings.TrackedPreferenceMigrated", true)); |
| 228 EXPECT_EQ( |
| 229 initial_unchanged_count * 2, |
| 230 GetTrackedPrefHistogramCount("Settings.TrackedPreferenceUnchanged", |
| 231 false)); |
137 } | 232 } |
OLD | NEW |