| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/file_util.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/test/test_file_util.h" | 10 #include "base/test/test_file_util.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 22 class PreferenceServiceTest : public UITest { | 22 class PreferenceServiceTest : public UITest { |
| 23 public: | 23 public: |
| 24 void SetUp() { | 24 void SetUp() { |
| 25 PathService::Get(base::DIR_TEMP, &tmp_profile_); | 25 PathService::Get(base::DIR_TEMP, &tmp_profile_); |
| 26 tmp_profile_ = tmp_profile_.AppendASCII("tmp_profile"); | 26 tmp_profile_ = tmp_profile_.AppendASCII("tmp_profile"); |
| 27 | 27 |
| 28 // Create a fresh, empty copy of this directory. | 28 // Create a fresh, empty copy of this directory. |
| 29 file_util::Delete(tmp_profile_, true); | 29 file_util::Delete(tmp_profile_, true); |
| 30 file_util::CreateDirectory(tmp_profile_); | 30 file_util::CreateDirectory(tmp_profile_); |
| 31 | 31 |
| 32 FilePath reference_pref_file; | 32 FilePath reference_pref_file = |
| 33 if (new_profile_) { | 33 test_data_directory_ |
| 34 reference_pref_file = test_data_directory_ | |
| 35 .AppendASCII("profiles") | |
| 36 .AppendASCII("window_placement") | |
| 37 .AppendASCII("Default") | |
| 38 .Append(chrome::kPreferencesFilename); | |
| 39 tmp_pref_file_ = tmp_profile_.AppendASCII("Default"); | |
| 40 ASSERT_TRUE(file_util::CreateDirectory(tmp_pref_file_)); | |
| 41 tmp_pref_file_ = tmp_pref_file_.Append(chrome::kPreferencesFilename); | |
| 42 } else { | |
| 43 reference_pref_file = test_data_directory_ | |
| 44 .AppendASCII("profiles") | 34 .AppendASCII("profiles") |
| 45 .AppendASCII("window_placement") | 35 .AppendASCII("window_placement") |
| 46 .Append(chrome::kLocalStateFilename); | 36 .Append(chrome::kLocalStateFilename); |
| 47 tmp_pref_file_ = tmp_profile_.Append(chrome::kLocalStateFilename); | 37 |
| 48 } | 38 tmp_pref_file_ = tmp_profile_.Append(chrome::kLocalStateFilename); |
| 49 | 39 |
| 50 ASSERT_TRUE(file_util::PathExists(reference_pref_file)); | 40 ASSERT_TRUE(file_util::PathExists(reference_pref_file)); |
| 51 // Copy only the Preferences file if |new_profile_|, or Local State if not, | 41 |
| 52 // and the rest will be automatically created. | 42 // Copy only the Local State file, the rest will be automatically created |
| 53 ASSERT_TRUE(file_util::CopyFile(reference_pref_file, tmp_pref_file_)); | 43 ASSERT_TRUE(file_util::CopyFile(reference_pref_file, tmp_pref_file_)); |
| 54 | 44 |
| 55 #if defined(OS_WIN) | 45 #if defined(OS_WIN) |
| 56 // Make the copy writable. On POSIX we assume the umask allows files | 46 // Make the copy writable. On POSIX we assume the umask allows files |
| 57 // we create to be writable. | 47 // we create to be writable. |
| 58 ASSERT_TRUE(::SetFileAttributesW(tmp_pref_file_.value().c_str(), | 48 ASSERT_TRUE(::SetFileAttributesW(tmp_pref_file_.value().c_str(), |
| 59 FILE_ATTRIBUTE_NORMAL)); | 49 FILE_ATTRIBUTE_NORMAL)); |
| 60 #endif | 50 #endif |
| 61 | 51 |
| 62 launch_arguments_.AppendSwitchPath(switches::kUserDataDir, tmp_profile_); | 52 launch_arguments_.AppendSwitchPath(switches::kUserDataDir, tmp_profile_); |
| 63 } | 53 } |
| 64 | 54 |
| 65 bool LaunchAppWithProfile() { | 55 bool LaunchAppWithProfile() { |
| 66 if (!file_util::PathExists(tmp_pref_file_)) | 56 if (!file_util::PathExists(tmp_pref_file_)) |
| 67 return false; | 57 return false; |
| 68 UITest::SetUp(); | 58 UITest::SetUp(); |
| 69 return true; | 59 return true; |
| 70 } | 60 } |
| 71 | 61 |
| 72 void TearDown() { | 62 void TearDown() { |
| 73 UITest::TearDown(); | 63 UITest::TearDown(); |
| 74 | 64 |
| 75 EXPECT_TRUE(file_util::DieFileDie(tmp_profile_, true)); | 65 EXPECT_TRUE(file_util::DieFileDie(tmp_profile_, true)); |
| 76 } | 66 } |
| 77 | 67 |
| 78 public: | 68 public: |
| 79 bool new_profile_; | |
| 80 FilePath tmp_pref_file_; | 69 FilePath tmp_pref_file_; |
| 81 FilePath tmp_profile_; | 70 FilePath tmp_profile_; |
| 82 }; | 71 }; |
| 83 | 72 |
| 84 #if !defined(OS_LINUX) | 73 #if defined(OS_WIN) |
| 85 // This test verifies that the window position from the prefs file is restored | 74 // This test verifies that the window position from the prefs file is restored |
| 86 // when the app restores. This doesn't really make sense on Linux, where | 75 // when the app restores. This doesn't really make sense on Linux, where |
| 87 // the window manager might fight with you over positioning. However, we | 76 // the window manager might fight with you over positioning. However, we |
| 88 // might be able to make this work on buildbots. | 77 // might be able to make this work on buildbots. |
| 78 // Also, not sure what should happen on the mac. In any case, the code below |
| 79 // (minus the Windows bits) compiles fine on my Linux box now. |
| 89 // TODO(port): revisit this. | 80 // TODO(port): revisit this. |
| 90 TEST_F(PreferenceServiceTest, PreservedWindowPlacementIsLoaded) { | 81 TEST_F(PreferenceServiceTest, PreservedWindowPlacementIsLoaded) { |
| 91 // The window should open with the new reference profile, with window | 82 // The window should open with the reference profile |
| 92 // placement values stored in the user data directory. | |
| 93 new_profile_ = true; | |
| 94 ASSERT_TRUE(LaunchAppWithProfile()); | 83 ASSERT_TRUE(LaunchAppWithProfile()); |
| 95 | 84 |
| 96 ASSERT_TRUE(file_util::PathExists(tmp_pref_file_)); | 85 ASSERT_TRUE(file_util::PathExists(tmp_pref_file_)); |
| 97 | 86 |
| 98 JSONFileValueSerializer deserializer(tmp_pref_file_); | 87 JSONFileValueSerializer deserializer(tmp_pref_file_); |
| 99 scoped_ptr<Value> root(deserializer.Deserialize(NULL, NULL)); | 88 scoped_ptr<Value> root(deserializer.Deserialize(NULL, NULL)); |
| 100 | 89 |
| 101 ASSERT_TRUE(root.get()); | 90 ASSERT_TRUE(root.get()); |
| 102 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 91 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
| 103 | 92 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 | 125 |
| 137 // Find if launched window is maximized. | 126 // Find if launched window is maximized. |
| 138 bool is_window_maximized = false; | 127 bool is_window_maximized = false; |
| 139 ASSERT_TRUE(window->IsMaximized(&is_window_maximized)); | 128 ASSERT_TRUE(window->IsMaximized(&is_window_maximized)); |
| 140 bool is_maximized = false; | 129 bool is_maximized = false; |
| 141 EXPECT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + ".maximized", | 130 EXPECT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + ".maximized", |
| 142 &is_maximized)); | 131 &is_maximized)); |
| 143 EXPECT_EQ(is_maximized, is_window_maximized); | 132 EXPECT_EQ(is_maximized, is_window_maximized); |
| 144 } | 133 } |
| 145 #endif | 134 #endif |
| 146 | |
| 147 #if !defined(OS_LINUX) | |
| 148 TEST_F(PreferenceServiceTest, PreservedWindowPlacementIsMigrated) { | |
| 149 // The window should open with the old reference profile, with window | |
| 150 // placement values stored in Local State. | |
| 151 new_profile_ = false; | |
| 152 ASSERT_TRUE(LaunchAppWithProfile()); | |
| 153 | |
| 154 ASSERT_TRUE(file_util::PathExists(tmp_pref_file_)); | |
| 155 | |
| 156 JSONFileValueSerializer deserializer(tmp_pref_file_); | |
| 157 scoped_ptr<Value> root(deserializer.Deserialize(NULL, NULL)); | |
| 158 | |
| 159 ASSERT_TRUE(root.get()); | |
| 160 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | |
| 161 | |
| 162 // Retrieve the screen rect for the launched window | |
| 163 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); | |
| 164 ASSERT_TRUE(browser.get()); | |
| 165 scoped_refptr<WindowProxy> window(browser->GetWindow()); | |
| 166 ASSERT_TRUE(window.get()); | |
| 167 | |
| 168 gfx::Rect bounds; | |
| 169 ASSERT_TRUE(window->GetBounds(&bounds)); | |
| 170 | |
| 171 // Values from old reference profile in Local State should have been | |
| 172 // correctly migrated to the user's Preferences -- if so, the window | |
| 173 // should be set to values taken from the user's Local State. | |
| 174 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); | |
| 175 | |
| 176 // Retrieve the expected rect values from User Preferences, where they | |
| 177 // should have been migrated from Local State. | |
| 178 int bottom = 0; | |
| 179 std::string kBrowserWindowPlacement(prefs::kBrowserWindowPlacement); | |
| 180 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".bottom", | |
| 181 &bottom)); | |
| 182 EXPECT_EQ(bottom, bounds.y() + bounds.height()); | |
| 183 | |
| 184 int top = 0; | |
| 185 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".top", | |
| 186 &top)); | |
| 187 EXPECT_EQ(top, bounds.y()); | |
| 188 | |
| 189 int left = 0; | |
| 190 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".left", | |
| 191 &left)); | |
| 192 EXPECT_EQ(left, bounds.x()); | |
| 193 | |
| 194 int right = 0; | |
| 195 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".right", | |
| 196 &right)); | |
| 197 EXPECT_EQ(right, bounds.x() + bounds.width()); | |
| 198 | |
| 199 // Find if launched window is maximized. | |
| 200 bool is_window_maximized = false; | |
| 201 ASSERT_TRUE(window->IsMaximized(&is_window_maximized)); | |
| 202 bool is_maximized = false; | |
| 203 EXPECT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + ".maximized", | |
| 204 &is_maximized)); | |
| 205 EXPECT_EQ(is_maximized, is_window_maximized); | |
| 206 } | |
| 207 #endif | |
| 208 | |
| OLD | NEW |