| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "build/build_config.h" |
| 11 #include "chrome/common/chrome_constants.h" | 12 #include "chrome/common/chrome_constants.h" |
| 12 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/json_value_serializer.h" | 14 #include "chrome/common/json_value_serializer.h" |
| 14 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 15 #include "chrome/test/automation/browser_proxy.h" | 16 #include "chrome/test/automation/browser_proxy.h" |
| 16 #include "chrome/test/automation/window_proxy.h" | 17 #include "chrome/test/automation/window_proxy.h" |
| 17 #include "chrome/test/ui/ui_test.h" | 18 #include "chrome/test/ui/ui_test.h" |
| 18 | 19 |
| 19 class PreferenceServiceTest : public UITest { | 20 class PreferenceServiceTest : public UITest { |
| 20 public: | 21 public: |
| 21 void SetUp() { | 22 void SetUp() { |
| 22 PathService::Get(base::DIR_TEMP, &tmp_profile_); | 23 PathService::Get(base::DIR_TEMP, &tmp_profile_); |
| 23 file_util::AppendToPath(&tmp_profile_, L"tmp_profile"); | 24 tmp_profile_ = tmp_profile_.AppendASCII("tmp_profile"); |
| 24 | 25 |
| 25 // Create a fresh, empty copy of this directory. | 26 // Create a fresh, empty copy of this directory. |
| 26 file_util::Delete(tmp_profile_, true); | 27 file_util::Delete(tmp_profile_, true); |
| 27 ::CreateDirectory(tmp_profile_.c_str(), NULL); | 28 file_util::CreateDirectory(tmp_profile_); |
| 28 | 29 |
| 29 std::wstring reference_pref_file(test_data_directory_); | 30 FilePath reference_pref_file = |
| 30 file_util::AppendToPath(&reference_pref_file, L"profiles"); | 31 FilePath::FromWStringHack(test_data_directory_) |
| 31 file_util::AppendToPath(&reference_pref_file, L"window_placement"); | 32 .AppendASCII("profiles") |
| 32 file_util::AppendToPath(&reference_pref_file, chrome::kLocalStateFilename); | 33 .AppendASCII("window_placement") |
| 34 .Append(chrome::kLocalStateFilename); |
| 33 | 35 |
| 34 tmp_pref_file_ = tmp_profile_; | 36 tmp_pref_file_ = tmp_profile_.Append(chrome::kLocalStateFilename); |
| 35 file_util::AppendToPath(&tmp_pref_file_, chrome::kLocalStateFilename); | |
| 36 | 37 |
| 37 ASSERT_TRUE(file_util::PathExists(reference_pref_file)); | 38 ASSERT_TRUE(file_util::PathExists(reference_pref_file)); |
| 38 | 39 |
| 39 // Copy only the Local State file, the rest will be automatically created | 40 // Copy only the Local State file, the rest will be automatically created |
| 40 ASSERT_TRUE(::CopyFileW(reference_pref_file.c_str(), tmp_pref_file_.c_str(), | 41 ASSERT_TRUE(file_util::CopyFile(reference_pref_file, tmp_pref_file_)); |
| 41 TRUE)); | |
| 42 | 42 |
| 43 // Make the copy writable | 43 #if defined(OS_WIN) |
| 44 ASSERT_TRUE(::SetFileAttributesW(tmp_pref_file_.c_str(), | 44 // Make the copy writable. On POSIX we assume the umask allows files |
| 45 // we create to be writable. |
| 46 ASSERT_TRUE(::SetFileAttributesW(tmp_pref_file_.value().c_str(), |
| 45 FILE_ATTRIBUTE_NORMAL)); | 47 FILE_ATTRIBUTE_NORMAL)); |
| 48 #endif |
| 46 | 49 |
| 47 launch_arguments_.AppendSwitchWithValue(switches::kUserDataDir, | 50 launch_arguments_.AppendSwitchWithValue(switches::kUserDataDir, |
| 48 tmp_profile_); | 51 tmp_profile_.ToWStringHack()); |
| 49 } | 52 } |
| 50 | 53 |
| 51 bool LaunchAppWithProfile() { | 54 bool LaunchAppWithProfile() { |
| 52 if (!file_util::PathExists(tmp_pref_file_)) | 55 if (!file_util::PathExists(tmp_pref_file_)) |
| 53 return false; | 56 return false; |
| 54 UITest::SetUp(); | 57 UITest::SetUp(); |
| 55 return true; | 58 return true; |
| 56 } | 59 } |
| 57 | 60 |
| 58 void TearDown() { | 61 void TearDown() { |
| 59 UITest::TearDown(); | 62 UITest::TearDown(); |
| 60 | 63 |
| 61 EXPECT_TRUE(DieFileDie(tmp_profile_, true)); | 64 EXPECT_TRUE(DieFileDie(tmp_profile_, true)); |
| 62 } | 65 } |
| 63 | 66 |
| 64 public: | 67 public: |
| 65 std::wstring tmp_pref_file_; | 68 FilePath tmp_pref_file_; |
| 66 std::wstring tmp_profile_; | 69 FilePath tmp_profile_; |
| 67 }; | 70 }; |
| 68 | 71 |
| 72 #if defined(OS_WIN) |
| 73 // This test verifies that the window position from the prefs file is restored |
| 74 // when the app restores. This doesn't really make sense on Linux, where |
| 75 // the window manager might fight with you over positioning. However, we |
| 76 // might be able to make this work on buildbots. |
| 77 // Also, not sure what should happen on the mac. In any case, the code below |
| 78 // (minus the Windows bits) compiles fine on my Linux box now. |
| 79 // TODO(port): revisit this. |
| 69 TEST_F(PreferenceServiceTest, PreservedWindowPlacementIsLoaded) { | 80 TEST_F(PreferenceServiceTest, PreservedWindowPlacementIsLoaded) { |
| 70 // The window should open with the reference profile | 81 // The window should open with the reference profile |
| 71 ASSERT_TRUE(LaunchAppWithProfile()); | 82 ASSERT_TRUE(LaunchAppWithProfile()); |
| 72 | 83 |
| 73 ASSERT_TRUE(file_util::PathExists(tmp_pref_file_)); | 84 ASSERT_TRUE(file_util::PathExists(tmp_pref_file_)); |
| 74 | 85 |
| 75 JSONFileValueSerializer deserializer = | 86 JSONFileValueSerializer deserializer(tmp_pref_file_); |
| 76 FilePath::FromWStringHack(tmp_pref_file_); | |
| 77 scoped_ptr<Value> root(deserializer.Deserialize(NULL)); | 87 scoped_ptr<Value> root(deserializer.Deserialize(NULL)); |
| 78 | 88 |
| 79 ASSERT_TRUE(root.get()); | 89 ASSERT_TRUE(root.get()); |
| 80 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 90 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
| 81 | 91 |
| 82 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); | 92 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); |
| 83 | 93 |
| 84 // Retrieve the screen rect for the launched window | 94 // Retrieve the screen rect for the launched window |
| 85 scoped_ptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); | 95 scoped_ptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); |
| 86 ASSERT_TRUE(browser.get()); | 96 ASSERT_TRUE(browser.get()); |
| 87 scoped_ptr<WindowProxy> window(browser->GetWindow()); | 97 scoped_ptr<WindowProxy> window(browser->GetWindow()); |
| 98 |
| 88 HWND hWnd; | 99 HWND hWnd; |
| 89 ASSERT_TRUE(window->GetHWND(&hWnd)); | 100 ASSERT_TRUE(window->GetHWND(&hWnd)); |
| 90 | 101 |
| 91 WINDOWPLACEMENT window_placement; | 102 WINDOWPLACEMENT window_placement; |
| 92 ASSERT_TRUE(GetWindowPlacement(hWnd, &window_placement)); | 103 ASSERT_TRUE(GetWindowPlacement(hWnd, &window_placement)); |
| 93 | 104 |
| 94 // Retrieve the expected rect values from "Preferences" | 105 // Retrieve the expected rect values from "Preferences" |
| 95 int bottom = 0; | 106 int bottom = 0; |
| 96 std::wstring kBrowserWindowPlacement(prefs::kBrowserWindowPlacement); | 107 std::wstring kBrowserWindowPlacement(prefs::kBrowserWindowPlacement); |
| 97 ASSERT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + L".bottom", | 108 ASSERT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + L".bottom", |
| (...skipping 16 matching lines...) Expand all Loading... |
| 114 ASSERT_EQ(right, window_placement.rcNormalPosition.right); | 125 ASSERT_EQ(right, window_placement.rcNormalPosition.right); |
| 115 | 126 |
| 116 // Find if launched window is maximized | 127 // Find if launched window is maximized |
| 117 bool is_window_maximized = (window_placement.showCmd == SW_MAXIMIZE); | 128 bool is_window_maximized = (window_placement.showCmd == SW_MAXIMIZE); |
| 118 | 129 |
| 119 bool is_maximized = false; | 130 bool is_maximized = false; |
| 120 ASSERT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + L".maximized", | 131 ASSERT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + L".maximized", |
| 121 &is_maximized)); | 132 &is_maximized)); |
| 122 ASSERT_EQ(is_maximized, is_window_maximized); | 133 ASSERT_EQ(is_maximized, is_window_maximized); |
| 123 } | 134 } |
| 135 #endif |
| OLD | NEW |