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);
68 base::FilePath reference_pref_file;
70 reference_pref_file = ui_test_utils::GetTestFilePath(
71 base::FilePath().AppendASCII("profiles").
72 AppendASCII("window_placement").
73 AppendASCII("Default"),
74 base::FilePath().Append(chrome::kPreferencesFilename));
76 user_data_directory.AppendASCII(TestingProfile::kTestUserProfileDir);
77 CHECK(file_util::CreateDirectory(tmp_pref_file_));
78 tmp_pref_file_ = tmp_pref_file_.Append(chrome::kPreferencesFilename);
80 reference_pref_file = ui_test_utils::GetTestFilePath(
81 base::FilePath().AppendASCII("profiles").
82 AppendASCII("window_placement"),
83 base::FilePath().Append(chrome::kLocalStateFilename));
84 tmp_pref_file_ = user_data_directory.Append(chrome::kLocalStateFilename);
87 CHECK(base::PathExists(reference_pref_file));
88 // Copy only the Preferences file if |new_profile_|, or Local State if not,
89 // and the rest will be automatically created.
90 CHECK(base::CopyFile(reference_pref_file, tmp_pref_file_));
93 // Make the copy writable. On POSIX we assume the umask allows files
94 // we create to be writable.
95 CHECK(::SetFileAttributesW(tmp_pref_file_.value().c_str(),
96 FILE_ATTRIBUTE_NORMAL));
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(tmp_pref_file_);
131 scoped_ptr<Value> root(deserializer.Deserialize(NULL, NULL));
133 ASSERT_TRUE(root.get());
134 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
136 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
138 // Retrieve the screen rect for the launched window
139 gfx::Rect bounds = browser()->window()->GetRestoredBounds();
141 // Retrieve the expected rect values from "Preferences"
143 std::string kBrowserWindowPlacement(prefs::kBrowserWindowPlacement);
144 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".bottom",
146 EXPECT_EQ(bottom, bounds.y() + bounds.height());
149 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".top",
151 EXPECT_EQ(top, bounds.y());
154 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".left",
156 EXPECT_EQ(left, bounds.x());
159 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".right",
161 EXPECT_EQ(right, bounds.x() + bounds.width());
163 // Find if launched window is maximized.
164 bool is_window_maximized = browser()->window()->IsMaximized();
165 bool is_maximized = false;
166 EXPECT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + ".maximized",
168 EXPECT_EQ(is_maximized, is_window_maximized);
172 #if defined(OS_WIN) || defined(OS_MACOSX)
174 class PreservedWindowPlacementIsMigrated : public PreferenceServiceTest {
176 PreservedWindowPlacementIsMigrated() : PreferenceServiceTest(false) {
180 IN_PROC_BROWSER_TEST_F(PreservedWindowPlacementIsMigrated, Test) {
181 #if defined(OS_WIN) && defined(USE_ASH)
182 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
183 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
187 // The window should open with the old reference profile, with window
188 // placement values stored in Local State.
190 JSONFileValueSerializer deserializer(tmp_pref_file_);
191 scoped_ptr<Value> root(deserializer.Deserialize(NULL, NULL));
193 ASSERT_TRUE(root.get());
194 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
196 // Retrieve the screen rect for the launched window
197 gfx::Rect bounds = browser()->window()->GetRestoredBounds();
199 // Values from old reference profile in Local State should have been
200 // correctly migrated to the user's Preferences -- if so, the window
201 // should be set to values taken from the user's Local State.
202 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
204 // Retrieve the expected rect values from User Preferences, where they
205 // should have been migrated from Local State.
207 std::string kBrowserWindowPlacement(prefs::kBrowserWindowPlacement);
208 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".bottom",
210 EXPECT_EQ(bottom, bounds.y() + bounds.height());
213 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".top",
215 EXPECT_EQ(top, bounds.y());
218 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".left",
220 EXPECT_EQ(left, bounds.x());
223 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".right",
225 EXPECT_EQ(right, bounds.x() + bounds.width());
227 // Find if launched window is maximized.
228 bool is_window_maximized = browser()->window()->IsMaximized();
229 bool is_maximized = false;
230 EXPECT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + ".maximized",
232 EXPECT_EQ(is_maximized, is_window_maximized);