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