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

Side by Side Diff: chrome/browser/profiles/profile_browsertest.cc

Issue 225693005: Try to fix ProfileBrowserTest. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleanup: Common helper function to spin threads. Created 6 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/profiles/profile.h" 5 #include "chrome/browser/profiles/profile.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/prefs/pref_service.h" 9 #include "base/prefs/pref_service.h"
10 #include "base/synchronization/waitable_event.h"
10 #include "base/version.h" 11 #include "base/version.h"
11 #include "chrome/browser/chrome_notification_types.h" 12 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/profiles/chrome_version_service.h" 13 #include "chrome/browser/profiles/chrome_version_service.h"
13 #include "chrome/browser/profiles/profile_impl.h" 14 #include "chrome/browser/profiles/profile_impl.h"
14 #include "chrome/browser/profiles/startup_task_runner_service.h"
15 #include "chrome/browser/profiles/startup_task_runner_service_factory.h"
16 #include "chrome/common/chrome_constants.h" 15 #include "chrome/common/chrome_constants.h"
17 #include "chrome/common/chrome_version_info.h" 16 #include "chrome/common/chrome_version_info.h"
18 #include "chrome/common/pref_names.h" 17 #include "chrome/common/pref_names.h"
19 #include "chrome/test/base/in_process_browser_test.h" 18 #include "chrome/test/base/in_process_browser_test.h"
20 #include "chrome/test/base/ui_test_utils.h" 19 #include "chrome/test/base/ui_test_utils.h"
21 #include "testing/gmock/include/gmock/gmock.h" 20 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
23 22
24 namespace { 23 namespace {
25 24
(...skipping 17 matching lines...) Expand all
43 created_by_version = version_info.Version(); 42 created_by_version = version_info.Version();
44 } else { 43 } else {
45 created_by_version = "1.0.0.0"; 44 created_by_version = "1.0.0.0";
46 } 45 }
47 std::string pref_version = 46 std::string pref_version =
48 ChromeVersionService::GetVersion(profile->GetPrefs()); 47 ChromeVersionService::GetVersion(profile->GetPrefs());
49 // Assert that created_by_version pref gets set to current version. 48 // Assert that created_by_version pref gets set to current version.
50 EXPECT_EQ(created_by_version, pref_version); 49 EXPECT_EQ(created_by_version, pref_version);
51 } 50 }
52 51
52 void BlockThread(
53 base::WaitableEvent* is_blocked,
54 base::WaitableEvent* unblock) {
55 is_blocked->Signal();
56 unblock->Wait();
57 }
58
59 void SpinThreads() {
60 // Give threads a chance to do their stuff before shutting down (i.e.
61 // deleting scoped temp dir etc).
62 // Should not be necessary anymore once Profile deletion is fixed
63 // (see crbug.com/88586).
64 content::RunAllPendingInMessageLoop();
65 content::RunAllPendingInMessageLoop(content::BrowserThread::DB);
66 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
67 }
68
53 } // namespace 69 } // namespace
54 70
55 typedef InProcessBrowserTest ProfileBrowserTest; 71 typedef InProcessBrowserTest ProfileBrowserTest;
56 72
57 // Test OnProfileCreate is called with is_new_profile set to true when 73 // Test OnProfileCreate is called with is_new_profile set to true when
58 // creating a new profile synchronously. 74 // creating a new profile synchronously.
59 // 75 //
60 // Flaky (sometimes timeout): http://crbug.com/141141 76 // Flaky (sometimes timeout): http://crbug.com/141141
61 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, 77 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest,
62 DISABLED_CreateNewProfileSynchronous) { 78 DISABLED_CreateNewProfileSynchronous) {
63 base::ScopedTempDir temp_dir; 79 base::ScopedTempDir temp_dir;
64 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 80 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
65 81
66 MockProfileDelegate delegate; 82 MockProfileDelegate delegate;
67 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); 83 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true));
68 84
69 scoped_ptr<Profile> profile(Profile::CreateProfile( 85 {
70 temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS)); 86 scoped_ptr<Profile> profile(Profile::CreateProfile(
71 ASSERT_TRUE(profile.get()); 87 temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS));
72 CheckChromeVersion(profile.get(), true); 88 ASSERT_TRUE(profile.get());
73 StartupTaskRunnerServiceFactory::GetForProfile(profile.get())-> 89 CheckChromeVersion(profile.get(), true);
74 StartDeferredTaskRunners(); 90 }
91
92 SpinThreads();
75 } 93 }
76 94
77 // Test OnProfileCreate is called with is_new_profile set to false when 95 // Test OnProfileCreate is called with is_new_profile set to false when
78 // creating a profile synchronously with an existing prefs file. 96 // creating a profile synchronously with an existing prefs file.
79 // Flaky: http://crbug.com/141517 97 // Flaky: http://crbug.com/141517
80 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, 98 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest,
81 DISABLED_CreateOldProfileSynchronous) { 99 DISABLED_CreateOldProfileSynchronous) {
82 base::ScopedTempDir temp_dir; 100 base::ScopedTempDir temp_dir;
83 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 101 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
84 CreatePrefsFileInDirectory(temp_dir.path()); 102 CreatePrefsFileInDirectory(temp_dir.path());
85 103
86 MockProfileDelegate delegate; 104 MockProfileDelegate delegate;
87 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, false)); 105 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, false));
88 106
89 scoped_ptr<Profile> profile(Profile::CreateProfile( 107 {
90 temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS)); 108 scoped_ptr<Profile> profile(Profile::CreateProfile(
91 ASSERT_TRUE(profile.get()); 109 temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS));
92 CheckChromeVersion(profile.get(), false); 110 ASSERT_TRUE(profile.get());
93 StartupTaskRunnerServiceFactory::GetForProfile(profile.get())-> 111 CheckChromeVersion(profile.get(), false);
94 StartDeferredTaskRunners(); 112 }
113
114 SpinThreads();
95 } 115 }
96 116
97 // Test OnProfileCreate is called with is_new_profile set to true when 117 // Test OnProfileCreate is called with is_new_profile set to true when
98 // creating a new profile asynchronously. 118 // creating a new profile asynchronously.
99 // This test is flaky on Linux, Win and Mac. See crbug.com/142787 119 // This test is flaky on Linux, Win and Mac. See crbug.com/142787
100 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, 120 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest,
101 DISABLED_CreateNewProfileAsynchronous) { 121 DISABLED_CreateNewProfileAsynchronous) {
102 base::ScopedTempDir temp_dir; 122 base::ScopedTempDir temp_dir;
103 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 123 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
104 124
105 MockProfileDelegate delegate; 125 MockProfileDelegate delegate;
106 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); 126 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true));
107 127
108 scoped_ptr<Profile> profile(Profile::CreateProfile( 128 {
109 temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS)); 129 content::WindowedNotificationObserver observer(
110 ASSERT_TRUE(profile.get()); 130 chrome::NOTIFICATION_PROFILE_CREATED,
111 StartupTaskRunnerServiceFactory::GetForProfile(profile.get())-> 131 content::NotificationService::AllSources());
112 StartDeferredTaskRunners();
113 132
114 // Wait for the profile to be created. 133 scoped_ptr<Profile> profile(Profile::CreateProfile(
115 content::WindowedNotificationObserver observer( 134 temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS));
116 chrome::NOTIFICATION_PROFILE_CREATED, 135 ASSERT_TRUE(profile.get());
117 content::Source<Profile>(profile.get())); 136
118 observer.Wait(); 137 // Wait for the profile to be created.
119 CheckChromeVersion(profile.get(), true); 138 observer.Wait();
139 CheckChromeVersion(profile.get(), true);
140 }
141
142 SpinThreads();
120 } 143 }
121 144
122 // Test OnProfileCreate is called with is_new_profile set to false when 145 // Test OnProfileCreate is called with is_new_profile set to false when
123 // creating a profile asynchronously with an existing prefs file. 146 // creating a profile asynchronously with an existing prefs file.
124 // Flaky: http://crbug.com/141517 147 // Flaky: http://crbug.com/141517
125 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, 148 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest,
126 DISABLED_CreateOldProfileAsynchronous) { 149 DISABLED_CreateOldProfileAsynchronous) {
127 base::ScopedTempDir temp_dir; 150 base::ScopedTempDir temp_dir;
128 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 151 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
129 CreatePrefsFileInDirectory(temp_dir.path()); 152 CreatePrefsFileInDirectory(temp_dir.path());
130 153
131 MockProfileDelegate delegate; 154 MockProfileDelegate delegate;
132 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, false)); 155 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, false));
133 scoped_ptr<Profile> profile(Profile::CreateProfile(
134 temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS));
135 ASSERT_TRUE(profile.get());
136 StartupTaskRunnerServiceFactory::GetForProfile(profile.get())->
137 StartDeferredTaskRunners();
138 156
139 // Wait for the profile to be created. 157 {
140 content::WindowedNotificationObserver observer( 158 content::WindowedNotificationObserver observer(
141 chrome::NOTIFICATION_PROFILE_CREATED, 159 chrome::NOTIFICATION_PROFILE_CREATED,
142 content::Source<Profile>(profile.get())); 160 content::NotificationService::AllSources());
143 observer.Wait(); 161
144 CheckChromeVersion(profile.get(), false); 162 scoped_ptr<Profile> profile(Profile::CreateProfile(
163 temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS));
164 ASSERT_TRUE(profile.get());
165
166 // Wait for the profile to be created.
167 observer.Wait();
168 CheckChromeVersion(profile.get(), false);
169 }
170
171 SpinThreads();
145 } 172 }
146 173
147 // Test that a README file is created for profiles that didn't have it. 174 // Test that a README file is created for profiles that didn't have it.
148 // Flaky: http://crbug.com/140882 175 // Flaky: http://crbug.com/140882
149 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, DISABLED_ProfileReadmeCreated) { 176 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, DISABLED_ProfileReadmeCreated) {
150 base::ScopedTempDir temp_dir; 177 base::ScopedTempDir temp_dir;
151 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 178 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
152 179
153 MockProfileDelegate delegate; 180 MockProfileDelegate delegate;
154 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); 181 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true));
155 182
156 // No delay before README creation. 183 // No delay before README creation.
157 ProfileImpl::create_readme_delay_ms = 0; 184 ProfileImpl::create_readme_delay_ms = 0;
158 185
159 scoped_ptr<Profile> profile(Profile::CreateProfile( 186 {
160 temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS)); 187 content::WindowedNotificationObserver observer(
161 ASSERT_TRUE(profile.get()); 188 chrome::NOTIFICATION_PROFILE_CREATED,
162 StartupTaskRunnerServiceFactory::GetForProfile(profile.get())-> 189 content::NotificationService::AllSources());
163 StartDeferredTaskRunners();
164 190
165 // Wait for the profile to be created. 191 scoped_ptr<Profile> profile(Profile::CreateProfile(
166 content::WindowedNotificationObserver observer( 192 temp_dir.path(), &delegate, Profile::CREATE_MODE_ASYNCHRONOUS));
167 chrome::NOTIFICATION_PROFILE_CREATED, 193 ASSERT_TRUE(profile.get());
168 content::Source<Profile>(profile.get()));
169 observer.Wait();
170 194
171 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); 195 // Wait for the profile to be created.
196 observer.Wait();
172 197
173 // Verify that README exists. 198 // Wait for file thread to create the README.
174 EXPECT_TRUE(base::PathExists( 199 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
175 temp_dir.path().Append(chrome::kReadmeFilename))); 200
201 // Verify that README exists.
202 EXPECT_TRUE(base::PathExists(
203 temp_dir.path().Append(chrome::kReadmeFilename)));
204 }
205
206 SpinThreads();
176 } 207 }
177 208
178 // Test that Profile can be deleted before README file is created. 209 // Test that Profile can be deleted before README file is created.
179 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, ProfileDeletedBeforeReadmeCreated) { 210 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, ProfileDeletedBeforeReadmeCreated) {
180 base::ScopedTempDir temp_dir; 211 base::ScopedTempDir temp_dir;
181 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 212 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
182 213
183 MockProfileDelegate delegate; 214 MockProfileDelegate delegate;
184 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); 215 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true));
185 216
186 // No delay before README creation. 217 // No delay before README creation.
187 ProfileImpl::create_readme_delay_ms = 0; 218 ProfileImpl::create_readme_delay_ms = 0;
188 219
220 base::WaitableEvent is_blocked(false, false);
221 base::WaitableEvent* unblock = new base::WaitableEvent(false, false);
222
223 // Block file thread.
224 content::BrowserThread::PostTask(
225 content::BrowserThread::FILE, FROM_HERE,
226 base::Bind(&BlockThread, &is_blocked, base::Owned(unblock)));
227 // Wait for file thread to actually be blocked.
228 is_blocked.Wait();
229
189 scoped_ptr<Profile> profile(Profile::CreateProfile( 230 scoped_ptr<Profile> profile(Profile::CreateProfile(
190 temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS)); 231 temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS));
191 ASSERT_TRUE(profile.get()); 232 ASSERT_TRUE(profile.get());
192 StartupTaskRunnerServiceFactory::GetForProfile(profile.get())->
193 StartDeferredTaskRunners();
194 233
195 // Delete the Profile instance and run pending tasks (this includes the task 234 // Delete the Profile instance before we give the file thread a chance to
196 // for README creation). 235 // create the README.
197 profile.reset(); 236 profile.reset();
198 content::RunAllPendingInMessageLoop(); 237
199 content::RunAllPendingInMessageLoop(content::BrowserThread::DB); 238 // Now unblock the file thread again and run pending tasks (this includes the
200 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); 239 // task for README creation).
240 unblock->Signal();
241
242 SpinThreads();
201 } 243 }
202 244
203 // Test that repeated setting of exit type is handled correctly. 245 // Test that repeated setting of exit type is handled correctly.
204 #if defined(OS_WIN) 246 #if defined(OS_WIN)
205 // Flaky on Windows: http://crbug.com/163713 247 // Flaky on Windows: http://crbug.com/163713
206 #define MAYBE_ExitType DISABLED_ExitType 248 #define MAYBE_ExitType DISABLED_ExitType
207 #else 249 #else
208 #define MAYBE_ExitType ExitType 250 #define MAYBE_ExitType ExitType
209 #endif 251 #endif
210 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, MAYBE_ExitType) { 252 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, MAYBE_ExitType) {
211 base::ScopedTempDir temp_dir; 253 base::ScopedTempDir temp_dir;
212 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 254 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
213 255
214 MockProfileDelegate delegate; 256 MockProfileDelegate delegate;
215 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true)); 257 EXPECT_CALL(delegate, OnProfileCreated(testing::NotNull(), true, true));
258 {
259 scoped_ptr<Profile> profile(Profile::CreateProfile(
260 temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS));
261 ASSERT_TRUE(profile.get());
216 262
217 scoped_ptr<Profile> profile(Profile::CreateProfile( 263 PrefService* prefs = profile->GetPrefs();
218 temp_dir.path(), &delegate, Profile::CREATE_MODE_SYNCHRONOUS)); 264 // The initial state is crashed; store for later reference.
219 ASSERT_TRUE(profile.get()); 265 std::string crash_value(prefs->GetString(prefs::kSessionExitType));
220 StartupTaskRunnerServiceFactory::GetForProfile(profile.get())->
221 StartDeferredTaskRunners();
222 266
223 PrefService* prefs = profile->GetPrefs(); 267 // The first call to a type other than crashed should change the value.
224 // The initial state is crashed; store for later reference. 268 profile->SetExitType(Profile::EXIT_SESSION_ENDED);
225 std::string crash_value(prefs->GetString(prefs::kSessionExitType)); 269 std::string first_call_value(prefs->GetString(prefs::kSessionExitType));
270 EXPECT_NE(crash_value, first_call_value);
226 271
227 // The first call to a type other than crashed should change the value. 272 // Subsequent calls to a non-crash value should be ignored.
228 profile->SetExitType(Profile::EXIT_SESSION_ENDED); 273 profile->SetExitType(Profile::EXIT_NORMAL);
229 std::string first_call_value(prefs->GetString(prefs::kSessionExitType)); 274 std::string second_call_value(prefs->GetString(prefs::kSessionExitType));
230 EXPECT_NE(crash_value, first_call_value); 275 EXPECT_EQ(first_call_value, second_call_value);
231 276
232 // Subsequent calls to a non-crash value should be ignored. 277 // Setting back to a crashed value should work.
233 profile->SetExitType(Profile::EXIT_NORMAL); 278 profile->SetExitType(Profile::EXIT_CRASHED);
234 std::string second_call_value(prefs->GetString(prefs::kSessionExitType)); 279 std::string final_value(prefs->GetString(prefs::kSessionExitType));
235 EXPECT_EQ(first_call_value, second_call_value); 280 EXPECT_EQ(crash_value, final_value);
281 }
236 282
237 // Setting back to a crashed value should work. 283 SpinThreads();
238 profile->SetExitType(Profile::EXIT_CRASHED);
239 std::string final_value(prefs->GetString(prefs::kSessionExitType));
240 EXPECT_EQ(crash_value, final_value);
241
242 // This test runs fast enough that the WebDataService may still be
243 // initializing (which uses the temp directory) when the test
244 // ends. Give it a chance to complete.
245 profile.reset();
246 content::RunAllPendingInMessageLoop();
247 content::RunAllPendingInMessageLoop(content::BrowserThread::DB);
248 } 284 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698