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.
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/json/json_file_value_serializer.h"
11 #include "base/path_service.h"
12 #include "base/test/test_file_util.h"
13 #include "base/values.h"
14 #include "build/build_config.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/browser_window_state.h"
18 #include "chrome/common/chrome_constants.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/test/base/in_process_browser_test.h"
22 #include "chrome/test/base/test_switches.h"
23 #include "chrome/test/base/testing_profile.h"
24 #include "chrome/test/base/ui_test_utils.h"
25 #include "ui/gfx/rect.h"
27 // On GTK, resizing happens asynchronously and we currently don't have a way to
28 // get called back (it's probably possible, but we don't have that code). Since
29 // the GTK code is going away, not spending more time on this.
30 #if !defined(TOOLKIT_GTK)
32 typedef InProcessBrowserTest PreservedWindowPlacement
;
34 IN_PROC_BROWSER_TEST_F(PreservedWindowPlacement
, PRE_Test
) {
35 browser()->window()->SetBounds(gfx::Rect(20, 30, 400, 500));
38 // Fails on Chrome OS as the browser thinks it is restarting after a crash, see
39 // http://crbug.com/168044
40 #if defined(OS_CHROMEOS)
41 #define MAYBE_Test DISABLED_Test
43 #define MAYBE_Test Test
45 IN_PROC_BROWSER_TEST_F(PreservedWindowPlacement
, MAYBE_Test
) {
46 #if defined(OS_WIN) && defined(USE_ASH)
47 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
48 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
52 gfx::Rect bounds
= browser()->window()->GetBounds();
53 gfx::Rect
expected_bounds(gfx::Rect(20, 30, 400, 500));
54 ASSERT_EQ(expected_bounds
.ToString(), bounds
.ToString());
57 #endif // defined(TOOLKIT_GTK)
59 class PreferenceServiceTest
: public InProcessBrowserTest
{
61 explicit PreferenceServiceTest(bool new_profile
) : new_profile_(new_profile
) {
64 virtual bool SetUpUserDataDirectory() OVERRIDE
{
65 base::FilePath user_data_directory
;
66 PathService::Get(chrome::DIR_USER_DATA
, &user_data_directory
);
69 original_pref_file_
= ui_test_utils::GetTestFilePath(
70 base::FilePath().AppendASCII("profiles").
71 AppendASCII("window_placement").
72 AppendASCII("Default"),
73 base::FilePath().Append(chrome::kPreferencesFilename
));
75 user_data_directory
.AppendASCII(TestingProfile::kTestUserProfileDir
);
76 CHECK(base::CreateDirectory(tmp_pref_file_
));
77 tmp_pref_file_
= tmp_pref_file_
.Append(chrome::kPreferencesFilename
);
79 original_pref_file_
= ui_test_utils::GetTestFilePath(
80 base::FilePath().AppendASCII("profiles").
81 AppendASCII("window_placement"),
82 base::FilePath().Append(chrome::kLocalStateFilename
));
83 tmp_pref_file_
= user_data_directory
.Append(chrome::kLocalStateFilename
);
86 CHECK(base::PathExists(original_pref_file_
));
87 // Copy only the Preferences file if |new_profile_|, or Local State if not,
88 // and the rest will be automatically created.
89 CHECK(base::CopyFile(original_pref_file_
, tmp_pref_file_
));
92 // Make the copy writable. On POSIX we assume the umask allows files
93 // we create to be writable.
94 CHECK(::SetFileAttributesW(tmp_pref_file_
.value().c_str(),
95 FILE_ATTRIBUTE_NORMAL
));
101 base::FilePath original_pref_file_
;
102 base::FilePath tmp_pref_file_
;
108 #if defined(OS_WIN) || defined(OS_MACOSX)
109 // This test verifies that the window position from the prefs file is restored
110 // when the app restores. This doesn't really make sense on Linux, where
111 // the window manager might fight with you over positioning. However, we
112 // might be able to make this work on buildbots.
113 // TODO(port): revisit this.
115 class PreservedWindowPlacementIsLoaded
: public PreferenceServiceTest
{
117 PreservedWindowPlacementIsLoaded() : PreferenceServiceTest(true) {
121 IN_PROC_BROWSER_TEST_F(PreservedWindowPlacementIsLoaded
, Test
) {
122 #if defined(OS_WIN) && defined(USE_ASH)
123 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
124 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
128 // The window should open with the new reference profile, with window
129 // placement values stored in the user data directory.
130 JSONFileValueSerializer
deserializer(original_pref_file_
);
131 scoped_ptr
<base::Value
> root(deserializer
.Deserialize(NULL
, NULL
));
133 ASSERT_TRUE(root
.get());
134 ASSERT_TRUE(root
->IsType(base::Value::TYPE_DICTIONARY
));
136 base::DictionaryValue
* root_dict
=
137 static_cast<base::DictionaryValue
*>(root
.get());
139 // Retrieve the screen rect for the launched window
140 gfx::Rect bounds
= browser()->window()->GetRestoredBounds();
142 // Retrieve the expected rect values from "Preferences"
144 std::string
kBrowserWindowPlacement(prefs::kBrowserWindowPlacement
);
145 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".bottom",
147 EXPECT_EQ(bottom
, bounds
.y() + bounds
.height());
150 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".top",
152 EXPECT_EQ(top
, bounds
.y());
155 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".left",
157 EXPECT_EQ(left
, bounds
.x());
160 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".right",
162 EXPECT_EQ(right
, bounds
.x() + bounds
.width());
164 // Find if launched window is maximized.
165 bool is_window_maximized
= browser()->window()->IsMaximized();
166 bool is_maximized
= false;
167 EXPECT_TRUE(root_dict
->GetBoolean(kBrowserWindowPlacement
+ ".maximized",
169 EXPECT_EQ(is_maximized
, is_window_maximized
);
173 #if defined(OS_WIN) || defined(OS_MACOSX)
175 class PreservedWindowPlacementIsMigrated
: public PreferenceServiceTest
{
177 PreservedWindowPlacementIsMigrated() : PreferenceServiceTest(false) {
181 IN_PROC_BROWSER_TEST_F(PreservedWindowPlacementIsMigrated
, Test
) {
182 #if defined(OS_WIN) && defined(USE_ASH)
183 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
184 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
188 // The window should open with the old reference profile, with window
189 // placement values stored in Local State.
191 JSONFileValueSerializer
deserializer(original_pref_file_
);
192 scoped_ptr
<base::Value
> root(deserializer
.Deserialize(NULL
, NULL
));
194 ASSERT_TRUE(root
.get());
195 ASSERT_TRUE(root
->IsType(base::Value::TYPE_DICTIONARY
));
197 // Retrieve the screen rect for the launched window
198 gfx::Rect bounds
= browser()->window()->GetRestoredBounds();
200 // Values from old reference profile in Local State should have been
201 // correctly migrated to the user's Preferences -- if so, the window
202 // should be set to values taken from the user's Local State.
203 base::DictionaryValue
* root_dict
=
204 static_cast<base::DictionaryValue
*>(root
.get());
206 // Retrieve the expected rect values from User Preferences, where they
207 // should have been migrated from Local State.
209 std::string
kBrowserWindowPlacement(prefs::kBrowserWindowPlacement
);
210 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".bottom",
212 EXPECT_EQ(bottom
, bounds
.y() + bounds
.height());
215 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".top",
217 EXPECT_EQ(top
, bounds
.y());
220 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".left",
222 EXPECT_EQ(left
, bounds
.x());
225 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".right",
227 EXPECT_EQ(right
, bounds
.x() + bounds
.width());
229 // Find if launched window is maximized.
230 bool is_window_maximized
= browser()->window()->IsMaximized();
231 bool is_maximized
= false;
232 EXPECT_TRUE(root_dict
->GetBoolean(kBrowserWindowPlacement
+ ".maximized",
234 EXPECT_EQ(is_maximized
, is_window_maximized
);