| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/memory/scoped_ptr.h" | |
| 6 #include "base/utf_string_conversions.h" | |
| 7 #include "base/values.h" | |
| 8 #include "chrome/browser/prefs/session_startup_pref.h" | |
| 9 #include "chrome/browser/protector/base_setting_change.h" | |
| 10 #include "chrome/browser/ui/startup/startup_tab.h" | |
| 11 #include "chrome/test/base/testing_profile.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 #include "grit/generated_resources.h" | |
| 14 #include "ui/base/l10n/l10n_util.h" | |
| 15 | |
| 16 namespace protector { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 const char kStartupUrl1[] = "http://google.com"; | |
| 21 const char kStartupUrl2[] = "http://example.com"; | |
| 22 const char kStartupUrl3[] = "http://example.org"; | |
| 23 const char kStartupHost1[] = "google.com"; | |
| 24 const char kStartupHost2[] = "example.com"; | |
| 25 const char kStartupHost3[] = "example.org"; | |
| 26 | |
| 27 const BaseSettingChange::DisplayName kNoDisplayName( | |
| 28 BaseSettingChange::kDefaultNamePriority, string16()); | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 class SessionStartupChangeTest : public testing::Test { | |
| 33 public: | |
| 34 SessionStartupChangeTest() | |
| 35 : initial_startup_pref_(SessionStartupPref::DEFAULT) { | |
| 36 } | |
| 37 | |
| 38 virtual void SetUp() OVERRIDE { | |
| 39 // Ensure initial session startup pref. | |
| 40 SessionStartupPref::SetStartupPref(&profile_, initial_startup_pref_); | |
| 41 } | |
| 42 | |
| 43 protected: | |
| 44 TestingProfile profile_; | |
| 45 SessionStartupPref initial_startup_pref_; | |
| 46 StartupTabs empty_pinned_tabs_; | |
| 47 }; | |
| 48 | |
| 49 TEST_F(SessionStartupChangeTest, InitAndApply) { | |
| 50 // Create a change and apply it. | |
| 51 SessionStartupPref backup_startup_pref(SessionStartupPref::LAST); | |
| 52 scoped_ptr<BaseSettingChange> change( | |
| 53 CreateSessionStartupChange(initial_startup_pref_, empty_pinned_tabs_, | |
| 54 backup_startup_pref, empty_pinned_tabs_)); | |
| 55 ASSERT_TRUE(change->Init(&profile_)); | |
| 56 // Setting is initially reverted to backup. | |
| 57 EXPECT_EQ(SessionStartupPref::LAST, | |
| 58 SessionStartupPref::GetStartupPref(&profile_).type); | |
| 59 change->Apply(NULL); // |browser| is unused. | |
| 60 // New setting active now. | |
| 61 EXPECT_EQ(SessionStartupPref::DEFAULT, | |
| 62 SessionStartupPref::GetStartupPref(&profile_).type); | |
| 63 } | |
| 64 | |
| 65 TEST_F(SessionStartupChangeTest, InitAndDiscard) { | |
| 66 // Create a change and discard it. | |
| 67 SessionStartupPref backup_startup_pref(SessionStartupPref::LAST); | |
| 68 scoped_ptr<BaseSettingChange> change( | |
| 69 CreateSessionStartupChange(initial_startup_pref_, empty_pinned_tabs_, | |
| 70 backup_startup_pref, empty_pinned_tabs_)); | |
| 71 ASSERT_TRUE(change->Init(&profile_)); | |
| 72 // Setting is initially reverted to backup. | |
| 73 EXPECT_EQ(SessionStartupPref::LAST, | |
| 74 SessionStartupPref::GetStartupPref(&profile_).type); | |
| 75 change->Discard(NULL); // |browser| is unused. | |
| 76 // Nothing changed by Discard. | |
| 77 EXPECT_EQ(SessionStartupPref::LAST, | |
| 78 SessionStartupPref::GetStartupPref(&profile_).type); | |
| 79 } | |
| 80 | |
| 81 TEST_F(SessionStartupChangeTest, ApplyButtonCaptions) { | |
| 82 // Apply button captions for "Open NTP" and "Open specific URLs" cases. | |
| 83 string16 open_ntp_caption = | |
| 84 l10n_util::GetStringUTF16(IDS_CHANGE_STARTUP_SETTINGS_NTP); | |
| 85 string16 open_url1_etc_caption = | |
| 86 l10n_util::GetStringFUTF16(IDS_CHANGE_STARTUP_SETTINGS_URLS, | |
| 87 UTF8ToUTF16(GURL(kStartupUrl1).host())); | |
| 88 string16 open_url2_etc_caption = | |
| 89 l10n_util::GetStringFUTF16(IDS_CHANGE_STARTUP_SETTINGS_URLS, | |
| 90 UTF8ToUTF16(GURL(kStartupUrl2).host())); | |
| 91 string16 open_url3_etc_caption = | |
| 92 l10n_util::GetStringFUTF16(IDS_CHANGE_STARTUP_SETTINGS_URLS, | |
| 93 UTF8ToUTF16(GURL(kStartupUrl3).host())); | |
| 94 | |
| 95 // Open NTP. | |
| 96 initial_startup_pref_.type = SessionStartupPref::DEFAULT; | |
| 97 SessionStartupPref backup_startup_pref(SessionStartupPref::DEFAULT); | |
| 98 scoped_ptr<BaseSettingChange> change( | |
| 99 CreateSessionStartupChange(initial_startup_pref_, empty_pinned_tabs_, | |
| 100 backup_startup_pref, empty_pinned_tabs_)); | |
| 101 ASSERT_TRUE(change->Init(&profile_)); | |
| 102 EXPECT_EQ(open_ntp_caption, change->GetApplyButtonText()); | |
| 103 EXPECT_EQ(GURL(), change->GetNewSettingURL()); | |
| 104 EXPECT_EQ(kNoDisplayName, change->GetApplyDisplayName()); | |
| 105 | |
| 106 // Pinned tabs count as startup URLs as well. | |
| 107 StartupTabs new_pinned_tabs; | |
| 108 StartupTab pinned_tab; | |
| 109 pinned_tab.url = GURL(kStartupUrl3); | |
| 110 new_pinned_tabs.push_back(pinned_tab); | |
| 111 change.reset( | |
| 112 CreateSessionStartupChange(initial_startup_pref_, new_pinned_tabs, | |
| 113 backup_startup_pref, empty_pinned_tabs_)); | |
| 114 ASSERT_TRUE(change->Init(&profile_)); | |
| 115 EXPECT_EQ(open_url3_etc_caption, change->GetApplyButtonText()); | |
| 116 EXPECT_EQ(GURL(kStartupUrl3), change->GetNewSettingURL()); | |
| 117 EXPECT_EQ(UTF8ToUTF16(kStartupHost3), change->GetApplyDisplayName().second); | |
| 118 | |
| 119 // "Open URLs" with no URLs is the same as "Open NTP". | |
| 120 initial_startup_pref_.type = SessionStartupPref::URLS; | |
| 121 change.reset( | |
| 122 CreateSessionStartupChange(initial_startup_pref_, empty_pinned_tabs_, | |
| 123 backup_startup_pref, empty_pinned_tabs_)); | |
| 124 ASSERT_TRUE(change->Init(&profile_)); | |
| 125 EXPECT_EQ(open_ntp_caption, change->GetApplyButtonText()); | |
| 126 EXPECT_EQ(GURL(), change->GetNewSettingURL()); | |
| 127 EXPECT_EQ(kNoDisplayName, change->GetApplyDisplayName()); | |
| 128 | |
| 129 // Single URL. | |
| 130 initial_startup_pref_.urls.push_back(GURL(kStartupUrl1)); | |
| 131 change.reset( | |
| 132 CreateSessionStartupChange(initial_startup_pref_, empty_pinned_tabs_, | |
| 133 backup_startup_pref, empty_pinned_tabs_)); | |
| 134 ASSERT_TRUE(change->Init(&profile_)); | |
| 135 EXPECT_EQ(open_url1_etc_caption, change->GetApplyButtonText()); | |
| 136 EXPECT_EQ(GURL(kStartupUrl1), change->GetNewSettingURL()); | |
| 137 EXPECT_EQ(UTF8ToUTF16(kStartupHost1), change->GetApplyDisplayName().second); | |
| 138 | |
| 139 // Multiple URLs: name of the first one used. | |
| 140 initial_startup_pref_.urls.push_back(GURL(kStartupUrl2)); | |
| 141 change.reset( | |
| 142 CreateSessionStartupChange(initial_startup_pref_, empty_pinned_tabs_, | |
| 143 backup_startup_pref, empty_pinned_tabs_)); | |
| 144 ASSERT_TRUE(change->Init(&profile_)); | |
| 145 EXPECT_EQ(open_url1_etc_caption, change->GetApplyButtonText()); | |
| 146 EXPECT_EQ(GURL(kStartupUrl1), change->GetNewSettingURL()); | |
| 147 EXPECT_EQ(UTF8ToUTF16(kStartupHost1), change->GetApplyDisplayName().second); | |
| 148 | |
| 149 // Pinned tabs go after the startup URLs. | |
| 150 change.reset( | |
| 151 CreateSessionStartupChange(initial_startup_pref_, new_pinned_tabs, | |
| 152 backup_startup_pref, empty_pinned_tabs_)); | |
| 153 ASSERT_TRUE(change->Init(&profile_)); | |
| 154 EXPECT_EQ(open_url1_etc_caption, change->GetApplyButtonText()); | |
| 155 EXPECT_EQ(GURL(kStartupUrl1), change->GetNewSettingURL()); | |
| 156 EXPECT_EQ(UTF8ToUTF16(kStartupHost1), change->GetApplyDisplayName().second); | |
| 157 | |
| 158 // Multiple URLs, the first present in both backup and new settings. | |
| 159 backup_startup_pref.type = SessionStartupPref::URLS; | |
| 160 backup_startup_pref.urls.push_back(GURL(kStartupUrl1)); | |
| 161 change.reset( | |
| 162 CreateSessionStartupChange(initial_startup_pref_, empty_pinned_tabs_, | |
| 163 backup_startup_pref, empty_pinned_tabs_)); | |
| 164 ASSERT_TRUE(change->Init(&profile_)); | |
| 165 EXPECT_EQ(open_url2_etc_caption, change->GetApplyButtonText()); | |
| 166 EXPECT_EQ(GURL(kStartupUrl2), change->GetNewSettingURL()); | |
| 167 EXPECT_EQ(UTF8ToUTF16(kStartupHost2), change->GetApplyDisplayName().second); | |
| 168 | |
| 169 // Multiple URLs, all present in both backup and new settings: the first one | |
| 170 // is displayed in that case. | |
| 171 backup_startup_pref.urls.push_back(GURL(kStartupUrl2)); | |
| 172 change.reset( | |
| 173 CreateSessionStartupChange(initial_startup_pref_, empty_pinned_tabs_, | |
| 174 backup_startup_pref, empty_pinned_tabs_)); | |
| 175 ASSERT_TRUE(change->Init(&profile_)); | |
| 176 EXPECT_EQ(open_url1_etc_caption, change->GetApplyButtonText()); | |
| 177 EXPECT_EQ(GURL(kStartupUrl1), change->GetNewSettingURL()); | |
| 178 EXPECT_EQ(UTF8ToUTF16(kStartupHost1), change->GetApplyDisplayName().second); | |
| 179 | |
| 180 // Multiple URLs, all present in both backup and new settings, new pinned tabs | |
| 181 // added: the first pinned tab URL is used. | |
| 182 change.reset( | |
| 183 CreateSessionStartupChange(initial_startup_pref_, new_pinned_tabs, | |
| 184 backup_startup_pref, empty_pinned_tabs_)); | |
| 185 ASSERT_TRUE(change->Init(&profile_)); | |
| 186 EXPECT_EQ(open_url3_etc_caption, change->GetApplyButtonText()); | |
| 187 EXPECT_EQ(GURL(kStartupUrl3), change->GetNewSettingURL()); | |
| 188 EXPECT_EQ(UTF8ToUTF16(kStartupHost3), change->GetApplyDisplayName().second); | |
| 189 } | |
| 190 | |
| 191 } // namespace protector | |
| OLD | NEW |