[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 | |
| 5 | #include "chrome/browser/prefs/session_startup_pref.h" |
| 6 | #include "chrome/common/pref_names.h" |
[email protected] | 8ad3636e | 2011-08-01 22:31:40 | [diff] [blame] | 7 | #include "chrome/test/base/testing_pref_service.h" |
[email protected] | f9dfa84 | 2010-10-07 15:47:04 | [diff] [blame] | 8 | #include "testing/gmock/include/gmock/gmock.h" |
| 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | |
[email protected] | cab6acf | 2012-05-10 13:49:01 | [diff] [blame] | 11 | #if defined(OS_MACOSX) |
| 12 | #include "chrome/browser/ui/cocoa/window_restore_utils.h" |
| 13 | #endif |
| 14 | |
[email protected] | f9dfa84 | 2010-10-07 15:47:04 | [diff] [blame] | 15 | // Unit tests for SessionStartupPref. |
| 16 | class SessionStartupPrefTest : public testing::Test { |
| 17 | public: |
| 18 | virtual void SetUp() { |
| 19 | pref_service_.reset(new TestingPrefService); |
| 20 | SessionStartupPref::RegisterUserPrefs(pref_service_.get()); |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 21 | pref_service_->RegisterBooleanPref(prefs::kHomePageIsNewTabPage, true); |
[email protected] | 9f9ab27 | 2012-06-29 12:12:43 | [diff] [blame] | 22 | // Make the tests independent of the Mac startup pref migration (see |
| 23 | // SessionStartupPref::MigrateMacDefaultPrefIfNecessary). |
| 24 | pref_service_->RegisterStringPref(prefs::kProfileCreatedByVersion, |
| 25 | "22.0.0.0"); |
[email protected] | f9dfa84 | 2010-10-07 15:47:04 | [diff] [blame] | 26 | } |
| 27 | |
[email protected] | cab6acf | 2012-05-10 13:49:01 | [diff] [blame] | 28 | bool IsUseLastOpenDefault() { |
[email protected] | 9f9ab27 | 2012-06-29 12:12:43 | [diff] [blame] | 29 | // On ChromeOS, the default SessionStartupPref is LAST. |
[email protected] | cab6acf | 2012-05-10 13:49:01 | [diff] [blame] | 30 | #if defined(OS_CHROMEOS) |
| 31 | return true; |
[email protected] | cab6acf | 2012-05-10 13:49:01 | [diff] [blame] | 32 | #else |
| 33 | return false; |
| 34 | #endif |
| 35 | } |
| 36 | |
[email protected] | f9dfa84 | 2010-10-07 15:47:04 | [diff] [blame] | 37 | scoped_ptr<TestingPrefService> pref_service_; |
| 38 | }; |
| 39 | |
| 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); |
| 45 | |
| 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()); |
| 51 | } |
| 52 | |
| 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); |
| 57 | |
| 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, |
| 63 | url_pref_list2); |
| 64 | |
| 65 | SessionStartupPref result = |
| 66 | SessionStartupPref::GetStartupPref(pref_service_.get()); |
| 67 | EXPECT_EQ(3u, result.urls.size()); |
| 68 | |
| 69 | SessionStartupPref override_test = |
[email protected] | 1c51cf7 | 2011-02-25 13:39:41 | [diff] [blame] | 70 | SessionStartupPref(SessionStartupPref::URLS); |
[email protected] | f9dfa84 | 2010-10-07 15:47:04 | [diff] [blame] | 71 | override_test.urls.push_back(GURL("dev.chromium.org")); |
| 72 | SessionStartupPref::SetStartupPref(pref_service_.get(), override_test); |
| 73 | |
| 74 | result = SessionStartupPref::GetStartupPref(pref_service_.get()); |
| 75 | EXPECT_EQ(3u, result.urls.size()); |
| 76 | } |
[email protected] | 330d5722 | 2012-03-06 20:56:47 | [diff] [blame] | 77 | |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 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); |
| 85 | |
| 86 | EXPECT_FALSE(pref_service_->HasPrefPath(prefs::kRestoreOnStartup)); |
| 87 | |
| 88 | SessionStartupPref pref = SessionStartupPref::GetStartupPref( |
| 89 | pref_service_.get()); |
| 90 | |
[email protected] | cab6acf | 2012-05-10 13:49:01 | [diff] [blame] | 91 | if (IsUseLastOpenDefault()) { |
| 92 | EXPECT_EQ(SessionStartupPref::LAST, pref.type); |
| 93 | EXPECT_EQ(0U, pref.urls.size()); |
| 94 | } else { |
| 95 | EXPECT_EQ(SessionStartupPref::URLS, pref.type); |
| 96 | EXPECT_EQ(1U, pref.urls.size()); |
| 97 | EXPECT_EQ(GURL("http://chromium.org/"), pref.urls[0]); |
| 98 | } |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 99 | } |
| 100 | |
| 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); |
| 109 | |
| 110 | EXPECT_FALSE(pref_service_->HasPrefPath(prefs::kRestoreOnStartup)); |
| 111 | |
| 112 | SessionStartupPref pref = SessionStartupPref::GetStartupPref( |
| 113 | pref_service_.get()); |
| 114 | |
[email protected] | cab6acf | 2012-05-10 13:49:01 | [diff] [blame] | 115 | if (IsUseLastOpenDefault()) |
| 116 | EXPECT_EQ(SessionStartupPref::LAST, pref.type); |
| 117 | else |
| 118 | EXPECT_EQ(SessionStartupPref::DEFAULT, pref.type); |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 119 | |
| 120 | // The "URLs to restore on startup" shouldn't get migrated. |
| 121 | EXPECT_EQ(0U, pref.urls.size()); |
| 122 | } |
| 123 | |
| 124 | // Checks to make sure that if the user had previously selected "Open the |
| 125 | // "homepage", their preferences are migrated on upgrade to M19. |
[email protected] | 330d5722 | 2012-03-06 20:56:47 | [diff] [blame] | 126 | TEST_F(SessionStartupPrefTest, HomePageMigration) { |
| 127 | pref_service_->RegisterStringPref(prefs::kHomePage, "http://google.com/"); |
| 128 | |
| 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/"); |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 134 | pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, false); |
[email protected] | 330d5722 | 2012-03-06 20:56:47 | [diff] [blame] | 135 | |
| 136 | SessionStartupPref pref = SessionStartupPref::GetStartupPref( |
| 137 | pref_service_.get()); |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 138 | |
[email protected] | 330d5722 | 2012-03-06 20:56:47 | [diff] [blame] | 139 | EXPECT_EQ(SessionStartupPref::URLS, pref.type); |
| 140 | EXPECT_EQ(1U, pref.urls.size()); |
| 141 | EXPECT_EQ(GURL("http://chromium.org/"), pref.urls[0]); |
| 142 | } |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 143 | |
| 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/"); |
| 149 | |
| 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); |
| 156 | |
| 157 | SessionStartupPref pref = SessionStartupPref::GetStartupPref( |
| 158 | pref_service_.get()); |
| 159 | |
| 160 | EXPECT_EQ(SessionStartupPref::DEFAULT, pref.type); |
| 161 | } |
[email protected] | a379a1a | 2012-07-03 17:26:11 | [diff] [blame^] | 162 | |
| 163 | #if defined(OS_MACOSX) |
| 164 | // See SessionStartupPref::MigrateMacDefaultPrefIfNecessary. |
| 165 | TEST_F(SessionStartupPrefTest, MacDefaultStartupPrefMigration) { |
| 166 | if (!restore_utils::IsWindowRestoreEnabled()) |
| 167 | return; |
| 168 | |
| 169 | // Use an old profile. |
| 170 | pref_service_->SetString(prefs::kProfileCreatedByVersion, "19.0.0.0"); |
| 171 | ASSERT_TRUE(SessionStartupPref::TypeIsDefault(pref_service_.get())); |
| 172 | |
| 173 | // Trigger the migration. |
| 174 | SessionStartupPref pref = SessionStartupPref::GetStartupPref( |
| 175 | pref_service_.get()); |
| 176 | |
| 177 | // The pref is now explicit. |
| 178 | EXPECT_EQ(SessionStartupPref::LAST, pref.type); |
| 179 | EXPECT_FALSE(SessionStartupPref::TypeIsDefault(pref_service_.get())); |
| 180 | } |
| 181 | #endif |