Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / prefs / pref_service_browsertest.cc
blob26f88204cb352170da41a4670b27f34af471c90f
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.
5 #include <string>
7 #include "base/command_line.h"
8 #include "base/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"
27 // On GTK, resizing happens asynchronously and we currently don't have a way to
28 // get called back (it's probably possible, but we don't have that code). Since
29 // the GTK code is going away, not spending more time on this.
30 #if !defined(TOOLKIT_GTK)
32 typedef InProcessBrowserTest PreservedWindowPlacement;
34 IN_PROC_BROWSER_TEST_F(PreservedWindowPlacement, PRE_Test) {
35 browser()->window()->SetBounds(gfx::Rect(20, 30, 400, 500));
38 // Fails on Chrome OS as the browser thinks it is restarting after a crash, see
39 // http://crbug.com/168044
40 #if defined(OS_CHROMEOS)
41 #define MAYBE_Test DISABLED_Test
42 #else
43 #define MAYBE_Test Test
44 #endif
45 IN_PROC_BROWSER_TEST_F(PreservedWindowPlacement, MAYBE_Test) {
46 #if defined(OS_WIN) && defined(USE_ASH)
47 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
48 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
49 return;
50 #endif
52 gfx::Rect bounds = browser()->window()->GetBounds();
53 gfx::Rect expected_bounds(gfx::Rect(20, 30, 400, 500));
54 ASSERT_EQ(expected_bounds.ToString(), bounds.ToString());
57 #endif // defined(TOOLKIT_GTK)
59 class PreferenceServiceTest : public InProcessBrowserTest {
60 public:
61 explicit PreferenceServiceTest(bool new_profile) : new_profile_(new_profile) {
64 virtual bool SetUpUserDataDirectory() OVERRIDE {
65 base::FilePath user_data_directory;
66 PathService::Get(chrome::DIR_USER_DATA, &user_data_directory);
68 if (new_profile_) {
69 original_pref_file_ = ui_test_utils::GetTestFilePath(
70 base::FilePath().AppendASCII("profiles").
71 AppendASCII("window_placement").
72 AppendASCII("Default"),
73 base::FilePath().Append(chrome::kPreferencesFilename));
74 tmp_pref_file_ =
75 user_data_directory.AppendASCII(TestingProfile::kTestUserProfileDir);
76 CHECK(base::CreateDirectory(tmp_pref_file_));
77 tmp_pref_file_ = tmp_pref_file_.Append(chrome::kPreferencesFilename);
78 } else {
79 original_pref_file_ = ui_test_utils::GetTestFilePath(
80 base::FilePath().AppendASCII("profiles").
81 AppendASCII("window_placement"),
82 base::FilePath().Append(chrome::kLocalStateFilename));
83 tmp_pref_file_ = user_data_directory.Append(chrome::kLocalStateFilename);
86 CHECK(base::PathExists(original_pref_file_));
87 // Copy only the Preferences file if |new_profile_|, or Local State if not,
88 // and the rest will be automatically created.
89 CHECK(base::CopyFile(original_pref_file_, tmp_pref_file_));
91 #if defined(OS_WIN)
92 // Make the copy writable. On POSIX we assume the umask allows files
93 // we create to be writable.
94 CHECK(::SetFileAttributesW(tmp_pref_file_.value().c_str(),
95 FILE_ATTRIBUTE_NORMAL));
96 #endif
97 return true;
100 protected:
101 base::FilePath original_pref_file_;
102 base::FilePath tmp_pref_file_;
104 private:
105 bool new_profile_;
108 #if defined(OS_WIN) || defined(OS_MACOSX)
109 // This test verifies that the window position from the prefs file is restored
110 // when the app restores. This doesn't really make sense on Linux, where
111 // the window manager might fight with you over positioning. However, we
112 // might be able to make this work on buildbots.
113 // TODO(port): revisit this.
115 class PreservedWindowPlacementIsLoaded : public PreferenceServiceTest {
116 public:
117 PreservedWindowPlacementIsLoaded() : PreferenceServiceTest(true) {
121 IN_PROC_BROWSER_TEST_F(PreservedWindowPlacementIsLoaded, Test) {
122 #if defined(OS_WIN) && defined(USE_ASH)
123 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
124 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
125 return;
126 #endif
128 // The window should open with the new reference profile, with window
129 // placement values stored in the user data directory.
130 JSONFileValueSerializer deserializer(original_pref_file_);
131 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, NULL));
133 ASSERT_TRUE(root.get());
134 ASSERT_TRUE(root->IsType(base::Value::TYPE_DICTIONARY));
136 base::DictionaryValue* root_dict =
137 static_cast<base::DictionaryValue*>(root.get());
139 // Retrieve the screen rect for the launched window
140 gfx::Rect bounds = browser()->window()->GetRestoredBounds();
142 // Retrieve the expected rect values from "Preferences"
143 int bottom = 0;
144 std::string kBrowserWindowPlacement(prefs::kBrowserWindowPlacement);
145 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".bottom",
146 &bottom));
147 EXPECT_EQ(bottom, bounds.y() + bounds.height());
149 int top = 0;
150 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".top",
151 &top));
152 EXPECT_EQ(top, bounds.y());
154 int left = 0;
155 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".left",
156 &left));
157 EXPECT_EQ(left, bounds.x());
159 int right = 0;
160 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".right",
161 &right));
162 EXPECT_EQ(right, bounds.x() + bounds.width());
164 // Find if launched window is maximized.
165 bool is_window_maximized = browser()->window()->IsMaximized();
166 bool is_maximized = false;
167 EXPECT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + ".maximized",
168 &is_maximized));
169 EXPECT_EQ(is_maximized, is_window_maximized);
171 #endif
173 #if defined(OS_WIN) || defined(OS_MACOSX)
175 class PreservedWindowPlacementIsMigrated : public PreferenceServiceTest {
176 public:
177 PreservedWindowPlacementIsMigrated() : PreferenceServiceTest(false) {
181 IN_PROC_BROWSER_TEST_F(PreservedWindowPlacementIsMigrated, Test) {
182 #if defined(OS_WIN) && defined(USE_ASH)
183 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
184 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
185 return;
186 #endif
188 // The window should open with the old reference profile, with window
189 // placement values stored in Local State.
191 JSONFileValueSerializer deserializer(original_pref_file_);
192 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, NULL));
194 ASSERT_TRUE(root.get());
195 ASSERT_TRUE(root->IsType(base::Value::TYPE_DICTIONARY));
197 // Retrieve the screen rect for the launched window
198 gfx::Rect bounds = browser()->window()->GetRestoredBounds();
200 // Values from old reference profile in Local State should have been
201 // correctly migrated to the user's Preferences -- if so, the window
202 // should be set to values taken from the user's Local State.
203 base::DictionaryValue* root_dict =
204 static_cast<base::DictionaryValue*>(root.get());
206 // Retrieve the expected rect values from User Preferences, where they
207 // should have been migrated from Local State.
208 int bottom = 0;
209 std::string kBrowserWindowPlacement(prefs::kBrowserWindowPlacement);
210 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".bottom",
211 &bottom));
212 EXPECT_EQ(bottom, bounds.y() + bounds.height());
214 int top = 0;
215 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".top",
216 &top));
217 EXPECT_EQ(top, bounds.y());
219 int left = 0;
220 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".left",
221 &left));
222 EXPECT_EQ(left, bounds.x());
224 int right = 0;
225 EXPECT_TRUE(root_dict->GetInteger(kBrowserWindowPlacement + ".right",
226 &right));
227 EXPECT_EQ(right, bounds.x() + bounds.width());
229 // Find if launched window is maximized.
230 bool is_window_maximized = browser()->window()->IsMaximized();
231 bool is_maximized = false;
232 EXPECT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + ".maximized",
233 &is_maximized));
234 EXPECT_EQ(is_maximized, is_window_maximized);
236 #endif