blob: af2e178217f8d76cb609b7a5a844e8514f2ba6ad [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2012 The Chromium Authors
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include <string>
6
7#include "base/command_line.h"
thestig18dfb7a52014-08-26 10:44:048#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:239#include "base/files/scoped_temp_dir.h"
[email protected]ffbec692012-02-26 20:26:4210#include "base/json/json_file_value_serializer.h"
[email protected]870983d2012-04-09 15:43:2511#include "base/path_service.h"
[email protected]fb895c62009-10-09 18:20:3012#include "base/test/test_file_util.h"
jam3f2d3932017-04-26 20:28:5113#include "base/threading/thread_restrictions.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/values.h"
[email protected]5cc063692009-04-07 23:21:3115#include "build/build_config.h"
Yuta Hijikata31a359c2020-11-27 07:50:2616#include "build/chromeos_buildflags.h"
[email protected]870983d2012-04-09 15:43:2517#include "chrome/browser/ui/browser.h"
18#include "chrome/browser/ui/browser_window.h"
[email protected]1f0b50b2012-06-22 20:37:1619#include "chrome/browser/ui/browser_window_state.h"
initial.commit09911bf2008-07-26 23:55:2920#include "chrome/common/chrome_constants.h"
[email protected]870983d2012-04-09 15:43:2521#include "chrome/common/chrome_paths.h"
initial.commit09911bf2008-07-26 23:55:2922#include "chrome/common/pref_names.h"
[email protected]870983d2012-04-09 15:43:2523#include "chrome/test/base/in_process_browser_test.h"
[email protected]89dbb1772012-07-17 13:47:2524#include "chrome/test/base/testing_profile.h"
[email protected]870983d2012-04-09 15:43:2525#include "chrome/test/base/ui_test_utils.h"
Peter Kasting919ce652020-05-07 10:22:3626#include "content/public/test/browser_test.h"
Carlos Caballeroaf9c26e2021-12-01 19:17:3327#include "testing/gmock/include/gmock/gmock.h"
tfarina3b0452d2014-12-31 15:20:0928#include "ui/gfx/geometry/rect.h"
initial.commit09911bf2008-07-26 23:55:2929
[email protected]3cc00b62012-08-06 15:43:4830typedef InProcessBrowserTest PreservedWindowPlacement;
Carlos Caballeroaf9c26e2021-12-01 19:17:3331using ::testing::Optional;
[email protected]3cc00b62012-08-06 15:43:4832
Jayson Adams40e50052023-07-20 16:30:4933namespace {
34
35const gfx::Rect window_frame = gfx::Rect(20, 40, 600, 600);
36
37} // namespace
38
[email protected]3cc00b62012-08-06 15:43:4839IN_PROC_BROWSER_TEST_F(PreservedWindowPlacement, PRE_Test) {
Jayson Adams40e50052023-07-20 16:30:4940 browser()->window()->SetBounds(window_frame);
[email protected]3cc00b62012-08-06 15:43:4841}
42
[email protected]bee96122013-01-03 02:40:4543// Fails on Chrome OS as the browser thinks it is restarting after a crash, see
44// http://crbug.com/168044
Yuta Hijikata31a359c2020-11-27 07:50:2645#if BUILDFLAG(IS_CHROMEOS_ASH)
[email protected]bee96122013-01-03 02:40:4546#define MAYBE_Test DISABLED_Test
47#else
48#define MAYBE_Test Test
49#endif
50IN_PROC_BROWSER_TEST_F(PreservedWindowPlacement, MAYBE_Test) {
[email protected]3cc00b62012-08-06 15:43:4851 gfx::Rect bounds = browser()->window()->GetBounds();
Jayson Adams40e50052023-07-20 16:30:4952 gfx::Rect expected_bounds(window_frame);
[email protected]cda278d2012-10-30 20:31:4053 ASSERT_EQ(expected_bounds.ToString(), bounds.ToString());
[email protected]3cc00b62012-08-06 15:43:4854}
55
[email protected]870983d2012-04-09 15:43:2556class PreferenceServiceTest : public InProcessBrowserTest {
[email protected]84a059d2010-03-31 01:11:5957 public:
dcheng8f4b8622014-10-23 16:37:4858 bool SetUpUserDataDirectory() override {
[email protected]650b2d52013-02-10 03:41:4559 base::FilePath user_data_directory;
Avi Drissman9098f9002018-05-04 00:11:5260 base::PathService::Get(chrome::DIR_USER_DATA, &user_data_directory);
initial.commit09911bf2008-07-26 23:55:2961
rkaplowf216caf2015-03-02 22:37:4562 original_pref_file_ = ui_test_utils::GetTestFilePath(
63 base::FilePath()
64 .AppendASCII("profiles")
65 .AppendASCII("window_placement")
66 .AppendASCII("Default"),
67 base::FilePath().Append(chrome::kPreferencesFilename));
68 tmp_pref_file_ =
69 user_data_directory.AppendASCII(TestingProfile::kTestUserProfileDir);
70 EXPECT_TRUE(base::CreateDirectory(tmp_pref_file_));
71 tmp_pref_file_ = tmp_pref_file_.Append(chrome::kPreferencesFilename);
initial.commit09911bf2008-07-26 23:55:2972
rkaplowf216caf2015-03-02 22:37:4573 EXPECT_TRUE(base::PathExists(original_pref_file_));
74 EXPECT_TRUE(base::CopyFile(original_pref_file_, tmp_pref_file_));
initial.commit09911bf2008-07-26 23:55:2975
Xiaohan Wangae7f9182022-01-16 00:37:5276#if BUILDFLAG(IS_WIN)
[email protected]5cc063692009-04-07 23:21:3177 // Make the copy writable. On POSIX we assume the umask allows files
78 // we create to be writable.
rkaplowf216caf2015-03-02 22:37:4579 EXPECT_TRUE(::SetFileAttributesW(tmp_pref_file_.value().c_str(),
80 FILE_ATTRIBUTE_NORMAL));
[email protected]5cc063692009-04-07 23:21:3181#endif
initial.commit09911bf2008-07-26 23:55:2982 return true;
83 }
84
[email protected]870983d2012-04-09 15:43:2585 protected:
[email protected]56cbcb3a2013-12-23 21:24:4686 base::FilePath original_pref_file_;
[email protected]650b2d52013-02-10 03:41:4587 base::FilePath tmp_pref_file_;
initial.commit09911bf2008-07-26 23:55:2988};
89
Xiaohan Wangae7f9182022-01-16 00:37:5290#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
[email protected]5cc063692009-04-07 23:21:3191// This test verifies that the window position from the prefs file is restored
92// when the app restores. This doesn't really make sense on Linux, where
93// the window manager might fight with you over positioning. However, we
94// might be able to make this work on buildbots.
[email protected]5cc063692009-04-07 23:21:3195// TODO(port): revisit this.
[email protected]870983d2012-04-09 15:43:2596
rkaplowf216caf2015-03-02 22:37:4597IN_PROC_BROWSER_TEST_F(PreferenceServiceTest, Test) {
[email protected]f87919d72011-02-02 18:46:1698 // The window should open with the new reference profile, with window
99 // placement values stored in the user data directory.
prashhir54a994502015-03-05 09:30:57100 JSONFileValueDeserializer deserializer(original_pref_file_);
jam3f2d3932017-04-26 20:28:51101 std::unique_ptr<base::Value> root;
102 {
Francois Doraye6fb2d02017-10-18 21:29:13103 base::ScopedAllowBlockingForTesting allow_blocking;
jam3f2d3932017-04-26 20:28:51104 root = deserializer.Deserialize(NULL, NULL);
105 }
initial.commit09911bf2008-07-26 23:55:29106
[email protected]b4cebf82008-12-29 19:59:08107 ASSERT_TRUE(root.get());
jdoerrie1f536b22017-10-23 17:15:11108 ASSERT_TRUE(root->is_dict());
Muyao Xu98f0f682022-12-29 15:39:33109 base::Value::Dict& root_dict = root->GetDict();
initial.commit09911bf2008-07-26 23:55:29110
111 // Retrieve the screen rect for the launched window
[email protected]870983d2012-04-09 15:43:25112 gfx::Rect bounds = browser()->window()->GetRestoredBounds();
initial.commit09911bf2008-07-26 23:55:29113
114 // Retrieve the expected rect values from "Preferences"
[email protected]57ecc4b2010-08-11 03:02:51115 std::string kBrowserWindowPlacement(prefs::kBrowserWindowPlacement);
Muyao Xu98f0f682022-12-29 15:39:33116 EXPECT_THAT(
117 root_dict.FindIntByDottedPath(kBrowserWindowPlacement + ".bottom"),
118 Optional(bounds.y() + bounds.height()));
initial.commit09911bf2008-07-26 23:55:29119
Muyao Xu98f0f682022-12-29 15:39:33120 EXPECT_THAT(root_dict.FindIntByDottedPath(kBrowserWindowPlacement + ".top"),
Carlos Caballeroaf9c26e2021-12-01 19:17:33121 Optional(bounds.y()));
initial.commit09911bf2008-07-26 23:55:29122
Muyao Xu98f0f682022-12-29 15:39:33123 EXPECT_THAT(root_dict.FindIntByDottedPath(kBrowserWindowPlacement + ".left"),
Carlos Caballeroaf9c26e2021-12-01 19:17:33124 Optional(bounds.x()));
initial.commit09911bf2008-07-26 23:55:29125
Muyao Xu98f0f682022-12-29 15:39:33126 EXPECT_THAT(root_dict.FindIntByDottedPath(kBrowserWindowPlacement + ".right"),
Carlos Caballeroaf9c26e2021-12-01 19:17:33127 Optional(bounds.x() + bounds.width()));
initial.commit09911bf2008-07-26 23:55:29128
[email protected]8dd404bb2009-09-22 19:57:24129 // Find if launched window is maximized.
[email protected]6a9b53a22012-10-09 02:43:34130 bool is_window_maximized = browser()->window()->IsMaximized();
Muyao Xu98f0f682022-12-29 15:39:33131 EXPECT_THAT(
132 root_dict.FindBoolByDottedPath(kBrowserWindowPlacement + ".maximized"),
133 Optional(is_window_maximized));
initial.commit09911bf2008-07-26 23:55:29134}
[email protected]5cc063692009-04-07 23:21:31135#endif