OLD | NEW |
1 // Copyright (c) 2009 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/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 test_data_directory_ | 33 if (new_profile_) { |
| 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_ |
34 .AppendASCII("profiles") | 44 .AppendASCII("profiles") |
35 .AppendASCII("window_placement") | 45 .AppendASCII("window_placement") |
36 .Append(chrome::kLocalStateFilename); | 46 .Append(chrome::kLocalStateFilename); |
37 | 47 tmp_pref_file_ = tmp_profile_.Append(chrome::kLocalStateFilename); |
38 tmp_pref_file_ = tmp_profile_.Append(chrome::kLocalStateFilename); | 48 } |
39 | 49 |
40 ASSERT_TRUE(file_util::PathExists(reference_pref_file)); | 50 ASSERT_TRUE(file_util::PathExists(reference_pref_file)); |
41 | 51 // Copy only the Preferences file if |new_profile_|, or Local State if not, |
42 // Copy only the Local State file, the rest will be automatically created | 52 // and the rest will be automatically created. |
43 ASSERT_TRUE(file_util::CopyFile(reference_pref_file, tmp_pref_file_)); | 53 ASSERT_TRUE(file_util::CopyFile(reference_pref_file, tmp_pref_file_)); |
44 | 54 |
45 #if defined(OS_WIN) | 55 #if defined(OS_WIN) |
46 // Make the copy writable. On POSIX we assume the umask allows files | 56 // Make the copy writable. On POSIX we assume the umask allows files |
47 // we create to be writable. | 57 // we create to be writable. |
48 ASSERT_TRUE(::SetFileAttributesW(tmp_pref_file_.value().c_str(), | 58 ASSERT_TRUE(::SetFileAttributesW(tmp_pref_file_.value().c_str(), |
49 FILE_ATTRIBUTE_NORMAL)); | 59 FILE_ATTRIBUTE_NORMAL)); |
50 #endif | 60 #endif |
51 | 61 |
52 launch_arguments_.AppendSwitchPath(switches::kUserDataDir, tmp_profile_); | 62 launch_arguments_.AppendSwitchPath(switches::kUserDataDir, tmp_profile_); |
53 } | 63 } |
54 | 64 |
55 bool LaunchAppWithProfile() { | 65 bool LaunchAppWithProfile() { |
56 if (!file_util::PathExists(tmp_pref_file_)) | 66 if (!file_util::PathExists(tmp_pref_file_)) |
57 return false; | 67 return false; |
58 UITest::SetUp(); | 68 UITest::SetUp(); |
59 return true; | 69 return true; |
60 } | 70 } |
61 | 71 |
62 void TearDown() { | 72 void TearDown() { |
63 UITest::TearDown(); | 73 UITest::TearDown(); |
64 | 74 |
65 EXPECT_TRUE(file_util::DieFileDie(tmp_profile_, true)); | 75 EXPECT_TRUE(file_util::DieFileDie(tmp_profile_, true)); |
66 } | 76 } |
67 | 77 |
68 public: | 78 public: |
| 79 bool new_profile_; |
69 FilePath tmp_pref_file_; | 80 FilePath tmp_pref_file_; |
70 FilePath tmp_profile_; | 81 FilePath tmp_profile_; |
71 }; | 82 }; |
72 | 83 |
73 #if defined(OS_WIN) | 84 #if !defined(OS_LINUX) |
74 // This test verifies that the window position from the prefs file is restored | 85 // This test verifies that the window position from the prefs file is restored |
75 // when the app restores. This doesn't really make sense on Linux, where | 86 // when the app restores. This doesn't really make sense on Linux, where |
76 // the window manager might fight with you over positioning. However, we | 87 // the window manager might fight with you over positioning. However, we |
77 // might be able to make this work on buildbots. | 88 // 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. | |
80 // TODO(port): revisit this. | 89 // TODO(port): revisit this. |
81 TEST_F(PreferenceServiceTest, PreservedWindowPlacementIsLoaded) { | 90 TEST_F(PreferenceServiceTest, PreservedWindowPlacementIsLoaded) { |
82 // The window should open with the reference profile | 91 // The window should open with the new reference profile, with window |
| 92 // placement values stored in the user data directory. |
| 93 new_profile_ = true; |
83 ASSERT_TRUE(LaunchAppWithProfile()); | 94 ASSERT_TRUE(LaunchAppWithProfile()); |
84 | 95 |
85 ASSERT_TRUE(file_util::PathExists(tmp_pref_file_)); | 96 ASSERT_TRUE(file_util::PathExists(tmp_pref_file_)); |
86 | 97 |
87 JSONFileValueSerializer deserializer(tmp_pref_file_); | 98 JSONFileValueSerializer deserializer(tmp_pref_file_); |
88 scoped_ptr<Value> root(deserializer.Deserialize(NULL, NULL)); | 99 scoped_ptr<Value> root(deserializer.Deserialize(NULL, NULL)); |
89 | 100 |
90 ASSERT_TRUE(root.get()); | 101 ASSERT_TRUE(root.get()); |
91 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 102 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
92 | 103 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 | 136 |
126 // Find if launched window is maximized. | 137 // Find if launched window is maximized. |
127 bool is_window_maximized = false; | 138 bool is_window_maximized = false; |
128 ASSERT_TRUE(window->IsMaximized(&is_window_maximized)); | 139 ASSERT_TRUE(window->IsMaximized(&is_window_maximized)); |
129 bool is_maximized = false; | 140 bool is_maximized = false; |
130 EXPECT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + ".maximized", | 141 EXPECT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + ".maximized", |
131 &is_maximized)); | 142 &is_maximized)); |
132 EXPECT_EQ(is_maximized, is_window_maximized); | 143 EXPECT_EQ(is_maximized, is_window_maximized); |
133 } | 144 } |
134 #endif | 145 #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 |