| 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/ui/toolbar/toolbar_model.h" | 5 #include "chrome/browser/ui/toolbar/toolbar_model.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 | 146 |
| 147 class ToolbarModelTest : public BrowserWithTestWindowTest { | 147 class ToolbarModelTest : public BrowserWithTestWindowTest { |
| 148 public: | 148 public: |
| 149 ToolbarModelTest(); | 149 ToolbarModelTest(); |
| 150 virtual ~ToolbarModelTest(); | 150 virtual ~ToolbarModelTest(); |
| 151 | 151 |
| 152 // BrowserWithTestWindowTest: | 152 // BrowserWithTestWindowTest: |
| 153 virtual void SetUp() OVERRIDE; | 153 virtual void SetUp() OVERRIDE; |
| 154 | 154 |
| 155 protected: | 155 protected: |
| 156 void ResetDefaultTemplateURL(); | |
| 157 void NavigateAndCheckText(const GURL& url, | 156 void NavigateAndCheckText(const GURL& url, |
| 158 const string16& expected_text, | 157 const string16& expected_text, |
| 159 const string16& expected_replace_text, | 158 const string16& expected_replace_text, |
| 160 bool would_perform_search_term_replacement, | 159 bool would_perform_search_term_replacement, |
| 161 bool should_display_url); | 160 bool should_display_url); |
| 162 | 161 |
| 163 private: | 162 private: |
| 164 void ResetTemplateURLForInstant(const GURL& instant_url); | |
| 165 void NavigateAndCheckTextImpl(const GURL& url, | 163 void NavigateAndCheckTextImpl(const GURL& url, |
| 166 bool allow_search_term_replacement, | 164 bool allow_search_term_replacement, |
| 167 const string16 expected_text, | 165 const string16 expected_text, |
| 168 bool would_perform_search_term_replacement, | 166 bool would_perform_search_term_replacement, |
| 169 bool should_display); | 167 bool should_display); |
| 170 | 168 |
| 171 DISALLOW_COPY_AND_ASSIGN(ToolbarModelTest); | 169 DISALLOW_COPY_AND_ASSIGN(ToolbarModelTest); |
| 172 }; | 170 }; |
| 173 | 171 |
| 174 ToolbarModelTest::ToolbarModelTest() { | 172 ToolbarModelTest::ToolbarModelTest() { |
| 175 } | 173 } |
| 176 | 174 |
| 177 ToolbarModelTest::~ToolbarModelTest() { | 175 ToolbarModelTest::~ToolbarModelTest() { |
| 178 } | 176 } |
| 179 | 177 |
| 180 void ToolbarModelTest::SetUp() { | 178 void ToolbarModelTest::SetUp() { |
| 181 BrowserWithTestWindowTest::SetUp(); | 179 BrowserWithTestWindowTest::SetUp(); |
| 182 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 180 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 183 profile(), &TemplateURLServiceFactory::BuildInstanceFor); | 181 profile(), &TemplateURLServiceFactory::BuildInstanceFor); |
| 184 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse( | 182 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse( |
| 185 profile(), &AutocompleteClassifierFactory::BuildInstanceFor); | 183 profile(), &AutocompleteClassifierFactory::BuildInstanceFor); |
| 186 UIThreadSearchTermsData::SetGoogleBaseURL("http://google.com/"); | 184 UIThreadSearchTermsData::SetGoogleBaseURL("http://google.com/"); |
| 187 } | 185 } |
| 188 | 186 |
| 189 void ToolbarModelTest::ResetDefaultTemplateURL() { | |
| 190 ResetTemplateURLForInstant(GURL("http://does/not/exist")); | |
| 191 } | |
| 192 | |
| 193 void ToolbarModelTest::NavigateAndCheckText( | 187 void ToolbarModelTest::NavigateAndCheckText( |
| 194 const GURL& url, | 188 const GURL& url, |
| 195 const string16& expected_text, | 189 const string16& expected_text, |
| 196 const string16& expected_replace_text, | 190 const string16& expected_replace_text, |
| 197 bool would_perform_search_term_replacement, | 191 bool would_perform_search_term_replacement, |
| 198 bool should_display_url) { | 192 bool should_display_url) { |
| 199 NavigateAndCheckTextImpl(url, false, expected_text, | 193 NavigateAndCheckTextImpl(url, false, expected_text, |
| 200 would_perform_search_term_replacement, | 194 would_perform_search_term_replacement, |
| 201 should_display_url); | 195 should_display_url); |
| 202 NavigateAndCheckTextImpl(url, true, expected_replace_text, | 196 NavigateAndCheckTextImpl(url, true, |
| 197 expected_replace_text, |
| 203 would_perform_search_term_replacement, | 198 would_perform_search_term_replacement, |
| 204 should_display_url); | 199 should_display_url); |
| 205 } | 200 } |
| 206 | 201 |
| 207 void ToolbarModelTest::ResetTemplateURLForInstant(const GURL& instant_url) { | |
| 208 TemplateURLData data; | |
| 209 data.short_name = ASCIIToUTF16("Google"); | |
| 210 data.SetURL("{google:baseURL}search?q={searchTerms}"); | |
| 211 data.instant_url = instant_url.spec(); | |
| 212 data.search_terms_replacement_key = "{google:instantExtendedEnabledKey}"; | |
| 213 TemplateURL* search_template_url = new TemplateURL(profile(), data); | |
| 214 TemplateURLService* template_url_service = | |
| 215 TemplateURLServiceFactory::GetForProfile(profile()); | |
| 216 template_url_service->Add(search_template_url); | |
| 217 template_url_service->SetDefaultSearchProvider(search_template_url); | |
| 218 ASSERT_NE(0, search_template_url->id()); | |
| 219 template_url_service->Load(); | |
| 220 } | |
| 221 | |
| 222 void ToolbarModelTest::NavigateAndCheckTextImpl( | 202 void ToolbarModelTest::NavigateAndCheckTextImpl( |
| 223 const GURL& url, | 203 const GURL& url, |
| 224 bool allow_search_term_replacement, | 204 bool allow_search_term_replacement, |
| 225 const string16 expected_text, | 205 const string16 expected_text, |
| 226 bool would_perform_search_term_replacement, | 206 bool would_perform_search_term_replacement, |
| 227 bool should_display_url) { | 207 bool should_display_url) { |
| 228 // The URL being navigated to should be treated as the Instant URL. Else | |
| 229 // there will be no search term extraction. | |
| 230 ResetTemplateURLForInstant(url); | |
| 231 | |
| 232 // Check while loading. | 208 // Check while loading. |
| 233 content::NavigationController* controller = | 209 content::NavigationController* controller = |
| 234 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); | 210 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); |
| 235 controller->LoadURL(url, content::Referrer(), content::PAGE_TRANSITION_LINK, | 211 controller->LoadURL(url, content::Referrer(), content::PAGE_TRANSITION_LINK, |
| 236 std::string()); | 212 std::string()); |
| 237 ToolbarModel* toolbar_model = browser()->toolbar_model(); | 213 ToolbarModel* toolbar_model = browser()->toolbar_model(); |
| 238 EXPECT_EQ(should_display_url, toolbar_model->ShouldDisplayURL()); | 214 EXPECT_EQ(should_display_url, toolbar_model->ShouldDisplayURL()); |
| 239 EXPECT_EQ(expected_text, | 215 EXPECT_EQ(expected_text, |
| 240 toolbar_model->GetText(allow_search_term_replacement)); | 216 toolbar_model->GetText(allow_search_term_replacement)); |
| 241 EXPECT_EQ(would_perform_search_term_replacement, | 217 EXPECT_EQ(would_perform_search_term_replacement, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 271 | 247 |
| 272 // Test that we only replace URLs when query extraction and search term | 248 // Test that we only replace URLs when query extraction and search term |
| 273 // replacement are enabled. | 249 // replacement are enabled. |
| 274 TEST_F(ToolbarModelTest, ShouldDisplayURL) { | 250 TEST_F(ToolbarModelTest, ShouldDisplayURL) { |
| 275 // Before we enable instant extended, query extraction is disabled. | 251 // Before we enable instant extended, query extraction is disabled. |
| 276 EXPECT_FALSE(chrome::IsQueryExtractionEnabled()) | 252 EXPECT_FALSE(chrome::IsQueryExtractionEnabled()) |
| 277 << "This test expects query extraction to be disabled."; | 253 << "This test expects query extraction to be disabled."; |
| 278 AddTab(browser(), GURL(content::kAboutBlankURL)); | 254 AddTab(browser(), GURL(content::kAboutBlankURL)); |
| 279 for (size_t i = 0; i < arraysize(test_items); ++i) { | 255 for (size_t i = 0; i < arraysize(test_items); ++i) { |
| 280 const TestItem& test_item = test_items[i]; | 256 const TestItem& test_item = test_items[i]; |
| 281 NavigateAndCheckText(test_item.url, test_item.expected_text, | 257 NavigateAndCheckText(test_item.url, |
| 258 test_item.expected_text, |
| 282 test_item.expected_replace_text_inactive, false, | 259 test_item.expected_replace_text_inactive, false, |
| 283 test_item.should_display_url); | 260 test_item.should_display_url); |
| 284 } | 261 } |
| 285 | 262 |
| 286 // Once we enable it, query extraction and search term replacement are | 263 chrome::EnableQueryExtractionForTesting(); |
| 287 // enabled by default. | |
| 288 chrome::EnableInstantExtendedAPIForTesting(); | |
| 289 EXPECT_TRUE(chrome::IsQueryExtractionEnabled()); | 264 EXPECT_TRUE(chrome::IsQueryExtractionEnabled()); |
| 290 EXPECT_TRUE(browser()->toolbar_model()->search_term_replacement_enabled()); | 265 EXPECT_TRUE(browser()->toolbar_model()->search_term_replacement_enabled()); |
| 291 for (size_t i = 0; i < arraysize(test_items); ++i) { | 266 for (size_t i = 0; i < arraysize(test_items); ++i) { |
| 292 const TestItem& test_item = test_items[i]; | 267 const TestItem& test_item = test_items[i]; |
| 293 NavigateAndCheckText(test_item.url, test_item.expected_text, | 268 NavigateAndCheckText(test_item.url, |
| 269 test_item.expected_text, |
| 294 test_item.expected_replace_text_active, | 270 test_item.expected_replace_text_active, |
| 295 test_item.would_perform_search_term_replacement, | 271 test_item.would_perform_search_term_replacement, |
| 296 test_item.should_display_url); | 272 test_item.should_display_url); |
| 297 } | 273 } |
| 298 | 274 |
| 299 // Disabling search term replacement should reset to only showing URLs. | 275 // Disabling search term replacement should reset to only showing URLs. |
| 300 browser()->toolbar_model()->set_search_term_replacement_enabled(false); | 276 browser()->toolbar_model()->set_search_term_replacement_enabled(false); |
| 301 for (size_t i = 0; i < arraysize(test_items); ++i) { | 277 for (size_t i = 0; i < arraysize(test_items); ++i) { |
| 302 const TestItem& test_item = test_items[i]; | 278 const TestItem& test_item = test_items[i]; |
| 303 NavigateAndCheckText(test_item.url, test_item.expected_text, | 279 NavigateAndCheckText(test_item.url, |
| 280 test_item.expected_text, |
| 304 test_item.expected_replace_text_inactive, false, | 281 test_item.expected_replace_text_inactive, false, |
| 305 test_item.should_display_url); | 282 test_item.should_display_url); |
| 306 } | 283 } |
| 307 } | 284 } |
| 308 | 285 |
| 309 // Verify that search terms are extracted while the page is loading. | 286 // Verify that search terms are extracted while the page is loading. |
| 310 TEST_F(ToolbarModelTest, SearchTermsWhileLoading) { | 287 TEST_F(ToolbarModelTest, SearchTermsWhileLoading) { |
| 311 chrome::EnableInstantExtendedAPIForTesting(); | 288 chrome::EnableQueryExtractionForTesting(); |
| 312 ResetDefaultTemplateURL(); | |
| 313 AddTab(browser(), GURL(content::kAboutBlankURL)); | 289 AddTab(browser(), GURL(content::kAboutBlankURL)); |
| 314 | 290 |
| 315 // While loading, we should be willing to extract search terms. | 291 // While loading, we should be willing to extract search terms. |
| 316 content::NavigationController* controller = | 292 content::NavigationController* controller = |
| 317 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); | 293 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); |
| 318 controller->LoadURL(GURL("https://google.com/search?q=tractor+supply&espv=1"), | 294 controller->LoadURL(GURL("https://google.com/search?q=tractor+supply&espv=1"), |
| 319 content::Referrer(), content::PAGE_TRANSITION_LINK, | 295 content::Referrer(), content::PAGE_TRANSITION_LINK, |
| 320 std::string()); | 296 std::string()); |
| 321 ToolbarModel* toolbar_model = browser()->toolbar_model(); | 297 ToolbarModel* toolbar_model = browser()->toolbar_model(); |
| 322 controller->GetVisibleEntry()->GetSSL().security_style = | 298 controller->GetVisibleEntry()->GetSSL().security_style = |
| 323 content::SECURITY_STYLE_UNKNOWN; | 299 content::SECURITY_STYLE_UNKNOWN; |
| 324 EXPECT_TRUE(toolbar_model->WouldPerformSearchTermReplacement(false)); | 300 EXPECT_TRUE(toolbar_model->WouldPerformSearchTermReplacement(false)); |
| 325 | 301 |
| 326 // When done loading, we shouldn't extract search terms if we didn't get an | 302 // When done loading, we shouldn't extract search terms if we didn't get an |
| 327 // authenticated connection. | 303 // authenticated connection. |
| 328 CommitPendingLoad(controller); | 304 CommitPendingLoad(controller); |
| 329 controller->GetVisibleEntry()->GetSSL().security_style = | 305 controller->GetVisibleEntry()->GetSSL().security_style = |
| 330 content::SECURITY_STYLE_UNKNOWN; | 306 content::SECURITY_STYLE_UNKNOWN; |
| 331 EXPECT_FALSE(toolbar_model->WouldPerformSearchTermReplacement(false)); | 307 EXPECT_FALSE(toolbar_model->WouldPerformSearchTermReplacement(false)); |
| 332 } | 308 } |
| 333 | 309 |
| 334 // When the Google base URL is overridden on the command line, we should extract | 310 // When the Google base URL is overridden on the command line, we should extract |
| 335 // search terms from URLs that start with that base URL even when they're not | 311 // search terms from URLs that start with that base URL even when they're not |
| 336 // secure. | 312 // secure. |
| 337 TEST_F(ToolbarModelTest, GoogleBaseURL) { | 313 TEST_F(ToolbarModelTest, GoogleBaseURL) { |
| 338 chrome::EnableInstantExtendedAPIForTesting(); | 314 chrome::EnableQueryExtractionForTesting(); |
| 339 AddTab(browser(), GURL(content::kAboutBlankURL)); | 315 AddTab(browser(), GURL(content::kAboutBlankURL)); |
| 340 | 316 |
| 341 // If the Google base URL wasn't specified on the command line, then if it's | 317 // If the Google base URL wasn't specified on the command line, then if it's |
| 342 // HTTP, we should not extract search terms. | 318 // HTTP, we should not extract search terms. |
| 343 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.foo.com/"); | 319 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.foo.com/"); |
| 344 NavigateAndCheckText( | 320 NavigateAndCheckText( |
| 345 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"), | 321 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"), |
| 346 ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), | 322 ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), |
| 347 ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), false, | 323 ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), false, |
| 348 true); | 324 true); |
| 349 | 325 |
| 350 // The same URL, when specified on the command line, should allow search term | 326 // The same URL, when specified on the command line, should allow search term |
| 351 // extraction. | 327 // extraction. |
| 352 UIThreadSearchTermsData::SetGoogleBaseURL(std::string()); | 328 UIThreadSearchTermsData::SetGoogleBaseURL(std::string()); |
| 353 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL, | 329 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL, |
| 354 "http://www.foo.com/"); | 330 "http://www.foo.com/"); |
| 355 NavigateAndCheckText( | 331 NavigateAndCheckText( |
| 356 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"), | 332 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"), |
| 357 ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), | 333 ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), |
| 358 ASCIIToUTF16("tractor supply"), true, true); | 334 ASCIIToUTF16("tractor supply"), true, true); |
| 359 } | 335 } |
| OLD | NEW |