1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/command_line.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram_base.h"
#include "base/metrics/histogram_samples.h"
#include "base/metrics/statistics_recorder.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/search_engines/template_url_service.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/metrics/entropy_provider.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/web_contents.h"
namespace chrome {
TEST(EmbeddedSearchFieldTrialTest, GetFieldTrialInfo) {
FieldTrialFlags flags;
uint64 group_number = 0;
const uint64 ZERO = 0;
EXPECT_FALSE(GetFieldTrialInfo(std::string(), &flags, &group_number));
EXPECT_EQ(ZERO, group_number);
EXPECT_EQ(ZERO, flags.size());
EXPECT_TRUE(GetFieldTrialInfo("Group77", &flags, &group_number));
EXPECT_EQ(uint64(77), group_number);
EXPECT_EQ(ZERO, flags.size());
group_number = 0;
EXPECT_FALSE(GetFieldTrialInfo("Group77.2", &flags, &group_number));
EXPECT_EQ(ZERO, group_number);
EXPECT_EQ(ZERO, flags.size());
EXPECT_FALSE(GetFieldTrialInfo("Invalid77", &flags, &group_number));
EXPECT_EQ(ZERO, group_number);
EXPECT_EQ(ZERO, flags.size());
EXPECT_FALSE(GetFieldTrialInfo("Invalid77", &flags, NULL));
EXPECT_EQ(ZERO, flags.size());
EXPECT_TRUE(GetFieldTrialInfo("Group77 ", &flags, &group_number));
EXPECT_EQ(uint64(77), group_number);
EXPECT_EQ(ZERO, flags.size());
group_number = 0;
flags.clear();
EXPECT_EQ(uint64(9999), GetUInt64ValueForFlagWithDefault("foo", 9999, flags));
EXPECT_TRUE(GetFieldTrialInfo("Group77 foo:6", &flags, &group_number));
EXPECT_EQ(uint64(77), group_number);
EXPECT_EQ(uint64(1), flags.size());
EXPECT_EQ(uint64(6), GetUInt64ValueForFlagWithDefault("foo", 9999, flags));
group_number = 0;
flags.clear();
EXPECT_TRUE(GetFieldTrialInfo(
"Group77 bar:1 baz:7 cat:dogs", &flags, &group_number));
EXPECT_EQ(uint64(77), group_number);
EXPECT_EQ(uint64(3), flags.size());
EXPECT_EQ(true, GetBoolValueForFlagWithDefault("bar", false, flags));
EXPECT_EQ(uint64(7), GetUInt64ValueForFlagWithDefault("baz", 0, flags));
EXPECT_EQ("dogs",
GetStringValueForFlagWithDefault("cat", std::string(), flags));
EXPECT_EQ("default",
GetStringValueForFlagWithDefault("moose", "default", flags));
group_number = 0;
flags.clear();
EXPECT_FALSE(GetFieldTrialInfo(
"Group77 bar:1 baz:7 cat:dogs DISABLED", &flags, &group_number));
EXPECT_EQ(ZERO, group_number);
EXPECT_EQ(ZERO, flags.size());
}
class InstantExtendedAPIEnabledTest : public BrowserWithTestWindowTest {
public:
InstantExtendedAPIEnabledTest() : histogram_(NULL) {
}
protected:
virtual void SetUp() OVERRIDE {
BrowserWithTestWindowTest::SetUp();
field_trial_list_.reset(new base::FieldTrialList(
new metrics::SHA1EntropyProvider("42")));
base::StatisticsRecorder::Initialize();
ResetInstantExtendedOptInStateGateForTest();
previous_metrics_count_.resize(INSTANT_EXTENDED_OPT_IN_STATE_ENUM_COUNT, 0);
base::HistogramBase* histogram = GetHistogram();
if (histogram) {
scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
if (samples.get()) {
for (int state = INSTANT_EXTENDED_NOT_SET;
state < INSTANT_EXTENDED_OPT_IN_STATE_ENUM_COUNT; ++state) {
previous_metrics_count_[state] = samples->GetCount(state);
}
}
}
}
virtual CommandLine* GetCommandLine() const {
return CommandLine::ForCurrentProcess();
}
void ValidateMetrics(base::HistogramBase::Sample value) {
base::HistogramBase* histogram = GetHistogram();
if (histogram) {
scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
if (samples.get()) {
for (int state = INSTANT_EXTENDED_NOT_SET;
state < INSTANT_EXTENDED_OPT_IN_STATE_ENUM_COUNT; ++state) {
if (state == value) {
EXPECT_EQ(previous_metrics_count_[state] + 1,
samples->GetCount(state));
} else {
EXPECT_EQ(previous_metrics_count_[state], samples->GetCount(state));
}
}
}
}
}
private:
base::HistogramBase* GetHistogram() {
if (!histogram_) {
histogram_ = base::StatisticsRecorder::FindHistogram(
"InstantExtended.OptInState");
}
return histogram_;
}
base::HistogramBase* histogram_;
scoped_ptr<base::FieldTrialList> field_trial_list_;
std::vector<int> previous_metrics_count_;
};
TEST_F(InstantExtendedAPIEnabledTest, EnabledViaCommandLineFlag) {
GetCommandLine()->AppendSwitch(switches::kEnableInstantExtendedAPI);
EXPECT_TRUE(IsInstantExtendedAPIEnabled());
EXPECT_FALSE(IsLocalOnlyInstantExtendedAPIEnabled());
#if defined(OS_IOS) || defined(OS_ANDROID)
EXPECT_EQ(1ul, EmbeddedSearchPageVersion(profile()));
#else
EXPECT_EQ(2ul, EmbeddedSearchPageVersion(profile()));
#endif
ValidateMetrics(INSTANT_EXTENDED_OPT_IN);
}
TEST_F(InstantExtendedAPIEnabledTest, EnabledViaFinchFlag) {
ASSERT_TRUE(base::FieldTrialList::CreateTrialsFromString(
"InstantExtended/Group1 espv:42/"));
EXPECT_TRUE(IsInstantExtendedAPIEnabled());
EXPECT_FALSE(IsLocalOnlyInstantExtendedAPIEnabled());
EXPECT_EQ(42ul, EmbeddedSearchPageVersion(profile()));
ValidateMetrics(INSTANT_EXTENDED_NOT_SET);
}
TEST_F(InstantExtendedAPIEnabledTest, DisabledViaCommandLineFlag) {
GetCommandLine()->AppendSwitch(switches::kDisableInstantExtendedAPI);
ASSERT_TRUE(base::FieldTrialList::CreateTrialsFromString(
"InstantExtended/Group1 espv:2/"));
EXPECT_FALSE(IsInstantExtendedAPIEnabled());
EXPECT_FALSE(IsLocalOnlyInstantExtendedAPIEnabled());
EXPECT_EQ(0ul, EmbeddedSearchPageVersion(profile()));
ValidateMetrics(INSTANT_EXTENDED_OPT_OUT);
}
TEST_F(InstantExtendedAPIEnabledTest, LocalOnlyEnabledViaCommandLineFlag) {
GetCommandLine()->AppendSwitch(switches::kEnableLocalOnlyInstantExtendedAPI);
EXPECT_TRUE(IsInstantExtendedAPIEnabled());
EXPECT_TRUE(IsLocalOnlyInstantExtendedAPIEnabled());
EXPECT_EQ(0ul, EmbeddedSearchPageVersion(profile()));
ValidateMetrics(INSTANT_EXTENDED_OPT_IN_LOCAL);
}
TEST_F(InstantExtendedAPIEnabledTest, LocalOnlyEnabledViaFinch) {
ASSERT_TRUE(base::FieldTrialList::CreateTrialsFromString(
"InstantExtended/Group1 local_only:1/"));
EXPECT_TRUE(IsInstantExtendedAPIEnabled());
EXPECT_TRUE(IsLocalOnlyInstantExtendedAPIEnabled());
EXPECT_EQ(0ul, EmbeddedSearchPageVersion(profile()));
ValidateMetrics(INSTANT_EXTENDED_NOT_SET);
}
TEST_F(InstantExtendedAPIEnabledTest, BothLocalAndRegularOptOutCommandLine) {
GetCommandLine()->AppendSwitch(switches::kDisableLocalOnlyInstantExtendedAPI);
GetCommandLine()->AppendSwitch(switches::kDisableInstantExtendedAPI);
EXPECT_FALSE(IsInstantExtendedAPIEnabled());
EXPECT_FALSE(IsLocalOnlyInstantExtendedAPIEnabled());
ValidateMetrics(INSTANT_EXTENDED_OPT_OUT_BOTH);
}
TEST_F(InstantExtendedAPIEnabledTest, BothLocalAndRegularOptInCommandLine) {
GetCommandLine()->AppendSwitch(switches::kEnableLocalOnlyInstantExtendedAPI);
GetCommandLine()->AppendSwitch(switches::kEnableInstantExtendedAPI);
EXPECT_TRUE(IsInstantExtendedAPIEnabled());
EXPECT_TRUE(IsLocalOnlyInstantExtendedAPIEnabled());
ValidateMetrics(INSTANT_EXTENDED_OPT_IN_LOCAL);
}
TEST_F(InstantExtendedAPIEnabledTest,
LocalOnlyCommandLineTrumpedByCommandLine) {
GetCommandLine()->AppendSwitch(switches::kEnableLocalOnlyInstantExtendedAPI);
GetCommandLine()->AppendSwitch(switches::kDisableInstantExtendedAPI);
EXPECT_FALSE(IsInstantExtendedAPIEnabled());
EXPECT_FALSE(IsLocalOnlyInstantExtendedAPIEnabled());
EXPECT_EQ(0ul, EmbeddedSearchPageVersion(profile()));
ValidateMetrics(INSTANT_EXTENDED_OPT_OUT);
}
TEST_F(InstantExtendedAPIEnabledTest, LocalOnlyCommandLineTrumpsFinch) {
GetCommandLine()->AppendSwitch(switches::kEnableLocalOnlyInstantExtendedAPI);
ASSERT_TRUE(base::FieldTrialList::CreateTrialsFromString(
"InstantExtended/Group1 espv:2/"));
EXPECT_TRUE(IsInstantExtendedAPIEnabled());
EXPECT_TRUE(IsLocalOnlyInstantExtendedAPIEnabled());
EXPECT_EQ(0ul, EmbeddedSearchPageVersion(profile()));
ValidateMetrics(INSTANT_EXTENDED_OPT_IN_LOCAL);
}
TEST_F(InstantExtendedAPIEnabledTest, LocalOnlyFinchTrumpedByCommandLine) {
ASSERT_TRUE(base::FieldTrialList::CreateTrialsFromString(
"InstantExtended/Group1 local_only:1/"));
GetCommandLine()->AppendSwitch(switches::kDisableInstantExtendedAPI);
EXPECT_FALSE(IsInstantExtendedAPIEnabled());
EXPECT_FALSE(IsLocalOnlyInstantExtendedAPIEnabled());
EXPECT_EQ(0ul, EmbeddedSearchPageVersion(profile()));
ValidateMetrics(INSTANT_EXTENDED_OPT_OUT);
}
TEST_F(InstantExtendedAPIEnabledTest, LocalOnlyFinchTrumpsFinch) {
ASSERT_TRUE(base::FieldTrialList::CreateTrialsFromString(
"InstantExtended/Group1 espv:1 local_only:1/"));
EXPECT_TRUE(IsInstantExtendedAPIEnabled());
EXPECT_TRUE(IsLocalOnlyInstantExtendedAPIEnabled());
EXPECT_EQ(0ul, EmbeddedSearchPageVersion(profile()));
ValidateMetrics(INSTANT_EXTENDED_NOT_SET);
}
TEST_F(InstantExtendedAPIEnabledTest, LocalOnlyDisabledViaCommandLineFlag) {
GetCommandLine()->AppendSwitch(switches::kDisableLocalOnlyInstantExtendedAPI);
ASSERT_TRUE(base::FieldTrialList::CreateTrialsFromString(
"InstantExtended/Group1 espv:2/"));
EXPECT_TRUE(IsInstantExtendedAPIEnabled());
EXPECT_FALSE(IsLocalOnlyInstantExtendedAPIEnabled());
EXPECT_EQ(2ul, EmbeddedSearchPageVersion(profile()));
ValidateMetrics(INSTANT_EXTENDED_OPT_OUT_LOCAL);
}
class ShouldPreloadLocalOnlyNTPtest : public InstantExtendedAPIEnabledTest {
};
TEST_F(ShouldPreloadLocalOnlyNTPtest, PreloadByDefault) {
EXPECT_TRUE(ShouldPreloadLocalOnlyNTP());
}
TEST_F(ShouldPreloadLocalOnlyNTPtest, SuppressPreload) {
ASSERT_TRUE(base::FieldTrialList::CreateTrialsFromString(
"InstantExtended/Group1 preload_local_only_ntp:0/"));
EXPECT_FALSE(ShouldPreloadLocalOnlyNTP());
}
TEST_F(ShouldPreloadLocalOnlyNTPtest, ForcePreload) {
ASSERT_TRUE(base::FieldTrialList::CreateTrialsFromString(
"InstantExtended/Group1 preload_local_only_ntp:1/"));
EXPECT_TRUE(ShouldPreloadLocalOnlyNTP());
}
class SearchTest : public BrowserWithTestWindowTest {
protected:
virtual void SetUp() OVERRIDE {
BrowserWithTestWindowTest::SetUp();
TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
profile(), &TemplateURLServiceFactory::BuildInstanceFor);
TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(profile());
ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service);
SetSearchProvider(false);
}
void SetSearchProvider(bool is_google) {
TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(profile());
TemplateURLData data;
if (is_google) {
data.SetURL("http://www.google.com/");
data.instant_url = "http://www.google.com/";
} else {
data.SetURL("http://foo.com/url?bar={searchTerms}");
data.instant_url = "http://foo.com/instant?"
"{google:omniboxStartMarginParameter}foo=foo#foo=foo&strk";
data.alternate_urls.push_back("http://foo.com/alt#quux={searchTerms}");
data.search_terms_replacement_key = "strk";
}
TemplateURL* template_url = new TemplateURL(profile(), data);
// Takes ownership of |template_url|.
template_url_service->Add(template_url);
template_url_service->SetDefaultSearchProvider(template_url);
}
// Build an Instant URL with or without a valid search terms replacement key
// as per |has_search_term_replacement_key|. Set that URL as the instant URL
// for the default search provider.
void SetDefaultInstantTemplateUrl(bool has_search_term_replacement_key) {
TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(profile());
static const char kInstantURLWithStrk[] =
"http://foo.com/instant?foo=foo#foo=foo&strk";
static const char kInstantURLNoStrk[] =
"http://foo.com/instant?foo=foo#foo=foo";
TemplateURLData data;
data.SetURL("http://foo.com/url?bar={searchTerms}");
data.instant_url = (has_search_term_replacement_key ?
kInstantURLWithStrk : kInstantURLNoStrk);
data.search_terms_replacement_key = "strk";
TemplateURL* template_url = new TemplateURL(profile(), data);
// Takes ownership of |template_url|.
template_url_service->Add(template_url);
template_url_service->SetDefaultSearchProvider(template_url);
}
};
struct SearchTestCase {
const char* url;
bool expected_result;
const char* comment;
};
TEST_F(SearchTest, ShouldAssignURLToInstantRendererExtendedDisabled) {
const SearchTestCase kTestCases[] = {
{"chrome-search://foo/bar", true, ""},
{"http://foo.com/instant", true, ""},
{"http://foo.com/instant?foo=bar", true, ""},
{"https://foo.com/instant", true, ""},
{"https://foo.com/instant#foo=bar", true, ""},
{"HtTpS://fOo.CoM/instant", true, ""},
{"http://foo.com:80/instant", true, ""},
{"invalid URL", false, "Invalid URL"},
{"unknown://scheme/path", false, "Unknown scheme"},
{"ftp://foo.com/instant", false, "Non-HTTP scheme"},
{"http://sub.foo.com/instant", false, "Non-exact host"},
{"http://foo.com:26/instant", false, "Non-default port"},
{"http://foo.com/instant/bar", false, "Non-exact path"},
{"http://foo.com/Instant", false, "Case sensitive path"},
{"http://foo.com/", false, "Non-exact path"},
{"https://foo.com/", false, "Non-exact path"},
{"https://foo.com/url?strk", false, "Non-extended mode"},
{"https://foo.com/alt?strk", false, "Non-extended mode"},
};
for (size_t i = 0; i < arraysize(kTestCases); ++i) {
const SearchTestCase& test = kTestCases[i];
EXPECT_EQ(test.expected_result,
ShouldAssignURLToInstantRenderer(GURL(test.url), profile()))
<< test.url << " " << test.comment;
}
}
TEST_F(SearchTest, ShouldAssignURLToInstantRendererExtendedEnabled) {
EnableInstantExtendedAPIForTesting();
const SearchTestCase kTestCases[] = {
{chrome::kChromeSearchLocalNtpUrl, true, ""},
{chrome::kChromeSearchLocalGoogleNtpUrl, true, ""},
{"https://foo.com/instant?strk", true, ""},
{"https://foo.com/instant#strk", true, ""},
{"https://foo.com/instant?strk=0", true, ""},
{"https://foo.com/url?strk", true, ""},
{"https://foo.com/alt?strk", true, ""},
{"http://foo.com/instant", false, "Non-HTTPS"},
{"http://foo.com/instant?strk", false, "Non-HTTPS"},
{"http://foo.com/instant?strk=1", false, "Non-HTTPS"},
{"https://foo.com/instant", false, "No search terms replacement"},
{"https://foo.com/?strk", false, "Non-exact path"},
};
for (size_t i = 0; i < arraysize(kTestCases); ++i) {
const SearchTestCase& test = kTestCases[i];
EXPECT_EQ(test.expected_result,
ShouldAssignURLToInstantRenderer(GURL(test.url), profile()))
<< test.url << " " << test.comment;
}
}
TEST_F(SearchTest, CoerceCommandLineURLToTemplateURL) {
TemplateURL* template_url =
TemplateURLServiceFactory::GetForProfile(profile())->
GetDefaultSearchProvider();
EXPECT_EQ(
GURL("https://foo.com/dev?bar=bar#bar=bar"),
CoerceCommandLineURLToTemplateURL(
GURL("http://myserver.com:9000/dev?bar=bar#bar=bar"),
template_url->instant_url_ref(), kDisableStartMargin));
}
const SearchTestCase kInstantNTPTestCases[] = {
{"https://foo.com/instant?strk", true, "Valid Instant URL"},
{"https://foo.com/instant#strk", true, "Valid Instant URL"},
{"https://foo.com/url?strk", true, "Valid search URL"},
{"https://foo.com/url#strk", true, "Valid search URL"},
{"https://foo.com/alt?strk", true, "Valid alternative URL"},
{"https://foo.com/alt#strk", true, "Valid alternative URL"},
{"https://foo.com/url?strk&bar=", true, "No query terms"},
{"https://foo.com/url?strk&q=abc", true, "No query terms key"},
{"https://foo.com/url?strk#bar=abc", true, "Query terms key in ref"},
{"https://foo.com/url?strk&bar=abc", false, "Has query terms"},
{"http://foo.com/instant?strk=1", false, "Insecure URL"},
{"https://foo.com/instant", false, "No search term replacement"},
{"chrome://blank/", false, "Chrome scheme"},
{"chrome-search://foo", false, "Chrome-search scheme"},
{chrome::kChromeSearchLocalNtpUrl, true, "Local new tab page"},
{chrome::kChromeSearchLocalGoogleNtpUrl, true, "Local new tab page"},
{"https://bar.com/instant?strk=1", false, "Random non-search page"},
};
TEST_F(SearchTest, InstantExtendedEmbeddedSearchDisabledForIncognito) {
#if !defined(OS_IOS) && !defined(OS_ANDROID)
EnableInstantExtendedAPIForTesting();
profile()->set_incognito(true);
EXPECT_TRUE(IsInstantExtendedAPIEnabled());
EXPECT_EQ(0ul, EmbeddedSearchPageVersion(profile()));
EXPECT_FALSE(IsQueryExtractionEnabled(profile()));
#endif // !defined(OS_IOS) && !defined(OS_ANDROID)
}
TEST_F(SearchTest, InstantNTPExtendedEnabled) {
EnableInstantExtendedAPIForTesting();
AddTab(browser(), GURL("chrome://blank"));
for (size_t i = 0; i < arraysize(kInstantNTPTestCases); ++i) {
const SearchTestCase& test = kInstantNTPTestCases[i];
NavigateAndCommitActiveTab(GURL(test.url));
SetSearchProvider(test.url == chrome::kChromeSearchLocalGoogleNtpUrl);
const content::WebContents* contents =
browser()->tab_strip_model()->GetWebContentsAt(0);
EXPECT_EQ(test.expected_result, IsInstantNTP(contents))
<< test.url << " " << test.comment;
}
}
TEST_F(SearchTest, InstantNTPExtendedDisabled) {
AddTab(browser(), GURL("chrome://blank"));
for (size_t i = 0; i < arraysize(kInstantNTPTestCases); ++i) {
const SearchTestCase& test = kInstantNTPTestCases[i];
NavigateAndCommitActiveTab(GURL(test.url));
const content::WebContents* contents =
browser()->tab_strip_model()->GetWebContentsAt(0);
EXPECT_FALSE(IsInstantNTP(contents)) << test.url << " " << test.comment;
}
}
TEST_F(SearchTest, InstantNTPCustomNavigationEntry) {
EnableInstantExtendedAPIForTesting();
AddTab(browser(), GURL("chrome://blank"));
for (size_t i = 0; i < arraysize(kInstantNTPTestCases); ++i) {
const SearchTestCase& test = kInstantNTPTestCases[i];
NavigateAndCommitActiveTab(GURL(test.url));
SetSearchProvider(test.url == chrome::kChromeSearchLocalGoogleNtpUrl);
content::WebContents* contents =
browser()->tab_strip_model()->GetWebContentsAt(0);
content::NavigationController& controller = contents->GetController();
controller.SetTransientEntry(
controller.CreateNavigationEntry(GURL("chrome://blank"),
content::Referrer(),
content::PAGE_TRANSITION_LINK,
false,
std::string(),
contents->GetBrowserContext()));
// The active entry is chrome://blank and not an NTP.
EXPECT_FALSE(IsInstantNTP(contents));
EXPECT_EQ(test.expected_result,
NavEntryIsInstantNTP(contents,
controller.GetLastCommittedEntry()))
<< test.url << " " << test.comment;
}
}
TEST_F(SearchTest, GetInstantURLExtendedEnabled) {
// Instant is disabled, so no Instant URL.
EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin));
// Enable Instant. Still no Instant URL because "strk" is missing.
EnableInstantExtendedAPIForTesting();
profile()->GetPrefs()->SetBoolean(prefs::kSearchInstantEnabled, true);
SetDefaultInstantTemplateUrl(false);
EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin));
// Set an Instant URL with a valid search terms replacement key.
SetDefaultInstantTemplateUrl(true);
// Now there should be a valid Instant URL. Note the HTTPS "upgrade".
EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"),
GetInstantURL(profile(), kDisableStartMargin));
// Enable suggest. No difference.
profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true);
EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"),
GetInstantURL(profile(), kDisableStartMargin));
// Disable Instant. No difference, because suggest is still enabled.
profile()->GetPrefs()->SetBoolean(prefs::kSearchInstantEnabled, false);
EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"),
GetInstantURL(profile(), kDisableStartMargin));
// Override the Instant URL on the commandline. Oops, forgot "strk".
CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kInstantURL,
"http://myserver.com:9000/dev?bar=bar#bar=bar");
EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin));
// Override with "strk". For fun, put it in the query, instead of the ref.
CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kInstantURL,
"http://myserver.com:9000/dev?bar=bar&strk#bar=bar");
EXPECT_EQ(GURL("http://myserver.com:9000/dev?bar=bar&strk#bar=bar"),
GetInstantURL(profile(), kDisableStartMargin));
// Disable suggest. No Instant URL.
profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false);
EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin));
}
TEST_F(SearchTest, StartMarginCGI) {
// Instant is disabled, so no Instant URL.
EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin));
// Enable Instant. No margin.
EnableInstantExtendedAPIForTesting();
profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true);
EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"),
GetInstantURL(profile(), kDisableStartMargin));
// With start margin.
EXPECT_EQ(GURL("https://foo.com/instant?es_sm=10&foo=foo#foo=foo&strk"),
GetInstantURL(profile(), 10));
}
TEST_F(SearchTest, DefaultSearchProviderSupportsInstant) {
// Enable Instant.
EnableInstantExtendedAPIForTesting();
profile()->GetPrefs()->SetBoolean(prefs::kSearchInstantEnabled, true);
SetDefaultInstantTemplateUrl(false);
// No default search provider support yet.
EXPECT_FALSE(DefaultSearchProviderSupportsInstant(profile()));
// Set an Instant URL with a valid search terms replacement key.
SetDefaultInstantTemplateUrl(true);
// Default search provider should now support instant.
EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile()));
// Enable suggest. No difference.
profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true);
EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile()));
// Disable Instant. No difference.
profile()->GetPrefs()->SetBoolean(prefs::kSearchInstantEnabled, false);
EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile()));
// Override the Instant URL on the commandline. Oops, forgot "strk".
CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kInstantURL,
"http://myserver.com:9000/dev?bar=bar#bar=bar");
EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin));
// Check that command line overrides don't affect the default search provider.
EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile()));
// Disable suggest. No Instant URL.
profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false);
EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin));
// Even with suggest disabled, the default search provider still supports
// instant.
EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile()));
// Set an Instant URL with no valid search terms replacement key.
SetDefaultInstantTemplateUrl(false);
EXPECT_FALSE(DefaultSearchProviderSupportsInstant(profile()));
}
TEST_F(SearchTest, IsInstantCheckboxEnabledExtendedEnabled) {
// Enable instant extended.
EnableInstantExtendedAPIForTesting();
// Enable suggest.
profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true);
// Set an Instant URL with a valid search terms replacement key.
SetDefaultInstantTemplateUrl(true);
profile()->GetPrefs()->SetBoolean(prefs::kSearchInstantEnabled, true);
EXPECT_TRUE(IsInstantCheckboxEnabled(profile()));
EXPECT_TRUE(IsInstantCheckboxChecked(profile()));
// Set an Instant URL with no valid search terms replacement key.
SetDefaultInstantTemplateUrl(false);
// For extended instant, the checkbox should now be disabled.
EXPECT_FALSE(IsInstantCheckboxEnabled(profile()));
// Disable suggest.
profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false);
EXPECT_FALSE(IsInstantCheckboxEnabled(profile()));
EXPECT_FALSE(IsInstantCheckboxChecked(profile()));
// Set an Instant URL with a search terms replacement key.
SetDefaultInstantTemplateUrl(true);
// Should still be disabled, since suggest is still off.
EXPECT_FALSE(IsInstantCheckboxEnabled(profile()));
profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true);
// Now that suggest is back on and the instant url is good, the checkbox
// should be enabled and checked again.
EXPECT_TRUE(IsInstantCheckboxEnabled(profile()));
EXPECT_TRUE(IsInstantCheckboxChecked(profile()));
}
} // namespace chrome
|