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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
|
// Copyright (c) 2012 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 <stdio.h>
#include "base/command_line.h"
#include "base/scoped_observer.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/autocomplete/history_quick_provider.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/location_bar/location_bar.h"
#include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
#include "chrome/browser/ui/omnibox/omnibox_view.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/toolbar/test_toolbar_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_utils.h"
#include "components/bookmarks/test/bookmark_test_helpers.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/browser/history_service_observer.h"
#include "components/omnibox/autocomplete_input.h"
#include "components/omnibox/autocomplete_match.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_service.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/web_contents.h"
#include "net/dns/mock_host_resolver.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/geometry/point.h"
using base::ASCIIToUTF16;
using base::UTF16ToUTF8;
using base::Time;
using base::TimeDelta;
using bookmarks::BookmarkModel;
namespace {
const char kSearchKeyword[] = "foo";
const char kSearchKeyword2[] = "footest.com";
const ui::KeyboardCode kSearchKeywordKeys[] = {
ui::VKEY_F, ui::VKEY_O, ui::VKEY_O, ui::VKEY_UNKNOWN
};
const ui::KeyboardCode kSearchKeywordPrefixKeys[] = {
ui::VKEY_F, ui::VKEY_O, ui::VKEY_UNKNOWN
};
const ui::KeyboardCode kSearchKeywordCompletionKeys[] = {
ui::VKEY_O, ui::VKEY_UNKNOWN
};
const char kSearchURL[] = "http://www.foo.com/search?q={searchTerms}";
const char kSearchShortName[] = "foo";
const char kSearchText[] = "abc";
const ui::KeyboardCode kSearchTextKeys[] = {
ui::VKEY_A, ui::VKEY_B, ui::VKEY_C, ui::VKEY_UNKNOWN
};
const char kSearchTextURL[] = "http://www.foo.com/search?q=abc";
const char kInlineAutocompleteText[] = "def";
const ui::KeyboardCode kInlineAutocompleteTextKeys[] = {
ui::VKEY_D, ui::VKEY_E, ui::VKEY_F, ui::VKEY_UNKNOWN
};
// Hostnames that shall be blocked by host resolver.
const char *kBlockedHostnames[] = {
"foo",
"*.foo.com",
"bar",
"*.bar.com",
"abc",
"*.abc.com",
"def",
"*.def.com",
"*.site.com",
"history",
"z"
};
const struct TestHistoryEntry {
const char* url;
const char* title;
int visit_count;
int typed_count;
bool starred;
} kHistoryEntries[] = {
{"http://www.bar.com/1", "Page 1", 10, 10, false },
{"http://www.bar.com/2", "Page 2", 9, 9, false },
{"http://www.bar.com/3", "Page 3", 8, 8, false },
{"http://www.bar.com/4", "Page 4", 7, 7, false },
{"http://www.bar.com/5", "Page 5", 6, 6, false },
{"http://www.bar.com/6", "Page 6", 5, 5, false },
{"http://www.bar.com/7", "Page 7", 4, 4, false },
{"http://www.bar.com/8", "Page 8", 3, 3, false },
{"http://www.bar.com/9", "Page 9", 2, 2, false },
{"http://www.site.com/path/1", "Site 1", 4, 4, false },
{"http://www.site.com/path/2", "Site 2", 3, 3, false },
{"http://www.site.com/path/3", "Site 3", 2, 2, false },
// To trigger inline autocomplete.
{"http://www.def.com", "Page def", 10000, 10000, true },
// Used in particular for the desired TLD test. This makes it test
// the interesting case when there's an intranet host with the same
// name as the .com.
{"http://bar/", "Bar", 1, 0, false },
};
// Stores the given text to clipboard.
void SetClipboardText(const base::string16& text) {
ui::ScopedClipboardWriter writer(ui::CLIPBOARD_TYPE_COPY_PASTE);
writer.WriteText(text);
}
#if defined(OS_MACOSX)
const int kCtrlOrCmdMask = ui::EF_COMMAND_DOWN;
#else
const int kCtrlOrCmdMask = ui::EF_CONTROL_DOWN;
#endif
} // namespace
class OmniboxViewTest : public InProcessBrowserTest,
public content::NotificationObserver,
public history::HistoryServiceObserver {
public:
OmniboxViewTest() : observer_(this) {}
// history::HisoryServiceObserver
void OnHistoryServiceLoaded(HistoryService* history_service) override {
base::MessageLoop::current()->Quit();
}
protected:
void SetUpOnMainThread() override {
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
ASSERT_NO_FATAL_FAILURE(SetupComponents());
chrome::FocusLocationBar(browser());
ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
}
static void GetOmniboxViewForBrowser(
const Browser* browser,
OmniboxView** omnibox_view) {
BrowserWindow* window = browser->window();
ASSERT_TRUE(window);
LocationBar* location_bar = window->GetLocationBar();
ASSERT_TRUE(location_bar);
*omnibox_view = location_bar->GetOmniboxView();
ASSERT_TRUE(*omnibox_view);
}
void GetOmniboxView(OmniboxView** omnibox_view) {
GetOmniboxViewForBrowser(browser(), omnibox_view);
}
static void SendKeyForBrowser(const Browser* browser,
ui::KeyboardCode key,
int modifiers) {
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
browser, key,
(modifiers & ui::EF_CONTROL_DOWN) != 0,
(modifiers & ui::EF_SHIFT_DOWN) != 0,
(modifiers & ui::EF_ALT_DOWN) != 0,
(modifiers & ui::EF_COMMAND_DOWN) != 0));
}
void SendKey(ui::KeyboardCode key, int modifiers) {
SendKeyForBrowser(browser(), key, modifiers);
}
void SendKeySequence(const ui::KeyboardCode* keys) {
for (; *keys != ui::VKEY_UNKNOWN; ++keys)
ASSERT_NO_FATAL_FAILURE(SendKey(*keys, 0));
}
bool SendKeyAndWait(const Browser* browser,
ui::KeyboardCode key,
int modifiers,
int type,
const content::NotificationSource& source)
WARN_UNUSED_RESULT {
return ui_test_utils::SendKeyPressAndWait(
browser, key,
(modifiers & ui::EF_CONTROL_DOWN) != 0,
(modifiers & ui::EF_SHIFT_DOWN) != 0,
(modifiers & ui::EF_ALT_DOWN) != 0,
(modifiers & ui::EF_COMMAND_DOWN) != 0,
type, source);
}
void WaitForTabOpenOrCloseForBrowser(const Browser* browser,
int expected_tab_count) {
int tab_count = browser->tab_strip_model()->count();
if (tab_count == expected_tab_count)
return;
content::NotificationRegistrar registrar;
registrar.Add(this,
(tab_count < expected_tab_count) ?
static_cast<int>(chrome::NOTIFICATION_TAB_PARENTED) :
static_cast<int>(content::NOTIFICATION_WEB_CONTENTS_DESTROYED),
content::NotificationService::AllSources());
while (!HasFailure() &&
browser->tab_strip_model()->count() != expected_tab_count) {
content::RunMessageLoop();
}
ASSERT_EQ(expected_tab_count, browser->tab_strip_model()->count());
}
void WaitForTabOpenOrClose(int expected_tab_count) {
WaitForTabOpenOrCloseForBrowser(browser(), expected_tab_count);
}
void WaitForAutocompleteControllerDone() {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
AutocompleteController* controller =
omnibox_view->model()->autocomplete_controller();
ASSERT_TRUE(controller);
if (controller->done())
return;
content::NotificationRegistrar registrar;
registrar.Add(this,
chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY,
content::Source<AutocompleteController>(controller));
while (!HasFailure() && !controller->done())
content::RunMessageLoop();
ASSERT_TRUE(controller->done());
}
void SetupSearchEngine() {
Profile* profile = browser()->profile();
TemplateURLService* model =
TemplateURLServiceFactory::GetForProfile(profile);
ASSERT_TRUE(model);
ui_test_utils::WaitForTemplateURLServiceToLoad(model);
ASSERT_TRUE(model->loaded());
TemplateURLData data;
data.short_name = ASCIIToUTF16(kSearchShortName);
data.SetKeyword(ASCIIToUTF16(kSearchKeyword));
data.SetURL(kSearchURL);
TemplateURL* template_url = new TemplateURL(data);
model->Add(template_url);
model->SetUserSelectedDefaultSearchProvider(template_url);
data.SetKeyword(ASCIIToUTF16(kSearchKeyword2));
model->Add(new TemplateURL(data));
// Remove built-in template urls, like google.com, bing.com etc., as they
// may appear as autocomplete suggests and interfere with our tests.
TemplateURLService::TemplateURLVector urls = model->GetTemplateURLs();
for (TemplateURLService::TemplateURLVector::const_iterator i = urls.begin();
i != urls.end();
++i) {
if ((*i)->prepopulate_id() != 0)
model->Remove(*i);
}
}
void AddHistoryEntry(const TestHistoryEntry& entry, const Time& time) {
Profile* profile = browser()->profile();
HistoryService* history_service = HistoryServiceFactory::GetForProfile(
profile, ServiceAccessType::EXPLICIT_ACCESS);
ASSERT_TRUE(history_service);
if (!history_service->BackendLoaded()) {
observer_.Add(history_service);
content::RunMessageLoop();
}
BookmarkModel* bookmark_model =
BookmarkModelFactory::GetForProfile(profile);
ASSERT_TRUE(bookmark_model);
bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model);
GURL url(entry.url);
// Add everything in order of time. We don't want to have a time that
// is "right now" or it will nondeterministically appear in the results.
history_service->AddPageWithDetails(url, base::UTF8ToUTF16(entry.title),
entry.visit_count,
entry.typed_count, time, false,
history::SOURCE_BROWSED);
if (entry.starred)
bookmarks::AddIfNotBookmarked(bookmark_model, url, base::string16());
// Wait at least for the AddPageWithDetails() call to finish.
{
ScopedObserver<HistoryService, history::HistoryServiceObserver> observer(
this);
observer.Add(history_service);
content::RunMessageLoop();
// We don't want to return until all observers have processed this
// notification, because some (e.g. the in-memory history database) may do
// something important. Since we don't know where in the observer list we
// stand, just spin the message loop once more to allow the current
// callstack to complete.
content::RunAllPendingInMessageLoop();
}
}
void SetupHistory() {
// Add enough history pages containing |kSearchText| to trigger
// open history page url in autocomplete result.
for (size_t i = 0; i < arraysize(kHistoryEntries); i++) {
// Add everything in order of time. We don't want to have a time that
// is "right now" or it will nondeterministically appear in the results.
Time t = Time::Now() - TimeDelta::FromHours(i + 1);
ASSERT_NO_FATAL_FAILURE(AddHistoryEntry(kHistoryEntries[i], t));
}
}
void SetupHostResolver() {
for (size_t i = 0; i < arraysize(kBlockedHostnames); ++i)
host_resolver()->AddSimulatedFailure(kBlockedHostnames[i]);
}
void SetupComponents() {
ASSERT_NO_FATAL_FAILURE(SetupHostResolver());
ASSERT_NO_FATAL_FAILURE(SetupSearchEngine());
ASSERT_NO_FATAL_FAILURE(SetupHistory());
}
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override {
switch (type) {
case content::NOTIFICATION_WEB_CONTENTS_DESTROYED:
case chrome::NOTIFICATION_TAB_PARENTED:
case chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY:
break;
default:
FAIL() << "Unexpected notification type";
}
base::MessageLoop::current()->Quit();
}
void OnURLsModified(HistoryService* history_service,
const history::URLRows& changed_urls) override {
base::MessageLoop::current()->Quit();
}
private:
ScopedObserver<HistoryService, OmniboxViewTest> observer_;
DISALLOW_COPY_AND_ASSIGN(OmniboxViewTest);
};
// Test if ctrl-* accelerators are workable in omnibox.
// See http://crbug.com/19193: omnibox blocks ctrl-* commands
//
// Flaky on interactive tests (dbg), http://crbug.com/69433
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DISABLED_BrowserAccelerators) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
int tab_count = browser()->tab_strip_model()->count();
// Create a new Tab.
chrome::NewTab(browser());
ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count + 1));
// Select the first Tab.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_1, kCtrlOrCmdMask));
ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
chrome::FocusLocationBar(browser());
// Select the second Tab.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_2, kCtrlOrCmdMask));
ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
chrome::FocusLocationBar(browser());
// Try ctrl-w to close a Tab.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_W, kCtrlOrCmdMask));
ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count));
// Try ctrl-l to focus location bar.
omnibox_view->SetUserText(ASCIIToUTF16("Hello world"));
EXPECT_FALSE(omnibox_view->IsSelectAll());
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_L, kCtrlOrCmdMask));
EXPECT_TRUE(omnibox_view->IsSelectAll());
// Try editing the location bar text.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT, 0));
EXPECT_FALSE(omnibox_view->IsSelectAll());
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_S, 0));
EXPECT_EQ(ASCIIToUTF16("Hello worlds"), omnibox_view->GetText());
// Try ctrl-x to cut text.
#if defined(OS_MACOSX)
// Mac uses alt-left/right to select a word.
ASSERT_NO_FATAL_FAILURE(
SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN));
#else
ASSERT_NO_FATAL_FAILURE(
SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN));
#endif
EXPECT_FALSE(omnibox_view->IsSelectAll());
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_X, kCtrlOrCmdMask));
EXPECT_EQ(ASCIIToUTF16("Hello "), omnibox_view->GetText());
#if !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
// Try alt-f4 to close the browser.
ASSERT_TRUE(SendKeyAndWait(
browser(), ui::VKEY_F4, ui::EF_ALT_DOWN,
chrome::NOTIFICATION_BROWSER_CLOSED,
content::Source<Browser>(browser())));
#endif
}
// Fails on Linux. http://crbug.com/408634
#if defined(OS_LINUX)
#define MAYBE_PopupAccelerators DISABLED_PopupAccelerators
#else
#define MAYBE_PopupAccelerators PopupAccelerators
#endif
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_PopupAccelerators) {
// Create a popup.
Browser* popup = CreateBrowserForPopup(browser()->profile());
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(popup));
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(
GetOmniboxViewForBrowser(popup, &omnibox_view));
chrome::FocusLocationBar(popup);
EXPECT_TRUE(omnibox_view->IsSelectAll());
#if !defined(OS_MACOSX)
// Try ctrl-w to close the popup.
// This piece of code doesn't work on Mac, because the Browser object won't
// be destroyed before finishing the current message loop iteration, thus
// No BROWSER_CLOSED notification will be sent.
ASSERT_TRUE(SendKeyAndWait(
popup, ui::VKEY_W, ui::EF_CONTROL_DOWN,
chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(popup)));
// Create another popup.
popup = CreateBrowserForPopup(browser()->profile());
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(popup));
ASSERT_NO_FATAL_FAILURE(
GetOmniboxViewForBrowser(popup, &omnibox_view));
#endif
// Set the edit text to "Hello world".
omnibox_view->SetUserText(ASCIIToUTF16("Hello world"));
chrome::FocusLocationBar(popup);
EXPECT_TRUE(omnibox_view->IsSelectAll());
// Try editing the location bar text -- should be disallowed.
ASSERT_NO_FATAL_FAILURE(SendKeyForBrowser(popup, ui::VKEY_S, 0));
EXPECT_EQ(ASCIIToUTF16("Hello world"), omnibox_view->GetText());
EXPECT_TRUE(omnibox_view->IsSelectAll());
ASSERT_NO_FATAL_FAILURE(
SendKeyForBrowser(popup, ui::VKEY_X, kCtrlOrCmdMask));
EXPECT_EQ(ASCIIToUTF16("Hello world"), omnibox_view->GetText());
EXPECT_TRUE(omnibox_view->IsSelectAll());
#if !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
// Try alt-f4 to close the popup.
ASSERT_TRUE(SendKeyAndWait(
popup, ui::VKEY_F4, ui::EF_ALT_DOWN,
chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(popup)));
#endif
}
// http://crbug.com/133341
#if defined(OS_LINUX)
#define MAYBE_BackspaceInKeywordMode DISABLED_BackspaceInKeywordMode
#else
#define MAYBE_BackspaceInKeywordMode BackspaceInKeywordMode
#endif
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_BackspaceInKeywordMode) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
// Trigger keyword hint mode.
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
// Trigger keyword mode.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
// Backspace without search text should bring back keyword hint mode.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
// Trigger keyword mode again.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
// Input something as search text.
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
// Should stay in keyword mode while deleting search text by pressing
// backspace.
for (size_t i = 0; i < arraysize(kSearchText) - 1; ++i) {
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
}
// Input something as search text.
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
// Move cursor to the beginning of the search text.
#if defined(OS_MACOSX)
// Home doesn't work on Mac trybot.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A, ui::EF_CONTROL_DOWN));
#else
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_HOME, 0));
#endif
// Backspace at the beginning of the search text shall turn off
// the keyword mode.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(base::string16(), omnibox_view->model()->keyword());
ASSERT_EQ(std::string(kSearchKeyword) + kSearchText,
UTF16ToUTF8(omnibox_view->GetText()));
}
// http://crbug.com/158913
#if defined(USE_AURA)
#define MAYBE_Escape DISABLED_Escape
#else
#define MAYBE_Escape Escape
#endif
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_Escape) {
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIHistoryURL));
chrome::FocusLocationBar(browser());
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
base::string16 old_text = omnibox_view->GetText();
EXPECT_FALSE(old_text.empty());
EXPECT_TRUE(omnibox_view->IsSelectAll());
// Delete all text in omnibox.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
EXPECT_TRUE(omnibox_view->GetText().empty());
// Escape shall revert the text in omnibox.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_ESCAPE, 0));
EXPECT_EQ(old_text, omnibox_view->GetText());
EXPECT_TRUE(omnibox_view->IsSelectAll());
}
#undef MAYBE_ESCAPE
#if defined(OS_LINUX)
#define MAYBE_DesiredTLD DISABLED_DesiredTLD
#else
#define MAYBE_DesiredTLD DesiredTLD
#endif
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_DesiredTLD) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
ASSERT_TRUE(popup_model);
// Test ctrl-Enter.
const ui::KeyboardCode kKeys[] = {
ui::VKEY_B, ui::VKEY_A, ui::VKEY_R, ui::VKEY_UNKNOWN
};
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_TRUE(popup_model->IsOpen());
// ctrl-Enter triggers desired_tld feature, thus www.bar.com shall be
// opened.
ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN, ui::EF_CONTROL_DOWN,
content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<content::NavigationController>(
&browser()->tab_strip_model()->GetActiveWebContents()->
GetController())));
GURL url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
EXPECT_EQ("www.bar.com", url.host());
EXPECT_EQ("/", url.path());
}
#if defined(OS_LINUX)
#define MAYBE_DesiredTLDWithTemporaryText DISABLED_DesiredTLDWithTemporaryText
#else
#define MAYBE_DesiredTLDWithTemporaryText DesiredTLDWithTemporaryText
#endif
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_DesiredTLDWithTemporaryText) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
ASSERT_TRUE(popup_model);
Profile* profile = browser()->profile();
TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(profile);
// Add a non-substituting keyword. This ensures the popup will have a
// non-verbatim entry with "ab" as a prefix. This way, by arrowing down, we
// can set "abc" as temporary text in the omnibox.
TemplateURLData data;
data.short_name = ASCIIToUTF16("abc");
data.SetKeyword(ASCIIToUTF16(kSearchText));
data.SetURL("http://abc.com/");
template_url_service->Add(new TemplateURL(data));
// Send "ab", so that an "abc" entry appears in the popup.
const ui::KeyboardCode kSearchTextPrefixKeys[] = {
ui::VKEY_A, ui::VKEY_B, ui::VKEY_UNKNOWN
};
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextPrefixKeys));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_TRUE(popup_model->IsOpen());
// Arrow down to the "abc" entry in the popup.
size_t size = popup_model->result().size();
while (popup_model->selected_line() < size - 1) {
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN, 0));
if (omnibox_view->GetText() == ASCIIToUTF16("abc"))
break;
}
ASSERT_EQ(ASCIIToUTF16("abc"), omnibox_view->GetText());
// Hitting ctrl-enter should navigate based on the current text rather than
// the original input, i.e. to www.abc.com instead of www.ab.com.
ASSERT_TRUE(SendKeyAndWait(
browser(), ui::VKEY_RETURN, ui::EF_CONTROL_DOWN,
content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<content::NavigationController>(
&browser()->tab_strip_model()->GetActiveWebContents()->
GetController())));
GURL url(browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
EXPECT_EQ("www.abc.com", url.host());
EXPECT_EQ("/", url.path());
}
// See http://crbug.com/431575.
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, ClearUserTextAfterBackgroundCommit) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
// Navigate in first tab and enter text into the omnibox.
GURL url1("data:text/html,page1");
ui_test_utils::NavigateToURL(browser(), url1);
omnibox_view->SetUserText(ASCIIToUTF16("foo"));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
// Create another tab in the foreground.
AddTabAtIndex(1, url1, ui::PAGE_TRANSITION_TYPED);
EXPECT_EQ(2, browser()->tab_strip_model()->count());
EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
// Navigate in the first tab, currently in the background.
GURL url2("data:text/html,page2");
chrome::NavigateParams params(browser(), url2, ui::PAGE_TRANSITION_LINK);
params.source_contents = contents;
params.disposition = CURRENT_TAB;
ui_test_utils::NavigateToURL(¶ms);
// Switch back to the first tab. The user text should be cleared, and the
// omnibox should have the new URL.
browser()->tab_strip_model()->ActivateTabAt(0, true);
EXPECT_EQ(ASCIIToUTF16(url2.spec()), omnibox_view->GetText());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, AltEnter) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
omnibox_view->SetUserText(ASCIIToUTF16(chrome::kChromeUIHistoryURL));
int tab_count = browser()->tab_strip_model()->count();
// alt-Enter opens a new tab.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RETURN, ui::EF_ALT_DOWN));
ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count + 1));
}
// http://crbug.com/133354, http://crbug.com/146953
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DISABLED_EnterToSearch) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
ASSERT_TRUE(popup_model);
// Test Enter to search.
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_TRUE(popup_model->IsOpen());
// Check if the default match result is Search Primary Provider.
ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
popup_model->result().default_match()->type);
// Open the default match.
ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN, 0,
content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<content::NavigationController>(
&browser()->tab_strip_model()->GetActiveWebContents()->
GetController())));
GURL url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
EXPECT_EQ(kSearchTextURL, url.spec());
// Test that entering a single character then Enter performs a search.
const ui::KeyboardCode kSearchSingleCharKeys[] = {
ui::VKEY_Z, ui::VKEY_UNKNOWN
};
chrome::FocusLocationBar(browser());
EXPECT_TRUE(omnibox_view->IsSelectAll());
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchSingleCharKeys));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_TRUE(popup_model->IsOpen());
EXPECT_EQ("z", UTF16ToUTF8(omnibox_view->GetText()));
// Check if the default match result is Search Primary Provider.
ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
popup_model->result().default_match()->type);
// Open the default match.
ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN, 0,
content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<content::NavigationController>(
&browser()->tab_strip_model()->GetActiveWebContents()->
GetController())));
url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
EXPECT_EQ("http://www.foo.com/search?q=z", url.spec());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, EscapeToDefaultMatch) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
ASSERT_TRUE(popup_model);
// Input something to trigger inline autocomplete.
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_TRUE(popup_model->IsOpen());
base::string16 old_text = omnibox_view->GetText();
// Make sure inline autocomplete is triggerred.
EXPECT_GT(old_text.length(), arraysize(kInlineAutocompleteText) - 1);
size_t old_selected_line = popup_model->selected_line();
EXPECT_EQ(0U, old_selected_line);
// Move to another line with different text.
size_t size = popup_model->result().size();
while (popup_model->selected_line() < size - 1) {
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN, 0));
ASSERT_NE(old_selected_line, popup_model->selected_line());
if (old_text != omnibox_view->GetText())
break;
}
EXPECT_NE(old_text, omnibox_view->GetText());
// Escape shall revert back to the default match item.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_ESCAPE, 0));
EXPECT_EQ(old_text, omnibox_view->GetText());
EXPECT_EQ(old_selected_line, popup_model->selected_line());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, BasicTextOperations) {
ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL));
chrome::FocusLocationBar(browser());
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
base::string16 old_text = omnibox_view->GetText();
EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), old_text);
EXPECT_TRUE(omnibox_view->IsSelectAll());
size_t start, end;
omnibox_view->GetSelectionBounds(&start, &end);
#if defined(OS_WIN) || defined(OS_LINUX)
// Views textfields select-all in reverse to show the leading text.
std::swap(start, end);
#endif
EXPECT_EQ(0U, start);
EXPECT_EQ(old_text.size(), end);
// Move the cursor to the end.
#if defined(OS_MACOSX)
// End doesn't work on Mac trybot.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_E, ui::EF_CONTROL_DOWN));
#else
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_END, 0));
#endif
EXPECT_FALSE(omnibox_view->IsSelectAll());
// Make sure the cursor is placed correctly.
omnibox_view->GetSelectionBounds(&start, &end);
EXPECT_EQ(old_text.size(), start);
EXPECT_EQ(old_text.size(), end);
// Insert one character at the end. Make sure we won't insert
// anything after the special ZWS mark used in gtk implementation.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A, 0));
EXPECT_EQ(old_text + base::char16('a'), omnibox_view->GetText());
// Delete one character from the end. Make sure we won't delete the special
// ZWS mark used in gtk implementation.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
EXPECT_EQ(old_text, omnibox_view->GetText());
omnibox_view->SelectAll(true);
EXPECT_TRUE(omnibox_view->IsSelectAll());
omnibox_view->GetSelectionBounds(&start, &end);
#if defined(OS_WIN) || defined(OS_LINUX)
// Views textfields select-all in reverse to show the leading text.
std::swap(start, end);
#endif
EXPECT_EQ(0U, start);
EXPECT_EQ(old_text.size(), end);
// Delete the content
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DELETE, 0));
EXPECT_TRUE(omnibox_view->IsSelectAll());
omnibox_view->GetSelectionBounds(&start, &end);
EXPECT_EQ(0U, start);
EXPECT_EQ(0U, end);
EXPECT_TRUE(omnibox_view->GetText().empty());
// Check if RevertAll() can set text and cursor correctly.
omnibox_view->RevertAll();
EXPECT_FALSE(omnibox_view->IsSelectAll());
EXPECT_EQ(old_text, omnibox_view->GetText());
omnibox_view->GetSelectionBounds(&start, &end);
EXPECT_EQ(old_text.size(), start);
EXPECT_EQ(old_text.size(), end);
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, AcceptKeywordBySpace) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
base::string16 search_keyword(ASCIIToUTF16(kSearchKeyword));
// Trigger keyword hint mode.
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
ASSERT_EQ(search_keyword, omnibox_view->GetText());
// Trigger keyword mode by space.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
ASSERT_TRUE(omnibox_view->GetText().empty());
// Revert to keyword hint mode.
omnibox_view->model()->ClearKeyword(base::string16());
ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
ASSERT_EQ(search_keyword, omnibox_view->GetText());
// Keyword should also be accepted by typing an ideographic space.
omnibox_view->OnBeforePossibleChange();
omnibox_view->SetWindowTextAndCaretPos(search_keyword +
base::WideToUTF16(L"\x3000"), search_keyword.length() + 1, false, false);
omnibox_view->OnAfterPossibleChange();
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
ASSERT_TRUE(omnibox_view->GetText().empty());
// Revert to keyword hint mode.
omnibox_view->model()->ClearKeyword(base::string16());
ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
ASSERT_EQ(search_keyword, omnibox_view->GetText());
// Keyword shouldn't be accepted by pressing space with a trailing
// whitespace.
omnibox_view->SetWindowTextAndCaretPos(search_keyword + base::char16(' '),
search_keyword.length() + 1, false, false);
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
ASSERT_EQ(search_keyword + ASCIIToUTF16(" "), omnibox_view->GetText());
// Keyword shouldn't be accepted by deleting the trailing space.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
ASSERT_EQ(search_keyword + base::char16(' '), omnibox_view->GetText());
// Keyword shouldn't be accepted by pressing space before a trailing space.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
ASSERT_EQ(search_keyword + ASCIIToUTF16(" "), omnibox_view->GetText());
// Keyword should be accepted by pressing space in the middle of context and
// just after the keyword.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
ASSERT_EQ(ASCIIToUTF16("a "), omnibox_view->GetText());
size_t start, end;
omnibox_view->GetSelectionBounds(&start, &end);
EXPECT_EQ(0U, start);
EXPECT_EQ(0U, end);
// Keyword shouldn't be accepted by pasting "foo bar".
omnibox_view->SetUserText(base::string16());
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_TRUE(omnibox_view->model()->keyword().empty());
omnibox_view->OnBeforePossibleChange();
omnibox_view->model()->OnPaste();
omnibox_view->SetWindowTextAndCaretPos(search_keyword +
ASCIIToUTF16(" bar"), search_keyword.length() + 4, false, false);
omnibox_view->OnAfterPossibleChange();
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_TRUE(omnibox_view->model()->keyword().empty());
ASSERT_EQ(search_keyword + ASCIIToUTF16(" bar"), omnibox_view->GetText());
// Keyword shouldn't be accepted for case like: "foo b|ar" -> "foo b |ar".
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_TRUE(omnibox_view->model()->keyword().empty());
ASSERT_EQ(search_keyword + ASCIIToUTF16(" b ar"), omnibox_view->GetText());
// Keyword could be accepted by pressing space with a selected range at the
// end of text.
omnibox_view->OnBeforePossibleChange();
omnibox_view->OnInlineAutocompleteTextMaybeChanged(
search_keyword + ASCIIToUTF16(" "), search_keyword.length());
omnibox_view->OnAfterPossibleChange();
ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
ASSERT_EQ(search_keyword + ASCIIToUTF16(" "), omnibox_view->GetText());
omnibox_view->GetSelectionBounds(&start, &end);
ASSERT_NE(start, end);
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
ASSERT_EQ(base::string16(), omnibox_view->GetText());
// Space should accept keyword even when inline autocomplete is available.
omnibox_view->SetUserText(base::string16());
const TestHistoryEntry kHistoryFoobar = {
"http://www.foobar.com", "Page foobar", 100, 100, true
};
// Add a history entry to trigger inline autocomplete when typing "foo".
ASSERT_NO_FATAL_FAILURE(
AddHistoryEntry(kHistoryFoobar, Time::Now() - TimeDelta::FromHours(1)));
// Type "fo" to trigger inline autocomplete.
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordPrefixKeys));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_TRUE(omnibox_view->model()->popup_model()->IsOpen());
ASSERT_NE(search_keyword, omnibox_view->GetText());
// Keyword hint shouldn't be visible.
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_TRUE(omnibox_view->model()->keyword().empty());
// Add the "o". Inline autocompletion should still happen, but now we
// should also get a keyword hint because we've typed a keyword exactly.
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordCompletionKeys));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_TRUE(omnibox_view->model()->popup_model()->IsOpen());
ASSERT_NE(search_keyword, omnibox_view->GetText());
ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
ASSERT_FALSE(omnibox_view->model()->keyword().empty());
// Trigger keyword mode by space.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
ASSERT_TRUE(omnibox_view->GetText().empty());
// Space in the middle of a temporary text, which separates the text into
// keyword and replacement portions, should trigger keyword mode.
omnibox_view->SetUserText(base::string16());
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
ASSERT_TRUE(popup_model->IsOpen());
ASSERT_EQ(ASCIIToUTF16("foobar.com"), omnibox_view->GetText());
omnibox_view->model()->OnUpOrDownKeyPressed(1);
omnibox_view->model()->OnUpOrDownKeyPressed(-1);
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
ASSERT_EQ(ASCIIToUTF16("bar.com"), omnibox_view->GetText());
// Space after temporary text that looks like a keyword, when the original
// input does not look like a keyword, should trigger keyword mode.
omnibox_view->SetUserText(base::string16());
const TestHistoryEntry kHistoryFoo = {
"http://footest.com", "Page footest", 1000, 1000, true
};
// Add a history entry to trigger HQP matching with text == keyword when
// typing "fo te".
ASSERT_NO_FATAL_FAILURE(
AddHistoryEntry(kHistoryFoo, Time::Now() - TimeDelta::FromMinutes(10)));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_F, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_O, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_T, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_E, 0));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_TRUE(popup_model->IsOpen());
base::string16 search_keyword2(ASCIIToUTF16(kSearchKeyword2));
while ((omnibox_view->GetText() != search_keyword2) &&
(popup_model->selected_line() < popup_model->result().size() - 1))
omnibox_view->model()->OnUpOrDownKeyPressed(1);
ASSERT_EQ(search_keyword2, omnibox_view->GetText());
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(search_keyword2, omnibox_view->model()->keyword());
ASSERT_TRUE(omnibox_view->GetText().empty());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, NonSubstitutingKeywordTest) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
ASSERT_TRUE(popup_model);
Profile* profile = browser()->profile();
TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(profile);
// Add a non-default substituting keyword.
TemplateURLData data;
data.short_name = ASCIIToUTF16("Search abc");
data.SetKeyword(ASCIIToUTF16(kSearchText));
data.SetURL("http://abc.com/{searchTerms}");
TemplateURL* template_url = new TemplateURL(data);
template_url_service->Add(template_url);
omnibox_view->SetUserText(base::string16());
// Non-default substituting keyword shouldn't be matched by default.
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_TRUE(popup_model->IsOpen());
// Check if the default match result is Search Primary Provider.
ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
popup_model->result().default_match()->type);
ASSERT_EQ(kSearchTextURL,
popup_model->result().default_match()->destination_url.spec());
omnibox_view->SetUserText(base::string16());
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_FALSE(popup_model->IsOpen());
// Try a non-substituting keyword.
template_url_service->Remove(template_url);
data.short_name = ASCIIToUTF16("abc");
data.SetURL("http://abc.com/");
template_url_service->Add(new TemplateURL(data));
// We always allow exact matches for non-substituting keywords.
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_TRUE(popup_model->IsOpen());
ASSERT_EQ(AutocompleteMatchType::HISTORY_KEYWORD,
popup_model->result().default_match()->type);
ASSERT_EQ("http://abc.com/",
popup_model->result().default_match()->destination_url.spec());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DeleteItem) {
// Disable the search provider, to make sure the popup contains only history
// items.
TemplateURLService* model =
TemplateURLServiceFactory::GetForProfile(browser()->profile());
model->SetUserSelectedDefaultSearchProvider(NULL);
ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL));
chrome::FocusLocationBar(browser());
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
ASSERT_TRUE(popup_model);
base::string16 old_text = omnibox_view->GetText();
// Input something that can match history items.
omnibox_view->SetUserText(ASCIIToUTF16("site.com/p"));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_TRUE(popup_model->IsOpen());
// Delete the inline autocomplete part.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DELETE, 0));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_TRUE(popup_model->IsOpen());
ASSERT_GE(popup_model->result().size(), 3U);
base::string16 user_text = omnibox_view->GetText();
ASSERT_EQ(ASCIIToUTF16("site.com/p"), user_text);
omnibox_view->SelectAll(true);
ASSERT_TRUE(omnibox_view->IsSelectAll());
// Move down.
size_t default_line = popup_model->selected_line();
omnibox_view->model()->OnUpOrDownKeyPressed(1);
ASSERT_EQ(default_line + 1, popup_model->selected_line());
base::string16 selected_text =
popup_model->result().match_at(default_line + 1).fill_into_edit;
// Temporary text is shown.
ASSERT_EQ(selected_text, omnibox_view->GetText());
ASSERT_FALSE(omnibox_view->IsSelectAll());
// Delete the item.
popup_model->TryDeletingCurrentItem();
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
// The selected line shouldn't be changed, because we have more than two
// items.
ASSERT_EQ(default_line + 1, popup_model->selected_line());
// Make sure the item is really deleted.
ASSERT_NE(selected_text,
popup_model->result().match_at(default_line + 1).fill_into_edit);
selected_text =
popup_model->result().match_at(default_line + 1).fill_into_edit;
// New temporary text is shown.
ASSERT_EQ(selected_text, omnibox_view->GetText());
// Revert to the default match.
ASSERT_TRUE(omnibox_view->model()->OnEscapeKeyPressed());
ASSERT_EQ(default_line, popup_model->selected_line());
ASSERT_EQ(user_text, omnibox_view->GetText());
ASSERT_TRUE(omnibox_view->IsSelectAll());
// Move down and up to select the default match as temporary text.
omnibox_view->model()->OnUpOrDownKeyPressed(1);
ASSERT_EQ(default_line + 1, popup_model->selected_line());
omnibox_view->model()->OnUpOrDownKeyPressed(-1);
ASSERT_EQ(default_line, popup_model->selected_line());
selected_text = popup_model->result().match_at(default_line).fill_into_edit;
// New temporary text is shown.
ASSERT_EQ(selected_text, omnibox_view->GetText());
ASSERT_FALSE(omnibox_view->IsSelectAll());
#if 0
// TODO(mrossetti): http://crbug.com/82335
// Delete the default item.
popup_model->TryDeletingCurrentItem();
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
// The selected line shouldn't be changed, but the default item should have
// been changed.
ASSERT_EQ(default_line, popup_model->selected_line());
// Make sure the item is really deleted.
EXPECT_NE(selected_text,
popup_model->result().match_at(default_line).fill_into_edit);
selected_text =
popup_model->result().match_at(default_line).fill_into_edit;
// New temporary text is shown.
ASSERT_EQ(selected_text, omnibox_view->GetText());
#endif
// As the current selected item is the new default item, pressing Escape key
// should revert all directly.
ASSERT_TRUE(omnibox_view->model()->OnEscapeKeyPressed());
ASSERT_EQ(old_text, omnibox_view->GetText());
ASSERT_TRUE(omnibox_view->IsSelectAll());
}
// http://crbug.com/133344
#if defined(OS_LINUX)
#define MAYBE_TabAcceptKeyword DISABLED_TabAcceptKeyword
#else
#define MAYBE_TabAcceptKeyword TabAcceptKeyword
#endif
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_TabAcceptKeyword) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
base::string16 text = ASCIIToUTF16(kSearchKeyword);
// Trigger keyword hint mode.
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(text, omnibox_view->model()->keyword());
ASSERT_EQ(text, omnibox_view->GetText());
// Trigger keyword mode by tab.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(text, omnibox_view->model()->keyword());
ASSERT_TRUE(omnibox_view->GetText().empty());
// Revert to keyword hint mode.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(text, omnibox_view->model()->keyword());
ASSERT_EQ(text, omnibox_view->GetText());
// The location bar should still have focus.
ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
// Trigger keyword mode by tab.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(text, omnibox_view->model()->keyword());
ASSERT_TRUE(omnibox_view->GetText().empty());
// Revert to keyword hint mode with SHIFT+TAB.
#if defined(OS_MACOSX)
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACKTAB, 0));
#else
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
#endif
ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(text, omnibox_view->model()->keyword());
ASSERT_EQ(text, omnibox_view->GetText());
ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
}
#if !defined(OS_MACOSX)
// Mac intentionally does not support this behavior.
// http://crbug.com/133360
#if defined(OS_LINUX)
#define MAYBE_TabTraverseResultsTest DISABLED_TabTraverseResultsTest
#else
#define MAYBE_TabTraverseResultsTest TabTraverseResultsTest
#endif
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_TabTraverseResultsTest) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
ASSERT_TRUE(popup_model);
// Input something to trigger results.
const ui::KeyboardCode kKeys[] = {
ui::VKEY_B, ui::VKEY_A, ui::VKEY_R, ui::VKEY_UNKNOWN
};
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_TRUE(popup_model->IsOpen());
size_t old_selected_line = popup_model->selected_line();
EXPECT_EQ(0U, old_selected_line);
// Move down the results.
for (size_t size = popup_model->result().size();
popup_model->selected_line() < size - 1;
old_selected_line = popup_model->selected_line()) {
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
ASSERT_LT(old_selected_line, popup_model->selected_line());
}
// Don't move past the end.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
ASSERT_EQ(old_selected_line, popup_model->selected_line());
ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
// Move back up the results.
for (; popup_model->selected_line() > 0U;
old_selected_line = popup_model->selected_line()) {
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
ASSERT_GT(old_selected_line, popup_model->selected_line());
}
// Don't move past the beginning.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
ASSERT_EQ(0U, popup_model->selected_line());
ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
const TestHistoryEntry kHistoryFoo = {
"http://foo/", "Page foo", 1, 1, false
};
// Add a history entry so "foo" gets multiple matches.
ASSERT_NO_FATAL_FAILURE(
AddHistoryEntry(kHistoryFoo, Time::Now() - TimeDelta::FromHours(1)));
// Load results.
ASSERT_NO_FATAL_FAILURE(omnibox_view->SelectAll(false));
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
// Trigger keyword mode by tab.
base::string16 text = ASCIIToUTF16(kSearchKeyword);
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(text, omnibox_view->model()->keyword());
ASSERT_TRUE(omnibox_view->GetText().empty());
// The location bar should still have focus.
ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
// Pressing tab again should move to the next result and clear keyword
// mode.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
ASSERT_EQ(1U, omnibox_view->model()->popup_model()->selected_line());
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_NE(text, omnibox_view->model()->keyword());
// The location bar should still have focus.
ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
// Moving back up should not show keyword mode.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(text, omnibox_view->model()->keyword());
ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
}
#endif
// http://crbug.com/133347
#if defined(OS_LINUX)
#define MAYBE_PersistKeywordModeOnTabSwitch \
DISABLED_PersistKeywordModeOnTabSwitch
#else
#define MAYBE_PersistKeywordModeOnTabSwitch PersistKeywordModeOnTabSwitch
#endif
IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
MAYBE_PersistKeywordModeOnTabSwitch) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
// Trigger keyword hint mode.
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
// Trigger keyword mode.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
// Input something as search text.
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
// Create a new tab.
chrome::NewTab(browser());
// Switch back to the first tab.
browser()->tab_strip_model()->ActivateTabAt(0, true);
// Make sure we're still in keyword mode.
ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
}
// http://crbug.com/133355
#if defined(OS_LINUX)
#define MAYBE_CtrlKeyPressedWithInlineAutocompleteTest \
DISABLED_CtrlKeyPressedWithInlineAutocompleteTest
#else
#define MAYBE_CtrlKeyPressedWithInlineAutocompleteTest \
CtrlKeyPressedWithInlineAutocompleteTest
#endif
IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
MAYBE_CtrlKeyPressedWithInlineAutocompleteTest) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
ASSERT_TRUE(popup_model);
// Input something to trigger inline autocomplete.
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_TRUE(popup_model->IsOpen());
base::string16 old_text = omnibox_view->GetText();
// Make sure inline autocomplete is triggerred.
EXPECT_GT(old_text.length(), arraysize(kInlineAutocompleteText) - 1);
// Press ctrl key.
omnibox_view->model()->OnControlKeyChanged(true);
// Inline autocomplete should still be there.
EXPECT_EQ(old_text, omnibox_view->GetText());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, UndoRedo) {
ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL));
chrome::FocusLocationBar(browser());
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
base::string16 old_text = omnibox_view->GetText();
EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), old_text);
EXPECT_TRUE(omnibox_view->IsSelectAll());
// Delete the text, then undo.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
EXPECT_TRUE(omnibox_view->GetText().empty());
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, kCtrlOrCmdMask));
EXPECT_EQ(old_text, omnibox_view->GetText());
// Redo should delete the text again.
ASSERT_NO_FATAL_FAILURE(
SendKey(ui::VKEY_Z, kCtrlOrCmdMask | ui::EF_SHIFT_DOWN));
EXPECT_TRUE(omnibox_view->GetText().empty());
// The toolkit-views undo manager doesn't support restoring selection. Cocoa
// does, so it needs to be cleared.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, kCtrlOrCmdMask));
#if defined(OS_MACOSX)
// TODO(tapted): This next line may fail if running a toolkit-views browser
// window on Mac. We should fix the toolkit-views undo manager to restore
// selection rather than deleting this #ifdef.
EXPECT_TRUE(omnibox_view->IsSelectAll());
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT, 0));
#endif
EXPECT_FALSE(omnibox_view->IsSelectAll());
// The cursor should be at the end.
size_t start, end;
omnibox_view->GetSelectionBounds(&start, &end);
EXPECT_EQ(old_text.size(), start);
EXPECT_EQ(old_text.size(), end);
// Delete three characters; "about:bl" should not trigger inline autocomplete.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
EXPECT_EQ(old_text.substr(0, old_text.size() - 3), omnibox_view->GetText());
// Undo delete.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, kCtrlOrCmdMask));
EXPECT_EQ(old_text, omnibox_view->GetText());
// Redo delete.
ASSERT_NO_FATAL_FAILURE(
SendKey(ui::VKEY_Z, kCtrlOrCmdMask | ui::EF_SHIFT_DOWN));
EXPECT_EQ(old_text.substr(0, old_text.size() - 3), omnibox_view->GetText());
// Delete everything.
omnibox_view->SelectAll(true);
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
EXPECT_TRUE(omnibox_view->GetText().empty());
// Undo delete everything.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, kCtrlOrCmdMask));
EXPECT_EQ(old_text.substr(0, old_text.size() - 3), omnibox_view->GetText());
// Undo delete two characters.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, kCtrlOrCmdMask));
EXPECT_EQ(old_text, omnibox_view->GetText());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, BackspaceDeleteHalfWidthKatakana) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
// Insert text: ダ. This is two, 3-byte UTF-8 characters:
// U+FF80 "HALFWIDTH KATAKANA LETTER TA" and
// U+FF9E "HALFWIDTH KATAKANA VOICED SOUND MARK".
omnibox_view->SetUserText(base::UTF8ToUTF16("\357\276\200\357\276\236"));
EXPECT_FALSE(omnibox_view->GetText().empty());
// Move the cursor to the end.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_END, 0));
// Backspace should delete the character. In http://crbug.com/192743, the bug
// was that nothing was deleted.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
#if defined(OS_MACOSX)
// Cocoa text fields attach the sound mark and delete the whole thing. This
// behavior should remain on Mac even when using a toolkit-views browser
// window.
EXPECT_TRUE(omnibox_view->GetText().empty());
#else
// Toolkit-views text fields delete just the sound mark.
EXPECT_EQ(base::UTF8ToUTF16("\357\276\200"), omnibox_view->GetText());
#endif
}
// Flaky test. crbug.com/356850
IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
DISABLED_DoesNotUpdateAutocompleteOnBlur) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
ASSERT_TRUE(popup_model);
// Input something to trigger inline autocomplete.
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_TRUE(popup_model->IsOpen());
size_t start, end;
omnibox_view->GetSelectionBounds(&start, &end);
EXPECT_TRUE(start != end);
base::string16 old_autocomplete_text =
omnibox_view->model()->autocomplete_controller()->input_.text();
// Unfocus the omnibox. This should clear the text field selection and
// close the popup, but should not run autocomplete.
// Note: GTK preserves the selection when the omnibox is unfocused.
ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
ASSERT_FALSE(popup_model->IsOpen());
omnibox_view->GetSelectionBounds(&start, &end);
EXPECT_TRUE(start == end);
EXPECT_EQ(old_autocomplete_text,
omnibox_view->model()->autocomplete_controller()->input_.text());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, Paste) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
ASSERT_TRUE(popup_model);
EXPECT_FALSE(popup_model->IsOpen());
// Paste should yield the expected text and open the popup.
SetClipboardText(ASCIIToUTF16(kSearchText));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V, kCtrlOrCmdMask));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
EXPECT_EQ(ASCIIToUTF16(kSearchText), omnibox_view->GetText());
EXPECT_TRUE(popup_model->IsOpen());
// Close the popup and select all.
omnibox_view->CloseOmniboxPopup();
omnibox_view->SelectAll(false);
EXPECT_FALSE(popup_model->IsOpen());
// Pasting the same text again over itself should re-open the popup.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V, kCtrlOrCmdMask));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
EXPECT_EQ(ASCIIToUTF16(kSearchText), omnibox_view->GetText());
EXPECT_TRUE(popup_model->IsOpen());
omnibox_view->CloseOmniboxPopup();
EXPECT_FALSE(popup_model->IsOpen());
// Pasting amid text should yield the expected text and re-open the popup.
omnibox_view->SetWindowTextAndCaretPos(ASCIIToUTF16("abcd"), 2, false, false);
SetClipboardText(ASCIIToUTF16("123"));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V, kCtrlOrCmdMask));
EXPECT_EQ(ASCIIToUTF16("ab123cd"), omnibox_view->GetText());
EXPECT_TRUE(popup_model->IsOpen());
// Ctrl/Cmd+Alt+V should not paste.
ASSERT_NO_FATAL_FAILURE(
SendKey(ui::VKEY_V, kCtrlOrCmdMask | ui::EF_ALT_DOWN));
EXPECT_EQ(ASCIIToUTF16("ab123cd"), omnibox_view->GetText());
// TODO(msw): Test that AltGr+V does not paste.
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CopyURLToClipboard) {
// Set permanent text thus making sure that omnibox treats 'google.com'
// as URL (not as ordinary user input).
TestToolbarModel* test_toolbar_model = new TestToolbarModel;
scoped_ptr<ToolbarModel> toolbar_model(test_toolbar_model);
test_toolbar_model->set_text(ASCIIToUTF16("http://www.google.com/"));
browser()->swap_toolbar_models(&toolbar_model);
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
OmniboxEditModel* edit_model = omnibox_view->model();
ASSERT_NE(static_cast<OmniboxEditModel*>(NULL), edit_model);
edit_model->UpdatePermanentText();
const char* target_url = "http://www.google.com/calendar";
omnibox_view->SetUserText(ASCIIToUTF16(target_url));
// Location bar must have focus.
chrome::FocusLocationBar(browser());
ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
// Select full URL and copy it to clipboard. General text and html should
// be available.
omnibox_view->SelectAll(true);
EXPECT_TRUE(omnibox_view->IsSelectAll());
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_COPY));
EXPECT_EQ(ASCIIToUTF16(target_url), omnibox_view->GetText());
EXPECT_TRUE(clipboard->IsFormatAvailable(
ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
// Make sure HTML format isn't written. See
// BookmarkNodeData::WriteToClipboard() for details.
EXPECT_FALSE(clipboard->IsFormatAvailable(
ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
// These platforms should read bookmark format.
#if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
base::string16 title;
std::string url;
clipboard->ReadBookmark(&title, &url);
EXPECT_EQ(target_url, url);
EXPECT_EQ(ASCIIToUTF16(target_url), title);
#endif
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CutURLToClipboard) {
// Set permanent text thus making sure that omnibox treats 'google.com'
// as URL (not as ordinary user input).
TestToolbarModel* test_toolbar_model = new TestToolbarModel;
scoped_ptr<ToolbarModel> toolbar_model(test_toolbar_model);
test_toolbar_model->set_text(ASCIIToUTF16("http://www.google.com/"));
browser()->swap_toolbar_models(&toolbar_model);
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
OmniboxEditModel* edit_model = omnibox_view->model();
ASSERT_NE(static_cast<OmniboxEditModel*>(NULL), edit_model);
edit_model->UpdatePermanentText();
const char* target_url = "http://www.google.com/calendar";
omnibox_view->SetUserText(ASCIIToUTF16(target_url));
// Location bar must have focus.
chrome::FocusLocationBar(browser());
ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
// Select full URL and cut it. General text and html should be available
// in the clipboard.
omnibox_view->SelectAll(true);
EXPECT_TRUE(omnibox_view->IsSelectAll());
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_CUT));
EXPECT_EQ(base::string16(), omnibox_view->GetText());
EXPECT_TRUE(clipboard->IsFormatAvailable(
ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
// Make sure HTML format isn't written. See
// BookmarkNodeData::WriteToClipboard() for details.
EXPECT_FALSE(clipboard->IsFormatAvailable(
ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
// These platforms should read bookmark format.
#if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
base::string16 title;
std::string url;
clipboard->ReadBookmark(&title, &url);
EXPECT_EQ(target_url, url);
EXPECT_EQ(ASCIIToUTF16(target_url), title);
#endif
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CopyTextToClipboard) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
const char* target_text = "foo";
omnibox_view->SetUserText(ASCIIToUTF16(target_text));
// Location bar must have focus.
chrome::FocusLocationBar(browser());
ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
// Select full text and copy it to the clipboard.
omnibox_view->SelectAll(true);
EXPECT_TRUE(omnibox_view->IsSelectAll());
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_COPY));
EXPECT_TRUE(clipboard->IsFormatAvailable(
ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
EXPECT_FALSE(clipboard->IsFormatAvailable(
ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
EXPECT_EQ(ASCIIToUTF16(target_text), omnibox_view->GetText());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CutTextToClipboard) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
const char* target_text = "foo";
omnibox_view->SetUserText(ASCIIToUTF16(target_text));
// Location bar must have focus.
chrome::FocusLocationBar(browser());
ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
// Select full text and cut it to the clipboard.
omnibox_view->SelectAll(true);
EXPECT_TRUE(omnibox_view->IsSelectAll());
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_CUT));
EXPECT_TRUE(clipboard->IsFormatAvailable(
ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
EXPECT_FALSE(clipboard->IsFormatAvailable(
ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
EXPECT_EQ(base::string16(), omnibox_view->GetText());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, EditSearchEngines) {
// Disable settings-in-a-window to simplify test.
base::CommandLine::ForCurrentProcess()->AppendSwitch(
::switches::kDisableSettingsWindow);
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_EDIT_SEARCH_ENGINES));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
const std::string target_url =
std::string(chrome::kChromeUISettingsURL) + chrome::kSearchEnginesSubPage;
EXPECT_EQ(ASCIIToUTF16(target_url), omnibox_view->GetText());
EXPECT_FALSE(omnibox_view->model()->popup_model()->IsOpen());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, BeginningShownAfterBlur) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
omnibox_view->OnBeforePossibleChange();
omnibox_view->SetWindowTextAndCaretPos(ASCIIToUTF16("data:text/plain,test"),
5U, false, false);
omnibox_view->OnAfterPossibleChange();
EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
size_t start, end;
omnibox_view->GetSelectionBounds(&start, &end);
EXPECT_EQ(5U, start);
EXPECT_EQ(5U, end);
ui_test_utils::FocusView(browser(), VIEW_ID_TAB_CONTAINER);
EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
omnibox_view->GetSelectionBounds(&start, &end);
EXPECT_EQ(0U, start);
EXPECT_EQ(0U, end);
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CtrlArrowAfterArrowSuggestions) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
ASSERT_TRUE(popup_model);
// Input something to trigger results.
const ui::KeyboardCode kKeys[] = {
ui::VKEY_B, ui::VKEY_A, ui::VKEY_R, ui::VKEY_UNKNOWN
};
ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_TRUE(popup_model->IsOpen());
ASSERT_EQ(ASCIIToUTF16("bar.com/1"), omnibox_view->GetText());
// Arrow down on a suggestion, and omnibox text should be the suggestion.
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN, 0));
ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
ASSERT_EQ(ASCIIToUTF16("www.bar.com/2"), omnibox_view->GetText());
// Highlight the last 2 words and the omnibox text should not change.
// Simulating Ctrl-shift-left only once does not seem to highlight anything
// on Linux.
#if defined(OS_MACOSX)
// Mac uses alt-left/right to select a word.
const int modifiers = ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN;
#else
const int modifiers = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN;
#endif
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, modifiers));
ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, modifiers));
ASSERT_EQ(ASCIIToUTF16("www.bar.com/2"), omnibox_view->GetText());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
PersistSearchReplacementAcrossTabSwitch) {
EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
browser()->toolbar_model()->set_url_replacement_enabled(false);
// Create a new tab.
chrome::NewTab(browser());
EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
// Switch back to the first tab.
browser()->tab_strip_model()->ActivateTabAt(0, true);
EXPECT_FALSE(browser()->toolbar_model()->url_replacement_enabled());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
DontUpdateURLWhileSearchTermReplacementIsDisabled) {
OmniboxView* omnibox_view = NULL;
ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
TestToolbarModel* test_toolbar_model = new TestToolbarModel;
scoped_ptr<ToolbarModel> toolbar_model(test_toolbar_model);
browser()->swap_toolbar_models(&toolbar_model);
base::string16 url_a(ASCIIToUTF16("http://www.a.com/"));
base::string16 url_b(ASCIIToUTF16("http://www.b.com/"));
base::string16 url_c(ASCIIToUTF16("http://www.c.com/"));
chrome::FocusLocationBar(browser());
test_toolbar_model->set_text(url_a);
omnibox_view->Update();
EXPECT_EQ(url_a, omnibox_view->GetText());
// Disable URL replacement and update. Because the omnibox has focus, the
// visible text shouldn't change; see comments in
// OmniboxEditModel::UpdatePermanentText().
browser()->toolbar_model()->set_url_replacement_enabled(false);
test_toolbar_model->set_text(url_b);
omnibox_view->Update();
EXPECT_EQ(url_a, omnibox_view->GetText());
// Re-enable URL replacement and ensure updating changes the text.
browser()->toolbar_model()->set_url_replacement_enabled(true);
// We have to change the toolbar model text here, or Update() will do nothing.
// This is because the previous update already updated the permanent text.
test_toolbar_model->set_text(url_c);
omnibox_view->Update();
EXPECT_EQ(url_c, omnibox_view->GetText());
// The same test, but using RevertAll() to reset search term replacement.
test_toolbar_model->set_text(url_a);
omnibox_view->Update();
EXPECT_EQ(url_a, omnibox_view->GetText());
browser()->toolbar_model()->set_url_replacement_enabled(false);
test_toolbar_model->set_text(url_b);
omnibox_view->Update();
EXPECT_EQ(url_a, omnibox_view->GetText());
omnibox_view->RevertAll();
EXPECT_EQ(url_b, omnibox_view->GetText());
test_toolbar_model->set_text(url_c);
omnibox_view->Update();
EXPECT_EQ(url_c, omnibox_view->GetText());
}
|