| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/prefs/session_startup_pref.h" | 5 #include "chrome/browser/prefs/session_startup_pref.h" |
| 6 #include "chrome/common/pref_names.h" | 6 #include "chrome/common/pref_names.h" |
| 7 #include "chrome/test/base/testing_pref_service_syncable.h" | 7 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 8 #include "components/pref_registry/pref_registry_syncable.h" | 8 #include "components/pref_registry/pref_registry_syncable.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 #if defined(OS_MACOSX) | 12 #if defined(OS_MACOSX) |
| 13 #include "chrome/browser/ui/cocoa/window_restore_utils.h" | 13 #include "chrome/browser/ui/cocoa/window_restore_utils.h" |
| 14 #endif | 14 #endif |
| 15 | 15 |
| 16 // Unit tests for SessionStartupPref. | 16 // Unit tests for SessionStartupPref. |
| 17 class SessionStartupPrefTest : public testing::Test { | 17 class SessionStartupPrefTest : public testing::Test { |
| 18 public: | 18 public: |
| 19 void SetUp() override { | 19 void SetUp() override { |
| 20 pref_service_.reset(new TestingPrefServiceSyncable); | 20 pref_service_.reset(new TestingPrefServiceSyncable); |
| 21 SessionStartupPref::RegisterProfilePrefs(registry()); | 21 SessionStartupPref::RegisterProfilePrefs(registry()); |
| 22 registry()->RegisterBooleanPref( | 22 registry()->RegisterBooleanPref(prefs::kHomePageIsNewTabPage, true); |
| 23 prefs::kHomePageIsNewTabPage, | |
| 24 true, | |
| 25 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
| 26 // Make the tests independent of the Mac startup pref migration (see | 23 // Make the tests independent of the Mac startup pref migration (see |
| 27 // SessionStartupPref::MigrateMacDefaultPrefIfNecessary). | 24 // SessionStartupPref::MigrateMacDefaultPrefIfNecessary). |
| 28 registry()->RegisterStringPref( | 25 registry()->RegisterStringPref(prefs::kProfileCreatedByVersion, "22.0.0.0"); |
| 29 prefs::kProfileCreatedByVersion, | |
| 30 "22.0.0.0", | |
| 31 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
| 32 } | 26 } |
| 33 | 27 |
| 34 bool IsUseLastOpenDefault() { | 28 bool IsUseLastOpenDefault() { |
| 35 // On ChromeOS, the default SessionStartupPref is LAST. | 29 // On ChromeOS, the default SessionStartupPref is LAST. |
| 36 #if defined(OS_CHROMEOS) | 30 #if defined(OS_CHROMEOS) |
| 37 return true; | 31 return true; |
| 38 #else | 32 #else |
| 39 return false; | 33 return false; |
| 40 #endif | 34 #endif |
| 41 } | 35 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 SessionStartupPref::SetStartupPref(pref_service_.get(), override_test); | 76 SessionStartupPref::SetStartupPref(pref_service_.get(), override_test); |
| 83 | 77 |
| 84 result = SessionStartupPref::GetStartupPref(pref_service_.get()); | 78 result = SessionStartupPref::GetStartupPref(pref_service_.get()); |
| 85 EXPECT_EQ(3u, result.urls.size()); | 79 EXPECT_EQ(3u, result.urls.size()); |
| 86 } | 80 } |
| 87 | 81 |
| 88 // Checks to make sure that if the user had previously not selected anything | 82 // 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), | 83 // (so that, in effect, the default value "Open the homepage" was selected), |
| 90 // their preferences are migrated on upgrade to m19. | 84 // their preferences are migrated on upgrade to m19. |
| 91 TEST_F(SessionStartupPrefTest, DefaultMigration) { | 85 TEST_F(SessionStartupPrefTest, DefaultMigration) { |
| 92 registry()->RegisterStringPref( | 86 registry()->RegisterStringPref(prefs::kHomePage, "http://google.com/"); |
| 93 prefs::kHomePage, | |
| 94 "http://google.com/", | |
| 95 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
| 96 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); | 87 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); |
| 97 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, false); | 88 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, false); |
| 98 | 89 |
| 99 EXPECT_FALSE(pref_service_->HasPrefPath(prefs::kRestoreOnStartup)); | 90 EXPECT_FALSE(pref_service_->HasPrefPath(prefs::kRestoreOnStartup)); |
| 100 | 91 |
| 101 SessionStartupPref pref = SessionStartupPref::GetStartupPref( | 92 SessionStartupPref pref = SessionStartupPref::GetStartupPref( |
| 102 pref_service_.get()); | 93 pref_service_.get()); |
| 103 | 94 |
| 104 if (IsUseLastOpenDefault()) { | 95 if (IsUseLastOpenDefault()) { |
| 105 EXPECT_EQ(SessionStartupPref::LAST, pref.type); | 96 EXPECT_EQ(SessionStartupPref::LAST, pref.type); |
| 106 EXPECT_EQ(0U, pref.urls.size()); | 97 EXPECT_EQ(0U, pref.urls.size()); |
| 107 } else { | 98 } else { |
| 108 EXPECT_EQ(SessionStartupPref::URLS, pref.type); | 99 EXPECT_EQ(SessionStartupPref::URLS, pref.type); |
| 109 EXPECT_EQ(1U, pref.urls.size()); | 100 EXPECT_EQ(1U, pref.urls.size()); |
| 110 EXPECT_EQ(GURL("http://chromium.org/"), pref.urls[0]); | 101 EXPECT_EQ(GURL("http://chromium.org/"), pref.urls[0]); |
| 111 } | 102 } |
| 112 } | 103 } |
| 113 | 104 |
| 114 // Checks to make sure that if the user had previously not selected anything | 105 // 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), | 106 // (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 | 107 // and the NTP is being used for the homepage, their preferences are migrated |
| 117 // to "Open the New Tab Page" on upgrade to M19. | 108 // to "Open the New Tab Page" on upgrade to M19. |
| 118 TEST_F(SessionStartupPrefTest, DefaultMigrationHomepageIsNTP) { | 109 TEST_F(SessionStartupPrefTest, DefaultMigrationHomepageIsNTP) { |
| 119 registry()->RegisterStringPref( | 110 registry()->RegisterStringPref(prefs::kHomePage, "http://google.com/"); |
| 120 prefs::kHomePage, | |
| 121 "http://google.com/", | |
| 122 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
| 123 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); | 111 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); |
| 124 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, true); | 112 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, true); |
| 125 | 113 |
| 126 EXPECT_FALSE(pref_service_->HasPrefPath(prefs::kRestoreOnStartup)); | 114 EXPECT_FALSE(pref_service_->HasPrefPath(prefs::kRestoreOnStartup)); |
| 127 | 115 |
| 128 SessionStartupPref pref = SessionStartupPref::GetStartupPref( | 116 SessionStartupPref pref = SessionStartupPref::GetStartupPref( |
| 129 pref_service_.get()); | 117 pref_service_.get()); |
| 130 | 118 |
| 131 if (IsUseLastOpenDefault()) | 119 if (IsUseLastOpenDefault()) |
| 132 EXPECT_EQ(SessionStartupPref::LAST, pref.type); | 120 EXPECT_EQ(SessionStartupPref::LAST, pref.type); |
| 133 else | 121 else |
| 134 EXPECT_EQ(SessionStartupPref::DEFAULT, pref.type); | 122 EXPECT_EQ(SessionStartupPref::DEFAULT, pref.type); |
| 135 | 123 |
| 136 // The "URLs to restore on startup" shouldn't get migrated. | 124 // The "URLs to restore on startup" shouldn't get migrated. |
| 137 EXPECT_EQ(0U, pref.urls.size()); | 125 EXPECT_EQ(0U, pref.urls.size()); |
| 138 } | 126 } |
| 139 | 127 |
| 140 // Checks to make sure that if the user had previously selected "Open the | 128 // Checks to make sure that if the user had previously selected "Open the |
| 141 // "homepage", their preferences are migrated on upgrade to M19. | 129 // "homepage", their preferences are migrated on upgrade to M19. |
| 142 TEST_F(SessionStartupPrefTest, HomePageMigration) { | 130 TEST_F(SessionStartupPrefTest, HomePageMigration) { |
| 143 registry()->RegisterStringPref( | 131 registry()->RegisterStringPref(prefs::kHomePage, "http://google.com/"); |
| 144 prefs::kHomePage, | |
| 145 "http://google.com/", | |
| 146 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
| 147 | 132 |
| 148 // By design, it's impossible to set the 'restore on startup' pref to 0 | 133 // By design, it's impossible to set the 'restore on startup' pref to 0 |
| 149 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it | 134 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it |
| 150 // using the pref service directly. | 135 // using the pref service directly. |
| 151 pref_service_->SetInteger(prefs::kRestoreOnStartup, /*kPrefValueHomePage*/ 0); | 136 pref_service_->SetInteger(prefs::kRestoreOnStartup, /*kPrefValueHomePage*/ 0); |
| 152 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); | 137 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); |
| 153 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, false); | 138 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, false); |
| 154 | 139 |
| 155 SessionStartupPref pref = SessionStartupPref::GetStartupPref( | 140 SessionStartupPref pref = SessionStartupPref::GetStartupPref( |
| 156 pref_service_.get()); | 141 pref_service_.get()); |
| 157 | 142 |
| 158 EXPECT_EQ(SessionStartupPref::URLS, pref.type); | 143 EXPECT_EQ(SessionStartupPref::URLS, pref.type); |
| 159 EXPECT_EQ(1U, pref.urls.size()); | 144 EXPECT_EQ(1U, pref.urls.size()); |
| 160 EXPECT_EQ(GURL("http://chromium.org/"), pref.urls[0]); | 145 EXPECT_EQ(GURL("http://chromium.org/"), pref.urls[0]); |
| 161 } | 146 } |
| 162 | 147 |
| 163 // Checks to make sure that if the user had previously selected "Open the | 148 // 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 | 149 // "homepage", and the NTP is being used for the homepage, their preferences |
| 165 // are migrated on upgrade to M19. | 150 // are migrated on upgrade to M19. |
| 166 TEST_F(SessionStartupPrefTest, HomePageMigrationHomepageIsNTP) { | 151 TEST_F(SessionStartupPrefTest, HomePageMigrationHomepageIsNTP) { |
| 167 registry()->RegisterStringPref( | 152 registry()->RegisterStringPref(prefs::kHomePage, "http://google.com/"); |
| 168 prefs::kHomePage, | |
| 169 "http://google.com/", | |
| 170 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
| 171 | 153 |
| 172 // By design, it's impossible to set the 'restore on startup' pref to 0 | 154 // By design, it's impossible to set the 'restore on startup' pref to 0 |
| 173 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it | 155 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it |
| 174 // using the pref service directly. | 156 // using the pref service directly. |
| 175 pref_service_->SetInteger(prefs::kRestoreOnStartup, /*kPrefValueHomePage*/ 0); | 157 pref_service_->SetInteger(prefs::kRestoreOnStartup, /*kPrefValueHomePage*/ 0); |
| 176 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); | 158 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); |
| 177 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, true); | 159 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, true); |
| 178 | 160 |
| 179 SessionStartupPref pref = SessionStartupPref::GetStartupPref( | 161 SessionStartupPref pref = SessionStartupPref::GetStartupPref( |
| 180 pref_service_.get()); | 162 pref_service_.get()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 194 | 176 |
| 195 // Trigger the migration. | 177 // Trigger the migration. |
| 196 SessionStartupPref pref = SessionStartupPref::GetStartupPref( | 178 SessionStartupPref pref = SessionStartupPref::GetStartupPref( |
| 197 pref_service_.get()); | 179 pref_service_.get()); |
| 198 | 180 |
| 199 // The pref is now explicit. | 181 // The pref is now explicit. |
| 200 EXPECT_EQ(SessionStartupPref::LAST, pref.type); | 182 EXPECT_EQ(SessionStartupPref::LAST, pref.type); |
| 201 EXPECT_FALSE(SessionStartupPref::TypeIsDefault(pref_service_.get())); | 183 EXPECT_FALSE(SessionStartupPref::TypeIsDefault(pref_service_.get())); |
| 202 } | 184 } |
| 203 #endif | 185 #endif |
| OLD | NEW |