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.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 #if defined(OS_MACOSX)
12 #include "chrome/browser/ui/cocoa/window_restore_utils.h"
15 // Unit tests for SessionStartupPref.
16 class SessionStartupPrefTest
: public testing::Test
{
18 virtual void SetUp() {
19 pref_service_
.reset(new TestingPrefService
);
20 SessionStartupPref::RegisterUserPrefs(pref_service_
.get());
21 pref_service_
->RegisterBooleanPref(prefs::kHomePageIsNewTabPage
, true);
22 // Make the tests independent of the Mac startup pref migration (see
23 // SessionStartupPref::MigrateMacDefaultPrefIfNecessary).
24 pref_service_
->RegisterStringPref(prefs::kProfileCreatedByVersion
,
28 bool IsUseLastOpenDefault() {
29 // On ChromeOS, the default SessionStartupPref is LAST.
30 #if defined(OS_CHROMEOS)
37 scoped_ptr
<TestingPrefService
> pref_service_
;
40 TEST_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
);
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());
53 TEST_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
);
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
,
65 SessionStartupPref result
=
66 SessionStartupPref::GetStartupPref(pref_service_
.get());
67 EXPECT_EQ(3u, result
.urls
.size());
69 SessionStartupPref override_test
=
70 SessionStartupPref(SessionStartupPref::URLS
);
71 override_test
.urls
.push_back(GURL("dev.chromium.org"));
72 SessionStartupPref::SetStartupPref(pref_service_
.get(), override_test
);
74 result
= SessionStartupPref::GetStartupPref(pref_service_
.get());
75 EXPECT_EQ(3u, result
.urls
.size());
78 // 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.
81 TEST_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);
86 EXPECT_FALSE(pref_service_
->HasPrefPath(prefs::kRestoreOnStartup
));
88 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
91 if (IsUseLastOpenDefault()) {
92 EXPECT_EQ(SessionStartupPref::LAST
, pref
.type
);
93 EXPECT_EQ(0U, pref
.urls
.size());
95 EXPECT_EQ(SessionStartupPref::URLS
, pref
.type
);
96 EXPECT_EQ(1U, pref
.urls
.size());
97 EXPECT_EQ(GURL("http://chromium.org/"), pref
.urls
[0]);
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.
105 TEST_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);
110 EXPECT_FALSE(pref_service_
->HasPrefPath(prefs::kRestoreOnStartup
));
112 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
113 pref_service_
.get());
115 if (IsUseLastOpenDefault())
116 EXPECT_EQ(SessionStartupPref::LAST
, pref
.type
);
118 EXPECT_EQ(SessionStartupPref::DEFAULT
, pref
.type
);
120 // The "URLs to restore on startup" shouldn't get migrated.
121 EXPECT_EQ(0U, pref
.urls
.size());
124 // Checks to make sure that if the user had previously selected "Open the
125 // "homepage", their preferences are migrated on upgrade to M19.
126 TEST_F(SessionStartupPrefTest
, HomePageMigration
) {
127 pref_service_
->RegisterStringPref(prefs::kHomePage
, "http://google.com/");
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/");
134 pref_service_
->SetBoolean(prefs::kHomePageIsNewTabPage
, false);
136 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
137 pref_service_
.get());
139 EXPECT_EQ(SessionStartupPref::URLS
, pref
.type
);
140 EXPECT_EQ(1U, pref
.urls
.size());
141 EXPECT_EQ(GURL("http://chromium.org/"), pref
.urls
[0]);
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.
147 TEST_F(SessionStartupPrefTest
, HomePageMigrationHomepageIsNTP
) {
148 pref_service_
->RegisterStringPref(prefs::kHomePage
, "http://google.com/");
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);
157 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
158 pref_service_
.get());
160 EXPECT_EQ(SessionStartupPref::DEFAULT
, pref
.type
);
163 #if defined(OS_MACOSX)
164 // See SessionStartupPref::MigrateMacDefaultPrefIfNecessary.
165 TEST_F(SessionStartupPrefTest
, MacDefaultStartupPrefMigration
) {
166 if (!restore_utils::IsWindowRestoreEnabled())
169 // Use an old profile.
170 pref_service_
->SetString(prefs::kProfileCreatedByVersion
, "19.0.0.0");
171 ASSERT_TRUE(SessionStartupPref::TypeIsDefault(pref_service_
.get()));
173 // Trigger the migration.
174 SessionStartupPref pref
= SessionStartupPref::GetStartupPref(
175 pref_service_
.get());
177 // The pref is now explicit.
178 EXPECT_EQ(SessionStartupPref::LAST
, pref
.type
);
179 EXPECT_FALSE(SessionStartupPref::TypeIsDefault(pref_service_
.get()));