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 bool SetUpUserDataDirectory() override
{
56 base::FilePath user_data_directory
;
57 PathService::Get(chrome::DIR_USER_DATA
, &user_data_directory
);
59 original_pref_file_
= ui_test_utils::GetTestFilePath(
61 .AppendASCII("profiles")
62 .AppendASCII("window_placement")
63 .AppendASCII("Default"),
64 base::FilePath().Append(chrome::kPreferencesFilename
));
66 user_data_directory
.AppendASCII(TestingProfile::kTestUserProfileDir
);
67 EXPECT_TRUE(base::CreateDirectory(tmp_pref_file_
));
68 tmp_pref_file_
= tmp_pref_file_
.Append(chrome::kPreferencesFilename
);
70 EXPECT_TRUE(base::PathExists(original_pref_file_
));
71 EXPECT_TRUE(base::CopyFile(original_pref_file_
, tmp_pref_file_
));
74 // Make the copy writable. On POSIX we assume the umask allows files
75 // we create to be writable.
76 EXPECT_TRUE(::SetFileAttributesW(tmp_pref_file_
.value().c_str(),
77 FILE_ATTRIBUTE_NORMAL
));
83 base::FilePath original_pref_file_
;
84 base::FilePath tmp_pref_file_
;
87 #if defined(OS_WIN) || defined(OS_MACOSX)
88 // This test verifies that the window position from the prefs file is restored
89 // when the app restores. This doesn't really make sense on Linux, where
90 // the window manager might fight with you over positioning. However, we
91 // might be able to make this work on buildbots.
92 // TODO(port): revisit this.
94 IN_PROC_BROWSER_TEST_F(PreferenceServiceTest
, Test
) {
95 #if defined(OS_WIN) && defined(USE_ASH)
96 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
97 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
98 switches::kAshBrowserTests
))
102 // The window should open with the new reference profile, with window
103 // placement values stored in the user data directory.
104 JSONFileValueDeserializer
deserializer(original_pref_file_
);
105 scoped_ptr
<base::Value
> root(deserializer
.Deserialize(NULL
, NULL
));
107 ASSERT_TRUE(root
.get());
108 ASSERT_TRUE(root
->IsType(base::Value::TYPE_DICTIONARY
));
110 base::DictionaryValue
* root_dict
=
111 static_cast<base::DictionaryValue
*>(root
.get());
113 // Retrieve the screen rect for the launched window
114 gfx::Rect bounds
= browser()->window()->GetRestoredBounds();
116 // Retrieve the expected rect values from "Preferences"
118 std::string
kBrowserWindowPlacement(prefs::kBrowserWindowPlacement
);
119 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".bottom",
121 EXPECT_EQ(bottom
, bounds
.y() + bounds
.height());
124 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".top",
126 EXPECT_EQ(top
, bounds
.y());
129 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".left",
131 EXPECT_EQ(left
, bounds
.x());
134 EXPECT_TRUE(root_dict
->GetInteger(kBrowserWindowPlacement
+ ".right",
136 EXPECT_EQ(right
, bounds
.x() + bounds
.width());
138 // Find if launched window is maximized.
139 bool is_window_maximized
= browser()->window()->IsMaximized();
140 bool is_maximized
= false;
141 EXPECT_TRUE(root_dict
->GetBoolean(kBrowserWindowPlacement
+ ".maximized",
143 EXPECT_EQ(is_maximized
, is_window_maximized
);