[email protected] | 330d5722 | 2012-03-06 20:56:47 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | f9dfa84 | 2010-10-07 15:47:04 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 5 | #include "chrome/browser/prefs/pref_registry_syncable.h" |
[email protected] | f9dfa84 | 2010-10-07 15:47:04 | [diff] [blame] | 6 | #include "chrome/browser/prefs/session_startup_pref.h" |
| 7 | #include "chrome/common/pref_names.h" |
[email protected] | 7688968a | 2013-02-12 21:45:13 | [diff] [blame] | 8 | #include "chrome/test/base/testing_pref_service_syncable.h" |
[email protected] | f9dfa84 | 2010-10-07 15:47:04 | [diff] [blame] | 9 | #include "testing/gmock/include/gmock/gmock.h" |
| 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
[email protected] | cab6acf | 2012-05-10 13:49:01 | [diff] [blame] | 12 | #if defined(OS_MACOSX) |
| 13 | #include "chrome/browser/ui/cocoa/window_restore_utils.h" |
| 14 | #endif |
| 15 | |
[email protected] | f9dfa84 | 2010-10-07 15:47:04 | [diff] [blame] | 16 | // Unit tests for SessionStartupPref. |
| 17 | class SessionStartupPrefTest : public testing::Test { |
| 18 | public: |
| 19 | virtual void SetUp() { |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 20 | pref_service_.reset(new TestingPrefServiceSyncable); |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 21 | SessionStartupPref::RegisterUserPrefs(registry()); |
| 22 | registry()->RegisterBooleanPref(prefs::kHomePageIsNewTabPage, |
| 23 | true, |
| 24 | PrefRegistrySyncable::UNSYNCABLE_PREF); |
[email protected] | 9f9ab27 | 2012-06-29 12:12:43 | [diff] [blame] | 25 | // Make the tests independent of the Mac startup pref migration (see |
| 26 | // SessionStartupPref::MigrateMacDefaultPrefIfNecessary). |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 27 | registry()->RegisterStringPref(prefs::kProfileCreatedByVersion, |
| 28 | "22.0.0.0", |
| 29 | PrefRegistrySyncable::UNSYNCABLE_PREF); |
[email protected] | f9dfa84 | 2010-10-07 15:47:04 | [diff] [blame] | 30 | } |
| 31 | |
[email protected] | cab6acf | 2012-05-10 13:49:01 | [diff] [blame] | 32 | bool IsUseLastOpenDefault() { |
[email protected] | 9f9ab27 | 2012-06-29 12:12:43 | [diff] [blame] | 33 | // On ChromeOS, the default SessionStartupPref is LAST. |
[email protected] | cab6acf | 2012-05-10 13:49:01 | [diff] [blame] | 34 | #if defined(OS_CHROMEOS) |
| 35 | return true; |
[email protected] | cab6acf | 2012-05-10 13:49:01 | [diff] [blame] | 36 | #else |
| 37 | return false; |
| 38 | #endif |
| 39 | } |
| 40 | |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 41 | PrefRegistrySyncable* registry() { |
| 42 | return pref_service_->registry(); |
| 43 | } |
| 44 | |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 45 | scoped_ptr<TestingPrefServiceSyncable> pref_service_; |
[email protected] | f9dfa84 | 2010-10-07 15:47:04 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | TEST_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 | |
| 61 | TEST_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] | 1c51cf7 | 2011-02-25 13:39:41 | [diff] [blame] | 78 | SessionStartupPref(SessionStartupPref::URLS); |
[email protected] | f9dfa84 | 2010-10-07 15:47:04 | [diff] [blame] | 79 | 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] | 330d5722 | 2012-03-06 20:56:47 | [diff] [blame] | 85 | |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 86 | // 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. |
| 89 | TEST_F(SessionStartupPrefTest, DefaultMigration) { |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 90 | registry()->RegisterStringPref(prefs::kHomePage, "http://google.com/", |
| 91 | PrefRegistrySyncable::UNSYNCABLE_PREF); |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 92 | 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] | cab6acf | 2012-05-10 13:49:01 | [diff] [blame] | 100 | 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] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 108 | } |
| 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. |
| 114 | TEST_F(SessionStartupPrefTest, DefaultMigrationHomepageIsNTP) { |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 115 | registry()->RegisterStringPref(prefs::kHomePage, "http://google.com/", |
| 116 | PrefRegistrySyncable::UNSYNCABLE_PREF); |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 117 | 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] | cab6acf | 2012-05-10 13:49:01 | [diff] [blame] | 125 | if (IsUseLastOpenDefault()) |
| 126 | EXPECT_EQ(SessionStartupPref::LAST, pref.type); |
| 127 | else |
| 128 | EXPECT_EQ(SessionStartupPref::DEFAULT, pref.type); |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 129 | |
| 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] | 330d5722 | 2012-03-06 20:56:47 | [diff] [blame] | 136 | TEST_F(SessionStartupPrefTest, HomePageMigration) { |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 137 | registry()->RegisterStringPref(prefs::kHomePage, "http://google.com/", |
| 138 | PrefRegistrySyncable::UNSYNCABLE_PREF); |
[email protected] | 330d5722 | 2012-03-06 20:56:47 | [diff] [blame] | 139 | |
| 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] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 145 | pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, false); |
[email protected] | 330d5722 | 2012-03-06 20:56:47 | [diff] [blame] | 146 | |
| 147 | SessionStartupPref pref = SessionStartupPref::GetStartupPref( |
| 148 | pref_service_.get()); |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 149 | |
[email protected] | 330d5722 | 2012-03-06 20:56:47 | [diff] [blame] | 150 | 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] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 154 | |
| 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. |
| 158 | TEST_F(SessionStartupPrefTest, HomePageMigrationHomepageIsNTP) { |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 159 | registry()->RegisterStringPref(prefs::kHomePage, "http://google.com/", |
| 160 | PrefRegistrySyncable::UNSYNCABLE_PREF); |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 161 | |
| 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] | a379a1a | 2012-07-03 17:26:11 | [diff] [blame] | 174 | |
| 175 | #if defined(OS_MACOSX) |
| 176 | // See SessionStartupPref::MigrateMacDefaultPrefIfNecessary. |
| 177 | TEST_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 |