| 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/user_prefs/pref_registry_syncable.h" | 8 #include "components/user_prefs/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 virtual void SetUp() { | 19 virtual void SetUp() { |
| 20 pref_service_.reset(new TestingPrefServiceSyncable); | 20 pref_service_.reset(new TestingPrefServiceSyncable); |
| 21 SessionStartupPref::RegisterUserPrefs(registry()); | 21 SessionStartupPref::RegisterUserPrefs(registry()); |
| 22 registry()->RegisterBooleanPref(prefs::kHomePageIsNewTabPage, | 22 registry()->RegisterBooleanPref( |
| 23 true, | 23 prefs::kHomePageIsNewTabPage, |
| 24 PrefRegistrySyncable::UNSYNCABLE_PREF); | 24 true, |
| 25 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 25 // Make the tests independent of the Mac startup pref migration (see | 26 // Make the tests independent of the Mac startup pref migration (see |
| 26 // SessionStartupPref::MigrateMacDefaultPrefIfNecessary). | 27 // SessionStartupPref::MigrateMacDefaultPrefIfNecessary). |
| 27 registry()->RegisterStringPref(prefs::kProfileCreatedByVersion, | 28 registry()->RegisterStringPref( |
| 28 "22.0.0.0", | 29 prefs::kProfileCreatedByVersion, |
| 29 PrefRegistrySyncable::UNSYNCABLE_PREF); | 30 "22.0.0.0", |
| 31 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 30 } | 32 } |
| 31 | 33 |
| 32 bool IsUseLastOpenDefault() { | 34 bool IsUseLastOpenDefault() { |
| 33 // On ChromeOS, the default SessionStartupPref is LAST. | 35 // On ChromeOS, the default SessionStartupPref is LAST. |
| 34 #if defined(OS_CHROMEOS) | 36 #if defined(OS_CHROMEOS) |
| 35 return true; | 37 return true; |
| 36 #else | 38 #else |
| 37 return false; | 39 return false; |
| 38 #endif | 40 #endif |
| 39 } | 41 } |
| 40 | 42 |
| 41 PrefRegistrySyncable* registry() { | 43 user_prefs::PrefRegistrySyncable* registry() { |
| 42 return pref_service_->registry(); | 44 return pref_service_->registry(); |
| 43 } | 45 } |
| 44 | 46 |
| 45 scoped_ptr<TestingPrefServiceSyncable> pref_service_; | 47 scoped_ptr<TestingPrefServiceSyncable> pref_service_; |
| 46 }; | 48 }; |
| 47 | 49 |
| 48 TEST_F(SessionStartupPrefTest, URLListIsFixedUp) { | 50 TEST_F(SessionStartupPrefTest, URLListIsFixedUp) { |
| 49 ListValue* url_pref_list = new ListValue; | 51 ListValue* url_pref_list = new ListValue; |
| 50 url_pref_list->Set(0, Value::CreateStringValue("google.com")); | 52 url_pref_list->Set(0, Value::CreateStringValue("google.com")); |
| 51 url_pref_list->Set(1, Value::CreateStringValue("chromium.org")); | 53 url_pref_list->Set(1, Value::CreateStringValue("chromium.org")); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 80 SessionStartupPref::SetStartupPref(pref_service_.get(), override_test); | 82 SessionStartupPref::SetStartupPref(pref_service_.get(), override_test); |
| 81 | 83 |
| 82 result = SessionStartupPref::GetStartupPref(pref_service_.get()); | 84 result = SessionStartupPref::GetStartupPref(pref_service_.get()); |
| 83 EXPECT_EQ(3u, result.urls.size()); | 85 EXPECT_EQ(3u, result.urls.size()); |
| 84 } | 86 } |
| 85 | 87 |
| 86 // Checks to make sure that if the user had previously not selected anything | 88 // 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), | 89 // (so that, in effect, the default value "Open the homepage" was selected), |
| 88 // their preferences are migrated on upgrade to m19. | 90 // their preferences are migrated on upgrade to m19. |
| 89 TEST_F(SessionStartupPrefTest, DefaultMigration) { | 91 TEST_F(SessionStartupPrefTest, DefaultMigration) { |
| 90 registry()->RegisterStringPref(prefs::kHomePage, "http://google.com/", | 92 registry()->RegisterStringPref( |
| 91 PrefRegistrySyncable::UNSYNCABLE_PREF); | 93 prefs::kHomePage, |
| 94 "http://google.com/", |
| 95 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 92 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); | 96 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); |
| 93 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, false); | 97 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, false); |
| 94 | 98 |
| 95 EXPECT_FALSE(pref_service_->HasPrefPath(prefs::kRestoreOnStartup)); | 99 EXPECT_FALSE(pref_service_->HasPrefPath(prefs::kRestoreOnStartup)); |
| 96 | 100 |
| 97 SessionStartupPref pref = SessionStartupPref::GetStartupPref( | 101 SessionStartupPref pref = SessionStartupPref::GetStartupPref( |
| 98 pref_service_.get()); | 102 pref_service_.get()); |
| 99 | 103 |
| 100 if (IsUseLastOpenDefault()) { | 104 if (IsUseLastOpenDefault()) { |
| 101 EXPECT_EQ(SessionStartupPref::LAST, pref.type); | 105 EXPECT_EQ(SessionStartupPref::LAST, pref.type); |
| 102 EXPECT_EQ(0U, pref.urls.size()); | 106 EXPECT_EQ(0U, pref.urls.size()); |
| 103 } else { | 107 } else { |
| 104 EXPECT_EQ(SessionStartupPref::URLS, pref.type); | 108 EXPECT_EQ(SessionStartupPref::URLS, pref.type); |
| 105 EXPECT_EQ(1U, pref.urls.size()); | 109 EXPECT_EQ(1U, pref.urls.size()); |
| 106 EXPECT_EQ(GURL("http://chromium.org/"), pref.urls[0]); | 110 EXPECT_EQ(GURL("http://chromium.org/"), pref.urls[0]); |
| 107 } | 111 } |
| 108 } | 112 } |
| 109 | 113 |
| 110 // Checks to make sure that if the user had previously not selected anything | 114 // 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), | 115 // (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 | 116 // and the NTP is being used for the homepage, their preferences are migrated |
| 113 // to "Open the New Tab Page" on upgrade to M19. | 117 // to "Open the New Tab Page" on upgrade to M19. |
| 114 TEST_F(SessionStartupPrefTest, DefaultMigrationHomepageIsNTP) { | 118 TEST_F(SessionStartupPrefTest, DefaultMigrationHomepageIsNTP) { |
| 115 registry()->RegisterStringPref(prefs::kHomePage, "http://google.com/", | 119 registry()->RegisterStringPref( |
| 116 PrefRegistrySyncable::UNSYNCABLE_PREF); | 120 prefs::kHomePage, |
| 121 "http://google.com/", |
| 122 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 117 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); | 123 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); |
| 118 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, true); | 124 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, true); |
| 119 | 125 |
| 120 EXPECT_FALSE(pref_service_->HasPrefPath(prefs::kRestoreOnStartup)); | 126 EXPECT_FALSE(pref_service_->HasPrefPath(prefs::kRestoreOnStartup)); |
| 121 | 127 |
| 122 SessionStartupPref pref = SessionStartupPref::GetStartupPref( | 128 SessionStartupPref pref = SessionStartupPref::GetStartupPref( |
| 123 pref_service_.get()); | 129 pref_service_.get()); |
| 124 | 130 |
| 125 if (IsUseLastOpenDefault()) | 131 if (IsUseLastOpenDefault()) |
| 126 EXPECT_EQ(SessionStartupPref::LAST, pref.type); | 132 EXPECT_EQ(SessionStartupPref::LAST, pref.type); |
| 127 else | 133 else |
| 128 EXPECT_EQ(SessionStartupPref::DEFAULT, pref.type); | 134 EXPECT_EQ(SessionStartupPref::DEFAULT, pref.type); |
| 129 | 135 |
| 130 // The "URLs to restore on startup" shouldn't get migrated. | 136 // The "URLs to restore on startup" shouldn't get migrated. |
| 131 EXPECT_EQ(0U, pref.urls.size()); | 137 EXPECT_EQ(0U, pref.urls.size()); |
| 132 } | 138 } |
| 133 | 139 |
| 134 // Checks to make sure that if the user had previously selected "Open the | 140 // Checks to make sure that if the user had previously selected "Open the |
| 135 // "homepage", their preferences are migrated on upgrade to M19. | 141 // "homepage", their preferences are migrated on upgrade to M19. |
| 136 TEST_F(SessionStartupPrefTest, HomePageMigration) { | 142 TEST_F(SessionStartupPrefTest, HomePageMigration) { |
| 137 registry()->RegisterStringPref(prefs::kHomePage, "http://google.com/", | 143 registry()->RegisterStringPref( |
| 138 PrefRegistrySyncable::UNSYNCABLE_PREF); | 144 prefs::kHomePage, |
| 145 "http://google.com/", |
| 146 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 139 | 147 |
| 140 // By design, it's impossible to set the 'restore on startup' pref to 0 | 148 // By design, it's impossible to set the 'restore on startup' pref to 0 |
| 141 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it | 149 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it |
| 142 // using the pref service directly. | 150 // using the pref service directly. |
| 143 pref_service_->SetInteger(prefs::kRestoreOnStartup, /*kPrefValueHomePage*/ 0); | 151 pref_service_->SetInteger(prefs::kRestoreOnStartup, /*kPrefValueHomePage*/ 0); |
| 144 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); | 152 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); |
| 145 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, false); | 153 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, false); |
| 146 | 154 |
| 147 SessionStartupPref pref = SessionStartupPref::GetStartupPref( | 155 SessionStartupPref pref = SessionStartupPref::GetStartupPref( |
| 148 pref_service_.get()); | 156 pref_service_.get()); |
| 149 | 157 |
| 150 EXPECT_EQ(SessionStartupPref::URLS, pref.type); | 158 EXPECT_EQ(SessionStartupPref::URLS, pref.type); |
| 151 EXPECT_EQ(1U, pref.urls.size()); | 159 EXPECT_EQ(1U, pref.urls.size()); |
| 152 EXPECT_EQ(GURL("http://chromium.org/"), pref.urls[0]); | 160 EXPECT_EQ(GURL("http://chromium.org/"), pref.urls[0]); |
| 153 } | 161 } |
| 154 | 162 |
| 155 // Checks to make sure that if the user had previously selected "Open the | 163 // 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 | 164 // "homepage", and the NTP is being used for the homepage, their preferences |
| 157 // are migrated on upgrade to M19. | 165 // are migrated on upgrade to M19. |
| 158 TEST_F(SessionStartupPrefTest, HomePageMigrationHomepageIsNTP) { | 166 TEST_F(SessionStartupPrefTest, HomePageMigrationHomepageIsNTP) { |
| 159 registry()->RegisterStringPref(prefs::kHomePage, "http://google.com/", | 167 registry()->RegisterStringPref( |
| 160 PrefRegistrySyncable::UNSYNCABLE_PREF); | 168 prefs::kHomePage, |
| 169 "http://google.com/", |
| 170 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 161 | 171 |
| 162 // By design, it's impossible to set the 'restore on startup' pref to 0 | 172 // By design, it's impossible to set the 'restore on startup' pref to 0 |
| 163 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it | 173 // ("open the homepage") using SessionStartupPref::SetStartupPref(), so set it |
| 164 // using the pref service directly. | 174 // using the pref service directly. |
| 165 pref_service_->SetInteger(prefs::kRestoreOnStartup, /*kPrefValueHomePage*/ 0); | 175 pref_service_->SetInteger(prefs::kRestoreOnStartup, /*kPrefValueHomePage*/ 0); |
| 166 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); | 176 pref_service_->SetString(prefs::kHomePage, "http://chromium.org/"); |
| 167 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, true); | 177 pref_service_->SetBoolean(prefs::kHomePageIsNewTabPage, true); |
| 168 | 178 |
| 169 SessionStartupPref pref = SessionStartupPref::GetStartupPref( | 179 SessionStartupPref pref = SessionStartupPref::GetStartupPref( |
| 170 pref_service_.get()); | 180 pref_service_.get()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 184 | 194 |
| 185 // Trigger the migration. | 195 // Trigger the migration. |
| 186 SessionStartupPref pref = SessionStartupPref::GetStartupPref( | 196 SessionStartupPref pref = SessionStartupPref::GetStartupPref( |
| 187 pref_service_.get()); | 197 pref_service_.get()); |
| 188 | 198 |
| 189 // The pref is now explicit. | 199 // The pref is now explicit. |
| 190 EXPECT_EQ(SessionStartupPref::LAST, pref.type); | 200 EXPECT_EQ(SessionStartupPref::LAST, pref.type); |
| 191 EXPECT_FALSE(SessionStartupPref::TypeIsDefault(pref_service_.get())); | 201 EXPECT_FALSE(SessionStartupPref::TypeIsDefault(pref_service_.get())); |
| 192 } | 202 } |
| 193 #endif | 203 #endif |
| OLD | NEW |