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
3 // found in the LICENSE file.
5 #include "chrome/browser/profiles/profile.h"
7 #include "base/command_line.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/json/json_reader.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/sequenced_task_runner.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "base/values.h"
15 #include "base/version.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/chrome_notification_types.h"
18 #include "chrome/browser/profiles/chrome_version_service.h"
19 #include "chrome/browser/profiles/profile_impl.h"
20 #include "chrome/browser/profiles/profile_manager.h"
21 #include "chrome/browser/profiles/startup_task_runner_service.h"
22 #include "chrome/browser/profiles/startup_task_runner_service_factory.h"
23 #include "chrome/common/chrome_constants.h"
24 #include "chrome/common/chrome_version_info.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/test/base/in_process_browser_test.h"
27 #include "content/public/test/test_utils.h"
28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h"
31 #if defined(OS_CHROMEOS)
32 #include "chromeos/chromeos_switches.h"
37 class MockProfileDelegate
: public Profile::Delegate
{
39 MOCK_METHOD1(OnPrefsLoaded
, void(Profile
*));
40 MOCK_METHOD3(OnProfileCreated
, void(Profile
*, bool, bool));
43 // Creates a prefs file in the given directory.
44 void CreatePrefsFileInDirectory(const base::FilePath
& directory_path
) {
45 base::FilePath
pref_path(directory_path
.Append(chrome::kPreferencesFilename
));
46 std::string
data("{}");
47 ASSERT_TRUE(base::WriteFile(pref_path
, data
.c_str(), data
.size()));
50 void CheckChromeVersion(Profile
*profile
, bool is_new
) {
51 std::string created_by_version
;
53 chrome::VersionInfo version_info
;
54 created_by_version
= version_info
.Version();
56 created_by_version
= "1.0.0.0";
58 std::string pref_version
=
59 ChromeVersionService::GetVersion(profile
->GetPrefs());
60 // Assert that created_by_version pref gets set to current version.
61 EXPECT_EQ(created_by_version
, pref_version
);
65 base::WaitableEvent
* is_blocked
,
66 base::WaitableEvent
* unblock
) {
71 void FlushTaskRunner(base::SequencedTaskRunner
* runner
) {
73 base::WaitableEvent
unblock(false, false);
75 runner
->PostTask(FROM_HERE
,
76 base::Bind(&base::WaitableEvent::Signal
, base::Unretained(&unblock
)));
82 // Give threads a chance to do their stuff before shutting down (i.e.
83 // deleting scoped temp dir etc).
84 // Should not be necessary anymore once Profile deletion is fixed
85 // (see crbug.com/88586).
86 content::RunAllPendingInMessageLoop();
87 content::RunAllPendingInMessageLoop(content::BrowserThread::DB
);
88 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
93 class ProfileBrowserTest
: public InProcessBrowserTest
{
95 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
96 #if defined(OS_CHROMEOS)
97 command_line
->AppendSwitch(
98 chromeos::switches::kIgnoreUserProfileMappingForTests
);
102 scoped_ptr
<Profile
> CreateProfile(
103 const base::FilePath
& path
,
104 Profile::Delegate
* delegate
,
105 Profile::CreateMode create_mode
) {
106 scoped_ptr
<Profile
> profile(Profile::CreateProfile(
107 path
, delegate
, create_mode
));
108 EXPECT_TRUE(profile
.get());
110 // Store the Profile's IO task runner so we can wind it down.
111 profile_io_task_runner_
= profile
->GetIOTaskRunner();
113 return profile
.Pass();
116 void FlushIoTaskRunnerAndSpinThreads() {
117 FlushTaskRunner(profile_io_task_runner_
.get());
121 scoped_refptr
<base::SequencedTaskRunner
> profile_io_task_runner_
;
124 // Test OnProfileCreate is called with is_new_profile set to true when
125 // creating a new profile synchronously.
126 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
, CreateNewProfileSynchronous
) {
127 base::ScopedTempDir temp_dir
;
128 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
130 MockProfileDelegate delegate
;
131 EXPECT_CALL(delegate
, OnProfileCreated(testing::NotNull(), true, true));
134 scoped_ptr
<Profile
> profile(CreateProfile(
135 temp_dir
.path(), &delegate
, Profile::CREATE_MODE_SYNCHRONOUS
));
136 CheckChromeVersion(profile
.get(), true);
139 FlushIoTaskRunnerAndSpinThreads();
142 // Test OnProfileCreate is called with is_new_profile set to false when
143 // creating a profile synchronously with an existing prefs file.
144 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
, CreateOldProfileSynchronous
) {
145 base::ScopedTempDir temp_dir
;
146 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
147 CreatePrefsFileInDirectory(temp_dir
.path());
149 MockProfileDelegate delegate
;
150 EXPECT_CALL(delegate
, OnProfileCreated(testing::NotNull(), true, false));
153 scoped_ptr
<Profile
> profile(CreateProfile(
154 temp_dir
.path(), &delegate
, Profile::CREATE_MODE_SYNCHRONOUS
));
155 CheckChromeVersion(profile
.get(), false);
158 FlushIoTaskRunnerAndSpinThreads();
161 // Flaky: http://crbug.com/393177
162 // Test OnProfileCreate is called with is_new_profile set to true when
163 // creating a new profile asynchronously.
164 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
,
165 DISABLED_CreateNewProfileAsynchronous
) {
166 base::ScopedTempDir temp_dir
;
167 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
169 MockProfileDelegate delegate
;
170 EXPECT_CALL(delegate
, OnProfileCreated(testing::NotNull(), true, true));
173 content::WindowedNotificationObserver
observer(
174 chrome::NOTIFICATION_PROFILE_CREATED
,
175 content::NotificationService::AllSources());
177 scoped_ptr
<Profile
> profile(CreateProfile(
178 temp_dir
.path(), &delegate
, Profile::CREATE_MODE_ASYNCHRONOUS
));
180 // Wait for the profile to be created.
182 CheckChromeVersion(profile
.get(), true);
185 FlushIoTaskRunnerAndSpinThreads();
189 // Flaky: http://crbug.com/393177
190 // Test OnProfileCreate is called with is_new_profile set to false when
191 // creating a profile asynchronously with an existing prefs file.
192 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
,
193 DISABLED_CreateOldProfileAsynchronous
) {
194 base::ScopedTempDir temp_dir
;
195 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
196 CreatePrefsFileInDirectory(temp_dir
.path());
198 MockProfileDelegate delegate
;
199 EXPECT_CALL(delegate
, OnProfileCreated(testing::NotNull(), true, false));
202 content::WindowedNotificationObserver
observer(
203 chrome::NOTIFICATION_PROFILE_CREATED
,
204 content::NotificationService::AllSources());
206 scoped_ptr
<Profile
> profile(CreateProfile(
207 temp_dir
.path(), &delegate
, Profile::CREATE_MODE_ASYNCHRONOUS
));
209 // Wait for the profile to be created.
211 CheckChromeVersion(profile
.get(), false);
214 FlushIoTaskRunnerAndSpinThreads();
217 // Flaky: http://crbug.com/393177
218 // Test that a README file is created for profiles that didn't have it.
219 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
, DISABLED_ProfileReadmeCreated
) {
220 base::ScopedTempDir temp_dir
;
221 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
223 MockProfileDelegate delegate
;
224 EXPECT_CALL(delegate
, OnProfileCreated(testing::NotNull(), true, true));
226 // No delay before README creation.
227 ProfileImpl::create_readme_delay_ms
= 0;
230 content::WindowedNotificationObserver
observer(
231 chrome::NOTIFICATION_PROFILE_CREATED
,
232 content::NotificationService::AllSources());
234 scoped_ptr
<Profile
> profile(CreateProfile(
235 temp_dir
.path(), &delegate
, Profile::CREATE_MODE_ASYNCHRONOUS
));
237 // Wait for the profile to be created.
240 // Wait for file thread to create the README.
241 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
243 // Verify that README exists.
244 EXPECT_TRUE(base::PathExists(
245 temp_dir
.path().Append(chrome::kReadmeFilename
)));
248 FlushIoTaskRunnerAndSpinThreads();
251 // Test that Profile can be deleted before README file is created.
252 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
, ProfileDeletedBeforeReadmeCreated
) {
253 base::ScopedTempDir temp_dir
;
254 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
256 MockProfileDelegate delegate
;
257 EXPECT_CALL(delegate
, OnProfileCreated(testing::NotNull(), true, true));
259 // No delay before README creation.
260 ProfileImpl::create_readme_delay_ms
= 0;
262 base::WaitableEvent
is_blocked(false, false);
263 base::WaitableEvent
* unblock
= new base::WaitableEvent(false, false);
265 // Block file thread.
266 content::BrowserThread::PostTask(
267 content::BrowserThread::FILE, FROM_HERE
,
268 base::Bind(&BlockThread
, &is_blocked
, base::Owned(unblock
)));
269 // Wait for file thread to actually be blocked.
272 scoped_ptr
<Profile
> profile(CreateProfile(
273 temp_dir
.path(), &delegate
, Profile::CREATE_MODE_SYNCHRONOUS
));
275 // Delete the Profile instance before we give the file thread a chance to
276 // create the README.
279 // Now unblock the file thread again and run pending tasks (this includes the
280 // task for README creation).
283 FlushIoTaskRunnerAndSpinThreads();
286 // Test that repeated setting of exit type is handled correctly.
287 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
, ExitType
) {
288 base::ScopedTempDir temp_dir
;
289 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
291 MockProfileDelegate delegate
;
292 EXPECT_CALL(delegate
, OnProfileCreated(testing::NotNull(), true, true));
294 scoped_ptr
<Profile
> profile(CreateProfile(
295 temp_dir
.path(), &delegate
, Profile::CREATE_MODE_SYNCHRONOUS
));
297 PrefService
* prefs
= profile
->GetPrefs();
298 // The initial state is crashed; store for later reference.
299 std::string
crash_value(prefs
->GetString(prefs::kSessionExitType
));
301 // The first call to a type other than crashed should change the value.
302 profile
->SetExitType(Profile::EXIT_SESSION_ENDED
);
303 std::string
first_call_value(prefs
->GetString(prefs::kSessionExitType
));
304 EXPECT_NE(crash_value
, first_call_value
);
306 // Subsequent calls to a non-crash value should be ignored.
307 profile
->SetExitType(Profile::EXIT_NORMAL
);
308 std::string
second_call_value(prefs
->GetString(prefs::kSessionExitType
));
309 EXPECT_EQ(first_call_value
, second_call_value
);
311 // Setting back to a crashed value should work.
312 profile
->SetExitType(Profile::EXIT_CRASHED
);
313 std::string
final_value(prefs
->GetString(prefs::kSessionExitType
));
314 EXPECT_EQ(crash_value
, final_value
);
317 FlushIoTaskRunnerAndSpinThreads();
320 // The EndSession IO synchronization is only critical on Windows, but also
321 // happens under the USE_X11 define. See BrowserProcessImpl::EndSession.
322 #if defined(USE_X11) || defined(OS_WIN)
326 std::string
GetExitTypePreferenceFromDisk(Profile
* profile
) {
327 base::FilePath prefs_path
=
328 profile
->GetPath().Append(chrome::kPreferencesFilename
);
330 if (!base::ReadFileToString(prefs_path
, &prefs
))
331 return std::string();
333 scoped_ptr
<base::Value
> value(base::JSONReader::Read(prefs
));
335 return std::string();
337 base::DictionaryValue
* dict
= NULL
;
338 if (!value
->GetAsDictionary(&dict
) || !dict
)
339 return std::string();
341 std::string exit_type
;
342 if (!dict
->GetString("profile.exit_type", &exit_type
))
343 return std::string();
350 IN_PROC_BROWSER_TEST_F(ProfileBrowserTest
,
351 WritesProfilesSynchronouslyOnEndSession
) {
352 base::ScopedTempDir temp_dir
;
353 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
355 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
356 ASSERT_TRUE(profile_manager
);
357 std::vector
<Profile
*> loaded_profiles
= profile_manager
->GetLoadedProfiles();
359 ASSERT_NE(loaded_profiles
.size(), 0UL);
360 Profile
* profile
= loaded_profiles
[0];
362 // This retry loop reduces flakiness due to the fact that this ultimately
363 // tests whether or not a code path hits a timed wait.
364 bool succeeded
= false;
365 for (size_t retries
= 0; !succeeded
&& retries
< 3; ++retries
) {
366 // Flush the profile data to disk for all loaded profiles.
367 profile
->SetExitType(Profile::EXIT_CRASHED
);
368 FlushTaskRunner(profile
->GetIOTaskRunner().get());
370 // Make sure that the prefs file was written with the expected key/value.
371 ASSERT_EQ(GetExitTypePreferenceFromDisk(profile
), "Crashed");
373 // The blocking wait in EndSession has a timeout.
374 base::Time start
= base::Time::Now();
376 // This must not return until the profile data has been written to disk.
377 // If this test flakes, then logoff on Windows has broken again.
378 g_browser_process
->EndSession();
380 base::Time end
= base::Time::Now();
382 // The EndSession timeout is 10 seconds. If we take more than half that,
383 // go around again, as we may have timed out on the wait.
384 // This helps against flakes, and also ensures that if the IO thread starts
385 // blocking systemically for that length of time (e.g. deadlocking or such),
386 // we'll get a consistent test failure.
387 if (end
- start
> base::TimeDelta::FromSeconds(5))
390 // Make sure that the prefs file was written with the expected key/value.
391 ASSERT_EQ(GetExitTypePreferenceFromDisk(profile
), "SessionEnded");
397 ASSERT_TRUE(succeeded
) << "profile->EndSession() timed out too often.";
400 #endif // defined(USE_X11) || defined(OS_WIN)