Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(995)

Side by Side Diff: chrome/browser/ui/toolbar/toolbar_model_unittest.cc

Issue 24733003: Update defaults for InstantExtended. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More comments addressed. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
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(); 156 void ResetDefaultTemplateURL();
157 void NavigateAndCheckText(const GURL& url, 157 void NavigateAndCheckText(const GURL& url,
158 const std::string& template_search_url,
158 const string16& expected_text, 159 const string16& expected_text,
159 const string16& expected_replace_text, 160 const string16& expected_replace_text,
160 bool would_perform_search_term_replacement, 161 bool would_perform_search_term_replacement,
161 bool should_display_url); 162 bool should_display_url);
162 163
163 private: 164 private:
164 void ResetTemplateURLForInstant(const GURL& instant_url); 165 void ResetTemplateURLForInstant(const std::string& template_search_url);
165 void NavigateAndCheckTextImpl(const GURL& url, 166 void NavigateAndCheckTextImpl(const GURL& url,
167 const std::string& template_search_url,
166 bool allow_search_term_replacement, 168 bool allow_search_term_replacement,
167 const string16 expected_text, 169 const string16 expected_text,
168 bool would_perform_search_term_replacement, 170 bool would_perform_search_term_replacement,
169 bool should_display); 171 bool should_display);
170 172
171 DISALLOW_COPY_AND_ASSIGN(ToolbarModelTest); 173 DISALLOW_COPY_AND_ASSIGN(ToolbarModelTest);
172 }; 174 };
173 175
174 ToolbarModelTest::ToolbarModelTest() { 176 ToolbarModelTest::ToolbarModelTest() {
175 } 177 }
176 178
177 ToolbarModelTest::~ToolbarModelTest() { 179 ToolbarModelTest::~ToolbarModelTest() {
178 } 180 }
179 181
180 void ToolbarModelTest::SetUp() { 182 void ToolbarModelTest::SetUp() {
181 BrowserWithTestWindowTest::SetUp(); 183 BrowserWithTestWindowTest::SetUp();
182 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( 184 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
183 profile(), &TemplateURLServiceFactory::BuildInstanceFor); 185 profile(), &TemplateURLServiceFactory::BuildInstanceFor);
184 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse( 186 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse(
185 profile(), &AutocompleteClassifierFactory::BuildInstanceFor); 187 profile(), &AutocompleteClassifierFactory::BuildInstanceFor);
186 UIThreadSearchTermsData::SetGoogleBaseURL("http://google.com/"); 188 UIThreadSearchTermsData::SetGoogleBaseURL("http://google.com/");
187 } 189 }
188 190
189 void ToolbarModelTest::ResetDefaultTemplateURL() { 191 void ToolbarModelTest::ResetDefaultTemplateURL() {
190 ResetTemplateURLForInstant(GURL("http://does/not/exist")); 192 ResetTemplateURLForInstant("{google:baseURL}search?q={searchTerms}&espv=1");
191 } 193 }
192 194
193 void ToolbarModelTest::NavigateAndCheckText( 195 void ToolbarModelTest::NavigateAndCheckText(
194 const GURL& url, 196 const GURL& url,
197 const std::string& template_search_url,
195 const string16& expected_text, 198 const string16& expected_text,
196 const string16& expected_replace_text, 199 const string16& expected_replace_text,
197 bool would_perform_search_term_replacement, 200 bool would_perform_search_term_replacement,
198 bool should_display_url) { 201 bool should_display_url) {
199 NavigateAndCheckTextImpl(url, false, expected_text, 202 NavigateAndCheckTextImpl(url, template_search_url, false, expected_text,
200 would_perform_search_term_replacement, 203 would_perform_search_term_replacement,
201 should_display_url); 204 should_display_url);
202 NavigateAndCheckTextImpl(url, true, expected_replace_text, 205 NavigateAndCheckTextImpl(url, template_search_url, true,
206 expected_replace_text,
203 would_perform_search_term_replacement, 207 would_perform_search_term_replacement,
204 should_display_url); 208 should_display_url);
205 } 209 }
206 210
207 void ToolbarModelTest::ResetTemplateURLForInstant(const GURL& instant_url) { 211 void ToolbarModelTest::ResetTemplateURLForInstant(
212 const std::string& template_search_url) {
208 TemplateURLData data; 213 TemplateURLData data;
209 data.short_name = ASCIIToUTF16("Google"); 214 data.short_name = ASCIIToUTF16("Google");
210 data.SetURL("{google:baseURL}search?q={searchTerms}"); 215 data.SetURL(template_search_url);
211 data.instant_url = instant_url.spec(); 216 data.instant_url = "http://www.google.com/instant?strk=1";
212 data.search_terms_replacement_key = "{google:instantExtendedEnabledKey}"; 217 data.search_terms_replacement_key = "{google:instantExtendedEnabledKey}";
213 TemplateURL* search_template_url = new TemplateURL(profile(), data); 218 TemplateURL* search_template_url = new TemplateURL(profile(), data);
214 TemplateURLService* template_url_service = 219 TemplateURLService* template_url_service =
215 TemplateURLServiceFactory::GetForProfile(profile()); 220 TemplateURLServiceFactory::GetForProfile(profile());
216 template_url_service->Add(search_template_url); 221 template_url_service->Add(search_template_url);
217 template_url_service->SetDefaultSearchProvider(search_template_url); 222 template_url_service->SetDefaultSearchProvider(search_template_url);
218 ASSERT_NE(0, search_template_url->id()); 223 ASSERT_NE(0, search_template_url->id());
219 template_url_service->Load(); 224 template_url_service->Load();
220 } 225 }
221 226
222 void ToolbarModelTest::NavigateAndCheckTextImpl( 227 void ToolbarModelTest::NavigateAndCheckTextImpl(
223 const GURL& url, 228 const GURL& url,
229 const std::string& template_search_url,
224 bool allow_search_term_replacement, 230 bool allow_search_term_replacement,
225 const string16 expected_text, 231 const string16 expected_text,
226 bool would_perform_search_term_replacement, 232 bool would_perform_search_term_replacement,
227 bool should_display_url) { 233 bool should_display_url) {
228 // The URL being navigated to should be treated as the Instant URL. Else 234 // Override the template search URL with the provided one, if not empty.
229 // there will be no search term extraction. 235 if (!template_search_url.empty())
230 ResetTemplateURLForInstant(url); 236 ResetTemplateURLForInstant(template_search_url);
231 237
232 // Check while loading. 238 // Check while loading.
233 content::NavigationController* controller = 239 content::NavigationController* controller =
234 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); 240 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
235 controller->LoadURL(url, content::Referrer(), content::PAGE_TRANSITION_LINK, 241 controller->LoadURL(url, content::Referrer(), content::PAGE_TRANSITION_LINK,
236 std::string()); 242 std::string());
237 ToolbarModel* toolbar_model = browser()->toolbar_model(); 243 ToolbarModel* toolbar_model = browser()->toolbar_model();
238 EXPECT_EQ(should_display_url, toolbar_model->ShouldDisplayURL()); 244 EXPECT_EQ(should_display_url, toolbar_model->ShouldDisplayURL());
239 EXPECT_EQ(expected_text, 245 EXPECT_EQ(expected_text,
240 toolbar_model->GetText(allow_search_term_replacement)); 246 toolbar_model->GetText(allow_search_term_replacement));
(...skipping 30 matching lines...) Expand all
271 277
272 // Test that we only replace URLs when query extraction and search term 278 // Test that we only replace URLs when query extraction and search term
273 // replacement are enabled. 279 // replacement are enabled.
274 TEST_F(ToolbarModelTest, ShouldDisplayURL) { 280 TEST_F(ToolbarModelTest, ShouldDisplayURL) {
275 // Before we enable instant extended, query extraction is disabled. 281 // Before we enable instant extended, query extraction is disabled.
276 EXPECT_FALSE(chrome::IsQueryExtractionEnabled()) 282 EXPECT_FALSE(chrome::IsQueryExtractionEnabled())
277 << "This test expects query extraction to be disabled."; 283 << "This test expects query extraction to be disabled.";
278 AddTab(browser(), GURL(content::kAboutBlankURL)); 284 AddTab(browser(), GURL(content::kAboutBlankURL));
279 for (size_t i = 0; i < arraysize(test_items); ++i) { 285 for (size_t i = 0; i < arraysize(test_items); ++i) {
280 const TestItem& test_item = test_items[i]; 286 const TestItem& test_item = test_items[i];
281 NavigateAndCheckText(test_item.url, test_item.expected_text, 287 NavigateAndCheckText(test_item.url, "",
Peter Kasting 2013/10/02 00:21:40 Nit: "" -> std::string() (multiple places)
288 test_item.expected_text,
282 test_item.expected_replace_text_inactive, false, 289 test_item.expected_replace_text_inactive, false,
283 test_item.should_display_url); 290 test_item.should_display_url);
284 } 291 }
285 292
286 // Once we enable it, query extraction and search term replacement are 293 chrome::EnableQueryExtractionForTesting();
287 // enabled by default.
288 chrome::EnableInstantExtendedAPIForTesting();
289 EXPECT_TRUE(chrome::IsQueryExtractionEnabled()); 294 EXPECT_TRUE(chrome::IsQueryExtractionEnabled());
290 EXPECT_TRUE(browser()->toolbar_model()->search_term_replacement_enabled()); 295 EXPECT_TRUE(browser()->toolbar_model()->search_term_replacement_enabled());
291 for (size_t i = 0; i < arraysize(test_items); ++i) { 296 for (size_t i = 0; i < arraysize(test_items); ++i) {
292 const TestItem& test_item = test_items[i]; 297 const TestItem& test_item = test_items[i];
293 NavigateAndCheckText(test_item.url, test_item.expected_text, 298 NavigateAndCheckText(test_item.url, "",
299 test_item.expected_text,
294 test_item.expected_replace_text_active, 300 test_item.expected_replace_text_active,
295 test_item.would_perform_search_term_replacement, 301 test_item.would_perform_search_term_replacement,
296 test_item.should_display_url); 302 test_item.should_display_url);
297 } 303 }
298 304
299 // Disabling search term replacement should reset to only showing URLs. 305 // Disabling search term replacement should reset to only showing URLs.
300 browser()->toolbar_model()->set_search_term_replacement_enabled(false); 306 browser()->toolbar_model()->set_search_term_replacement_enabled(false);
301 for (size_t i = 0; i < arraysize(test_items); ++i) { 307 for (size_t i = 0; i < arraysize(test_items); ++i) {
302 const TestItem& test_item = test_items[i]; 308 const TestItem& test_item = test_items[i];
303 NavigateAndCheckText(test_item.url, test_item.expected_text, 309 NavigateAndCheckText(test_item.url, "",
310 test_item.expected_text,
304 test_item.expected_replace_text_inactive, false, 311 test_item.expected_replace_text_inactive, false,
305 test_item.should_display_url); 312 test_item.should_display_url);
306 } 313 }
307 } 314 }
308 315
309 // Verify that search terms are extracted while the page is loading. 316 // Verify that search terms are extracted while the page is loading.
310 TEST_F(ToolbarModelTest, SearchTermsWhileLoading) { 317 TEST_F(ToolbarModelTest, SearchTermsWhileLoading) {
311 chrome::EnableInstantExtendedAPIForTesting(); 318 chrome::EnableQueryExtractionForTesting();
312 ResetDefaultTemplateURL(); 319 ResetDefaultTemplateURL();
313 AddTab(browser(), GURL(content::kAboutBlankURL)); 320 AddTab(browser(), GURL(content::kAboutBlankURL));
314 321
315 // While loading, we should be willing to extract search terms. 322 // While loading, we should be willing to extract search terms.
316 content::NavigationController* controller = 323 content::NavigationController* controller =
317 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); 324 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
318 controller->LoadURL(GURL("https://google.com/search?q=tractor+supply&espv=1"), 325 controller->LoadURL(GURL("https://google.com/search?q=tractor+supply&espv=1"),
319 content::Referrer(), content::PAGE_TRANSITION_LINK, 326 content::Referrer(), content::PAGE_TRANSITION_LINK,
320 std::string()); 327 std::string());
321 ToolbarModel* toolbar_model = browser()->toolbar_model(); 328 ToolbarModel* toolbar_model = browser()->toolbar_model();
322 controller->GetVisibleEntry()->GetSSL().security_style = 329 controller->GetVisibleEntry()->GetSSL().security_style =
323 content::SECURITY_STYLE_UNKNOWN; 330 content::SECURITY_STYLE_UNKNOWN;
324 EXPECT_TRUE(toolbar_model->WouldPerformSearchTermReplacement(false)); 331 EXPECT_TRUE(toolbar_model->WouldPerformSearchTermReplacement(false));
325 332
326 // When done loading, we shouldn't extract search terms if we didn't get an 333 // When done loading, we shouldn't extract search terms if we didn't get an
327 // authenticated connection. 334 // authenticated connection.
328 CommitPendingLoad(controller); 335 CommitPendingLoad(controller);
329 controller->GetVisibleEntry()->GetSSL().security_style = 336 controller->GetVisibleEntry()->GetSSL().security_style =
330 content::SECURITY_STYLE_UNKNOWN; 337 content::SECURITY_STYLE_UNKNOWN;
331 EXPECT_FALSE(toolbar_model->WouldPerformSearchTermReplacement(false)); 338 EXPECT_FALSE(toolbar_model->WouldPerformSearchTermReplacement(false));
332 } 339 }
333 340
334 // When the Google base URL is overridden on the command line, we should extract 341 // 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 342 // search terms from URLs that start with that base URL even when they're not
336 // secure. 343 // secure.
337 TEST_F(ToolbarModelTest, GoogleBaseURL) { 344 TEST_F(ToolbarModelTest, GoogleBaseURL) {
338 chrome::EnableInstantExtendedAPIForTesting(); 345 chrome::EnableQueryExtractionForTesting();
339 AddTab(browser(), GURL(content::kAboutBlankURL)); 346 AddTab(browser(), GURL(content::kAboutBlankURL));
340 347
341 // If the Google base URL wasn't specified on the command line, then if it's 348 // If the Google base URL wasn't specified on the command line, then if it's
342 // HTTP, we should not extract search terms. 349 // HTTP, we should not extract search terms.
343 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.foo.com/"); 350 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.foo.com/");
344 NavigateAndCheckText( 351 NavigateAndCheckText(
345 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"), 352 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"),
353 "http://www.foo.com/search?q={queryTerms}&espv=1",
Peter Kasting 2013/10/02 00:21:40 Can we avoid passing this argument entirely by usi
samarth 2013/10/02 00:52:42 On further inspection, none of the template URL re
346 ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), 354 ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"),
347 ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), false, 355 ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), false,
348 true); 356 true);
349 357
350 // The same URL, when specified on the command line, should allow search term 358 // The same URL, when specified on the command line, should allow search term
351 // extraction. 359 // extraction.
352 UIThreadSearchTermsData::SetGoogleBaseURL(std::string()); 360 UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
353 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL, 361 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL,
354 "http://www.foo.com/"); 362 "http://www.foo.com/");
355 NavigateAndCheckText( 363 NavigateAndCheckText(
356 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"), 364 GURL("http://www.foo.com/search?q=tractor+supply&espv=1"),
365 "http://www.foo.com/search?q={searchTerms}&espv=1",
357 ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"), 366 ASCIIToUTF16("www.foo.com/search?q=tractor+supply&espv=1"),
358 ASCIIToUTF16("tractor supply"), true, true); 367 ASCIIToUTF16("tractor supply"), true, true);
359 } 368 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698