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

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

Issue 6793008: Replacing base::DIR_TEMP with ScopedTempDir when appropriate. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Paweł's comments on Round 2. Created 9 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <string> 5 #include <string>
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/file_util.h" 8 #include "base/memory/scoped_temp_dir.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
11 #include "chrome/browser/prefs/browser_prefs.h" 11 #include "chrome/browser/prefs/browser_prefs.h"
12 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/profiles/profile_manager.h" 13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/common/chrome_constants.h" 14 #include "chrome/common/chrome_constants.h"
15 #include "chrome/common/chrome_paths.h" 15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/chrome_switches.h" 16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/test/testing_browser_process.h" 17 #include "chrome/test/testing_browser_process.h"
18 #include "chrome/test/testing_pref_service.h" 18 #include "chrome/test/testing_pref_service.h"
19 #include "content/browser/browser_thread.h" 19 #include "content/browser/browser_thread.h"
20 #include "content/common/notification_service.h" 20 #include "content/common/notification_service.h"
21 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/base/system_monitor/system_monitor.h" 22 #include "ui/base/system_monitor/system_monitor.h"
23 23
24 class ProfileManagerTest : public testing::Test { 24 class ProfileManagerTest : public testing::Test {
25 protected: 25 protected:
26 ProfileManagerTest() 26 ProfileManagerTest()
27 : ui_thread_(BrowserThread::UI, &message_loop_), 27 : ui_thread_(BrowserThread::UI, &message_loop_),
28 file_thread_(BrowserThread::FILE, &message_loop_) { 28 file_thread_(BrowserThread::FILE, &message_loop_) {
29 } 29 }
30 30
31 virtual void SetUp() { 31 virtual void SetUp() {
32 // Name a subdirectory of the temp directory. 32 // Create a new temporary directory, and store the path
33 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); 33 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
34 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("ProfileManagerTest"));
35
36 // Create a fresh, empty copy of this directory.
37 file_util::Delete(test_dir_, true);
38 file_util::CreateDirectory(test_dir_);
39 34
40 // Create a local_state PrefService. 35 // Create a local_state PrefService.
41 browser::RegisterLocalState(&test_local_state_); 36 browser::RegisterLocalState(&test_local_state_);
42 TestingBrowserProcess* testing_browser_process = 37 TestingBrowserProcess* testing_browser_process =
43 static_cast<TestingBrowserProcess*>(g_browser_process); 38 static_cast<TestingBrowserProcess*>(g_browser_process);
44 testing_browser_process->SetPrefService(&test_local_state_); 39 testing_browser_process->SetPrefService(&test_local_state_);
45 } 40 }
46 41
47 virtual void TearDown() { 42 virtual void TearDown() {
48 // Clean up test directory
49 ASSERT_TRUE(file_util::Delete(test_dir_, true));
50 ASSERT_FALSE(file_util::PathExists(test_dir_));
51
52 TestingBrowserProcess* testing_browser_process = 43 TestingBrowserProcess* testing_browser_process =
53 static_cast<TestingBrowserProcess*>(g_browser_process); 44 static_cast<TestingBrowserProcess*>(g_browser_process);
54 testing_browser_process->SetPrefService(NULL); 45 testing_browser_process->SetPrefService(NULL);
55 } 46 }
56 47
48 // The path to temporary directory used to contain the test operations.
49 ScopedTempDir temp_dir_;
50
57 MessageLoopForUI message_loop_; 51 MessageLoopForUI message_loop_;
58 BrowserThread ui_thread_; 52 BrowserThread ui_thread_;
59 BrowserThread file_thread_; 53 BrowserThread file_thread_;
60 54
61 // the path to temporary directory used to contain the test operations
62 FilePath test_dir_;
63
64 TestingPrefService test_local_state_; 55 TestingPrefService test_local_state_;
65 }; 56 };
66 57
67 TEST_F(ProfileManagerTest, CreateProfile) { 58 TEST_F(ProfileManagerTest, CreateProfile) {
68 FilePath source_path; 59 FilePath source_path;
69 PathService::Get(chrome::DIR_TEST_DATA, &source_path); 60 PathService::Get(chrome::DIR_TEST_DATA, &source_path);
70 source_path = source_path.Append(FILE_PATH_LITERAL("profiles")); 61 source_path = source_path.Append(FILE_PATH_LITERAL("profiles"));
71 source_path = source_path.Append(FILE_PATH_LITERAL("sample")); 62 source_path = source_path.Append(FILE_PATH_LITERAL("sample"));
72 63
73 FilePath dest_path = test_dir_; 64 FilePath dest_path = temp_dir_.path();
74 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile")); 65 dest_path = dest_path.Append(FILE_PATH_LITERAL("New Profile"));
75 66
76 scoped_ptr<Profile> profile; 67 scoped_ptr<Profile> profile;
77 68
78 // Successfully create a profile. 69 // Successfully create a profile.
79 profile.reset(ProfileManager::CreateProfile(dest_path)); 70 profile.reset(ProfileManager::CreateProfile(dest_path));
80 ASSERT_TRUE(profile.get()); 71 ASSERT_TRUE(profile.get());
81 72
82 profile.reset(); 73 profile.reset();
83 74
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 FilePath().AppendASCII(chrome::kNotSignedInProfile); 111 FilePath().AppendASCII(chrome::kNotSignedInProfile);
121 EXPECT_EQ(expected_default.value(), 112 EXPECT_EQ(expected_default.value(),
122 profile_manager.GetCurrentProfileDir().value()); 113 profile_manager.GetCurrentProfileDir().value());
123 114
124 profile_manager.Observe(NotificationType::LOGIN_USER_CHANGED, 115 profile_manager.Observe(NotificationType::LOGIN_USER_CHANGED,
125 NotificationService::AllSources(), 116 NotificationService::AllSources(),
126 NotificationService::NoDetails()); 117 NotificationService::NoDetails());
127 FilePath expected_logged_in(profile_dir); 118 FilePath expected_logged_in(profile_dir);
128 EXPECT_EQ(expected_logged_in.value(), 119 EXPECT_EQ(expected_logged_in.value(),
129 profile_manager.GetCurrentProfileDir().value()); 120 profile_manager.GetCurrentProfileDir().value());
130 VLOG(1) << test_dir_.Append(profile_manager.GetCurrentProfileDir()).value(); 121 VLOG(1) << temp_dir_.path().Append(
122 profile_manager.GetCurrentProfileDir()).value();
131 } 123 }
132 124
133 #endif 125 #endif
134 126
135 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) { 127 TEST_F(ProfileManagerTest, CreateAndUseTwoProfiles) {
136 FilePath source_path; 128 FilePath source_path;
137 PathService::Get(chrome::DIR_TEST_DATA, &source_path); 129 PathService::Get(chrome::DIR_TEST_DATA, &source_path);
138 source_path = source_path.Append(FILE_PATH_LITERAL("profiles")); 130 source_path = source_path.Append(FILE_PATH_LITERAL("profiles"));
139 source_path = source_path.Append(FILE_PATH_LITERAL("sample")); 131 source_path = source_path.Append(FILE_PATH_LITERAL("sample"));
140 132
141 FilePath dest_path1 = test_dir_; 133 FilePath dest_path1 = temp_dir_.path();
142 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1")); 134 dest_path1 = dest_path1.Append(FILE_PATH_LITERAL("New Profile 1"));
143 135
144 FilePath dest_path2 = test_dir_; 136 FilePath dest_path2 = temp_dir_.path();
145 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2")); 137 dest_path2 = dest_path2.Append(FILE_PATH_LITERAL("New Profile 2"));
146 138
147 scoped_ptr<Profile> profile1; 139 scoped_ptr<Profile> profile1;
148 scoped_ptr<Profile> profile2; 140 scoped_ptr<Profile> profile2;
149 141
150 // Successfully create the profiles. 142 // Successfully create the profiles.
151 profile1.reset(ProfileManager::CreateProfile(dest_path1)); 143 profile1.reset(ProfileManager::CreateProfile(dest_path1));
152 ASSERT_TRUE(profile1.get()); 144 ASSERT_TRUE(profile1.get());
153 145
154 profile2.reset(ProfileManager::CreateProfile(dest_path2)); 146 profile2.reset(ProfileManager::CreateProfile(dest_path2));
155 ASSERT_TRUE(profile2.get()); 147 ASSERT_TRUE(profile2.get());
156 148
157 // Force lazy-init of some profile services to simulate use. 149 // Force lazy-init of some profile services to simulate use.
158 EXPECT_TRUE(profile1->GetHistoryService(Profile::EXPLICIT_ACCESS)); 150 EXPECT_TRUE(profile1->GetHistoryService(Profile::EXPLICIT_ACCESS));
159 EXPECT_TRUE(profile1->GetBookmarkModel()); 151 EXPECT_TRUE(profile1->GetBookmarkModel());
160 EXPECT_TRUE(profile2->GetBookmarkModel()); 152 EXPECT_TRUE(profile2->GetBookmarkModel());
161 EXPECT_TRUE(profile2->GetHistoryService(Profile::EXPLICIT_ACCESS)); 153 EXPECT_TRUE(profile2->GetHistoryService(Profile::EXPLICIT_ACCESS));
162 154
163 // Make sure any pending tasks run before we destroy the profiles. 155 // Make sure any pending tasks run before we destroy the profiles.
164 message_loop_.RunAllPending(); 156 message_loop_.RunAllPending();
165 157
166 profile1.reset(); 158 profile1.reset();
167 profile2.reset(); 159 profile2.reset();
168 160
169 // Make sure history cleans up correctly. 161 // Make sure history cleans up correctly.
170 message_loop_.RunAllPending(); 162 message_loop_.RunAllPending();
171 } 163 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698