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/geometry/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 (base::CommandLine::ForCurrentProcess()->HasSwitch(
44 switches::kAshBrowserTests
))
48 gfx::Rect bounds
= browser()->window()->GetBounds();
49 gfx::Rect
expected_bounds(gfx::Rect(20, 30, 400, 500));
50 ASSERT_EQ(expected_bounds
.ToString(), bounds
.ToString());
53 class PreferenceServiceTest
: public InProcessBrowserTest
{
55 explicit PreferenceServiceTest(bool new_profile
) : new_profile_(new_profile
) {
58 bool SetUpUserDataDirectory() override
{
59 base::FilePath user_data_directory
;
60 PathService::Get(chrome::DIR_USER_DATA
, &user_data_directory
);
63 original_pref_file_
= ui_test_utils::GetTestFilePath(
64 base::FilePath().AppendASCII("profiles").
65 AppendASCII("window_placement").
66 AppendASCII("Default"),
67 base::FilePath().Append(chrome::kPreferencesFilename
));
69 user_data_directory
.AppendASCII(TestingProfile::kTestUserProfileDir
);
70 CHECK(base::CreateDirectory(tmp_pref_file_
));
71 tmp_pref_file_
= tmp_pref_file_
.Append(chrome::kPreferencesFilename
);
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
);
80 CHECK(base::PathExists(original_pref_file_
));
81 // Copy only the Preferences file if |new_profile_|, or Local State if not,
82 // and the rest will be automatically created.
83 CHECK(base::CopyFile(original_pref_file_
, tmp_pref_file_
));
86 // Make the copy writable. On POSIX we assume the umask allows files
87 // we create to be writable.
88 CHECK(::SetFileAttributesW(tmp_pref_file_
.value().c_str(),
89 FILE_ATTRIBUTE_NORMAL
));
95 base::FilePath original_pref_file_
;
96 base::FilePath tmp_pref_file_
;
102 #if defined(OS_WIN) || defined(OS_MACOSX)
103 // 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
105 // the window manager might fight with you over positioning. However, we
106 // might be able to make this work on buildbots.
107 // TODO(port): revisit this.
109 class PreservedWindowPlacementIsLoaded
: public PreferenceServiceTest
{
111 PreservedWindowPlacementIsLoaded() : PreferenceServiceTest(true) {
115 IN_PROC_BROWSER_TEST_F(PreservedWindowPlacementIsLoaded
, Test
) {
116 #if defined(OS_WIN) && defined(USE_ASH)
117 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
118 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
119 switches::kAshBrowserTests
))
123 // The window should open with the new reference profile, with window
124 // placement values stored in the user data directory.
125 JSONFileValueSerializer
deserializer(original_pref_file_
);
126 scoped_ptr
<base::Value
> root(deserializer
.Deserialize(NULL
, NULL
));
128 ASSERT_TRUE(root
.get());
129 ASSERT_TRUE(root
->IsType(base::Value::TYPE_DICTIONARY
));
131 base::DictionaryValue
* root_dict
=
132 static_cast<base::DictionaryValue
*>(root
.get());
134 // Retrieve the screen rect for the launched window
135 gfx::Rect bounds
= browser()->window()->GetRestoredBounds();
137 // Retrieve the expected rect values from "Preferences"
139 std::string
kBrowserWindowPlacement(prefs::kBrowserWindowPlacement
);
140 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".bottom",
142 EXPECT_EQ(bottom
, bounds
.y() + bounds
.height());
145 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".top",
147 EXPECT_EQ(top
, bounds
.y());
150 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".left",
152 EXPECT_EQ(left
, bounds
.x());
155 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".right",
157 EXPECT_EQ(right
, bounds
.x() + bounds
.width());
159 // Find if launched window is maximized.
160 bool is_window_maximized
= browser()->window()->IsMaximized();
161 bool is_maximized
= false;
162 EXPECT_TRUE(root_dict
->GetBoolean(kBrowserWindowPlacement
+ ".maximized",
164 EXPECT_EQ(is_maximized
, is_window_maximized
);
168 #if defined(OS_WIN) || defined(OS_MACOSX)
170 class PreservedWindowPlacementIsMigrated
: public PreferenceServiceTest
{
172 PreservedWindowPlacementIsMigrated() : PreferenceServiceTest(false) {
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
))
184 // The window should open with the old reference profile, with window
185 // placement values stored in Local State.
187 JSONFileValueSerializer
deserializer(original_pref_file_
);
188 scoped_ptr
<base::Value
> root(deserializer
.Deserialize(NULL
, NULL
));
190 ASSERT_TRUE(root
.get());
191 ASSERT_TRUE(root
->IsType(base::Value::TYPE_DICTIONARY
));
193 // Retrieve the screen rect for the launched window
194 gfx::Rect bounds
= browser()->window()->GetRestoredBounds();
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());
202 // Retrieve the expected rect values from User Preferences, where they
203 // should have been migrated from Local State.
205 std::string
kBrowserWindowPlacement(prefs::kBrowserWindowPlacement
);
206 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".bottom",
208 EXPECT_EQ(bottom
, bounds
.y() + bounds
.height());
211 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".top",
213 EXPECT_EQ(top
, bounds
.y());
216 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".left",
218 EXPECT_EQ(left
, bounds
.x());
221 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".right",
223 EXPECT_EQ(right
, bounds
.x() + bounds
.width());
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",
230 EXPECT_EQ(is_maximized
, is_window_maximized
);