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 "components/pref_registry/pref_registry_syncable.h"
8 #include "components/syncable_prefs/testing_pref_service_syncable.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 // Unit tests for SessionStartupPref.
13 class SessionStartupPrefTest
: public testing::Test
{
15 void SetUp() override
{
16 pref_service_
.reset(new syncable_prefs::TestingPrefServiceSyncable
);
17 SessionStartupPref::RegisterProfilePrefs(registry());
18 registry()->RegisterBooleanPref(prefs::kHomePageIsNewTabPage
, true);
21 bool IsUseLastOpenDefault() {
22 // On ChromeOS, the default SessionStartupPref is LAST.
23 #if defined(OS_CHROMEOS)
30 user_prefs::PrefRegistrySyncable
* registry() {
31 return pref_service_
->registry();
34 scoped_ptr
<syncable_prefs::TestingPrefServiceSyncable
> pref_service_
;
37 TEST_F(SessionStartupPrefTest
, URLListIsFixedUp
) {
38 base::ListValue
* url_pref_list
= new base::ListValue
;
39 url_pref_list
->Set(0, new base::StringValue("google.com"));
40 url_pref_list
->Set(1, new base::StringValue("chromium.org"));
41 pref_service_
->SetUserPref(prefs::kURLsToRestoreOnStartup
, url_pref_list
);
43 SessionStartupPref result
=
44 SessionStartupPref::GetStartupPref(pref_service_
.get());
45 EXPECT_EQ(2u, result
.urls
.size());
46 EXPECT_EQ("http://google.com/", result
.urls
[0].spec());
47 EXPECT_EQ("http://chromium.org/", result
.urls
[1].spec());
50 TEST_F(SessionStartupPrefTest
, URLListManagedOverridesUser
) {
51 base::ListValue
* url_pref_list1
= new base::ListValue
;
52 url_pref_list1
->Set(0, new base::StringValue("chromium.org"));
53 pref_service_
->SetUserPref(prefs::kURLsToRestoreOnStartup
, url_pref_list1
);
55 base::ListValue
* url_pref_list2
= new base::ListValue
;
56 url_pref_list2
->Set(0, new base::StringValue("chromium.org"));
57 url_pref_list2
->Set(1, new base::StringValue("chromium.org"));
58 url_pref_list2
->Set(2, new base::StringValue("chromium.org"));
59 pref_service_
->SetManagedPref(prefs::kURLsToRestoreOnStartup
,
62 SessionStartupPref result
=
63 SessionStartupPref::GetStartupPref(pref_service_
.get());
64 EXPECT_EQ(3u, result
.urls
.size());
66 SessionStartupPref override_test
=
67 SessionStartupPref(SessionStartupPref::URLS
);
68 override_test
.urls
.push_back(GURL("dev.chromium.org"));
69 SessionStartupPref::SetStartupPref(pref_service_
.get(), override_test
);
71 result
= SessionStartupPref::GetStartupPref(pref_service_
.get());
72 EXPECT_EQ(3u, result
.urls
.size());
75 // Checks to make sure that if the user had previously not selected anything
76 // (so that, in effect, the default value "Open the homepage" was selected),
77 // their preferences are migrated on upgrade to m19.
78 TEST_F(SessionStartupPrefTest
, DefaultMigration
) {
79 registry()->RegisterStringPref(prefs::kHomePage
, "http://google.com/");
80 pref_service_
->SetString(prefs::kHomePage
, "http://chromium.org/");
81 pref_service_
->SetBoolean(prefs::kHomePageIsNewTabPage
, false);
83 EXPECT_FALSE(pref_service_
->HasPrefPath(prefs::kRestoreOnStartup
));
85 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
88 if (IsUseLastOpenDefault()) {
89 EXPECT_EQ(SessionStartupPref::LAST
, pref
.type
);
90 EXPECT_EQ(0U, pref
.urls
.size());
92 EXPECT_EQ(SessionStartupPref::URLS
, pref
.type
);
93 EXPECT_EQ(1U, pref
.urls
.size());
94 EXPECT_EQ(GURL("http://chromium.org/"), pref
.urls
[0]);
98 // Checks to make sure that if the user had previously not selected anything
99 // (so that, in effect, the default value "Open the homepage" was selected),
100 // and the NTP is being used for the homepage, their preferences are migrated
101 // to "Open the New Tab Page" on upgrade to M19.
102 TEST_F(SessionStartupPrefTest
, DefaultMigrationHomepageIsNTP
) {
103 registry()->RegisterStringPref(prefs::kHomePage
, "http://google.com/");
104 pref_service_
->SetString(prefs::kHomePage
, "http://chromium.org/");
105 pref_service_
->SetBoolean(prefs::kHomePageIsNewTabPage
, true);
107 EXPECT_FALSE(pref_service_
->HasPrefPath(prefs::kRestoreOnStartup
));
109 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
110 pref_service_
.get());
112 if (IsUseLastOpenDefault())
113 EXPECT_EQ(SessionStartupPref::LAST
, pref
.type
);
115 EXPECT_EQ(SessionStartupPref::DEFAULT
, pref
.type
);
117 // The "URLs to restore on startup" shouldn't get migrated.
118 EXPECT_EQ(0U, pref
.urls
.size());
121 // Checks to make sure that if the user had previously selected "Open the
122 // "homepage", their preferences are migrated on upgrade to M19.
123 TEST_F(SessionStartupPrefTest
, HomePageMigration
) {
124 registry()->RegisterStringPref(prefs::kHomePage
, "http://google.com/");
126 // By design, it's impossible to set the 'restore on startup' pref to 0
127 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it
128 // using the pref service directly.
129 pref_service_
->SetInteger(prefs::kRestoreOnStartup
, /*kPrefValueHomePage*/ 0);
130 pref_service_
->SetString(prefs::kHomePage
, "http://chromium.org/");
131 pref_service_
->SetBoolean(prefs::kHomePageIsNewTabPage
, false);
133 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
134 pref_service_
.get());
136 EXPECT_EQ(SessionStartupPref::URLS
, pref
.type
);
137 EXPECT_EQ(1U, pref
.urls
.size());
138 EXPECT_EQ(GURL("http://chromium.org/"), pref
.urls
[0]);
141 // Checks to make sure that if the user had previously selected "Open the
142 // "homepage", and the NTP is being used for the homepage, their preferences
143 // are migrated on upgrade to M19.
144 TEST_F(SessionStartupPrefTest
, HomePageMigrationHomepageIsNTP
) {
145 registry()->RegisterStringPref(prefs::kHomePage
, "http://google.com/");
147 // By design, it's impossible to set the 'restore on startup' pref to 0
148 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it
149 // using the pref service directly.
150 pref_service_
->SetInteger(prefs::kRestoreOnStartup
, /*kPrefValueHomePage*/ 0);
151 pref_service_
->SetString(prefs::kHomePage
, "http://chromium.org/");
152 pref_service_
->SetBoolean(prefs::kHomePageIsNewTabPage
, true);
154 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
155 pref_service_
.get());
157 EXPECT_EQ(SessionStartupPref::DEFAULT
, pref
.type
);