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. 4 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/user_prefs/pref_registry_syncable.h" 9 #include "testing/gmock/include/gmock/gmock.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 #if defined(OS_MACOSX) 13 #include "chrome/browser/ui/cocoa/window_restore_utils.h" 14 #endif 15 16 // Unit tests for SessionStartupPref. 17 class SessionStartupPrefTest : public testing::Test { 18 public: 19 virtual void SetUp() { 20 pref_service_.reset(new TestingPrefServiceSyncable); 21 SessionStartupPref::RegisterProfilePrefs(registry()); 22 registry()->RegisterBooleanPref( 23 prefs::kHomePageIsNewTabPage, 24 true, 25 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 26 // Make the tests independent of the Mac startup pref migration (see 27 // SessionStartupPref::MigrateMacDefaultPrefIfNecessary). 28 registry()->RegisterStringPref( 29 prefs::kProfileCreatedByVersion, 30 "22.0.0.0", 31 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 32 } 33 34 bool IsUseLastOpenDefault() { 35 // On ChromeOS, the default SessionStartupPref is LAST. 36 #if defined(OS_CHROMEOS) 37 return true; 38 #else 39 return false; 40 #endif 41 } 42 43 user_prefs::PrefRegistrySyncable* registry() { 44 return pref_service_->registry(); 45 } 46 47 scoped_ptr<TestingPrefServiceSyncable> pref_service_; 48 }; 49 50 TEST_F(SessionStartupPrefTest, URLListIsFixedUp) { 51 ListValue* url_pref_list = new ListValue; 52 url_pref_list->Set(0, Value::CreateStringValue("google.com")); 53 url_pref_list->Set(1, Value::CreateStringValue("chromium.org")); 54 pref_service_->SetUserPref(prefs::kURLsToRestoreOnStartup, url_pref_list); 55 56 SessionStartupPref result = 57 SessionStartupPref::GetStartupPref(pref_service_.get()); 58 EXPECT_EQ(2u, result.urls.size()); 59 EXPECT_EQ("http://google.com/", result.urls[0].spec()); 60 EXPECT_EQ("http://chromium.org/", result.urls[1].spec()); 61 } 62 63 TEST_F(SessionStartupPrefTest, URLListManagedOverridesUser) { 64 ListValue* url_pref_list1 = new ListValue; 65 url_pref_list1->Set(0, Value::CreateStringValue("chromium.org")); 66 pref_service_->SetUserPref(prefs::kURLsToRestoreOnStartup, url_pref_list1); 67 68 ListValue* url_pref_list2 = new ListValue; 69 url_pref_list2->Set(0, Value::CreateStringValue("chromium.org")); 70 url_pref_list2->Set(1, Value::CreateStringValue("chromium.org")); 71 url_pref_list2->Set(2, Value::CreateStringValue("chromium.org")); 72 pref_service_->SetManagedPref(prefs::kURLsToRestoreOnStartup, 73 url_pref_list2); 74 75 SessionStartupPref result = 76 SessionStartupPref::GetStartupPref(pref_service_.get()); 77 EXPECT_EQ(3u, result.urls.size()); 78 79 SessionStartupPref override_test = 80 SessionStartupPref(SessionStartupPref::URLS); 81 override_test.urls.push_back(GURL("dev.chromium.org")); 82 SessionStartupPref::SetStartupPref(pref_service_.get(), override_test); 83 84 result = SessionStartupPref::GetStartupPref(pref_service_.get()); 85 EXPECT_EQ(3u, result.urls.size()); 86 } 87 88 // Checks to make sure that if the user had previously not selected anything 89 // (so that, in effect, the default value "Open the homepage" was selected), 90 // their preferences are migrated on upgrade to m19. 91 TEST_F(SessionStartupPrefTest, DefaultMigration) { 92 registry()->RegisterStringPref( 93 prefs::kHomePage, 94 "http://google.com/", 95 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 96 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); 97 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, false); 98 99 EXPECT_FALSE(pref_service_->HasPrefPath(prefs::kRestoreOnStartup)); 100 101 SessionStartupPref pref = SessionStartupPref::GetStartupPref( 102 pref_service_.get()); 103 104 if (IsUseLastOpenDefault()) { 105 EXPECT_EQ(SessionStartupPref::LAST, pref.type); 106 EXPECT_EQ(0U, pref.urls.size()); 107 } else { 108 EXPECT_EQ(SessionStartupPref::URLS, pref.type); 109 EXPECT_EQ(1U, pref.urls.size()); 110 EXPECT_EQ(GURL("http://chromium.org/"), pref.urls[0]); 111 } 112 } 113 114 // Checks to make sure that if the user had previously not selected anything 115 // (so that, in effect, the default value "Open the homepage" was selected), 116 // and the NTP is being used for the homepage, their preferences are migrated 117 // to "Open the New Tab Page" on upgrade to M19. 118 TEST_F(SessionStartupPrefTest, DefaultMigrationHomepageIsNTP) { 119 registry()->RegisterStringPref( 120 prefs::kHomePage, 121 "http://google.com/", 122 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 123 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); 124 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, true); 125 126 EXPECT_FALSE(pref_service_->HasPrefPath(prefs::kRestoreOnStartup)); 127 128 SessionStartupPref pref = SessionStartupPref::GetStartupPref( 129 pref_service_.get()); 130 131 if (IsUseLastOpenDefault()) 132 EXPECT_EQ(SessionStartupPref::LAST, pref.type); 133 else 134 EXPECT_EQ(SessionStartupPref::DEFAULT, pref.type); 135 136 // The "URLs to restore on startup" shouldn't get migrated. 137 EXPECT_EQ(0U, pref.urls.size()); 138 } 139 140 // Checks to make sure that if the user had previously selected "Open the 141 // "homepage", their preferences are migrated on upgrade to M19. 142 TEST_F(SessionStartupPrefTest, HomePageMigration) { 143 registry()->RegisterStringPref( 144 prefs::kHomePage, 145 "http://google.com/", 146 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 147 148 // By design, it's impossible to set the 'restore on startup' pref to 0 149 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it 150 // using the pref service directly. 151 pref_service_->SetInteger(prefs::kRestoreOnStartup, /*kPrefValueHomePage*/ 0); 152 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); 153 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, false); 154 155 SessionStartupPref pref = SessionStartupPref::GetStartupPref( 156 pref_service_.get()); 157 158 EXPECT_EQ(SessionStartupPref::URLS, pref.type); 159 EXPECT_EQ(1U, pref.urls.size()); 160 EXPECT_EQ(GURL("http://chromium.org/"), pref.urls[0]); 161 } 162 163 // Checks to make sure that if the user had previously selected "Open the 164 // "homepage", and the NTP is being used for the homepage, their preferences 165 // are migrated on upgrade to M19. 166 TEST_F(SessionStartupPrefTest, HomePageMigrationHomepageIsNTP) { 167 registry()->RegisterStringPref( 168 prefs::kHomePage, 169 "http://google.com/", 170 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 171 172 // By design, it's impossible to set the 'restore on startup' pref to 0 173 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it 174 // using the pref service directly. 175 pref_service_->SetInteger(prefs::kRestoreOnStartup, /*kPrefValueHomePage*/ 0); 176 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); 177 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, true); 178 179 SessionStartupPref pref = SessionStartupPref::GetStartupPref( 180 pref_service_.get()); 181 182 EXPECT_EQ(SessionStartupPref::DEFAULT, pref.type); 183 } 184 185 #if defined(OS_MACOSX) 186 // See SessionStartupPref::MigrateMacDefaultPrefIfNecessary. 187 TEST_F(SessionStartupPrefTest, MacDefaultStartupPrefMigration) { 188 if (!restore_utils::IsWindowRestoreEnabled()) 189 return; 190 191 // Use an old profile. 192 pref_service_->SetString(prefs::kProfileCreatedByVersion, "19.0.0.0"); 193 ASSERT_TRUE(SessionStartupPref::TypeIsDefault(pref_service_.get())); 194 195 // Trigger the migration. 196 SessionStartupPref pref = SessionStartupPref::GetStartupPref( 197 pref_service_.get()); 198 199 // The pref is now explicit. 200 EXPECT_EQ(SessionStartupPref::LAST, pref.type); 201 EXPECT_FALSE(SessionStartupPref::TypeIsDefault(pref_service_.get())); 202 } 203 #endif 204