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/files/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 typedef InProcessBrowserTest PreservedWindowPlacement
;
29 IN_PROC_BROWSER_TEST_F(PreservedWindowPlacement
, PRE_Test
) {
30 browser()->window()->SetBounds(gfx::Rect(20, 30, 400, 500));
33 // Fails on Chrome OS as the browser thinks it is restarting after a crash, see
34 // http://crbug.com/168044
35 #if defined(OS_CHROMEOS)
36 #define MAYBE_Test DISABLED_Test
38 #define MAYBE_Test Test
40 IN_PROC_BROWSER_TEST_F(PreservedWindowPlacement
, MAYBE_Test
) {
41 #if defined(OS_WIN) && defined(USE_ASH)
42 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
43 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
47 gfx::Rect bounds
= browser()->window()->GetBounds();
48 gfx::Rect
expected_bounds(gfx::Rect(20, 30, 400, 500));
49 ASSERT_EQ(expected_bounds
.ToString(), bounds
.ToString());
52 class PreferenceServiceTest
: public InProcessBrowserTest
{
54 explicit PreferenceServiceTest(bool new_profile
) : new_profile_(new_profile
) {
57 virtual bool SetUpUserDataDirectory() OVERRIDE
{
58 base::FilePath user_data_directory
;
59 PathService::Get(chrome::DIR_USER_DATA
, &user_data_directory
);
62 original_pref_file_
= ui_test_utils::GetTestFilePath(
63 base::FilePath().AppendASCII("profiles").
64 AppendASCII("window_placement").
65 AppendASCII("Default"),
66 base::FilePath().Append(chrome::kPreferencesFilename
));
68 user_data_directory
.AppendASCII(TestingProfile::kTestUserProfileDir
);
69 CHECK(base::CreateDirectory(tmp_pref_file_
));
70 tmp_pref_file_
= tmp_pref_file_
.Append(chrome::kPreferencesFilename
);
72 original_pref_file_
= ui_test_utils::GetTestFilePath(
73 base::FilePath().AppendASCII("profiles").
74 AppendASCII("window_placement"),
75 base::FilePath().Append(chrome::kLocalStateFilename
));
76 tmp_pref_file_
= user_data_directory
.Append(chrome::kLocalStateFilename
);
79 CHECK(base::PathExists(original_pref_file_
));
80 // Copy only the Preferences file if |new_profile_|, or Local State if not,
81 // and the rest will be automatically created.
82 CHECK(base::CopyFile(original_pref_file_
, tmp_pref_file_
));
85 // Make the copy writable. On POSIX we assume the umask allows files
86 // we create to be writable.
87 CHECK(::SetFileAttributesW(tmp_pref_file_
.value().c_str(),
88 FILE_ATTRIBUTE_NORMAL
));
94 base::FilePath original_pref_file_
;
95 base::FilePath tmp_pref_file_
;
101 #if defined(OS_WIN) || defined(OS_MACOSX)
102 // This test verifies that the window position from the prefs file is restored
103 // when the app restores. This doesn't really make sense on Linux, where
104 // the window manager might fight with you over positioning. However, we
105 // might be able to make this work on buildbots.
106 // TODO(port): revisit this.
108 class PreservedWindowPlacementIsLoaded
: public PreferenceServiceTest
{
110 PreservedWindowPlacementIsLoaded() : PreferenceServiceTest(true) {
114 IN_PROC_BROWSER_TEST_F(PreservedWindowPlacementIsLoaded
, Test
) {
115 #if defined(OS_WIN) && defined(USE_ASH)
116 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
117 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
121 // The window should open with the new reference profile, with window
122 // placement values stored in the user data directory.
123 JSONFileValueSerializer
deserializer(original_pref_file_
);
124 scoped_ptr
<base::Value
> root(deserializer
.Deserialize(NULL
, NULL
));
126 ASSERT_TRUE(root
.get());
127 ASSERT_TRUE(root
->IsType(base::Value::TYPE_DICTIONARY
));
129 base::DictionaryValue
* root_dict
=
130 static_cast<base::DictionaryValue
*>(root
.get());
132 // Retrieve the screen rect for the launched window
133 gfx::Rect bounds
= browser()->window()->GetRestoredBounds();
135 // Retrieve the expected rect values from "Preferences"
137 std::string
kBrowserWindowPlacement(prefs::kBrowserWindowPlacement
);
138 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".bottom",
140 EXPECT_EQ(bottom
, bounds
.y() + bounds
.height());
143 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".top",
145 EXPECT_EQ(top
, bounds
.y());
148 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".left",
150 EXPECT_EQ(left
, bounds
.x());
153 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".right",
155 EXPECT_EQ(right
, bounds
.x() + bounds
.width());
157 // Find if launched window is maximized.
158 bool is_window_maximized
= browser()->window()->IsMaximized();
159 bool is_maximized
= false;
160 EXPECT_TRUE(root_dict
->GetBoolean(kBrowserWindowPlacement
+ ".maximized",
162 EXPECT_EQ(is_maximized
, is_window_maximized
);
166 #if defined(OS_WIN) || defined(OS_MACOSX)
168 class PreservedWindowPlacementIsMigrated
: public PreferenceServiceTest
{
170 PreservedWindowPlacementIsMigrated() : PreferenceServiceTest(false) {
174 IN_PROC_BROWSER_TEST_F(PreservedWindowPlacementIsMigrated
, Test
) {
175 #if defined(OS_WIN) && defined(USE_ASH)
176 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
177 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
181 // The window should open with the old reference profile, with window
182 // placement values stored in Local State.
184 JSONFileValueSerializer
deserializer(original_pref_file_
);
185 scoped_ptr
<base::Value
> root(deserializer
.Deserialize(NULL
, NULL
));
187 ASSERT_TRUE(root
.get());
188 ASSERT_TRUE(root
->IsType(base::Value::TYPE_DICTIONARY
));
190 // Retrieve the screen rect for the launched window
191 gfx::Rect bounds
= browser()->window()->GetRestoredBounds();
193 // Values from old reference profile in Local State should have been
194 // correctly migrated to the user's Preferences -- if so, the window
195 // should be set to values taken from the user's Local State.
196 base::DictionaryValue
* root_dict
=
197 static_cast<base::DictionaryValue
*>(root
.get());
199 // Retrieve the expected rect values from User Preferences, where they
200 // should have been migrated from Local State.
202 std::string
kBrowserWindowPlacement(prefs::kBrowserWindowPlacement
);
203 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".bottom",
205 EXPECT_EQ(bottom
, bounds
.y() + bounds
.height());
208 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".top",
210 EXPECT_EQ(top
, bounds
.y());
213 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".left",
215 EXPECT_EQ(left
, bounds
.x());
218 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".right",
220 EXPECT_EQ(right
, bounds
.x() + bounds
.width());
222 // Find if launched window is maximized.
223 bool is_window_maximized
= browser()->window()->IsMaximized();
224 bool is_maximized
= false;
225 EXPECT_TRUE(root_dict
->GetBoolean(kBrowserWindowPlacement
+ ".maximized",
227 EXPECT_EQ(is_maximized
, is_window_maximized
);