blob: fac4ec15bf4c83351ef762758b569f9678149372 [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
[email protected]c753f142013-02-10 13:14:045#include "chrome/browser/prefs/pref_registry_syncable.h"
[email protected]f9dfa842010-10-07 15:47:046#include "chrome/browser/prefs/session_startup_pref.h"
7#include "chrome/common/pref_names.h"
[email protected]7688968a2013-02-12 21:45:138#include "chrome/test/base/testing_pref_service_syncable.h"
[email protected]f9dfa842010-10-07 15:47:049#include "testing/gmock/include/gmock/gmock.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
[email protected]cab6acf2012-05-10 13:49:0112#if defined(OS_MACOSX)
13#include "chrome/browser/ui/cocoa/window_restore_utils.h"
14#endif
15
[email protected]f9dfa842010-10-07 15:47:0416// Unit tests for SessionStartupPref.
17class SessionStartupPrefTest : public testing::Test {
18 public:
19 virtual void SetUp() {
[email protected]5b199522012-12-22 17:24:4420 pref_service_.reset(new TestingPrefServiceSyncable);
[email protected]c753f142013-02-10 13:14:0421 SessionStartupPref::RegisterUserPrefs(registry());
22 registry()->RegisterBooleanPref(prefs::kHomePageIsNewTabPage,
23 true,
24 PrefRegistrySyncable::UNSYNCABLE_PREF);
[email protected]9f9ab272012-06-29 12:12:4325 // Make the tests independent of the Mac startup pref migration (see
26 // SessionStartupPref::MigrateMacDefaultPrefIfNecessary).
[email protected]c753f142013-02-10 13:14:0427 registry()->RegisterStringPref(prefs::kProfileCreatedByVersion,
28 "22.0.0.0",
29 PrefRegistrySyncable::UNSYNCABLE_PREF);
[email protected]f9dfa842010-10-07 15:47:0430 }
31
[email protected]cab6acf2012-05-10 13:49:0132 bool IsUseLastOpenDefault() {
[email protected]9f9ab272012-06-29 12:12:4333 // On ChromeOS, the default SessionStartupPref is LAST.
[email protected]cab6acf2012-05-10 13:49:0134#if defined(OS_CHROMEOS)
35 return true;
[email protected]cab6acf2012-05-10 13:49:0136#else
37 return false;
38#endif
39 }
40
[email protected]c753f142013-02-10 13:14:0441 PrefRegistrySyncable* registry() {
42 return pref_service_->registry();
43 }
44
[email protected]5b199522012-12-22 17:24:4445 scoped_ptr<TestingPrefServiceSyncable> pref_service_;
[email protected]f9dfa842010-10-07 15:47:0446};
47
48TEST_F(SessionStartupPrefTest, URLListIsFixedUp) {
49 ListValue* url_pref_list = new ListValue;
50 url_pref_list->Set(0, Value::CreateStringValue("google.com"));
51 url_pref_list->Set(1, Value::CreateStringValue("chromium.org"));
52 pref_service_->SetUserPref(prefs::kURLsToRestoreOnStartup, url_pref_list);
53
54 SessionStartupPref result =
55 SessionStartupPref::GetStartupPref(pref_service_.get());
56 EXPECT_EQ(2u, result.urls.size());
57 EXPECT_EQ("http://google.com/", result.urls[0].spec());
58 EXPECT_EQ("http://chromium.org/", result.urls[1].spec());
59}
60
61TEST_F(SessionStartupPrefTest, URLListManagedOverridesUser) {
62 ListValue* url_pref_list1 = new ListValue;
63 url_pref_list1->Set(0, Value::CreateStringValue("chromium.org"));
64 pref_service_->SetUserPref(prefs::kURLsToRestoreOnStartup, url_pref_list1);
65
66 ListValue* url_pref_list2 = new ListValue;
67 url_pref_list2->Set(0, Value::CreateStringValue("chromium.org"));
68 url_pref_list2->Set(1, Value::CreateStringValue("chromium.org"));
69 url_pref_list2->Set(2, Value::CreateStringValue("chromium.org"));
70 pref_service_->SetManagedPref(prefs::kURLsToRestoreOnStartup,
71 url_pref_list2);
72
73 SessionStartupPref result =
74 SessionStartupPref::GetStartupPref(pref_service_.get());
75 EXPECT_EQ(3u, result.urls.size());
76
77 SessionStartupPref override_test =
[email protected]1c51cf72011-02-25 13:39:4178 SessionStartupPref(SessionStartupPref::URLS);
[email protected]f9dfa842010-10-07 15:47:0479 override_test.urls.push_back(GURL("dev.chromium.org"));
80 SessionStartupPref::SetStartupPref(pref_service_.get(), override_test);
81
82 result = SessionStartupPref::GetStartupPref(pref_service_.get());
83 EXPECT_EQ(3u, result.urls.size());
84}
[email protected]330d57222012-03-06 20:56:4785
[email protected]1a5a31f2012-04-26 20:21:3486// Checks to make sure that if the user had previously not selected anything
87// (so that, in effect, the default value "Open the homepage" was selected),
88// their preferences are migrated on upgrade to m19.
89TEST_F(SessionStartupPrefTest, DefaultMigration) {
[email protected]c753f142013-02-10 13:14:0490 registry()->RegisterStringPref(prefs::kHomePage, "http://google.com/",
91 PrefRegistrySyncable::UNSYNCABLE_PREF);
[email protected]1a5a31f2012-04-26 20:21:3492 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/");
93 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, false);
94
95 EXPECT_FALSE(pref_service_->HasPrefPath(prefs::kRestoreOnStartup));
96
97 SessionStartupPref pref = SessionStartupPref::GetStartupPref(
98 pref_service_.get());
99
[email protected]cab6acf2012-05-10 13:49:01100 if (IsUseLastOpenDefault()) {
101 EXPECT_EQ(SessionStartupPref::LAST, pref.type);
102 EXPECT_EQ(0U, pref.urls.size());
103 } else {
104 EXPECT_EQ(SessionStartupPref::URLS, pref.type);
105 EXPECT_EQ(1U, pref.urls.size());
106 EXPECT_EQ(GURL("http://chromium.org/"), pref.urls[0]);
107 }
[email protected]1a5a31f2012-04-26 20:21:34108}
109
110// Checks to make sure that if the user had previously not selected anything
111// (so that, in effect, the default value "Open the homepage" was selected),
112// and the NTP is being used for the homepage, their preferences are migrated
113// to "Open the New Tab Page" on upgrade to M19.
114TEST_F(SessionStartupPrefTest, DefaultMigrationHomepageIsNTP) {
[email protected]c753f142013-02-10 13:14:04115 registry()->RegisterStringPref(prefs::kHomePage, "http://google.com/",
116 PrefRegistrySyncable::UNSYNCABLE_PREF);
[email protected]1a5a31f2012-04-26 20:21:34117 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/");
118 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, true);
119
120 EXPECT_FALSE(pref_service_->HasPrefPath(prefs::kRestoreOnStartup));
121
122 SessionStartupPref pref = SessionStartupPref::GetStartupPref(
123 pref_service_.get());
124
[email protected]cab6acf2012-05-10 13:49:01125 if (IsUseLastOpenDefault())
126 EXPECT_EQ(SessionStartupPref::LAST, pref.type);
127 else
128 EXPECT_EQ(SessionStartupPref::DEFAULT, pref.type);
[email protected]1a5a31f2012-04-26 20:21:34129
130 // The "URLs to restore on startup" shouldn't get migrated.
131 EXPECT_EQ(0U, pref.urls.size());
132}
133
134// Checks to make sure that if the user had previously selected "Open the
135// "homepage", their preferences are migrated on upgrade to M19.
[email protected]330d57222012-03-06 20:56:47136TEST_F(SessionStartupPrefTest, HomePageMigration) {
[email protected]c753f142013-02-10 13:14:04137 registry()->RegisterStringPref(prefs::kHomePage, "http://google.com/",
138 PrefRegistrySyncable::UNSYNCABLE_PREF);
[email protected]330d57222012-03-06 20:56:47139
140 // By design, it's impossible to set the 'restore on startup' pref to 0
141 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it
142 // using the pref service directly.
143 pref_service_->SetInteger(prefs::kRestoreOnStartup, /*kPrefValueHomePage*/ 0);
144 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/");
[email protected]1a5a31f2012-04-26 20:21:34145 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, false);
[email protected]330d57222012-03-06 20:56:47146
147 SessionStartupPref pref = SessionStartupPref::GetStartupPref(
148 pref_service_.get());
[email protected]1a5a31f2012-04-26 20:21:34149
[email protected]330d57222012-03-06 20:56:47150 EXPECT_EQ(SessionStartupPref::URLS, pref.type);
151 EXPECT_EQ(1U, pref.urls.size());
152 EXPECT_EQ(GURL("http://chromium.org/"), pref.urls[0]);
153}
[email protected]1a5a31f2012-04-26 20:21:34154
155// Checks to make sure that if the user had previously selected "Open the
156// "homepage", and the NTP is being used for the homepage, their preferences
157// are migrated on upgrade to M19.
158TEST_F(SessionStartupPrefTest, HomePageMigrationHomepageIsNTP) {
[email protected]c753f142013-02-10 13:14:04159 registry()->RegisterStringPref(prefs::kHomePage, "http://google.com/",
160 PrefRegistrySyncable::UNSYNCABLE_PREF);
[email protected]1a5a31f2012-04-26 20:21:34161
162 // By design, it's impossible to set the 'restore on startup' pref to 0
163 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it
164 // using the pref service directly.
165 pref_service_->SetInteger(prefs::kRestoreOnStartup, /*kPrefValueHomePage*/ 0);
166 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/");
167 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, true);
168
169 SessionStartupPref pref = SessionStartupPref::GetStartupPref(
170 pref_service_.get());
171
172 EXPECT_EQ(SessionStartupPref::DEFAULT, pref.type);
173}
[email protected]a379a1a2012-07-03 17:26:11174
175#if defined(OS_MACOSX)
176// See SessionStartupPref::MigrateMacDefaultPrefIfNecessary.
177TEST_F(SessionStartupPrefTest, MacDefaultStartupPrefMigration) {
178 if (!restore_utils::IsWindowRestoreEnabled())
179 return;
180
181 // Use an old profile.
182 pref_service_->SetString(prefs::kProfileCreatedByVersion, "19.0.0.0");
183 ASSERT_TRUE(SessionStartupPref::TypeIsDefault(pref_service_.get()));
184
185 // Trigger the migration.
186 SessionStartupPref pref = SessionStartupPref::GetStartupPref(
187 pref_service_.get());
188
189 // The pref is now explicit.
190 EXPECT_EQ(SessionStartupPref::LAST, pref.type);
191 EXPECT_FALSE(SessionStartupPref::TypeIsDefault(pref_service_.get()));
192}
193#endif