Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include <string> |
| 6 | |
| 7 | #include "base/command_line.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 8 | #include "base/files/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 9 | #include "base/files/scoped_temp_dir.h" |
[email protected] | ffbec69 | 2012-02-26 20:26:42 | [diff] [blame] | 10 | #include "base/json/json_file_value_serializer.h" |
[email protected] | 870983d | 2012-04-09 15:43:25 | [diff] [blame] | 11 | #include "base/path_service.h" |
[email protected] | fb895c6 | 2009-10-09 18:20:30 | [diff] [blame] | 12 | #include "base/test/test_file_util.h" |
jam | 3f2d393 | 2017-04-26 20:28:51 | [diff] [blame] | 13 | #include "base/threading/thread_restrictions.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "base/values.h" |
[email protected] | 5cc06369 | 2009-04-07 23:21:31 | [diff] [blame] | 15 | #include "build/build_config.h" |
Yuta Hijikata | 31a359c | 2020-11-27 07:50:26 | [diff] [blame] | 16 | #include "build/chromeos_buildflags.h" |
[email protected] | 870983d | 2012-04-09 15:43:25 | [diff] [blame] | 17 | #include "chrome/browser/ui/browser.h" |
| 18 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | 1f0b50b | 2012-06-22 20:37:16 | [diff] [blame] | 19 | #include "chrome/browser/ui/browser_window_state.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 20 | #include "chrome/common/chrome_constants.h" |
[email protected] | 870983d | 2012-04-09 15:43:25 | [diff] [blame] | 21 | #include "chrome/common/chrome_paths.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | #include "chrome/common/pref_names.h" |
[email protected] | 870983d | 2012-04-09 15:43:25 | [diff] [blame] | 23 | #include "chrome/test/base/in_process_browser_test.h" |
[email protected] | 89dbb177 | 2012-07-17 13:47:25 | [diff] [blame] | 24 | #include "chrome/test/base/testing_profile.h" |
[email protected] | 870983d | 2012-04-09 15:43:25 | [diff] [blame] | 25 | #include "chrome/test/base/ui_test_utils.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 26 | #include "content/public/test/browser_test.h" |
Carlos Caballero | af9c26e | 2021-12-01 19:17:33 | [diff] [blame] | 27 | #include "testing/gmock/include/gmock/gmock.h" |
tfarina | 3b0452d | 2014-12-31 15:20:09 | [diff] [blame] | 28 | #include "ui/gfx/geometry/rect.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | |
[email protected] | 3cc00b6 | 2012-08-06 15:43:48 | [diff] [blame] | 30 | typedef InProcessBrowserTest PreservedWindowPlacement; |
Carlos Caballero | af9c26e | 2021-12-01 19:17:33 | [diff] [blame] | 31 | using ::testing::Optional; |
[email protected] | 3cc00b6 | 2012-08-06 15:43:48 | [diff] [blame] | 32 | |
Jayson Adams | 40e5005 | 2023-07-20 16:30:49 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | const gfx::Rect window_frame = gfx::Rect(20, 40, 600, 600); |
| 36 | |
| 37 | } // namespace |
| 38 | |
[email protected] | 3cc00b6 | 2012-08-06 15:43:48 | [diff] [blame] | 39 | IN_PROC_BROWSER_TEST_F(PreservedWindowPlacement, PRE_Test) { |
Jayson Adams | 40e5005 | 2023-07-20 16:30:49 | [diff] [blame] | 40 | browser()->window()->SetBounds(window_frame); |
[email protected] | 3cc00b6 | 2012-08-06 15:43:48 | [diff] [blame] | 41 | } |
| 42 | |
[email protected] | bee9612 | 2013-01-03 02:40:45 | [diff] [blame] | 43 | // Fails on Chrome OS as the browser thinks it is restarting after a crash, see |
| 44 | // http://crbug.com/168044 |
Yuta Hijikata | 31a359c | 2020-11-27 07:50:26 | [diff] [blame] | 45 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
[email protected] | bee9612 | 2013-01-03 02:40:45 | [diff] [blame] | 46 | #define MAYBE_Test DISABLED_Test |
| 47 | #else |
| 48 | #define MAYBE_Test Test |
| 49 | #endif |
| 50 | IN_PROC_BROWSER_TEST_F(PreservedWindowPlacement, MAYBE_Test) { |
[email protected] | 3cc00b6 | 2012-08-06 15:43:48 | [diff] [blame] | 51 | gfx::Rect bounds = browser()->window()->GetBounds(); |
Jayson Adams | 40e5005 | 2023-07-20 16:30:49 | [diff] [blame] | 52 | gfx::Rect expected_bounds(window_frame); |
[email protected] | cda278d | 2012-10-30 20:31:40 | [diff] [blame] | 53 | ASSERT_EQ(expected_bounds.ToString(), bounds.ToString()); |
[email protected] | 3cc00b6 | 2012-08-06 15:43:48 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | 870983d | 2012-04-09 15:43:25 | [diff] [blame] | 56 | class PreferenceServiceTest : public InProcessBrowserTest { |
[email protected] | 84a059d | 2010-03-31 01:11:59 | [diff] [blame] | 57 | public: |
dcheng | 8f4b862 | 2014-10-23 16:37:48 | [diff] [blame] | 58 | bool SetUpUserDataDirectory() override { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 59 | base::FilePath user_data_directory; |
Avi Drissman | 9098f900 | 2018-05-04 00:11:52 | [diff] [blame] | 60 | base::PathService::Get(chrome::DIR_USER_DATA, &user_data_directory); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | |
rkaplow | f216caf | 2015-03-02 22:37:45 | [diff] [blame] | 62 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | |
rkaplow | f216caf | 2015-03-02 22:37:45 | [diff] [blame] | 73 | EXPECT_TRUE(base::PathExists(original_pref_file_)); |
| 74 | EXPECT_TRUE(base::CopyFile(original_pref_file_, tmp_pref_file_)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | |
Xiaohan Wang | ae7f918 | 2022-01-16 00:37:52 | [diff] [blame] | 76 | #if BUILDFLAG(IS_WIN) |
[email protected] | 5cc06369 | 2009-04-07 23:21:31 | [diff] [blame] | 77 | // Make the copy writable. On POSIX we assume the umask allows files |
| 78 | // we create to be writable. |
rkaplow | f216caf | 2015-03-02 22:37:45 | [diff] [blame] | 79 | EXPECT_TRUE(::SetFileAttributesW(tmp_pref_file_.value().c_str(), |
| 80 | FILE_ATTRIBUTE_NORMAL)); |
[email protected] | 5cc06369 | 2009-04-07 23:21:31 | [diff] [blame] | 81 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | |
[email protected] | 870983d | 2012-04-09 15:43:25 | [diff] [blame] | 85 | protected: |
[email protected] | 56cbcb3a | 2013-12-23 21:24:46 | [diff] [blame] | 86 | base::FilePath original_pref_file_; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 87 | base::FilePath tmp_pref_file_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 88 | }; |
| 89 | |
Xiaohan Wang | ae7f918 | 2022-01-16 00:37:52 | [diff] [blame] | 90 | #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) |
[email protected] | 5cc06369 | 2009-04-07 23:21:31 | [diff] [blame] | 91 | // 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] | 5cc06369 | 2009-04-07 23:21:31 | [diff] [blame] | 95 | // TODO(port): revisit this. |
[email protected] | 870983d | 2012-04-09 15:43:25 | [diff] [blame] | 96 | |
rkaplow | f216caf | 2015-03-02 22:37:45 | [diff] [blame] | 97 | IN_PROC_BROWSER_TEST_F(PreferenceServiceTest, Test) { |
[email protected] | f87919d7 | 2011-02-02 18:46:16 | [diff] [blame] | 98 | // The window should open with the new reference profile, with window |
| 99 | // placement values stored in the user data directory. |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 100 | JSONFileValueDeserializer deserializer(original_pref_file_); |
jam | 3f2d393 | 2017-04-26 20:28:51 | [diff] [blame] | 101 | std::unique_ptr<base::Value> root; |
| 102 | { |
Francois Doray | e6fb2d0 | 2017-10-18 21:29:13 | [diff] [blame] | 103 | base::ScopedAllowBlockingForTesting allow_blocking; |
jam | 3f2d393 | 2017-04-26 20:28:51 | [diff] [blame] | 104 | root = deserializer.Deserialize(NULL, NULL); |
| 105 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 107 | ASSERT_TRUE(root.get()); |
jdoerrie | 1f536b2 | 2017-10-23 17:15:11 | [diff] [blame] | 108 | ASSERT_TRUE(root->is_dict()); |
Muyao Xu | 98f0f68 | 2022-12-29 15:39:33 | [diff] [blame] | 109 | base::Value::Dict& root_dict = root->GetDict(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 110 | |
| 111 | // Retrieve the screen rect for the launched window |
[email protected] | 870983d | 2012-04-09 15:43:25 | [diff] [blame] | 112 | gfx::Rect bounds = browser()->window()->GetRestoredBounds(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 113 | |
| 114 | // Retrieve the expected rect values from "Preferences" |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 115 | std::string kBrowserWindowPlacement(prefs::kBrowserWindowPlacement); |
Muyao Xu | 98f0f68 | 2022-12-29 15:39:33 | [diff] [blame] | 116 | EXPECT_THAT( |
| 117 | root_dict.FindIntByDottedPath(kBrowserWindowPlacement + ".bottom"), |
| 118 | Optional(bounds.y() + bounds.height())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | |
Muyao Xu | 98f0f68 | 2022-12-29 15:39:33 | [diff] [blame] | 120 | EXPECT_THAT(root_dict.FindIntByDottedPath(kBrowserWindowPlacement + ".top"), |
Carlos Caballero | af9c26e | 2021-12-01 19:17:33 | [diff] [blame] | 121 | Optional(bounds.y())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | |
Muyao Xu | 98f0f68 | 2022-12-29 15:39:33 | [diff] [blame] | 123 | EXPECT_THAT(root_dict.FindIntByDottedPath(kBrowserWindowPlacement + ".left"), |
Carlos Caballero | af9c26e | 2021-12-01 19:17:33 | [diff] [blame] | 124 | Optional(bounds.x())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 125 | |
Muyao Xu | 98f0f68 | 2022-12-29 15:39:33 | [diff] [blame] | 126 | EXPECT_THAT(root_dict.FindIntByDottedPath(kBrowserWindowPlacement + ".right"), |
Carlos Caballero | af9c26e | 2021-12-01 19:17:33 | [diff] [blame] | 127 | Optional(bounds.x() + bounds.width())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 128 | |
[email protected] | 8dd404bb | 2009-09-22 19:57:24 | [diff] [blame] | 129 | // Find if launched window is maximized. |
[email protected] | 6a9b53a2 | 2012-10-09 02:43:34 | [diff] [blame] | 130 | bool is_window_maximized = browser()->window()->IsMaximized(); |
Muyao Xu | 98f0f68 | 2022-12-29 15:39:33 | [diff] [blame] | 131 | EXPECT_THAT( |
| 132 | root_dict.FindBoolByDottedPath(kBrowserWindowPlacement + ".maximized"), |
| 133 | Optional(is_window_maximized)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | } |
[email protected] | 5cc06369 | 2009-04-07 23:21:31 | [diff] [blame] | 135 | #endif |