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. 4 5#include <string> 6 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/rect.h" 26 27typedef InProcessBrowserTest PreservedWindowPlacement; 28 29IN_PROC_BROWSER_TEST_F(PreservedWindowPlacement, PRE_Test) { 30 browser()->window()->SetBounds(gfx::Rect(20, 30, 400, 500)); 31} 32 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 37#else 38#define MAYBE_Test Test 39#endif 40IN_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 (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests)) 44 return; 45#endif 46 47 gfx::Rect bounds = browser()->window()->GetBounds(); 48 gfx::Rect expected_bounds(gfx::Rect(20, 30, 400, 500)); 49 ASSERT_EQ(expected_bounds.ToString(), bounds.ToString()); 50} 51 52class PreferenceServiceTest : public InProcessBrowserTest { 53 public: 54 explicit PreferenceServiceTest(bool new_profile) : new_profile_(new_profile) { 55 } 56 57 virtual bool SetUpUserDataDirectory() OVERRIDE { 58 base::FilePath user_data_directory; 59 PathService::Get(chrome::DIR_USER_DATA, &user_data_directory); 60 61 if (new_profile_) { 62 original_pref_file_ = ui_test_utils::GetTestFilePath( 63 base::FilePath().AppendASCII("profiles"). 64 AppendASCII("window_placement"). 65 AppendASCII("Default"), 66 base::FilePath().Append(chrome::kPreferencesFilename)); 67 tmp_pref_file_ = 68 user_data_directory.AppendASCII(TestingProfile::kTestUserProfileDir); 69 CHECK(base::CreateDirectory(tmp_pref_file_)); 70 tmp_pref_file_ = tmp_pref_file_.Append(chrome::kPreferencesFilename); 71 } else { 72 original_pref_file_ = ui_test_utils::GetTestFilePath( 73 base::FilePath().AppendASCII("profiles"). 74 AppendASCII("window_placement"), 75 base::FilePath().Append(chrome::kLocalStateFilename)); 76 tmp_pref_file_ = user_data_directory.Append(chrome::kLocalStateFilename); 77 } 78 79 CHECK(base::PathExists(original_pref_file_)); 80 // Copy only the Preferences file if |new_profile_|, or Local State if not, 81 // and the rest will be automatically created. 82 CHECK(base::CopyFile(original_pref_file_, tmp_pref_file_)); 83 84#if defined(OS_WIN) 85 // Make the copy writable. On POSIX we assume the umask allows files 86 // we create to be writable. 87 CHECK(::SetFileAttributesW(tmp_pref_file_.value().c_str(), 88 FILE_ATTRIBUTE_NORMAL)); 89#endif 90 return true; 91 } 92 93 protected: 94 base::FilePath original_pref_file_; 95 base::FilePath tmp_pref_file_; 96 97 private: 98 bool new_profile_; 99}; 100 101#if defined(OS_WIN) || defined(OS_MACOSX) 102// This test verifies that the window position from the prefs file is restored 103// when the app restores. This doesn't really make sense on Linux, where 104// the window manager might fight with you over positioning. However, we 105// might be able to make this work on buildbots. 106// TODO(port): revisit this. 107 108class PreservedWindowPlacementIsLoaded : public PreferenceServiceTest { 109 public: 110 PreservedWindowPlacementIsLoaded() : PreferenceServiceTest(true) { 111 } 112}; 113 114IN_PROC_BROWSER_TEST_F(PreservedWindowPlacementIsLoaded, Test) { 115#if defined(OS_WIN) && defined(USE_ASH) 116 // Disable this test in Metro+Ash for now (http://crbug.com/262796). 117 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests)) 118 return; 119#endif 120 121 // The window should open with the new reference profile, with window 122 // placement values stored in the user data directory. 123 JSONFileValueSerializer deserializer(original_pref_file_); 124 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, NULL)); 125 126 ASSERT_TRUE(root.get()); 127 ASSERT_TRUE(root->IsType(base::Value::TYPE_DICTIONARY)); 128 129 base::DictionaryValue* root_dict = 130 static_cast<base::DictionaryValue*>(root.get()); 131 132 // Retrieve the screen rect for the launched window 133 gfx::Rect bounds = browser()->window()->GetRestoredBounds(); 134 135 // Retrieve the expected rect values from "Preferences" 136 int bottom = 0; 137 std::string kBrowserWindowPlacement(prefs::kBrowserWindowPlacement); 138 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".bottom", 139 &bottom)); 140 EXPECT_EQ(bottom, bounds.y() + bounds.height()); 141 142 int top = 0; 143 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".top", 144 &top)); 145 EXPECT_EQ(top, bounds.y()); 146 147 int left = 0; 148 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".left", 149 &left)); 150 EXPECT_EQ(left, bounds.x()); 151 152 int right = 0; 153 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".right", 154 &right)); 155 EXPECT_EQ(right, bounds.x() + bounds.width()); 156 157 // Find if launched window is maximized. 158 bool is_window_maximized = browser()->window()->IsMaximized(); 159 bool is_maximized = false; 160 EXPECT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + ".maximized", 161 &is_maximized)); 162 EXPECT_EQ(is_maximized, is_window_maximized); 163} 164#endif 165 166#if defined(OS_WIN) || defined(OS_MACOSX) 167 168class PreservedWindowPlacementIsMigrated : public PreferenceServiceTest { 169 public: 170 PreservedWindowPlacementIsMigrated() : PreferenceServiceTest(false) { 171 } 172}; 173 174IN_PROC_BROWSER_TEST_F(PreservedWindowPlacementIsMigrated, Test) { 175#if defined(OS_WIN) && defined(USE_ASH) 176 // Disable this test in Metro+Ash for now (http://crbug.com/262796). 177 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests)) 178 return; 179#endif 180 181 // The window should open with the old reference profile, with window 182 // placement values stored in Local State. 183 184 JSONFileValueSerializer deserializer(original_pref_file_); 185 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, NULL)); 186 187 ASSERT_TRUE(root.get()); 188 ASSERT_TRUE(root->IsType(base::Value::TYPE_DICTIONARY)); 189 190 // Retrieve the screen rect for the launched window 191 gfx::Rect bounds = browser()->window()->GetRestoredBounds(); 192 193 // Values from old reference profile in Local State should have been 194 // correctly migrated to the user's Preferences -- if so, the window 195 // should be set to values taken from the user's Local State. 196 base::DictionaryValue* root_dict = 197 static_cast<base::DictionaryValue*>(root.get()); 198 199 // Retrieve the expected rect values from User Preferences, where they 200 // should have been migrated from Local State. 201 int bottom = 0; 202 std::string kBrowserWindowPlacement(prefs::kBrowserWindowPlacement); 203 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".bottom", 204 &bottom)); 205 EXPECT_EQ(bottom, bounds.y() + bounds.height()); 206 207 int top = 0; 208 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".top", 209 &top)); 210 EXPECT_EQ(top, bounds.y()); 211 212 int left = 0; 213 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".left", 214 &left)); 215 EXPECT_EQ(left, bounds.x()); 216 217 int right = 0; 218 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".right", 219 &right)); 220 EXPECT_EQ(right, bounds.x() + bounds.width()); 221 222 // Find if launched window is maximized. 223 bool is_window_maximized = browser()->window()->IsMaximized(); 224 bool is_maximized = false; 225 EXPECT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + ".maximized", 226 &is_maximized)); 227 EXPECT_EQ(is_maximized, is_window_maximized); 228} 229#endif 230