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 "chrome/browser/prefs/session_startup_pref.h"
6 #include "chrome/common/pref_names.h"
7 #include "chrome/test/base/testing_pref_service_syncable.h"
8 #include "components/pref_registry/pref_registry_syncable.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 #if defined(OS_MACOSX)
13 #include "chrome/browser/ui/cocoa/window_restore_utils.h"
16 // Unit tests for SessionStartupPref.
17 class SessionStartupPrefTest
: public testing::Test
{
19 virtual void SetUp() {
20 pref_service_
.reset(new TestingPrefServiceSyncable
);
21 SessionStartupPref::RegisterProfilePrefs(registry());
22 registry()->RegisterBooleanPref(
23 prefs::kHomePageIsNewTabPage
,
25 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
26 // Make the tests independent of the Mac startup pref migration (see
27 // SessionStartupPref::MigrateMacDefaultPrefIfNecessary).
28 registry()->RegisterStringPref(
29 prefs::kProfileCreatedByVersion
,
31 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
34 bool IsUseLastOpenDefault() {
35 // On ChromeOS, the default SessionStartupPref is LAST.
36 #if defined(OS_CHROMEOS)
43 user_prefs::PrefRegistrySyncable
* registry() {
44 return pref_service_
->registry();
47 scoped_ptr
<TestingPrefServiceSyncable
> pref_service_
;
50 TEST_F(SessionStartupPrefTest
, URLListIsFixedUp
) {
51 base::ListValue
* url_pref_list
= new base::ListValue
;
52 url_pref_list
->Set(0, new base::StringValue("google.com"));
53 url_pref_list
->Set(1, new base::StringValue("chromium.org"));
54 pref_service_
->SetUserPref(prefs::kURLsToRestoreOnStartup
, url_pref_list
);
56 SessionStartupPref result
=
57 SessionStartupPref::GetStartupPref(pref_service_
.get());
58 EXPECT_EQ(2u, result
.urls
.size());
59 EXPECT_EQ("http://google.com/", result
.urls
[0].spec());
60 EXPECT_EQ("http://chromium.org/", result
.urls
[1].spec());
63 TEST_F(SessionStartupPrefTest
, URLListManagedOverridesUser
) {
64 base::ListValue
* url_pref_list1
= new base::ListValue
;
65 url_pref_list1
->Set(0, new base::StringValue("chromium.org"));
66 pref_service_
->SetUserPref(prefs::kURLsToRestoreOnStartup
, url_pref_list1
);
68 base::ListValue
* url_pref_list2
= new base::ListValue
;
69 url_pref_list2
->Set(0, new base::StringValue("chromium.org"));
70 url_pref_list2
->Set(1, new base::StringValue("chromium.org"));
71 url_pref_list2
->Set(2, new base::StringValue("chromium.org"));
72 pref_service_
->SetManagedPref(prefs::kURLsToRestoreOnStartup
,
75 SessionStartupPref result
=
76 SessionStartupPref::GetStartupPref(pref_service_
.get());
77 EXPECT_EQ(3u, result
.urls
.size());
79 SessionStartupPref override_test
=
80 SessionStartupPref(SessionStartupPref::URLS
);
81 override_test
.urls
.push_back(GURL("dev.chromium.org"));
82 SessionStartupPref::SetStartupPref(pref_service_
.get(), override_test
);
84 result
= SessionStartupPref::GetStartupPref(pref_service_
.get());
85 EXPECT_EQ(3u, result
.urls
.size());
88 // Checks to make sure that if the user had previously not selected anything
89 // (so that, in effect, the default value "Open the homepage" was selected),
90 // their preferences are migrated on upgrade to m19.
91 TEST_F(SessionStartupPrefTest
, DefaultMigration
) {
92 registry()->RegisterStringPref(
95 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
96 pref_service_
->SetString(prefs::kHomePage
, "http://chromium.org/");
97 pref_service_
->SetBoolean(prefs::kHomePageIsNewTabPage
, false);
99 EXPECT_FALSE(pref_service_
->HasPrefPath(prefs::kRestoreOnStartup
));
101 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
102 pref_service_
.get());
104 if (IsUseLastOpenDefault()) {
105 EXPECT_EQ(SessionStartupPref::LAST
, pref
.type
);
106 EXPECT_EQ(0U, pref
.urls
.size());
108 EXPECT_EQ(SessionStartupPref::URLS
, pref
.type
);
109 EXPECT_EQ(1U, pref
.urls
.size());
110 EXPECT_EQ(GURL("http://chromium.org/"), pref
.urls
[0]);
114 // Checks to make sure that if the user had previously not selected anything
115 // (so that, in effect, the default value "Open the homepage" was selected),
116 // and the NTP is being used for the homepage, their preferences are migrated
117 // to "Open the New Tab Page" on upgrade to M19.
118 TEST_F(SessionStartupPrefTest
, DefaultMigrationHomepageIsNTP
) {
119 registry()->RegisterStringPref(
121 "http://google.com/",
122 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
123 pref_service_
->SetString(prefs::kHomePage
, "http://chromium.org/");
124 pref_service_
->SetBoolean(prefs::kHomePageIsNewTabPage
, true);
126 EXPECT_FALSE(pref_service_
->HasPrefPath(prefs::kRestoreOnStartup
));
128 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
129 pref_service_
.get());
131 if (IsUseLastOpenDefault())
132 EXPECT_EQ(SessionStartupPref::LAST
, pref
.type
);
134 EXPECT_EQ(SessionStartupPref::DEFAULT
, pref
.type
);
136 // The "URLs to restore on startup" shouldn't get migrated.
137 EXPECT_EQ(0U, pref
.urls
.size());
140 // Checks to make sure that if the user had previously selected "Open the
141 // "homepage", their preferences are migrated on upgrade to M19.
142 TEST_F(SessionStartupPrefTest
, HomePageMigration
) {
143 registry()->RegisterStringPref(
145 "http://google.com/",
146 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
148 // By design, it's impossible to set the 'restore on startup' pref to 0
149 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it
150 // using the pref service directly.
151 pref_service_
->SetInteger(prefs::kRestoreOnStartup
, /*kPrefValueHomePage*/ 0);
152 pref_service_
->SetString(prefs::kHomePage
, "http://chromium.org/");
153 pref_service_
->SetBoolean(prefs::kHomePageIsNewTabPage
, false);
155 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
156 pref_service_
.get());
158 EXPECT_EQ(SessionStartupPref::URLS
, pref
.type
);
159 EXPECT_EQ(1U, pref
.urls
.size());
160 EXPECT_EQ(GURL("http://chromium.org/"), pref
.urls
[0]);
163 // Checks to make sure that if the user had previously selected "Open the
164 // "homepage", and the NTP is being used for the homepage, their preferences
165 // are migrated on upgrade to M19.
166 TEST_F(SessionStartupPrefTest
, HomePageMigrationHomepageIsNTP
) {
167 registry()->RegisterStringPref(
169 "http://google.com/",
170 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
172 // By design, it's impossible to set the 'restore on startup' pref to 0
173 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it
174 // using the pref service directly.
175 pref_service_
->SetInteger(prefs::kRestoreOnStartup
, /*kPrefValueHomePage*/ 0);
176 pref_service_
->SetString(prefs::kHomePage
, "http://chromium.org/");
177 pref_service_
->SetBoolean(prefs::kHomePageIsNewTabPage
, true);
179 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
180 pref_service_
.get());
182 EXPECT_EQ(SessionStartupPref::DEFAULT
, pref
.type
);
185 #if defined(OS_MACOSX)
186 // See SessionStartupPref::MigrateMacDefaultPrefIfNecessary.
187 TEST_F(SessionStartupPrefTest
, MacDefaultStartupPrefMigration
) {
188 if (!restore_utils::IsWindowRestoreEnabled())
191 // Use an old profile.
192 pref_service_
->SetString(prefs::kProfileCreatedByVersion
, "19.0.0.0");
193 ASSERT_TRUE(SessionStartupPref::TypeIsDefault(pref_service_
.get()));
195 // Trigger the migration.
196 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
197 pref_service_
.get());
199 // The pref is now explicit.
200 EXPECT_EQ(SessionStartupPref::LAST
, pref
.type
);
201 EXPECT_FALSE(SessionStartupPref::TypeIsDefault(pref_service_
.get()));