Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / search / search_unittest.cc
blob9afc7625d267032cb5085026917b71b038302074
1 // Copyright (c) 2013 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.
5 #include "base/command_line.h"
6 #include "base/metrics/field_trial.h"
7 #include "base/metrics/histogram_base.h"
8 #include "base/metrics/histogram_samples.h"
9 #include "base/metrics/statistics_recorder.h"
10 #include "base/prefs/pref_service.h"
11 #include "chrome/browser/managed_mode/managed_mode_url_filter.h"
12 #include "chrome/browser/managed_mode/managed_user_service.h"
13 #include "chrome/browser/managed_mode/managed_user_service_factory.h"
14 #include "chrome/browser/search/instant_service.h"
15 #include "chrome/browser/search/instant_service_factory.h"
16 #include "chrome/browser/search/search.h"
17 #include "chrome/browser/search_engines/search_terms_data.h"
18 #include "chrome/browser/search_engines/template_url_service.h"
19 #include "chrome/browser/search_engines/template_url_service_factory.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/common/url_constants.h"
24 #include "chrome/test/base/browser_with_test_window_test.h"
25 #include "chrome/test/base/ui_test_utils.h"
26 #include "components/variations/entropy_provider.h"
27 #include "content/public/browser/render_process_host.h"
28 #include "content/public/browser/render_view_host.h"
29 #include "content/public/browser/site_instance.h"
30 #include "content/public/browser/web_contents.h"
31 #include "content/public/common/renderer_preferences.h"
32 #include "url/gurl.h"
34 namespace chrome {
36 class EmbeddedSearchFieldTrialTest : public testing::Test {
37 protected:
38 virtual void SetUp() {
39 field_trial_list_.reset(new base::FieldTrialList(
40 new metrics::SHA1EntropyProvider("42")));
41 base::StatisticsRecorder::Initialize();
44 private:
45 scoped_ptr<base::FieldTrialList> field_trial_list_;
48 TEST_F(EmbeddedSearchFieldTrialTest, GetFieldTrialInfoEmptyAndValid) {
49 FieldTrialFlags flags;
51 EXPECT_TRUE(GetFieldTrialInfo(&flags));
52 EXPECT_EQ(0ul, flags.size());
54 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
55 "Group77"));
56 EXPECT_TRUE(GetFieldTrialInfo(&flags));
57 EXPECT_EQ(0ul, flags.size());
60 TEST_F(EmbeddedSearchFieldTrialTest, GetFieldTrialInfoInvalidNumber) {
61 FieldTrialFlags flags;
63 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
64 "Group77.2"));
65 EXPECT_TRUE(GetFieldTrialInfo(&flags));
66 EXPECT_EQ(0ul, flags.size());
69 TEST_F(EmbeddedSearchFieldTrialTest, GetFieldTrialInfoInvalidName) {
70 FieldTrialFlags flags;
72 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
73 "Invalid77"));
74 EXPECT_TRUE(GetFieldTrialInfo(&flags));
75 EXPECT_EQ(0ul, flags.size());
78 TEST_F(EmbeddedSearchFieldTrialTest, GetFieldTrialInfoValidGroup) {
79 FieldTrialFlags flags;
81 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
82 "Group77"));
83 EXPECT_TRUE(GetFieldTrialInfo(&flags));
84 EXPECT_EQ(0ul, flags.size());
87 TEST_F(EmbeddedSearchFieldTrialTest, GetFieldTrialInfoValidFlag) {
88 FieldTrialFlags flags;
90 EXPECT_EQ(9999ul, GetUInt64ValueForFlagWithDefault("foo", 9999, flags));
91 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
92 "Group77 foo:6"));
93 EXPECT_TRUE(GetFieldTrialInfo(&flags));
94 EXPECT_EQ(1ul, flags.size());
95 EXPECT_EQ(6ul, GetUInt64ValueForFlagWithDefault("foo", 9999, flags));
98 TEST_F(EmbeddedSearchFieldTrialTest, GetFieldTrialInfoNewName) {
99 FieldTrialFlags flags;
101 EXPECT_EQ(9999ul, GetUInt64ValueForFlagWithDefault("foo", 9999, flags));
102 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
103 "Group77 foo:6"));
104 EXPECT_TRUE(GetFieldTrialInfo(&flags));
105 EXPECT_EQ(1ul, flags.size());
106 EXPECT_EQ(6ul, GetUInt64ValueForFlagWithDefault("foo", 9999, flags));
109 TEST_F(EmbeddedSearchFieldTrialTest, GetFieldTrialInfoNewNameOverridesOld) {
110 FieldTrialFlags flags;
112 EXPECT_EQ(9999ul, GetUInt64ValueForFlagWithDefault("foo", 9999, flags));
113 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
114 "Group77 foo:6"));
115 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
116 "Group78 foo:5"));
117 EXPECT_TRUE(GetFieldTrialInfo(&flags));
118 EXPECT_EQ(1ul, flags.size());
119 EXPECT_EQ(6ul, GetUInt64ValueForFlagWithDefault("foo", 9999, flags));
122 TEST_F(EmbeddedSearchFieldTrialTest, GetFieldTrialInfoLotsOfFlags) {
123 FieldTrialFlags flags;
125 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
126 "EmbeddedSearch", "Group77 bar:1 baz:7 cat:dogs"));
127 EXPECT_TRUE(GetFieldTrialInfo(&flags));
128 EXPECT_EQ(3ul, flags.size());
129 EXPECT_EQ(true, GetBoolValueForFlagWithDefault("bar", false, flags));
130 EXPECT_EQ(7ul, GetUInt64ValueForFlagWithDefault("baz", 0, flags));
131 EXPECT_EQ("dogs",
132 GetStringValueForFlagWithDefault("cat", std::string(), flags));
133 EXPECT_EQ("default",
134 GetStringValueForFlagWithDefault("moose", "default", flags));
137 TEST_F(EmbeddedSearchFieldTrialTest, GetFieldTrialInfoDisabled) {
138 FieldTrialFlags flags;
140 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
141 "EmbeddedSearch", "Group77 bar:1 baz:7 cat:dogs DISABLED"));
142 EXPECT_FALSE(GetFieldTrialInfo(&flags));
143 EXPECT_EQ(0ul, flags.size());
146 TEST_F(EmbeddedSearchFieldTrialTest, GetFieldTrialInfoControlFlags) {
147 FieldTrialFlags flags;
149 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
150 "EmbeddedSearch", "Control77 bar:1 baz:7 cat:dogs"));
151 EXPECT_TRUE(GetFieldTrialInfo(&flags));
152 EXPECT_EQ(3ul, flags.size());
155 class SearchTest : public BrowserWithTestWindowTest {
156 protected:
157 virtual void SetUp() OVERRIDE {
158 BrowserWithTestWindowTest::SetUp();
159 field_trial_list_.reset(new base::FieldTrialList(
160 new metrics::SHA1EntropyProvider("42")));
161 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
162 profile(), &TemplateURLServiceFactory::BuildInstanceFor);
163 TemplateURLService* template_url_service =
164 TemplateURLServiceFactory::GetForProfile(profile());
165 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
166 SetSearchProvider(true, false);
169 void SetSearchProvider(bool set_ntp_url, bool insecure_ntp_url) {
170 TemplateURLService* template_url_service =
171 TemplateURLServiceFactory::GetForProfile(profile());
172 TemplateURLData data;
173 data.SetURL("http://foo.com/url?bar={searchTerms}");
174 data.instant_url = "http://foo.com/instant?"
175 "{google:omniboxStartMarginParameter}{google:forceInstantResults}"
176 "foo=foo#foo=foo&strk";
177 if (set_ntp_url) {
178 data.new_tab_url = (insecure_ntp_url ? "http" : "https") +
179 std::string("://foo.com/newtab?strk");
181 data.alternate_urls.push_back("http://foo.com/alt#quux={searchTerms}");
182 data.search_terms_replacement_key = "strk";
184 TemplateURL* template_url = new TemplateURL(profile(), data);
185 // Takes ownership of |template_url|.
186 template_url_service->Add(template_url);
187 template_url_service->SetDefaultSearchProvider(template_url);
190 // Build an Instant URL with or without a valid search terms replacement key
191 // as per |has_search_term_replacement_key|. Set that URL as the instant URL
192 // for the default search provider.
193 void SetDefaultInstantTemplateUrl(bool has_search_term_replacement_key) {
194 TemplateURLService* template_url_service =
195 TemplateURLServiceFactory::GetForProfile(profile());
197 static const char kInstantURLWithStrk[] =
198 "http://foo.com/instant?foo=foo#foo=foo&strk";
199 static const char kInstantURLNoStrk[] =
200 "http://foo.com/instant?foo=foo#foo=foo";
202 TemplateURLData data;
203 data.SetURL("http://foo.com/url?bar={searchTerms}");
204 data.instant_url = (has_search_term_replacement_key ?
205 kInstantURLWithStrk : kInstantURLNoStrk);
206 data.search_terms_replacement_key = "strk";
208 TemplateURL* template_url = new TemplateURL(profile(), data);
209 // Takes ownership of |template_url|.
210 template_url_service->Add(template_url);
211 template_url_service->SetDefaultSearchProvider(template_url);
214 bool InInstantProcess(const content::WebContents* contents) {
215 InstantService* instant_service =
216 InstantServiceFactory::GetForProfile(profile());
217 return instant_service->IsInstantProcess(
218 contents->GetRenderProcessHost()->GetID());
221 scoped_ptr<base::FieldTrialList> field_trial_list_;
224 struct SearchTestCase {
225 const char* url;
226 bool expected_result;
227 const char* comment;
230 TEST_F(SearchTest, ShouldAssignURLToInstantRendererExtendedEnabled) {
231 EnableQueryExtractionForTesting();
233 const SearchTestCase kTestCases[] = {
234 {chrome::kChromeSearchLocalNtpUrl, true, ""},
235 {"https://foo.com/instant?strk", true, ""},
236 {"https://foo.com/instant#strk", true, ""},
237 {"https://foo.com/instant?strk=0", true, ""},
238 {"https://foo.com/url?strk", true, ""},
239 {"https://foo.com/alt?strk", true, ""},
240 {"http://foo.com/instant", false, "Non-HTTPS"},
241 {"http://foo.com/instant?strk", false, "Non-HTTPS"},
242 {"http://foo.com/instant?strk=1", false, "Non-HTTPS"},
243 {"https://foo.com/instant", false, "No search terms replacement"},
244 {"https://foo.com/?strk", false, "Non-exact path"},
247 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
248 const SearchTestCase& test = kTestCases[i];
249 EXPECT_EQ(test.expected_result,
250 ShouldAssignURLToInstantRenderer(GURL(test.url), profile()))
251 << test.url << " " << test.comment;
255 TEST_F(SearchTest, ShouldAssignURLToInstantRendererExtendedEnabledNotOnSRP) {
256 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
257 "EmbeddedSearch", "Group1 espv:2 suppress_on_srp:1"));
259 const SearchTestCase kTestCases[] = {
260 {chrome::kChromeSearchLocalNtpUrl, true, ""},
261 {"https://foo.com/instant?strk", true, ""},
262 {"https://foo.com/instant#strk", true, ""},
263 {"https://foo.com/instant?strk=0", true, ""},
264 {"https://foo.com/url?strk", false, "Disabled on SRP"},
265 {"https://foo.com/alt?strk", false, "Disabled ON SRP"},
266 {"http://foo.com/instant", false, "Non-HTTPS"},
267 {"http://foo.com/instant?strk", false, "Non-HTTPS"},
268 {"http://foo.com/instant?strk=1", false, "Non-HTTPS"},
269 {"https://foo.com/instant", false, "No search terms replacement"},
270 {"https://foo.com/?strk", false, "Non-exact path"},
273 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
274 const SearchTestCase& test = kTestCases[i];
275 EXPECT_EQ(test.expected_result,
276 ShouldAssignURLToInstantRenderer(GURL(test.url), profile()))
277 << test.url << " " << test.comment;
281 TEST_F(SearchTest, ShouldUseProcessPerSiteForInstantURL) {
282 EnableQueryExtractionForTesting();
284 const SearchTestCase kTestCases[] = {
285 {"chrome-search://local-ntp", true, "Local NTP"},
286 {"chrome-search://online-ntp", true, "Online NTP"},
287 {"invalid-scheme://local-ntp", false, "Invalid Local NTP URL"},
288 {"invalid-scheme://online-ntp", false, "Invalid Online NTP URL"},
289 {"chrome-search://foo.com", false, "Search result page"},
290 {"https://foo.com/instant?strk", false, ""},
291 {"https://foo.com/instant#strk", false, ""},
292 {"https://foo.com/instant?strk=0", false, ""},
293 {"https://foo.com/url?strk", false, ""},
294 {"https://foo.com/alt?strk", false, ""},
295 {"http://foo.com/instant", false, "Non-HTTPS"},
296 {"http://foo.com/instant?strk", false, "Non-HTTPS"},
297 {"http://foo.com/instant?strk=1", false, "Non-HTTPS"},
298 {"https://foo.com/instant", false, "No search terms replacement"},
299 {"https://foo.com/?strk", false, "Non-exact path"},
302 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
303 const SearchTestCase& test = kTestCases[i];
304 EXPECT_EQ(test.expected_result,
305 ShouldUseProcessPerSiteForInstantURL(GURL(test.url), profile()))
306 << test.url << " " << test.comment;
310 // Each test case represents a navigation to |start_url| followed by a
311 // navigation to |end_url|. We will check whether each navigation lands in an
312 // Instant process, and also whether the navigation from start to end re-uses
313 // the same SiteInstance (and hence the same RenderViewHost, etc.).
314 const struct ProcessIsolationTestCase {
315 const char* description;
316 const char* start_url;
317 bool start_in_instant_process;
318 const char* end_url;
319 bool end_in_instant_process;
320 bool same_site_instance;
321 } kProcessIsolationTestCases[] = {
322 {"Local NTP -> SRP",
323 "chrome-search://local-ntp", true,
324 "https://foo.com/url?strk", true, false },
325 {"Local NTP -> Regular",
326 "chrome-search://local-ntp", true,
327 "https://foo.com/other", false, false },
328 {"Remote NTP -> SRP",
329 "https://foo.com/instant?strk", true,
330 "https://foo.com/url?strk", true, false },
331 {"Remote NTP -> Regular",
332 "https://foo.com/instant?strk", true,
333 "https://foo.com/other", false, false },
334 {"SRP -> SRP",
335 "https://foo.com/url?strk", true,
336 "https://foo.com/url?strk", true, true },
337 {"SRP -> Regular",
338 "https://foo.com/url?strk", true,
339 "https://foo.com/other", false, false },
340 {"Regular -> SRP",
341 "https://foo.com/other", false,
342 "https://foo.com/url?strk", true, false },
345 TEST_F(SearchTest, ProcessIsolation) {
346 EnableQueryExtractionForTesting();
348 for (size_t i = 0; i < arraysize(kProcessIsolationTestCases); ++i) {
349 const ProcessIsolationTestCase& test = kProcessIsolationTestCases[i];
350 AddTab(browser(), GURL("chrome://blank"));
351 const content::WebContents* contents =
352 browser()->tab_strip_model()->GetActiveWebContents();
354 // Navigate to start URL.
355 NavigateAndCommitActiveTab(GURL(test.start_url));
356 EXPECT_EQ(test.start_in_instant_process, InInstantProcess(contents))
357 << test.description;
359 // Save state.
360 const scoped_refptr<content::SiteInstance> start_site_instance =
361 contents->GetSiteInstance();
362 const content::RenderProcessHost* start_rph =
363 contents->GetRenderProcessHost();
364 const content::RenderViewHost* start_rvh =
365 contents->GetRenderViewHost();
367 // Navigate to end URL.
368 NavigateAndCommitActiveTab(GURL(test.end_url));
369 EXPECT_EQ(test.end_in_instant_process, InInstantProcess(contents))
370 << test.description;
372 EXPECT_EQ(test.same_site_instance,
373 start_site_instance == contents->GetSiteInstance())
374 << test.description;
375 EXPECT_EQ(test.same_site_instance,
376 start_rvh == contents->GetRenderViewHost())
377 << test.description;
378 EXPECT_EQ(test.same_site_instance,
379 start_rph == contents->GetRenderProcessHost())
380 << test.description;
384 TEST_F(SearchTest, ProcessIsolation_RendererInitiated) {
385 EnableQueryExtractionForTesting();
387 for (size_t i = 0; i < arraysize(kProcessIsolationTestCases); ++i) {
388 const ProcessIsolationTestCase& test = kProcessIsolationTestCases[i];
389 AddTab(browser(), GURL("chrome://blank"));
390 content::WebContents* contents =
391 browser()->tab_strip_model()->GetActiveWebContents();
393 // Navigate to start URL.
394 NavigateAndCommitActiveTab(GURL(test.start_url));
395 EXPECT_EQ(test.start_in_instant_process, InInstantProcess(contents))
396 << test.description;
398 // Save state.
399 const scoped_refptr<content::SiteInstance> start_site_instance =
400 contents->GetSiteInstance();
401 const content::RenderProcessHost* start_rph =
402 contents->GetRenderProcessHost();
403 const content::RenderViewHost* start_rvh =
404 contents->GetRenderViewHost();
406 // Navigate to end URL via a renderer-initiated navigation.
407 content::NavigationController* controller = &contents->GetController();
408 content::NavigationController::LoadURLParams load_params(
409 GURL(test.end_url));
410 load_params.is_renderer_initiated = true;
411 load_params.transition_type = content::PAGE_TRANSITION_LINK;
413 controller->LoadURLWithParams(load_params);
414 CommitPendingLoad(controller);
415 EXPECT_EQ(test.end_in_instant_process, InInstantProcess(contents))
416 << test.description;
418 EXPECT_EQ(test.same_site_instance,
419 start_site_instance == contents->GetSiteInstance())
420 << test.description;
421 EXPECT_EQ(test.same_site_instance,
422 start_rvh == contents->GetRenderViewHost())
423 << test.description;
424 EXPECT_EQ(test.same_site_instance,
425 start_rph == contents->GetRenderProcessHost())
426 << test.description;
430 const SearchTestCase kInstantNTPTestCases[] = {
431 {"https://foo.com/instant?strk", false, "Valid Instant URL"},
432 {"https://foo.com/instant#strk", false, "Valid Instant URL"},
433 {"https://foo.com/url?strk", false, "Valid search URL"},
434 {"https://foo.com/url#strk", false, "Valid search URL"},
435 {"https://foo.com/alt?strk", false, "Valid alternative URL"},
436 {"https://foo.com/alt#strk", false, "Valid alternative URL"},
437 {"https://foo.com/url?strk&bar=", false, "No query terms"},
438 {"https://foo.com/url?strk&q=abc", false, "No query terms key"},
439 {"https://foo.com/url?strk#bar=abc", false, "Query terms key in ref"},
440 {"https://foo.com/url?strk&bar=abc", false, "Has query terms"},
441 {"http://foo.com/instant?strk=1", false, "Insecure URL"},
442 {"https://foo.com/instant", false, "No search term replacement"},
443 {"chrome://blank/", false, "Chrome scheme"},
444 {"chrome-search://foo", false, "Chrome-search scheme"},
445 {"https://bar.com/instant?strk=1", false, "Random non-search page"},
446 {chrome::kChromeSearchLocalNtpUrl, true, "Local new tab page"},
447 {"https://foo.com/newtab?strk", true, "New tab URL"},
448 {"http://foo.com/newtab?strk", false, "Insecure New tab URL"},
451 TEST_F(SearchTest, InstantNTPExtendedEnabled) {
452 EnableQueryExtractionForTesting();
453 AddTab(browser(), GURL("chrome://blank"));
454 for (size_t i = 0; i < arraysize(kInstantNTPTestCases); ++i) {
455 const SearchTestCase& test = kInstantNTPTestCases[i];
456 NavigateAndCommitActiveTab(GURL(test.url));
457 const content::WebContents* contents =
458 browser()->tab_strip_model()->GetWebContentsAt(0);
459 EXPECT_EQ(test.expected_result, IsInstantNTP(contents))
460 << test.url << " " << test.comment;
464 TEST_F(SearchTest, InstantNTPCustomNavigationEntry) {
465 EnableQueryExtractionForTesting();
466 AddTab(browser(), GURL("chrome://blank"));
467 for (size_t i = 0; i < arraysize(kInstantNTPTestCases); ++i) {
468 const SearchTestCase& test = kInstantNTPTestCases[i];
469 NavigateAndCommitActiveTab(GURL(test.url));
470 content::WebContents* contents =
471 browser()->tab_strip_model()->GetWebContentsAt(0);
472 content::NavigationController& controller = contents->GetController();
473 controller.SetTransientEntry(
474 controller.CreateNavigationEntry(GURL("chrome://blank"),
475 content::Referrer(),
476 content::PAGE_TRANSITION_LINK,
477 false,
478 std::string(),
479 contents->GetBrowserContext()));
480 // The active entry is chrome://blank and not an NTP.
481 EXPECT_FALSE(IsInstantNTP(contents));
482 EXPECT_EQ(test.expected_result,
483 NavEntryIsInstantNTP(contents,
484 controller.GetLastCommittedEntry()))
485 << test.url << " " << test.comment;
489 TEST_F(SearchTest, InstantCacheableNTPNavigationEntry) {
490 AddTab(browser(), GURL("chrome://blank"));
491 content::WebContents* contents =
492 browser()->tab_strip_model()->GetWebContentsAt(0);
493 content::NavigationController& controller = contents->GetController();
494 // Local NTP.
495 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl));
496 EXPECT_TRUE(NavEntryIsInstantNTP(contents,
497 controller.GetLastCommittedEntry()));
498 // Instant page is not cacheable NTP.
499 NavigateAndCommitActiveTab(GetInstantURL(profile(), kDisableStartMargin,
500 false));
501 EXPECT_FALSE(NavEntryIsInstantNTP(contents,
502 controller.GetLastCommittedEntry()));
503 // Test Cacheable NTP
504 NavigateAndCommitActiveTab(chrome::GetNewTabPageURL(profile()));
505 EXPECT_TRUE(NavEntryIsInstantNTP(contents,
506 controller.GetLastCommittedEntry()));
509 TEST_F(SearchTest, InstantCacheableNTPNavigationEntryNewProfile) {
510 SetSearchProvider(false, false);
511 AddTab(browser(), GURL(chrome::kChromeUINewTabURL));
512 content::WebContents* contents =
513 browser()->tab_strip_model()->GetWebContentsAt(0);
514 content::NavigationController& controller = contents->GetController();
515 // Test virtual url chrome://newtab for first NTP of a new profile
516 EXPECT_TRUE(NavEntryIsInstantNTP(contents,
517 controller.GetLastCommittedEntry()));
518 // The new_tab_url gets set after the first NTP is visible.
519 SetSearchProvider(true, false);
520 EXPECT_TRUE(NavEntryIsInstantNTP(contents,
521 controller.GetLastCommittedEntry()));
524 TEST_F(SearchTest, UseLocalNTPInIncognito) {
525 EXPECT_EQ(GURL(), chrome::GetNewTabPageURL(
526 profile()->GetOffTheRecordProfile()));
529 TEST_F(SearchTest, UseLocalNTPIfNTPURLIsInsecure) {
530 // Set an insecure new tab page URL and verify that it's ignored.
531 SetSearchProvider(true, true);
532 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl),
533 chrome::GetNewTabPageURL(profile()));
536 TEST_F(SearchTest, UseLocalNTPIfNTPURLIsNotSet) {
537 // Set an insecure new tab page URL and verify that it's ignored.
538 SetSearchProvider(false, true);
539 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl),
540 chrome::GetNewTabPageURL(profile()));
543 TEST_F(SearchTest, UseLocalNTPIfNTPURLIsBlockedForSupervisedUser) {
544 // Block access to foo.com in the URL filter.
545 ManagedUserService* managed_user_service =
546 ManagedUserServiceFactory::GetForProfile(profile());
547 ManagedModeURLFilter* url_filter =
548 managed_user_service->GetURLFilterForUIThread();
549 std::map<std::string, bool> hosts;
550 hosts["foo.com"] = false;
551 url_filter->SetManualHosts(&hosts);
553 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl),
554 chrome::GetNewTabPageURL(profile()));
555 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin, false));
558 TEST_F(SearchTest, GetInstantURL) {
559 // No Instant URL because "strk" is missing.
560 SetDefaultInstantTemplateUrl(false);
561 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin, false));
563 // Set an Instant URL with a valid search terms replacement key.
564 SetDefaultInstantTemplateUrl(true);
566 // Now there should be a valid Instant URL. Note the HTTPS "upgrade".
567 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"),
568 GetInstantURL(profile(), kDisableStartMargin, false));
570 // Enable suggest. No difference.
571 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true);
572 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"),
573 GetInstantURL(profile(), kDisableStartMargin, false));
575 // Disable suggest. No Instant URL.
576 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false);
577 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin, false));
580 TEST_F(SearchTest, StartMarginCGI) {
581 // No margin.
582 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true);
584 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"),
585 GetInstantURL(profile(), kDisableStartMargin, false));
587 // With start margin.
588 EXPECT_EQ(GURL("https://foo.com/instant?es_sm=10&foo=foo#foo=foo&strk"),
589 GetInstantURL(profile(), 10, false));
592 TEST_F(SearchTest, InstantSearchEnabledCGI) {
593 // Disable Instant Search.
594 // Make sure {google:forceInstantResults} is not set in the Instant URL.
595 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"),
596 GetInstantURL(profile(), kDisableStartMargin, false));
598 // Enable Instant Search.
599 // Make sure {google:forceInstantResults} is set in the Instant URL.
600 EXPECT_EQ(GURL("https://foo.com/instant?ion=1&foo=foo#foo=foo&strk"),
601 GetInstantURL(profile(), kDisableStartMargin, true));
604 TEST_F(SearchTest, CommandLineOverrides) {
605 GURL local_instant_url(GetLocalInstantURL(profile()));
606 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), local_instant_url);
608 TemplateURLService* template_url_service =
609 TemplateURLServiceFactory::GetForProfile(profile());
610 TemplateURLData data;
611 data.SetURL("{google:baseURL}search?q={searchTerms}");
612 data.instant_url = "{google:baseURL}webhp?strk";
613 data.search_terms_replacement_key = "strk";
614 TemplateURL* template_url = new TemplateURL(profile(), data);
615 // Takes ownership of |template_url|.
616 template_url_service->Add(template_url);
617 template_url_service->SetDefaultSearchProvider(template_url);
619 // By default, Instant Extended forces the instant URL to be HTTPS, so even if
620 // we set a Google base URL that is HTTP, we should get an HTTPS URL.
621 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.foo.com/");
622 GURL instant_url(GetInstantURL(profile(), kDisableStartMargin, false));
623 ASSERT_TRUE(instant_url.is_valid());
624 EXPECT_EQ("https://www.foo.com/webhp?strk", instant_url.spec());
626 // However, if the Google base URL is specified on the command line, the
627 // instant URL should just use it, even if it's HTTP.
628 UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
629 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL,
630 "http://www.bar.com/");
631 instant_url = GetInstantURL(profile(), kDisableStartMargin, false);
632 ASSERT_TRUE(instant_url.is_valid());
633 EXPECT_EQ("http://www.bar.com/webhp?strk", instant_url.spec());
635 // Similarly, setting a Google base URL on the command line should allow you
636 // to get the Google version of the local NTP, even though search provider's
637 // URL doesn't contain "google".
638 local_instant_url = GetLocalInstantURL(profile());
639 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), local_instant_url);
641 // If we specify extra search query params, they should be inserted into the
642 // query portion of the instant URL.
643 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
644 switches::kExtraSearchQueryParams, "a=b");
645 instant_url = GetInstantURL(profile(), kDisableStartMargin, false);
646 ASSERT_TRUE(instant_url.is_valid());
647 EXPECT_EQ("http://www.bar.com/webhp?a=b&strk", instant_url.spec());
650 TEST_F(SearchTest, ShouldPrefetchSearchResults_InstantExtendedAPIEnabled) {
651 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
652 "EmbeddedSearch",
653 "Group1 espv:2 prefetch_results:1"));
654 EXPECT_TRUE(ShouldPrefetchSearchResults());
655 #if defined(OS_IOS) || defined(OS_ANDROID)
656 EXPECT_EQ(1ul, EmbeddedSearchPageVersion());
657 #else
658 EXPECT_EQ(2ul, EmbeddedSearchPageVersion());
659 #endif
662 TEST_F(SearchTest, ShouldPrefetchSearchResults_DisabledViaFinch) {
663 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
664 "EmbeddedSearch",
665 "Group1 espv:89 prefetch_results:0"));
666 EXPECT_FALSE(ShouldPrefetchSearchResults());
667 EXPECT_EQ(89ul, EmbeddedSearchPageVersion());
670 TEST_F(SearchTest, ShouldPrefetchSearchResults_EnabledViaFinch) {
671 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
672 "EmbeddedSearch",
673 "Group1 espv:80 prefetch_results:1"));
674 EXPECT_TRUE(ShouldPrefetchSearchResults());
675 EXPECT_EQ(80ul, EmbeddedSearchPageVersion());
678 TEST_F(SearchTest, IsNTPURL) {
679 GURL invalid_url;
680 GURL ntp_url(chrome::kChromeUINewTabURL);
681 GURL local_ntp_url(GetLocalInstantURL(profile()));
683 EXPECT_FALSE(chrome::IsNTPURL(invalid_url, profile()));
684 // No margin.
685 EnableQueryExtractionForTesting();
686 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true);
687 GURL remote_ntp_url(GetInstantURL(profile(), kDisableStartMargin, false));
688 GURL search_url_with_search_terms("https://foo.com/url?strk&bar=abc");
689 GURL search_url_without_search_terms("https://foo.com/url?strk&bar");
691 EXPECT_FALSE(chrome::IsNTPURL(ntp_url, profile()));
692 EXPECT_TRUE(chrome::IsNTPURL(local_ntp_url, profile()));
693 EXPECT_TRUE(chrome::IsNTPURL(remote_ntp_url, profile()));
694 EXPECT_FALSE(chrome::IsNTPURL(search_url_with_search_terms, profile()));
695 EXPECT_TRUE(chrome::IsNTPURL(search_url_without_search_terms, profile()));
697 EXPECT_FALSE(chrome::IsNTPURL(ntp_url, NULL));
698 EXPECT_FALSE(chrome::IsNTPURL(local_ntp_url, NULL));
699 EXPECT_FALSE(chrome::IsNTPURL(remote_ntp_url, NULL));
700 EXPECT_FALSE(chrome::IsNTPURL(search_url_with_search_terms, NULL));
701 EXPECT_FALSE(chrome::IsNTPURL(search_url_without_search_terms, NULL));
704 TEST_F(SearchTest, GetSearchURLs) {
705 std::vector<GURL> search_urls = GetSearchURLs(profile());
706 EXPECT_EQ(2U, search_urls.size());
707 EXPECT_EQ("http://foo.com/alt#quux=", search_urls[0].spec());
708 EXPECT_EQ("http://foo.com/url?bar=", search_urls[1].spec());
711 TEST_F(SearchTest, GetSearchResultPrefetchBaseURL) {
712 // "prefetch_results" flag is disabled.
713 EXPECT_EQ(GURL(), GetSearchResultPrefetchBaseURL(profile()));
715 // "prefetch_results" flag is enabled via field trials.
716 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
717 "EmbeddedSearch",
718 "Group1 espv:80 prefetch_results:1"));
719 EXPECT_TRUE(ShouldPrefetchSearchResults());
721 EXPECT_EQ(GURL("https://foo.com/instant?ion=1&foo=foo#foo=foo&strk"),
722 GetSearchResultPrefetchBaseURL(profile()));
725 typedef SearchTest IsQueryExtractionEnabledTest;
727 TEST_F(IsQueryExtractionEnabledTest, NotSet) {
728 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
729 "EmbeddedSearch", "Group1 espv:2"));
730 EXPECT_TRUE(IsInstantExtendedAPIEnabled());
731 EXPECT_FALSE(IsQueryExtractionEnabled());
732 EXPECT_EQ(2ul, EmbeddedSearchPageVersion());
735 TEST_F(IsQueryExtractionEnabledTest, EnabledViaFinch) {
736 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
737 "EmbeddedSearch", "Group1 espv:2 query_extraction:1"));
738 EXPECT_TRUE(IsInstantExtendedAPIEnabled());
739 EXPECT_TRUE(IsQueryExtractionEnabled());
740 EXPECT_EQ(2ul, EmbeddedSearchPageVersion());
743 TEST_F(IsQueryExtractionEnabledTest, DisabledViaFinch) {
744 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
745 "EmbeddedSearch", "Group1 espv:2 query_extraction:0"));
746 EXPECT_TRUE(IsInstantExtendedAPIEnabled());
747 EXPECT_FALSE(IsQueryExtractionEnabled());
748 EXPECT_EQ(2ul, EmbeddedSearchPageVersion());
751 TEST_F(IsQueryExtractionEnabledTest, EnabledViaCommandLine) {
752 EnableQueryExtractionForTesting();
753 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
754 "EmbeddedSearch", "Group1 espv:2 query_extraction:0"));
755 EXPECT_TRUE(IsInstantExtendedAPIEnabled());
756 EXPECT_TRUE(IsQueryExtractionEnabled());
757 EXPECT_EQ(2ul, EmbeddedSearchPageVersion());
760 typedef SearchTest ShouldHideTopVerbatimTest;
762 TEST_F(ShouldHideTopVerbatimTest, DoNotHideByDefault) {
763 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
764 "Control"));
765 EXPECT_FALSE(ShouldHideTopVerbatimMatch());
768 TEST_F(ShouldHideTopVerbatimTest, DoNotHideInInstantExtended) {
769 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
770 "Group1"));
771 EXPECT_FALSE(ShouldHideTopVerbatimMatch());
774 TEST_F(ShouldHideTopVerbatimTest, EnableByFlagInInstantExtended) {
775 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
776 "Group1 hide_verbatim:1"));
777 EXPECT_TRUE(ShouldHideTopVerbatimMatch());
780 TEST_F(ShouldHideTopVerbatimTest, EnableByFlagOutsideInstantExtended) {
781 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
782 "EmbeddedSearch", "Controll1 hide_verbatim:1"));
783 EXPECT_TRUE(ShouldHideTopVerbatimMatch());
786 TEST_F(ShouldHideTopVerbatimTest, DisableByFlag) {
787 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
788 "Group1 hide_verbatim:0"));
789 EXPECT_FALSE(ShouldHideTopVerbatimMatch());
792 typedef SearchTest DisplaySearchButtonTest;
794 TEST_F(DisplaySearchButtonTest, NotSet) {
795 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
796 "EmbeddedSearch", "Group1 espv:2"));
797 EXPECT_EQ(DISPLAY_SEARCH_BUTTON_NEVER, GetDisplaySearchButtonConditions());
800 TEST_F(DisplaySearchButtonTest, Never) {
801 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
802 "EmbeddedSearch", "Group1 espv:2 display_search_button:0"));
803 EXPECT_EQ(DISPLAY_SEARCH_BUTTON_NEVER, GetDisplaySearchButtonConditions());
806 TEST_F(DisplaySearchButtonTest, CommandLineNever) {
807 CommandLine::ForCurrentProcess()->AppendSwitch(
808 switches::kDisableSearchButtonInOmnibox);
809 EXPECT_EQ(DISPLAY_SEARCH_BUTTON_NEVER, GetDisplaySearchButtonConditions());
811 // Command-line disable should override Finch.
812 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
813 "EmbeddedSearch", "Group1 espv:2 display_search_button:1"));
814 EXPECT_EQ(DISPLAY_SEARCH_BUTTON_NEVER, GetDisplaySearchButtonConditions());
817 TEST_F(DisplaySearchButtonTest, ForSearchTermReplacement) {
818 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
819 "EmbeddedSearch", "Group1 espv:2 display_search_button:1"));
820 EXPECT_EQ(DISPLAY_SEARCH_BUTTON_FOR_STR, GetDisplaySearchButtonConditions());
823 TEST_F(DisplaySearchButtonTest, CommandLineForSearchTermReplacement) {
824 CommandLine::ForCurrentProcess()->AppendSwitch(
825 switches::kEnableSearchButtonInOmniboxForStr);
826 EXPECT_EQ(DISPLAY_SEARCH_BUTTON_FOR_STR, GetDisplaySearchButtonConditions());
829 TEST_F(DisplaySearchButtonTest, ForSearchTermReplacementOrInputInProgress) {
830 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
831 "EmbeddedSearch", "Group1 espv:2 display_search_button:2"));
832 EXPECT_EQ(DISPLAY_SEARCH_BUTTON_FOR_STR_OR_IIP,
833 GetDisplaySearchButtonConditions());
836 TEST_F(DisplaySearchButtonTest,
837 CommandLineForSearchTermReplacementOrInputInProgress) {
838 CommandLine::ForCurrentProcess()->AppendSwitch(
839 switches::kEnableSearchButtonInOmniboxForStrOrIip);
840 EXPECT_EQ(DISPLAY_SEARCH_BUTTON_FOR_STR_OR_IIP,
841 GetDisplaySearchButtonConditions());
844 TEST_F(DisplaySearchButtonTest, Always) {
845 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
846 "EmbeddedSearch", "Group1 espv:2 display_search_button:3"));
847 EXPECT_EQ(DISPLAY_SEARCH_BUTTON_ALWAYS, GetDisplaySearchButtonConditions());
850 TEST_F(DisplaySearchButtonTest, CommandLineAlways) {
851 CommandLine::ForCurrentProcess()->AppendSwitch(
852 switches::kEnableSearchButtonInOmniboxAlways);
853 EXPECT_EQ(DISPLAY_SEARCH_BUTTON_ALWAYS, GetDisplaySearchButtonConditions());
856 TEST_F(DisplaySearchButtonTest, InvalidValue) {
857 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
858 "EmbeddedSearch", "Group1 espv:2 display_search_button:4"));
859 EXPECT_EQ(DISPLAY_SEARCH_BUTTON_NEVER, GetDisplaySearchButtonConditions());
862 typedef SearchTest OriginChipTest;
864 TEST_F(OriginChipTest, NotSet) {
865 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
866 "EmbeddedSearch", "Group1 espv:2"));
867 EXPECT_FALSE(ShouldDisplayOriginChip());
868 EXPECT_EQ(ORIGIN_CHIP_DISABLED, GetOriginChipPosition());
871 TEST_F(OriginChipTest, NoOriginChip) {
872 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
873 "EmbeddedSearch", "Group1 espv:2 origin_chip:0"));
874 EXPECT_FALSE(ShouldDisplayOriginChip());
875 EXPECT_EQ(ORIGIN_CHIP_DISABLED, GetOriginChipPosition());
878 TEST_F(OriginChipTest, OriginChipLeadingLocationBar) {
879 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
880 "EmbeddedSearch", "Group1 espv:2 origin_chip:1"));
881 EXPECT_TRUE(ShouldDisplayOriginChip());
882 EXPECT_EQ(ORIGIN_CHIP_LEADING_LOCATION_BAR, GetOriginChipPosition());
885 TEST_F(OriginChipTest, OriginChipTrailingLocationBar) {
886 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
887 "EmbeddedSearch", "Group1 espv:2 origin_chip:2"));
888 EXPECT_TRUE(ShouldDisplayOriginChip());
889 EXPECT_EQ(ORIGIN_CHIP_TRAILING_LOCATION_BAR, GetOriginChipPosition());
892 TEST_F(OriginChipTest, OriginChipLeadingMenuButton) {
893 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
894 "EmbeddedSearch", "Group1 espv:2 origin_chip:3"));
895 EXPECT_TRUE(ShouldDisplayOriginChip());
896 EXPECT_EQ(ORIGIN_CHIP_LEADING_MENU_BUTTON, GetOriginChipPosition());
899 TEST_F(OriginChipTest, OriginChipInvalidValue) {
900 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
901 "EmbeddedSearch", "Group1 espv:2 origin_chip:4"));
902 EXPECT_FALSE(ShouldDisplayOriginChip());
903 EXPECT_EQ(ORIGIN_CHIP_DISABLED, GetOriginChipPosition());
906 TEST_F(OriginChipTest, CommandLineNoOriginChip) {
907 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kDisableOriginChip);
908 EXPECT_FALSE(ShouldDisplayOriginChip());
909 EXPECT_EQ(ORIGIN_CHIP_DISABLED, GetOriginChipPosition());
911 // Command-line disable should override Finch.
912 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
913 "EmbeddedSearch", "Group1 espv:2 origin_chip:1"));
914 EXPECT_FALSE(ShouldDisplayOriginChip());
915 EXPECT_EQ(ORIGIN_CHIP_DISABLED, GetOriginChipPosition());
918 TEST_F(OriginChipTest, CommandLineOriginChip) {
919 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnableOriginChip);
920 EXPECT_TRUE(ShouldDisplayOriginChip());
921 EXPECT_EQ(ORIGIN_CHIP_TRAILING_LOCATION_BAR, GetOriginChipPosition());
923 // Command-line enable should override Finch.
924 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
925 "EmbeddedSearch", "Group1 espv:2 origin_chip:0"));
926 EXPECT_TRUE(ShouldDisplayOriginChip());
927 EXPECT_EQ(ORIGIN_CHIP_TRAILING_LOCATION_BAR, GetOriginChipPosition());
930 TEST_F(OriginChipTest, CommandLineOriginChipLeadingLocationBar) {
931 CommandLine::ForCurrentProcess()->AppendSwitch(
932 switches::kEnableOriginChipLeadingLocationBar);
933 EXPECT_TRUE(ShouldDisplayOriginChip());
934 EXPECT_EQ(ORIGIN_CHIP_LEADING_LOCATION_BAR, GetOriginChipPosition());
937 TEST_F(OriginChipTest, CommandLineOriginChipTrailingLocationBar) {
938 CommandLine::ForCurrentProcess()->AppendSwitch(
939 switches::kEnableOriginChipTrailingLocationBar);
940 EXPECT_TRUE(ShouldDisplayOriginChip());
941 EXPECT_EQ(ORIGIN_CHIP_TRAILING_LOCATION_BAR, GetOriginChipPosition());
944 TEST_F(OriginChipTest, CommandLineOriginChipLeadingMenuButton) {
945 CommandLine::ForCurrentProcess()->AppendSwitch(
946 switches::kEnableOriginChipLeadingMenuButton);
947 EXPECT_TRUE(ShouldDisplayOriginChip());
948 EXPECT_EQ(ORIGIN_CHIP_LEADING_MENU_BUTTON, GetOriginChipPosition());
951 } // namespace chrome