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
|
// Copyright (c) 2010 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 "chrome/browser/renderer_host/test/test_render_view_host.h"
#include "base/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/prefs/pref_change_registrar.h"
#include "chrome/browser/renderer_host/mock_render_process_host.h"
#include "chrome/browser/tab_contents/navigation_controller.h"
#include "chrome/browser/tab_contents/render_view_context_menu.h"
#include "chrome/browser/tab_contents/test_tab_contents.h"
#include "chrome/browser/translate/translate_infobar_delegate.h"
#include "chrome/browser/translate/translate_manager.h"
#include "chrome/browser/translate/translate_prefs.h"
#include "chrome/common/ipc_test_sink.h"
#include "chrome/common/notification_details.h"
#include "chrome/common/notification_observer_mock.h"
#include "chrome/common/notification_registrar.h"
#include "chrome/common/notification_type.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/net/test_url_fetcher_factory.h"
#include "chrome/test/testing_browser_process.h"
#include "chrome/test/testing_profile.h"
#include "grit/generated_resources.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/cld/languages/public/languages.h"
#include "third_party/WebKit/WebKit/chromium/public/WebContextMenuData.h"
using testing::_;
using testing::Pointee;
using testing::Property;
using WebKit::WebContextMenuData;
class TranslateManagerTest : public RenderViewHostTestHarness,
public NotificationObserver {
public:
TranslateManagerTest() {}
// Simluates navigating to a page and getting the page contents and language
// for that navigation.
void SimulateNavigation(const GURL& url,
const std::string& contents,
const std::string& lang,
bool page_translatable) {
NavigateAndCommit(url);
int page_id = RenderViewHostTestHarness::contents()->controller().
GetLastCommittedEntry()->page_id();
SimulateOnPageContents(url, page_id, contents, lang, page_translatable);
}
void SimulateOnPageContents(const GURL& url, int page_id,
const std::string& contents,
const std::string& lang,
bool page_translatable) {
rvh()->TestOnMessageReceived(ViewHostMsg_PageContents(0, url, page_id,
UTF8ToUTF16(contents),
lang,
page_translatable));
}
bool GetTranslateMessage(int* page_id,
std::string* original_lang,
std::string* target_lang) {
const IPC::Message* message =
process()->sink().GetFirstMessageMatching(ViewMsg_TranslatePage::ID);
if (!message)
return false;
Tuple4<int, std::string, std::string, std::string> translate_param;
ViewMsg_TranslatePage::Read(message, &translate_param);
if (page_id)
*page_id = translate_param.a;
// Ignore translate_param.b which is the script injected in the page.
if (original_lang)
*original_lang = translate_param.c;
if (target_lang)
*target_lang = translate_param.d;
return true;
}
// Returns the translate infobar if there is 1 infobar and it is a translate
// infobar.
TranslateInfoBarDelegate* GetTranslateInfoBar() {
if (contents()->infobar_delegate_count() != 1)
return NULL;
return contents()->GetInfoBarDelegateAt(0)->AsTranslateInfoBarDelegate();
}
// If there is 1 infobar and it is a translate infobar, closes it and returns
// true. Returns false otherwise.
bool CloseTranslateInfoBar() {
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
if (!infobar)
return false;
infobar->InfoBarDismissed(); // Simulates closing the infobar.
contents()->RemoveInfoBar(infobar);
return true;
}
// Checks whether |infobar| has been removed and clears the removed infobar
// list.
bool CheckInfoBarRemovedAndReset(InfoBarDelegate* infobar) {
bool found = std::find(removed_infobars_.begin(), removed_infobars_.end(),
infobar) != removed_infobars_.end();
removed_infobars_.clear();
return found;
}
// Returns true if at least one infobar was closed.
bool InfoBarRemoved() {
return !removed_infobars_.empty();
}
// Clears the list of stored removed infobars.
void ClearRemovedInfoBars() {
removed_infobars_.clear();
}
void ExpireTranslateScriptImmediately() {
TranslateManager::GetInstance()->set_translate_script_expiration_delay(0);
}
// If there is 1 infobar and it is a translate infobar, deny translation and
// returns true. Returns false otherwise.
bool DenyTranslation() {
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
if (!infobar)
return false;
infobar->TranslationDeclined();
contents()->RemoveInfoBar(infobar);
return true;
}
virtual void Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
DCHECK(type == NotificationType::TAB_CONTENTS_INFOBAR_REMOVED);
removed_infobars_.push_back(Details<InfoBarDelegate>(details).ptr());
}
protected:
virtual void SetUp() {
URLFetcher::set_factory(&url_fetcher_factory_);
// Access the TranslateManager singleton so it is created before we call
// RenderViewHostTestHarness::SetUp() to match what's done in Chrome, where
// the TranslateManager is created before the TabContents. This matters as
// they both register for similar events and we want the notifications to
// happen in the same sequence (TranslateManager first, TabContents second).
// Also clears the translate script so it is fetched everytime and sets the
// expiration delay to a large value by default (in case it was zeroed in
// a previous test).
TranslateManager::GetInstance()->ClearTranslateScript();
TranslateManager::GetInstance()->
set_translate_script_expiration_delay(60 * 60 * 1000);
RenderViewHostTestHarness::SetUp();
notification_registrar_.Add(
this,
NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
Source<TabContents>(contents()));
}
virtual void TearDown() {
process()->sink().ClearMessages();
notification_registrar_.Remove(
this,
NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
Source<TabContents>(contents()));
RenderViewHostTestHarness::TearDown();
URLFetcher::set_factory(NULL);
}
void SimulateURLFetch(bool success) {
TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
URLRequestStatus status;
status.set_status(success ? URLRequestStatus::SUCCESS :
URLRequestStatus::FAILED);
fetcher->delegate()->OnURLFetchComplete(fetcher, fetcher->original_url(),
status, success ? 200 : 500,
ResponseCookies(),
std::string());
}
void SetPrefObserverExpectation(const char* path) {
EXPECT_CALL(
pref_observer_,
Observe(NotificationType(NotificationType::PREF_CHANGED),
_,
Property(&Details<std::string>::ptr, Pointee(path))));
}
NotificationObserverMock pref_observer_;
private:
NotificationRegistrar notification_registrar_;
TestURLFetcherFactory url_fetcher_factory_;
// The list of infobars that have been removed.
// WARNING: the pointers points to deleted objects, use only for comparison.
std::vector<InfoBarDelegate*> removed_infobars_;
DISALLOW_COPY_AND_ASSIGN(TranslateManagerTest);
};
// An observer that keeps track of whether a navigation entry was committed.
class NavEntryCommittedObserver : public NotificationObserver {
public:
explicit NavEntryCommittedObserver(TabContents* tab_contents) {
registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
Source<NavigationController>(&tab_contents->controller()));
}
virtual void Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
DCHECK(type == NotificationType::NAV_ENTRY_COMMITTED);
details_ =
*(Details<NavigationController::LoadCommittedDetails>(details).ptr());
}
const NavigationController::LoadCommittedDetails&
get_load_commited_details() const {
return details_;
}
private:
NavigationController::LoadCommittedDetails details_;
NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(NavEntryCommittedObserver);
};
class TestRenderViewContextMenu : public RenderViewContextMenu {
public:
static TestRenderViewContextMenu* CreateContextMenu(
TabContents* tab_contents) {
ContextMenuParams params;
params.media_type = WebKit::WebContextMenuData::MediaTypeNone;
params.x = 0;
params.y = 0;
params.is_image_blocked = false;
params.media_flags = 0;
params.spellcheck_enabled = false;
params.is_editable = false;
params.page_url = tab_contents->controller().GetActiveEntry()->url();
#if defined(OS_MACOSX)
params.writing_direction_default = 0;
params.writing_direction_left_to_right = 0;
params.writing_direction_right_to_left = 0;
#endif // OS_MACOSX
params.edit_flags = WebContextMenuData::CanTranslate;
return new TestRenderViewContextMenu(tab_contents, params);
}
bool IsItemPresent(int id) {
return menu_model_.GetIndexOfCommandId(id) != -1;
}
virtual void PlatformInit() { }
virtual bool GetAcceleratorForCommandId(
int command_id,
menus::Accelerator* accelerator) { return false; }
private:
TestRenderViewContextMenu(TabContents* tab_contents,
const ContextMenuParams& params)
: RenderViewContextMenu(tab_contents, params) {
}
DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu);
};
TEST_F(TranslateManagerTest, NormalTranslate) {
// Simulate navigating to a page.
SimulateNavigation(GURL("http://www.google.fr"), "Le Google", "fr", true);
// We should have an infobar.
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type());
// Simulate clicking translate.
process()->sink().ClearMessages();
infobar->Translate();
// The "Translating..." infobar should be showing.
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->type());
// Simulate the translate script being retrieved (it only needs to be done
// once in the test as it is cached).
SimulateURLFetch(true);
// Test that we sent the right message to the renderer.
int page_id = 0;
std::string original_lang, target_lang;
EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
EXPECT_EQ("fr", original_lang);
EXPECT_EQ("en", target_lang);
// Simulate the render notifying the translation has been done.
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en",
TranslateErrors::NONE));
// The after translate infobar should be showing.
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->type());
// Simulate changing the original language, this should trigger a translation.
process()->sink().ClearMessages();
std::string new_original_lang = infobar->GetLanguageCodeAt(0);
infobar->SetOriginalLanguage(0);
EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
EXPECT_EQ(new_original_lang, original_lang);
EXPECT_EQ("en", target_lang);
// Simulate the render notifying the translation has been done.
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0,
new_original_lang, "en", TranslateErrors::NONE));
// infobar is now invalid.
TranslateInfoBarDelegate* new_infobar = GetTranslateInfoBar();
ASSERT_TRUE(new_infobar != NULL);
infobar = new_infobar;
// Simulate changing the target language, this should trigger a translation.
process()->sink().ClearMessages();
std::string new_target_lang = infobar->GetLanguageCodeAt(1);
infobar->SetTargetLanguage(1);
EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
EXPECT_EQ(new_original_lang, original_lang);
EXPECT_EQ(new_target_lang, target_lang);
// Simulate the render notifying the translation has been done.
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0,
new_original_lang, new_target_lang, TranslateErrors::NONE));
// infobar is now invalid.
new_infobar = GetTranslateInfoBar();
ASSERT_TRUE(new_infobar != NULL);
}
TEST_F(TranslateManagerTest, TranslateScriptNotAvailable) {
// Simulate navigating to a page.
SimulateNavigation(GURL("http://www.google.fr"), "Le Google", "fr", true);
// We should have an infobar.
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type());
// Simulate clicking translate.
process()->sink().ClearMessages();
infobar->Translate();
// Simulate a failure retrieving the translate script.
SimulateURLFetch(false);
// We should not have sent any message to translate to the renderer.
EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL));
// And we should have an error infobar showing.
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type());
}
// Ensures we deal correctly with pages for which the browser does not recognize
// the language (the translate server may or not detect the language).
TEST_F(TranslateManagerTest, TranslateUnknownLanguage) {
// Simulate navigating to a page ("und" is the string returned by the CLD for
// languages it does not recognize).
SimulateNavigation(GURL("http://www.google.mys"), "G00g1e", "und", true);
// We should not have an infobar as we don't know the language.
ASSERT_TRUE(GetTranslateInfoBar() == NULL);
// Translate the page anyway throught the context menu.
scoped_ptr<TestRenderViewContextMenu> menu(
TestRenderViewContextMenu::CreateContextMenu(contents()));
menu->Init();
menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE);
// To test that bug #49018 if fixed, make sure we deal correctly with errors.
SimulateURLFetch(false); // Simulate a failure to fetch the translate script.
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type());
EXPECT_TRUE(infobar->IsError());
infobar->MessageInfoBarButtonPressed();
SimulateURLFetch(true); // This time succeed.
// Simulate the render notifying the translation has been done, the server
// having detected the page was in a known and supported language.
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en",
TranslateErrors::NONE));
// The after translate infobar should be showing.
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->type());
EXPECT_EQ("fr", infobar->GetOriginalLanguageCode());
EXPECT_EQ("en", infobar->GetTargetLanguageCode());
// Let's run the same steps but this time the server detects the page is
// already in English.
SimulateNavigation(GURL("http://www.google.com"), "The Google", "und", true);
menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents()));
menu->Init();
menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE);
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(1, 0, "en", "en",
TranslateErrors::IDENTICAL_LANGUAGES));
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type());
EXPECT_EQ(TranslateErrors::IDENTICAL_LANGUAGES, infobar->error());
// Let's run the same steps again but this time the server fails to detect the
// page's language (it returns an empty string).
SimulateNavigation(GURL("http://www.google.com"), "The Google", "und", true);
menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents()));
menu->Init();
menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE);
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(2, 0, "", "en",
TranslateErrors::UNKNOWN_LANGUAGE));
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type());
EXPECT_EQ(TranslateErrors::UNKNOWN_LANGUAGE, infobar->error());
}
// Tests that we show/don't show an info-bar for all languages the CLD can
// report.
TEST_F(TranslateManagerTest, TestAllLanguages) {
// The index in kExpectation are the Language enum (see languages.pb.h).
// true if we expect a translate infobar for that language.
// Note the supported languages are in translation_manager.cc, see
// kSupportedLanguages.
bool kExpectations[] = {
// 0-9
false, true, true, true, true, true, true, true, true, true,
// 10-19
true, true, true, true, true, true, true, true, true, true,
// 20-29
true, true, true, true, true, false, false, true, true, true,
// 30-39
true, true, true, true, true, true, true, false, true, false,
// 40-49
true, false, true, false, false, true, false, true, false, false,
// 50-59
true, false, false, true, true, true, false, true, false, false,
// 60-69
false, false, true, true, false, true, true, false, true, true,
// 70-79
false, false, false, false, true, true, false, true, false, false,
// 80-89
false, false, false, false, false, false, false, false, false, false,
// 90-99
false, true, false, false, false, false, false, true, false, false,
// 100-109
false, true, false, false, false, false, false, false, false, false,
// 110-119
false, false, false, false, false, false, false, false, false, false,
// 120-129
false, false, false, false, false, false, false, false, false, false,
// 130-139
false, false, false, false, false, false, false, false, false, true,
// 140-149
false, false, false, false, false, false, false, false, false, false,
// 150-159
false, false, false, false, false, false, false, false, false, false,
// 160
false
};
GURL url("http://www.google.com");
for (size_t i = 0; i < arraysize(kExpectations); ++i) {
ASSERT_LT(i, static_cast<size_t>(NUM_LANGUAGES));
std::string lang = LanguageCodeWithDialects(static_cast<Language>(i));
SCOPED_TRACE(::testing::Message::Message() << "Iteration " << i <<
" language=" << lang);
// We should not have a translate infobar.
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar == NULL);
// Simulate navigating to a page.
NavigateAndCommit(url);
SimulateOnPageContents(url, i, "", lang, true);
// Verify we have/don't have an info-bar as expected.
infobar = GetTranslateInfoBar();
EXPECT_EQ(kExpectations[i], infobar != NULL);
// Close the info-bar if applicable.
if (infobar != NULL)
EXPECT_TRUE(CloseTranslateInfoBar());
}
}
// Tests auto-translate on page.
TEST_F(TranslateManagerTest, AutoTranslateOnNavigate) {
// Simulate navigating to a page and getting its language.
SimulateNavigation(GURL("http://www.google.fr"), "Le Google", "fr", true);
// Simulate the user translating.
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
infobar->Translate();
SimulateURLFetch(true); // Simulate the translate script being retrieved.
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en",
TranslateErrors::NONE));
// Now navigate to a new page in the same language.
process()->sink().ClearMessages();
SimulateNavigation(GURL("http://news.google.fr"), "Les news", "fr", true);
// This should have automatically triggered a translation.
int page_id = 0;
std::string original_lang, target_lang;
EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
EXPECT_EQ(1, page_id);
EXPECT_EQ("fr", original_lang);
EXPECT_EQ("en", target_lang);
// Now navigate to a page in a different language.
process()->sink().ClearMessages();
SimulateNavigation(GURL("http://news.google.es"), "Las news", "es", true);
// This should not have triggered a translate.
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
}
// Tests that multiple OnPageContents do not cause multiple infobars.
TEST_F(TranslateManagerTest, MultipleOnPageContents) {
// Simulate navigating to a page and getting its language.
SimulateNavigation(GURL("http://www.google.fr"), "Le Google", "fr", true);
// Simulate clicking 'Nope' (don't translate).
EXPECT_TRUE(DenyTranslation());
EXPECT_EQ(0, contents()->infobar_delegate_count());
// Send a new PageContents, we should not show an infobar.
SimulateOnPageContents(GURL("http://www.google.fr"), 0, "Le Google", "fr",
true);
EXPECT_EQ(0, contents()->infobar_delegate_count());
// Do the same steps but simulate closing the infobar this time.
SimulateNavigation(GURL("http://www.youtube.fr"), "Le YouTube", "fr",
true);
EXPECT_TRUE(CloseTranslateInfoBar());
EXPECT_EQ(0, contents()->infobar_delegate_count());
SimulateOnPageContents(GURL("http://www.youtube.fr"), 1, "Le YouTube", "fr",
true);
EXPECT_EQ(0, contents()->infobar_delegate_count());
}
// Test that reloading the page brings back the infobar.
TEST_F(TranslateManagerTest, Reload) {
// Simulate navigating to a page and getting its language.
SimulateNavigation(GURL("http://www.google.fr"), "Le Google", "fr", true);
// Close the infobar.
EXPECT_TRUE(CloseTranslateInfoBar());
// Reload should bring back the infobar.
NavEntryCommittedObserver nav_observer(contents());
Reload();
// Ensures it is really handled a reload.
const NavigationController::LoadCommittedDetails& nav_details =
nav_observer.get_load_commited_details();
EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation.
EXPECT_EQ(NavigationType::EXISTING_PAGE, nav_details.type);
// The TranslateManager class processes the navigation entry committed
// notification in a posted task; process that task.
MessageLoop::current()->RunAllPending();
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
// Test that reloading the page by way of typing again the URL in the
// location bar brings back the infobar.
TEST_F(TranslateManagerTest, ReloadFromLocationBar) {
GURL url("http://www.google.fr");
// Simulate navigating to a page and getting its language.
SimulateNavigation(url, "Le Google", "fr", true);
// Close the infobar.
EXPECT_TRUE(CloseTranslateInfoBar());
// Create a pending navigation and simulate a page load. That should be the
// equivalent of typing the URL again in the location bar.
NavEntryCommittedObserver nav_observer(contents());
contents()->controller().LoadURL(url, GURL(), PageTransition::TYPED);
rvh()->SendNavigate(0, url);
// Test that we are really getting a same page navigation, the test would be
// useless if it was not the case.
const NavigationController::LoadCommittedDetails& nav_details =
nav_observer.get_load_commited_details();
EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation.
EXPECT_EQ(NavigationType::SAME_PAGE, nav_details.type);
// The TranslateManager class processes the navigation entry committed
// notification in a posted task; process that task.
MessageLoop::current()->RunAllPending();
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
// Tests that a closed translate infobar does not reappear when navigating
// in-page.
TEST_F(TranslateManagerTest, CloseInfoBarInPageNavigation) {
// Simulate navigating to a page and getting its language.
SimulateNavigation(GURL("http://www.google.fr"), "Le Google", "fr", true);
// Close the infobar.
EXPECT_TRUE(CloseTranslateInfoBar());
// Navigate in page, no infobar should be shown.
SimulateNavigation(GURL("http://www.google.fr/#ref1"), "Le Google", "fr",
true);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
// Navigate out of page, a new infobar should show.
SimulateNavigation(GURL("http://www.google.fr/foot"), "Le Google", "fr",
true);
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
// Tests that a closed translate infobar does not reappear when navigating
// in a subframe. (http://crbug.com/48215)
TEST_F(TranslateManagerTest, CloseInfoBarInSubframeNavigation) {
// Simulate navigating to a page and getting its language.
SimulateNavigation(GURL("http://www.google.fr"), "Le Google", "fr", true);
// Close the infobar.
EXPECT_TRUE(CloseTranslateInfoBar());
// Simulate a sub-frame auto-navigating.
rvh()->SendNavigateWithTransition(1, GURL("http://pub.com"),
PageTransition::AUTO_SUBFRAME);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
// Simulate the user navigating in a sub-frame.
rvh()->SendNavigateWithTransition(2, GURL("http://pub.com"),
PageTransition::MANUAL_SUBFRAME);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
// Navigate out of page, a new infobar should show.
SimulateNavigation(GURL("http://www.google.fr/foot"), "Le Google", "fr",
true);
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
// Tests that denying translation is sticky when navigating in page.
TEST_F(TranslateManagerTest, DenyTranslateInPageNavigation) {
// Simulate navigating to a page and getting its language.
SimulateNavigation(GURL("http://www.google.fr"), "Le Google", "fr", true);
// Simulate clicking 'Nope' (don't translate).
EXPECT_TRUE(DenyTranslation());
// Navigate in page, no infobar should be shown.
SimulateNavigation(GURL("http://www.google.fr/#ref1"), "Le Google", "fr",
true);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
// Navigate out of page, a new infobar should show.
SimulateNavigation(GURL("http://www.google.fr/foot"), "Le Google", "fr",
true);
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
// Tests that after translating and closing the infobar, the infobar does not
// return when navigating in page.
TEST_F(TranslateManagerTest, TranslateCloseInfoBarInPageNavigation) {
// Simulate navigating to a page and getting its language.
SimulateNavigation(GURL("http://www.google.fr"), "Le Google", "fr", true);
// Simulate the user translating.
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
infobar->Translate();
SimulateURLFetch(true); // Simulate the translate script being retrieved.
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en",
TranslateErrors::NONE));
// Close the infobar.
EXPECT_TRUE(CloseTranslateInfoBar());
// Navigate in page, no infobar should be shown.
SimulateNavigation(GURL("http://www.google.fr/#ref1"), "Le Google", "fr",
true);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
// Navigate out of page, a new infobar should show.
// Note that we navigate to a page in a different language so we don't trigger
// the auto-translate feature (it would translate the page automatically and
// the before translate inforbar would not be shown).
SimulateNavigation(GURL("http://www.google.de"), "Das Google", "de", true);
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
// Tests that the after translate the infobar still shows when navigating
// in-page.
TEST_F(TranslateManagerTest, TranslateInPageNavigation) {
// Simulate navigating to a page and getting its language.
SimulateNavigation(GURL("http://www.google.fr"), "Le Google", "fr", true);
// Simulate the user translating.
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
infobar->Translate();
SimulateURLFetch(true); // Simulate the translate script being retrieved.
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en",
TranslateErrors::NONE));
// The after translate infobar is showing.
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
// Navigate in page, the same infobar should still be shown.
ClearRemovedInfoBars();
SimulateNavigation(GURL("http://www.google.fr/#ref1"), "Le Google", "fr",
true);
EXPECT_FALSE(InfoBarRemoved());
EXPECT_EQ(infobar, GetTranslateInfoBar());
// Navigate out of page, a new infobar should show.
// See note in TranslateCloseInfoBarInPageNavigation test on why it is
// important to navigate to a page in a different language for this test.
SimulateNavigation(GURL("http://www.google.de"), "Das Google", "de", true);
// The old infobar is gone.
EXPECT_TRUE(CheckInfoBarRemovedAndReset(infobar));
// And there is a new one.
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
// Tests that no translate infobar is shown when navigating to a page in an
// unsupported language.
TEST_F(TranslateManagerTest, CLDReportsUnsupportedPageLanguage) {
// Simulate navigating to a page and getting an unsupported language.
SimulateNavigation(GURL("http://www.google.com"), "Google", "qbz", true);
// No info-bar should be shown.
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
}
// Tests that we deal correctly with unsupported languages returned by the
// server.
// The translation server might return a language we don't support.
TEST_F(TranslateManagerTest, ServerReportsUnsupportedLanguage) {
// Simulate navigating to a page and translating it.
SimulateNavigation(GURL("http://mail.google.fr"), "Le Google", "fr", true);
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
process()->sink().ClearMessages();
infobar->Translate();
SimulateURLFetch(true);
// Simulate the render notifying the translation has been done, but it
// reports a language we don't support.
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "qbz", "en",
TranslateErrors::NONE));
// An error infobar should be showing to report that we don't support this
// language.
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type());
// This infobar should have a button (so the string should not be empty).
ASSERT_FALSE(infobar->GetMessageInfoBarButtonText().empty());
// Pressing the button on that infobar should revert to the original language.
process()->sink().ClearMessages();
infobar->MessageInfoBarButtonPressed();
const IPC::Message* message =
process()->sink().GetFirstMessageMatching(ViewMsg_RevertTranslation::ID);
EXPECT_TRUE(message != NULL);
// And it should have removed the infobar.
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
}
// Tests that no translate infobar is shown when Chrome is in a language that
// the translate server does not support.
TEST_F(TranslateManagerTest, UnsupportedUILanguage) {
TestingBrowserProcess* browser_process =
static_cast<TestingBrowserProcess*>(g_browser_process);
std::string original_lang = browser_process->GetApplicationLocale();
browser_process->SetApplicationLocale("qbz");
// Simulate navigating to a page in a language supported by the translate
// server.
SimulateNavigation(GURL("http://www.google.com"), "Google", "en", true);
// No info-bar should be shown.
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
browser_process->SetApplicationLocale(original_lang);
}
// Tests that the translate enabled preference is honored.
TEST_F(TranslateManagerTest, TranslateEnabledPref) {
// Make sure the pref allows translate.
PrefService* prefs = contents()->profile()->GetPrefs();
prefs->SetBoolean(prefs::kEnableTranslate, true);
// Simulate navigating to a page and getting its language.
SimulateNavigation(GURL("http://www.google.fr"), "Le Google", "fr", true);
// An infobar should be shown.
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
EXPECT_TRUE(infobar != NULL);
// Disable translate.
prefs->SetBoolean(prefs::kEnableTranslate, false);
// Navigate to a new page, that should close the previous infobar.
GURL url("http://www.youtube.fr");
NavigateAndCommit(url);
infobar = GetTranslateInfoBar();
EXPECT_TRUE(infobar == NULL);
// Simulate getting the page contents and language, that should not trigger
// a translate infobar.
SimulateOnPageContents(url, 1, "Le YouTube", "fr", true);
infobar = GetTranslateInfoBar();
EXPECT_TRUE(infobar == NULL);
}
// Tests the "Never translate <language>" pref.
TEST_F(TranslateManagerTest, NeverTranslateLanguagePref) {
// Simulate navigating to a page and getting its language.
GURL url("http://www.google.fr");
SimulateNavigation(url, "Le Google", "fr", true);
// An infobar should be shown.
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
// Select never translate this language.
PrefService* prefs = contents()->profile()->GetPrefs();
PrefChangeRegistrar registrar;
registrar.Init(prefs);
registrar.Add(TranslatePrefs::kPrefTranslateLanguageBlacklist,
&pref_observer_);
TranslatePrefs translate_prefs(prefs);
EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr"));
EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url));
SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateLanguageBlacklist);
translate_prefs.BlacklistLanguage("fr");
EXPECT_TRUE(translate_prefs.IsLanguageBlacklisted("fr"));
EXPECT_FALSE(translate_prefs.CanTranslate(prefs, "fr", url));
// Close the infobar.
EXPECT_TRUE(CloseTranslateInfoBar());
// Navigate to a new page also in French.
SimulateNavigation(GURL("http://wwww.youtube.fr"), "Le YouTube", "fr", true);
// There should not be a translate infobar.
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
// Remove the language from the blacklist.
SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateLanguageBlacklist);
translate_prefs.RemoveLanguageFromBlacklist("fr");
EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr"));
EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url));
// Navigate to a page in French.
SimulateNavigation(url, "Le Google", "fr", true);
// There should be a translate infobar.
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
// Tests the "Never translate this site" pref.
TEST_F(TranslateManagerTest, NeverTranslateSitePref) {
// Simulate navigating to a page and getting its language.
GURL url("http://www.google.fr");
std::string host(url.host());
SimulateNavigation(url, "Le Google", "fr", true);
// An infobar should be shown.
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
// Select never translate this site.
PrefService* prefs = contents()->profile()->GetPrefs();
PrefChangeRegistrar registrar;
registrar.Init(prefs);
registrar.Add(TranslatePrefs::kPrefTranslateSiteBlacklist,
&pref_observer_);
TranslatePrefs translate_prefs(prefs);
EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host));
EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url));
SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist);
translate_prefs.BlacklistSite(host);
EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(host));
EXPECT_FALSE(translate_prefs.CanTranslate(prefs, "fr", url));
// Close the infobar.
EXPECT_TRUE(CloseTranslateInfoBar());
// Navigate to a new page also on the same site.
SimulateNavigation(GURL("http://www.google.fr/hello"), "Bonjour", "fr", true);
// There should not be a translate infobar.
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
// Remove the site from the blacklist.
SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist);
translate_prefs.RemoveSiteFromBlacklist(host);
EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host));
EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url));
// Navigate to a page in French.
SimulateNavigation(url, "Le Google", "fr", true);
// There should be a translate infobar.
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
// Tests the "Always translate this language" pref.
TEST_F(TranslateManagerTest, AlwaysTranslateLanguagePref) {
// Select always translate French to English.
PrefService* prefs = contents()->profile()->GetPrefs();
PrefChangeRegistrar registrar;
registrar.Init(prefs);
registrar.Add(TranslatePrefs::kPrefTranslateWhitelists,
&pref_observer_);
TranslatePrefs translate_prefs(prefs);
SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists);
translate_prefs.WhitelistLanguagePair("fr", "en");
// Load a page in French.
SimulateNavigation(GURL("http://www.google.fr"), "Le Google", "fr", true);
// It should have triggered an automatic translation to English.
// The translating infobar should be showing.
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->type());
SimulateURLFetch(true); // Simulate the translate script being retrieved.
int page_id = 0;
std::string original_lang, target_lang;
EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
EXPECT_EQ("fr", original_lang);
EXPECT_EQ("en", target_lang);
process()->sink().ClearMessages();
// Try another language, it should not be autotranslated.
SimulateNavigation(GURL("http://www.google.es"), "El Google", "es", true);
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
EXPECT_TRUE(CloseTranslateInfoBar());
// Let's switch to incognito mode, it should not be autotranslated in that
// case either.
TestingProfile* test_profile =
static_cast<TestingProfile*>(contents()->profile());
test_profile->set_off_the_record(true);
SimulateNavigation(GURL("http://www.youtube.fr"), "Le YouTube", "fr", true);
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
EXPECT_TRUE(CloseTranslateInfoBar());
test_profile->set_off_the_record(false); // Get back to non incognito.
// Now revert the always translate pref and make sure we go back to expected
// behavior, which is show a "before translate" infobar.
SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists);
translate_prefs.RemoveLanguagePairFromWhitelist("fr", "en");
SimulateNavigation(GURL("http://www.google.fr"), "Le Google", "fr", true);
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type());
}
// Context menu.
TEST_F(TranslateManagerTest, ContextMenu) {
// Blacklist www.google.fr and French for translation.
GURL url("http://www.google.fr");
TranslatePrefs translate_prefs(contents()->profile()->GetPrefs());
translate_prefs.BlacklistLanguage("fr");
translate_prefs.BlacklistSite(url.host());
EXPECT_TRUE(translate_prefs.IsLanguageBlacklisted("fr"));
EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(url.host()));
// Simulate navigating to a page in French. The translate menu should show but
// should only be enabled when the page language has been received.
NavigateAndCommit(url);
scoped_ptr<TestRenderViewContextMenu> menu(
TestRenderViewContextMenu::CreateContextMenu(contents()));
menu->Init();
EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
// Simulate receiving the language.
SimulateOnPageContents(url, 0, "Le Google", "fr", true);
menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents()));
menu->Init();
EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
// Use the menu to translate the page.
menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE);
// That should have triggered a translation.
// The "translating..." infobar should be showing.
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->type());
SimulateURLFetch(true); // Simulate the translate script being retrieved.
int page_id = 0;
std::string original_lang, target_lang;
EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
EXPECT_EQ("fr", original_lang);
EXPECT_EQ("en", target_lang);
process()->sink().ClearMessages();
// This should also have reverted the blacklisting of this site and language.
EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr"));
EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host()));
// Let's simulate the page being translated.
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en",
TranslateErrors::NONE));
// The translate menu should now be disabled.
menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents()));
menu->Init();
EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
// Test that selecting translate in the context menu WHILE the page is being
// translated does nothing (this could happen if autotranslate kicks-in and
// the user selects the menu while the translation is being performed).
SimulateNavigation(GURL("http://www.google.es"), "El Google", "es", true);
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
infobar->Translate();
EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
process()->sink().ClearMessages();
menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents()));
menu->Init();
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE);
// No message expected since the translation should have been ignored.
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
// Now test that selecting translate in the context menu AFTER the page has
// been translated does nothing.
SimulateNavigation(GURL("http://www.google.de"), "Das Google", "de", true);
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
infobar->Translate();
EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
process()->sink().ClearMessages();
menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents()));
menu->Init();
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "de", "en",
TranslateErrors::NONE));
menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE);
// No message expected since the translation should have been ignored.
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
// Test that the translate context menu is disabled when the page is in the
// same language as the UI.
SimulateNavigation(url, "Google", "en", true);
menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents()));
menu->Init();
EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
// Test that the translate context menu is enabled when the page is in an
// unknown language.
SimulateNavigation(url, "G00g1e", "und", true);
menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents()));
menu->Init();
EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
// Test that the translate context menu is disabled when the page is in an
// unsupported language.
SimulateNavigation(url, "G00g1e", "qbz", true);
menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents()));
menu->Init();
EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
}
// Tests that an extra always/never translate button is shown on the "before
// translate" infobar when the translation is accepted/declined 3 times,
// only when not in incognito mode.
TEST_F(TranslateManagerTest, BeforeTranslateExtraButtons) {
TranslatePrefs translate_prefs(contents()->profile()->GetPrefs());
translate_prefs.ResetTranslationAcceptedCount("fr");
translate_prefs.ResetTranslationDeniedCount("fr");
translate_prefs.ResetTranslationAcceptedCount("de");
translate_prefs.ResetTranslationDeniedCount("de");
// We'll do 4 times in incognito mode first to make sure the button is not
// shown in that case, then 4 times in normal mode.
TranslateInfoBarDelegate* infobar;
TestingProfile* test_profile =
static_cast<TestingProfile*>(contents()->profile());
test_profile->set_off_the_record(true);
for (int i = 0; i < 8; ++i) {
SCOPED_TRACE(::testing::Message::Message() << "Iteration " << i <<
" incognito mode=" << test_profile->IsOffTheRecord());
SimulateNavigation(GURL("http://www.google.fr"), "Le Google", "fr", true);
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type());
if (i < 7) {
EXPECT_FALSE(infobar->ShouldShowAlwaysTranslateButton());
infobar->Translate();
process()->sink().ClearMessages();
} else {
EXPECT_TRUE(infobar->ShouldShowAlwaysTranslateButton());
}
if (i == 3)
test_profile->set_off_the_record(false);
}
// Simulate the user pressing "Always translate French".
infobar->AlwaysTranslatePageLanguage();
EXPECT_TRUE(translate_prefs.IsLanguagePairWhitelisted("fr", "en"));
// Simulate the translate script being retrieved (it only needs to be done
// once in the test as it is cached).
SimulateURLFetch(true);
// That should have triggered a page translate.
int page_id = 0;
std::string original_lang, target_lang;
EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
process()->sink().ClearMessages();
// Now test that declining the translation causes a "never translate" button
// to be shown (in non incognito mode only).
test_profile->set_off_the_record(true);
for (int i = 0; i < 8; ++i) {
SCOPED_TRACE(::testing::Message::Message() << "Iteration " << i <<
" incognito mode=" << test_profile->IsOffTheRecord());
SimulateNavigation(GURL("http://www.google.de"), "Das Google", "de", true);
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type());
if (i < 7) {
EXPECT_FALSE(infobar->ShouldShowNeverTranslateButton());
infobar->TranslationDeclined();
} else {
EXPECT_TRUE(infobar->ShouldShowNeverTranslateButton());
}
if (i == 3)
test_profile->set_off_the_record(false);
}
// Simulate the user pressing "Never translate French".
infobar->NeverTranslatePageLanguage();
EXPECT_TRUE(translate_prefs.IsLanguageBlacklisted("de"));
// No translation should have occured and the infobar should be gone.
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
process()->sink().ClearMessages();
ASSERT_TRUE(GetTranslateInfoBar() == NULL);
}
// Tests that we don't show a translate infobar when a page instructs that it
// should not be translated.
TEST_F(TranslateManagerTest, NonTranslatablePage) {
// Simulate navigating to a page.
SimulateNavigation(GURL("http://mail.google.fr"), "Le Google", "fr", false);
// We should not have an infobar.
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
// The context menu should be disabled.
scoped_ptr<TestRenderViewContextMenu> menu(
TestRenderViewContextMenu::CreateContextMenu(contents()));
menu->Init();
EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
}
// Tests that the script is expired and refetched as expected.
TEST_F(TranslateManagerTest, ScriptExpires) {
ExpireTranslateScriptImmediately();
// Simulate navigating to a page and translating it.
SimulateNavigation(GURL("http://www.google.fr"), "Le Google", "fr", true);
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
process()->sink().ClearMessages();
infobar->Translate();
SimulateURLFetch(true);
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en",
TranslateErrors::NONE));
// A task should have been posted to clear the script, run it.
MessageLoop::current()->RunAllPending();
// Do another navigation and translation.
SimulateNavigation(GURL("http://www.google.es"), "El Google", "es", true);
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
process()->sink().ClearMessages();
infobar->Translate();
// If we don't simulate the URL fetch, the TranslateManager should be waiting
// for the script and no message should have been sent to the renderer.
EXPECT_TRUE(
process()->sink().GetFirstMessageMatching(ViewMsg_TranslatePage::ID) ==
NULL);
// Now simulate the URL fetch.
SimulateURLFetch(true);
// Now the message should have been sent.
int page_id = 0;
std::string original_lang, target_lang;
EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
EXPECT_EQ("es", original_lang);
EXPECT_EQ("en", target_lang);
}
|