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