blob: 207cbcf7971a6a9dacec8b4a9d50f748e9c04580 [file] [log] [blame]
[email protected]330d57222012-03-06 20:56:471// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]f9dfa842010-10-07 15:47:042// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/prefs/session_startup_pref.h"
6#include "chrome/common/pref_names.h"
[email protected]8ad3636e2011-08-01 22:31:407#include "chrome/test/base/testing_pref_service.h"
[email protected]f9dfa842010-10-07 15:47:048#include "testing/gmock/include/gmock/gmock.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
[email protected]cab6acf2012-05-10 13:49:0111#if defined(OS_MACOSX)
12#include "chrome/browser/ui/cocoa/window_restore_utils.h"
13#endif
14
[email protected]f9dfa842010-10-07 15:47:0415// Unit tests for SessionStartupPref.
16class SessionStartupPrefTest : public testing::Test {
17 public:
18 virtual void SetUp() {
19 pref_service_.reset(new TestingPrefService);
20 SessionStartupPref::RegisterUserPrefs(pref_service_.get());
[email protected]1a5a31f2012-04-26 20:21:3421 pref_service_->RegisterBooleanPref(prefs::kHomePageIsNewTabPage, true);
[email protected]9f9ab272012-06-29 12:12:4322 // Make the tests independent of the Mac startup pref migration (see
23 // SessionStartupPref::MigrateMacDefaultPrefIfNecessary).
24 pref_service_->RegisterStringPref(prefs::kProfileCreatedByVersion,
25 "22.0.0.0");
[email protected]f9dfa842010-10-07 15:47:0426 }
27
[email protected]cab6acf2012-05-10 13:49:0128 bool IsUseLastOpenDefault() {
[email protected]9f9ab272012-06-29 12:12:4329 // On ChromeOS, the default SessionStartupPref is LAST.
[email protected]cab6acf2012-05-10 13:49:0130#if defined(OS_CHROMEOS)
31 return true;
[email protected]cab6acf2012-05-10 13:49:0132#else
33 return false;
34#endif
35 }
36
[email protected]f9dfa842010-10-07 15:47:0437 scoped_ptr<TestingPrefService> pref_service_;
38};
39
40TEST_F(SessionStartupPrefTest, URLListIsFixedUp) {
41 ListValue* url_pref_list = new ListValue;
42 url_pref_list->Set(0, Value::CreateStringValue("google.com"));
43 url_pref_list->Set(1, Value::CreateStringValue("chromium.org"));
44 pref_service_->SetUserPref(prefs::kURLsToRestoreOnStartup, url_pref_list);
45
46 SessionStartupPref result =
47 SessionStartupPref::GetStartupPref(pref_service_.get());
48 EXPECT_EQ(2u, result.urls.size());
49 EXPECT_EQ("http://google.com/", result.urls[0].spec());
50 EXPECT_EQ("http://chromium.org/", result.urls[1].spec());
51}
52
53TEST_F(SessionStartupPrefTest, URLListManagedOverridesUser) {
54 ListValue* url_pref_list1 = new ListValue;
55 url_pref_list1->Set(0, Value::CreateStringValue("chromium.org"));
56 pref_service_->SetUserPref(prefs::kURLsToRestoreOnStartup, url_pref_list1);
57
58 ListValue* url_pref_list2 = new ListValue;
59 url_pref_list2->Set(0, Value::CreateStringValue("chromium.org"));
60 url_pref_list2->Set(1, Value::CreateStringValue("chromium.org"));
61 url_pref_list2->Set(2, Value::CreateStringValue("chromium.org"));
62 pref_service_->SetManagedPref(prefs::kURLsToRestoreOnStartup,
63 url_pref_list2);
64
65 SessionStartupPref result =
66 SessionStartupPref::GetStartupPref(pref_service_.get());
67 EXPECT_EQ(3u, result.urls.size());
68
69 SessionStartupPref override_test =
[email protected]1c51cf72011-02-25 13:39:4170 SessionStartupPref(SessionStartupPref::URLS);
[email protected]f9dfa842010-10-07 15:47:0471 override_test.urls.push_back(GURL("dev.chromium.org"));
72 SessionStartupPref::SetStartupPref(pref_service_.get(), override_test);
73
74 result = SessionStartupPref::GetStartupPref(pref_service_.get());
75 EXPECT_EQ(3u, result.urls.size());
76}
[email protected]330d57222012-03-06 20:56:4777
[email protected]1a5a31f2012-04-26 20:21:3478// Checks to make sure that if the user had previously not selected anything
79// (so that, in effect, the default value "Open the homepage" was selected),
80// their preferences are migrated on upgrade to m19.
81TEST_F(SessionStartupPrefTest, DefaultMigration) {
82 pref_service_->RegisterStringPref(prefs::kHomePage, "http://google.com/");
83 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/");
84 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, false);
85
86 EXPECT_FALSE(pref_service_->HasPrefPath(prefs::kRestoreOnStartup));
87
88 SessionStartupPref pref = SessionStartupPref::GetStartupPref(
89 pref_service_.get());
90
[email protected]cab6acf2012-05-10 13:49:0191 if (IsUseLastOpenDefault()) {
92 EXPECT_EQ(SessionStartupPref::LAST, pref.type);
93 EXPECT_EQ(0U, pref.urls.size());
94 } else {
95 EXPECT_EQ(SessionStartupPref::URLS, pref.type);
96 EXPECT_EQ(1U, pref.urls.size());
97 EXPECT_EQ(GURL("http://chromium.org/"), pref.urls[0]);
98 }
[email protected]1a5a31f2012-04-26 20:21:3499}
100
101// Checks to make sure that if the user had previously not selected anything
102// (so that, in effect, the default value "Open the homepage" was selected),
103// and the NTP is being used for the homepage, their preferences are migrated
104// to "Open the New Tab Page" on upgrade to M19.
105TEST_F(SessionStartupPrefTest, DefaultMigrationHomepageIsNTP) {
106 pref_service_->RegisterStringPref(prefs::kHomePage, "http://google.com/");
107 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/");
108 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, true);
109
110 EXPECT_FALSE(pref_service_->HasPrefPath(prefs::kRestoreOnStartup));
111
112 SessionStartupPref pref = SessionStartupPref::GetStartupPref(
113 pref_service_.get());
114
[email protected]cab6acf2012-05-10 13:49:01115 if (IsUseLastOpenDefault())
116 EXPECT_EQ(SessionStartupPref::LAST, pref.type);
117 else
118 EXPECT_EQ(SessionStartupPref::DEFAULT, pref.type);
[email protected]1a5a31f2012-04-26 20:21:34119
120 // The "URLs to restore on startup" shouldn't get migrated.
121 EXPECT_EQ(0U, pref.urls.size());
122}
123
124// Checks to make sure that if the user had previously selected "Open the
125// "homepage", their preferences are migrated on upgrade to M19.
[email protected]330d57222012-03-06 20:56:47126TEST_F(SessionStartupPrefTest, HomePageMigration) {
127 pref_service_->RegisterStringPref(prefs::kHomePage, "http://google.com/");
128
129 // By design, it's impossible to set the 'restore on startup' pref to 0
130 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it
131 // using the pref service directly.
132 pref_service_->SetInteger(prefs::kRestoreOnStartup, /*kPrefValueHomePage*/ 0);
133 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/");
[email protected]1a5a31f2012-04-26 20:21:34134 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, false);
[email protected]330d57222012-03-06 20:56:47135
136 SessionStartupPref pref = SessionStartupPref::GetStartupPref(
137 pref_service_.get());
[email protected]1a5a31f2012-04-26 20:21:34138
[email protected]330d57222012-03-06 20:56:47139 EXPECT_EQ(SessionStartupPref::URLS, pref.type);
140 EXPECT_EQ(1U, pref.urls.size());
141 EXPECT_EQ(GURL("http://chromium.org/"), pref.urls[0]);
142}
[email protected]1a5a31f2012-04-26 20:21:34143
144// Checks to make sure that if the user had previously selected "Open the
145// "homepage", and the NTP is being used for the homepage, their preferences
146// are migrated on upgrade to M19.
147TEST_F(SessionStartupPrefTest, HomePageMigrationHomepageIsNTP) {
148 pref_service_->RegisterStringPref(prefs::kHomePage, "http://google.com/");
149
150 // By design, it's impossible to set the 'restore on startup' pref to 0
151 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it
152 // using the pref service directly.
153 pref_service_->SetInteger(prefs::kRestoreOnStartup, /*kPrefValueHomePage*/ 0);
154 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/");
155 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, true);
156
157 SessionStartupPref pref = SessionStartupPref::GetStartupPref(
158 pref_service_.get());
159
160 EXPECT_EQ(SessionStartupPref::DEFAULT, pref.type);
161}
[email protected]a379a1a2012-07-03 17:26:11162
163#if defined(OS_MACOSX)
164// See SessionStartupPref::MigrateMacDefaultPrefIfNecessary.
165TEST_F(SessionStartupPrefTest, MacDefaultStartupPrefMigration) {
166 if (!restore_utils::IsWindowRestoreEnabled())
167 return;
168
169 // Use an old profile.
170 pref_service_->SetString(prefs::kProfileCreatedByVersion, "19.0.0.0");
171 ASSERT_TRUE(SessionStartupPref::TypeIsDefault(pref_service_.get()));
172
173 // Trigger the migration.
174 SessionStartupPref pref = SessionStartupPref::GetStartupPref(
175 pref_service_.get());
176
177 // The pref is now explicit.
178 EXPECT_EQ(SessionStartupPref::LAST, pref.type);
179 EXPECT_FALSE(SessionStartupPref::TypeIsDefault(pref_service_.get()));
180}
181#endif