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 void SetUp() override
{
20 pref_service_
.reset(new TestingPrefServiceSyncable
);
21 SessionStartupPref::RegisterProfilePrefs(registry());
22 registry()->RegisterBooleanPref(prefs::kHomePageIsNewTabPage
, true);
23 // Make the tests independent of the Mac startup pref migration (see
24 // SessionStartupPref::MigrateMacDefaultPrefIfNecessary).
25 registry()->RegisterStringPref(prefs::kProfileCreatedByVersion
, "22.0.0.0");
28 bool IsUseLastOpenDefault() {
29 // On ChromeOS, the default SessionStartupPref is LAST.
30 #if defined(OS_CHROMEOS)
37 user_prefs::PrefRegistrySyncable
* registry() {
38 return pref_service_
->registry();
41 scoped_ptr
<TestingPrefServiceSyncable
> pref_service_
;
44 TEST_F(SessionStartupPrefTest
, URLListIsFixedUp
) {
45 base::ListValue
* url_pref_list
= new base::ListValue
;
46 url_pref_list
->Set(0, new base::StringValue("google.com"));
47 url_pref_list
->Set(1, new base::StringValue("chromium.org"));
48 pref_service_
->SetUserPref(prefs::kURLsToRestoreOnStartup
, url_pref_list
);
50 SessionStartupPref result
=
51 SessionStartupPref::GetStartupPref(pref_service_
.get());
52 EXPECT_EQ(2u, result
.urls
.size());
53 EXPECT_EQ("http://google.com/", result
.urls
[0].spec());
54 EXPECT_EQ("http://chromium.org/", result
.urls
[1].spec());
57 TEST_F(SessionStartupPrefTest
, URLListManagedOverridesUser
) {
58 base::ListValue
* url_pref_list1
= new base::ListValue
;
59 url_pref_list1
->Set(0, new base::StringValue("chromium.org"));
60 pref_service_
->SetUserPref(prefs::kURLsToRestoreOnStartup
, url_pref_list1
);
62 base::ListValue
* url_pref_list2
= new base::ListValue
;
63 url_pref_list2
->Set(0, new base::StringValue("chromium.org"));
64 url_pref_list2
->Set(1, new base::StringValue("chromium.org"));
65 url_pref_list2
->Set(2, new base::StringValue("chromium.org"));
66 pref_service_
->SetManagedPref(prefs::kURLsToRestoreOnStartup
,
69 SessionStartupPref result
=
70 SessionStartupPref::GetStartupPref(pref_service_
.get());
71 EXPECT_EQ(3u, result
.urls
.size());
73 SessionStartupPref override_test
=
74 SessionStartupPref(SessionStartupPref::URLS
);
75 override_test
.urls
.push_back(GURL("dev.chromium.org"));
76 SessionStartupPref::SetStartupPref(pref_service_
.get(), override_test
);
78 result
= SessionStartupPref::GetStartupPref(pref_service_
.get());
79 EXPECT_EQ(3u, result
.urls
.size());
82 // Checks to make sure that if the user had previously not selected anything
83 // (so that, in effect, the default value "Open the homepage" was selected),
84 // their preferences are migrated on upgrade to m19.
85 TEST_F(SessionStartupPrefTest
, DefaultMigration
) {
86 registry()->RegisterStringPref(prefs::kHomePage
, "http://google.com/");
87 pref_service_
->SetString(prefs::kHomePage
, "http://chromium.org/");
88 pref_service_
->SetBoolean(prefs::kHomePageIsNewTabPage
, false);
90 EXPECT_FALSE(pref_service_
->HasPrefPath(prefs::kRestoreOnStartup
));
92 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
95 if (IsUseLastOpenDefault()) {
96 EXPECT_EQ(SessionStartupPref::LAST
, pref
.type
);
97 EXPECT_EQ(0U, pref
.urls
.size());
99 EXPECT_EQ(SessionStartupPref::URLS
, pref
.type
);
100 EXPECT_EQ(1U, pref
.urls
.size());
101 EXPECT_EQ(GURL("http://chromium.org/"), pref
.urls
[0]);
105 // Checks to make sure that if the user had previously not selected anything
106 // (so that, in effect, the default value "Open the homepage" was selected),
107 // and the NTP is being used for the homepage, their preferences are migrated
108 // to "Open the New Tab Page" on upgrade to M19.
109 TEST_F(SessionStartupPrefTest
, DefaultMigrationHomepageIsNTP
) {
110 registry()->RegisterStringPref(prefs::kHomePage
, "http://google.com/");
111 pref_service_
->SetString(prefs::kHomePage
, "http://chromium.org/");
112 pref_service_
->SetBoolean(prefs::kHomePageIsNewTabPage
, true);
114 EXPECT_FALSE(pref_service_
->HasPrefPath(prefs::kRestoreOnStartup
));
116 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
117 pref_service_
.get());
119 if (IsUseLastOpenDefault())
120 EXPECT_EQ(SessionStartupPref::LAST
, pref
.type
);
122 EXPECT_EQ(SessionStartupPref::DEFAULT
, pref
.type
);
124 // The "URLs to restore on startup" shouldn't get migrated.
125 EXPECT_EQ(0U, pref
.urls
.size());
128 // Checks to make sure that if the user had previously selected "Open the
129 // "homepage", their preferences are migrated on upgrade to M19.
130 TEST_F(SessionStartupPrefTest
, HomePageMigration
) {
131 registry()->RegisterStringPref(prefs::kHomePage
, "http://google.com/");
133 // By design, it's impossible to set the 'restore on startup' pref to 0
134 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it
135 // using the pref service directly.
136 pref_service_
->SetInteger(prefs::kRestoreOnStartup
, /*kPrefValueHomePage*/ 0);
137 pref_service_
->SetString(prefs::kHomePage
, "http://chromium.org/");
138 pref_service_
->SetBoolean(prefs::kHomePageIsNewTabPage
, false);
140 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
141 pref_service_
.get());
143 EXPECT_EQ(SessionStartupPref::URLS
, pref
.type
);
144 EXPECT_EQ(1U, pref
.urls
.size());
145 EXPECT_EQ(GURL("http://chromium.org/"), pref
.urls
[0]);
148 // Checks to make sure that if the user had previously selected "Open the
149 // "homepage", and the NTP is being used for the homepage, their preferences
150 // are migrated on upgrade to M19.
151 TEST_F(SessionStartupPrefTest
, HomePageMigrationHomepageIsNTP
) {
152 registry()->RegisterStringPref(prefs::kHomePage
, "http://google.com/");
154 // By design, it's impossible to set the 'restore on startup' pref to 0
155 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it
156 // using the pref service directly.
157 pref_service_
->SetInteger(prefs::kRestoreOnStartup
, /*kPrefValueHomePage*/ 0);
158 pref_service_
->SetString(prefs::kHomePage
, "http://chromium.org/");
159 pref_service_
->SetBoolean(prefs::kHomePageIsNewTabPage
, true);
161 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
162 pref_service_
.get());
164 EXPECT_EQ(SessionStartupPref::DEFAULT
, pref
.type
);
167 #if defined(OS_MACOSX)
168 // See SessionStartupPref::MigrateMacDefaultPrefIfNecessary.
169 TEST_F(SessionStartupPrefTest
, MacDefaultStartupPrefMigration
) {
170 if (!restore_utils::IsWindowRestoreEnabled())
173 // Use an old profile.
174 pref_service_
->SetString(prefs::kProfileCreatedByVersion
, "19.0.0.0");
175 ASSERT_TRUE(SessionStartupPref::TypeIsDefault(pref_service_
.get()));
177 // Trigger the migration.
178 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
179 pref_service_
.get());
181 // The pref is now explicit.
182 EXPECT_EQ(SessionStartupPref::LAST
, pref
.type
);
183 EXPECT_FALSE(SessionStartupPref::TypeIsDefault(pref_service_
.get()));