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

Side by Side Diff: chrome/browser/prefs/pref_service_browsertest.cc

Issue 944433002: Revamp the MigrateBrowserPrefs and MigrateUserPrefs code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: change asserts to expect in setupuserdatadirectory Created 5 years, 9 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
« no previous file with comments | « chrome/browser/prefs/browser_prefs.cc ('k') | chrome/browser/profiles/profile_impl.cc » ('j') | 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 <string> 5 #include <string>
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/json/json_file_value_serializer.h" 10 #include "base/json/json_file_value_serializer.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 return; 45 return;
46 #endif 46 #endif
47 47
48 gfx::Rect bounds = browser()->window()->GetBounds(); 48 gfx::Rect bounds = browser()->window()->GetBounds();
49 gfx::Rect expected_bounds(gfx::Rect(20, 30, 400, 500)); 49 gfx::Rect expected_bounds(gfx::Rect(20, 30, 400, 500));
50 ASSERT_EQ(expected_bounds.ToString(), bounds.ToString()); 50 ASSERT_EQ(expected_bounds.ToString(), bounds.ToString());
51 } 51 }
52 52
53 class PreferenceServiceTest : public InProcessBrowserTest { 53 class PreferenceServiceTest : public InProcessBrowserTest {
54 public: 54 public:
55 explicit PreferenceServiceTest(bool new_profile) : new_profile_(new_profile) {
56 }
57
58 bool SetUpUserDataDirectory() override { 55 bool SetUpUserDataDirectory() override {
59 base::FilePath user_data_directory; 56 base::FilePath user_data_directory;
60 PathService::Get(chrome::DIR_USER_DATA, &user_data_directory); 57 PathService::Get(chrome::DIR_USER_DATA, &user_data_directory);
61 58
62 if (new_profile_) { 59 original_pref_file_ = ui_test_utils::GetTestFilePath(
63 original_pref_file_ = ui_test_utils::GetTestFilePath( 60 base::FilePath()
64 base::FilePath().AppendASCII("profiles"). 61 .AppendASCII("profiles")
65 AppendASCII("window_placement"). 62 .AppendASCII("window_placement")
66 AppendASCII("Default"), 63 .AppendASCII("Default"),
67 base::FilePath().Append(chrome::kPreferencesFilename)); 64 base::FilePath().Append(chrome::kPreferencesFilename));
68 tmp_pref_file_ = 65 tmp_pref_file_ =
69 user_data_directory.AppendASCII(TestingProfile::kTestUserProfileDir); 66 user_data_directory.AppendASCII(TestingProfile::kTestUserProfileDir);
70 CHECK(base::CreateDirectory(tmp_pref_file_)); 67 EXPECT_TRUE(base::CreateDirectory(tmp_pref_file_));
71 tmp_pref_file_ = tmp_pref_file_.Append(chrome::kPreferencesFilename); 68 tmp_pref_file_ = tmp_pref_file_.Append(chrome::kPreferencesFilename);
72 } else {
73 original_pref_file_ = ui_test_utils::GetTestFilePath(
74 base::FilePath().AppendASCII("profiles").
75 AppendASCII("window_placement"),
76 base::FilePath().Append(chrome::kLocalStateFilename));
77 tmp_pref_file_ = user_data_directory.Append(chrome::kLocalStateFilename);
78 }
79 69
80 CHECK(base::PathExists(original_pref_file_)); 70 EXPECT_TRUE(base::PathExists(original_pref_file_));
81 // Copy only the Preferences file if |new_profile_|, or Local State if not, 71 EXPECT_TRUE(base::CopyFile(original_pref_file_, tmp_pref_file_));
82 // and the rest will be automatically created.
83 CHECK(base::CopyFile(original_pref_file_, tmp_pref_file_));
84 72
85 #if defined(OS_WIN) 73 #if defined(OS_WIN)
86 // Make the copy writable. On POSIX we assume the umask allows files 74 // Make the copy writable. On POSIX we assume the umask allows files
87 // we create to be writable. 75 // we create to be writable.
88 CHECK(::SetFileAttributesW(tmp_pref_file_.value().c_str(), 76 EXPECT_TRUE(::SetFileAttributesW(tmp_pref_file_.value().c_str(),
89 FILE_ATTRIBUTE_NORMAL)); 77 FILE_ATTRIBUTE_NORMAL));
90 #endif 78 #endif
91 return true; 79 return true;
92 } 80 }
93 81
94 protected: 82 protected:
95 base::FilePath original_pref_file_; 83 base::FilePath original_pref_file_;
96 base::FilePath tmp_pref_file_; 84 base::FilePath tmp_pref_file_;
97
98 private:
99 bool new_profile_;
100 }; 85 };
101 86
102 #if defined(OS_WIN) || defined(OS_MACOSX) 87 #if defined(OS_WIN) || defined(OS_MACOSX)
103 // This test verifies that the window position from the prefs file is restored 88 // This test verifies that the window position from the prefs file is restored
104 // when the app restores. This doesn't really make sense on Linux, where 89 // when the app restores. This doesn't really make sense on Linux, where
105 // the window manager might fight with you over positioning. However, we 90 // the window manager might fight with you over positioning. However, we
106 // might be able to make this work on buildbots. 91 // might be able to make this work on buildbots.
107 // TODO(port): revisit this. 92 // TODO(port): revisit this.
108 93
109 class PreservedWindowPlacementIsLoaded : public PreferenceServiceTest { 94 IN_PROC_BROWSER_TEST_F(PreferenceServiceTest, Test) {
110 public:
111 PreservedWindowPlacementIsLoaded() : PreferenceServiceTest(true) {
112 }
113 };
114
115 IN_PROC_BROWSER_TEST_F(PreservedWindowPlacementIsLoaded, Test) {
116 #if defined(OS_WIN) && defined(USE_ASH) 95 #if defined(OS_WIN) && defined(USE_ASH)
117 // Disable this test in Metro+Ash for now (http://crbug.com/262796). 96 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
118 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 97 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
119 switches::kAshBrowserTests)) 98 switches::kAshBrowserTests))
120 return; 99 return;
121 #endif 100 #endif
122 101
123 // The window should open with the new reference profile, with window 102 // The window should open with the new reference profile, with window
124 // placement values stored in the user data directory. 103 // placement values stored in the user data directory.
125 JSONFileValueSerializer deserializer(original_pref_file_); 104 JSONFileValueSerializer deserializer(original_pref_file_);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 EXPECT_EQ(right, bounds.x() + bounds.width()); 136 EXPECT_EQ(right, bounds.x() + bounds.width());
158 137
159 // Find if launched window is maximized. 138 // Find if launched window is maximized.
160 bool is_window_maximized = browser()->window()->IsMaximized(); 139 bool is_window_maximized = browser()->window()->IsMaximized();
161 bool is_maximized = false; 140 bool is_maximized = false;
162 EXPECT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + ".maximized", 141 EXPECT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + ".maximized",
163 &is_maximized)); 142 &is_maximized));
164 EXPECT_EQ(is_maximized, is_window_maximized); 143 EXPECT_EQ(is_maximized, is_window_maximized);
165 } 144 }
166 #endif 145 #endif
167
168 #if defined(OS_WIN) || defined(OS_MACOSX)
169
170 class PreservedWindowPlacementIsMigrated : public PreferenceServiceTest {
171 public:
172 PreservedWindowPlacementIsMigrated() : PreferenceServiceTest(false) {
173 }
174 };
175
176 IN_PROC_BROWSER_TEST_F(PreservedWindowPlacementIsMigrated, Test) {
177 #if defined(OS_WIN) && defined(USE_ASH)
178 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
179 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
180 switches::kAshBrowserTests))
181 return;
182 #endif
183
184 // The window should open with the old reference profile, with window
185 // placement values stored in Local State.
186
187 JSONFileValueSerializer deserializer(original_pref_file_);
188 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, NULL));
189
190 ASSERT_TRUE(root.get());
191 ASSERT_TRUE(root->IsType(base::Value::TYPE_DICTIONARY));
192
193 // Retrieve the screen rect for the launched window
194 gfx::Rect bounds = browser()->window()->GetRestoredBounds();
195
196 // Values from old reference profile in Local State should have been
197 // correctly migrated to the user's Preferences -- if so, the window
198 // should be set to values taken from the user's Local State.
199 base::DictionaryValue* root_dict =
200 static_cast<base::DictionaryValue*>(root.get());
201
202 // Retrieve the expected rect values from User Preferences, where they
203 // should have been migrated from Local State.
204 int bottom = 0;
205 std::string kBrowserWindowPlacement(prefs::kBrowserWindowPlacement);
206 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".bottom",
207 &bottom));
208 EXPECT_EQ(bottom, bounds.y() + bounds.height());
209
210 int top = 0;
211 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".top",
212 &top));
213 EXPECT_EQ(top, bounds.y());
214
215 int left = 0;
216 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".left",
217 &left));
218 EXPECT_EQ(left, bounds.x());
219
220 int right = 0;
221 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".right",
222 &right));
223 EXPECT_EQ(right, bounds.x() + bounds.width());
224
225 // Find if launched window is maximized.
226 bool is_window_maximized = browser()->window()->IsMaximized();
227 bool is_maximized = false;
228 EXPECT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + ".maximized",
229 &is_maximized));
230 EXPECT_EQ(is_maximized, is_window_maximized);
231 }
232 #endif
OLDNEW
« no previous file with comments | « chrome/browser/prefs/browser_prefs.cc ('k') | chrome/browser/profiles/profile_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698