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
|
// Copyright 2020 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 "build/build_config.h"
#include "components/translate/content/browser/translate_waiter.h"
#include "components/translate/core/browser/language_state.h"
#include "components/translate/core/browser/translate_error_details.h"
#include "components/translate/core/browser/translate_manager.h"
#include "components/translate/core/common/translate_switches.h"
#include "content/public/browser/browser_context.h"
#include "net/base/mock_network_change_notifier.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "weblayer/browser/browser_context_impl.h"
#include "weblayer/browser/profile_impl.h"
#include "weblayer/browser/tab_impl.h"
#include "weblayer/browser/translate_client_impl.h"
#include "weblayer/public/tab.h"
#include "weblayer/shell/browser/shell.h"
#include "weblayer/test/weblayer_browser_test.h"
#include "weblayer/test/weblayer_browser_test_utils.h"
#if defined(OS_ANDROID)
#include "base/android/build_info.h"
#include "components/infobars/core/infobar_manager.h" // nogncheck
#include "components/translate/core/browser/translate_download_manager.h"
#include "weblayer/browser/infobar_android.h"
#include "weblayer/browser/infobar_service.h"
#include "weblayer/browser/translate_compact_infobar.h"
#endif
namespace weblayer {
namespace {
static std::string kTestValidScript = R"(
var google = {};
google.translate = (function() {
return {
TranslateService: function() {
return {
isAvailable : function() {
return true;
},
restore : function() {
return;
},
getDetectedLanguage : function() {
return "fr";
},
translatePage : function(originalLang, targetLang,
onTranslateProgress) {
var error = (originalLang == 'auto') ? true : false;
onTranslateProgress(100, true, error);
}
};
}
};
})();
cr.googleTranslate.onTranslateElementLoad();)";
static std::string kTestScriptInitializationError = R"(
var google = {};
google.translate = (function() {
return {
TranslateService: function() {
return error;
}
};
})();
cr.googleTranslate.onTranslateElementLoad();)";
static std::string kTestScriptTimeout = R"(
var google = {};
google.translate = (function() {
return {
TranslateService: function() {
return {
isAvailable : function() {
return false;
},
};
}
};
})();
cr.googleTranslate.onTranslateElementLoad();)";
TranslateClientImpl* GetTranslateClient(Shell* shell) {
return TranslateClientImpl::FromWebContents(
static_cast<TabImpl*>(shell->tab())->web_contents());
}
std::unique_ptr<translate::TranslateWaiter> CreateTranslateWaiter(
Shell* shell,
translate::TranslateWaiter::WaitEvent wait_event) {
return std::make_unique<translate::TranslateWaiter>(
GetTranslateClient(shell)->translate_driver(), wait_event);
}
void WaitUntilLanguageDetermined(Shell* shell) {
CreateTranslateWaiter(
shell, translate::TranslateWaiter::WaitEvent::kLanguageDetermined)
->Wait();
}
void WaitUntilPageTranslated(Shell* shell) {
CreateTranslateWaiter(shell,
translate::TranslateWaiter::WaitEvent::kPageTranslated)
->Wait();
}
} // namespace
#if defined(OS_ANDROID)
class TestInfoBarManagerObserver : public infobars::InfoBarManager::Observer {
public:
TestInfoBarManagerObserver() = default;
~TestInfoBarManagerObserver() override = default;
void OnInfoBarAdded(infobars::InfoBar* infobar) override {
if (on_infobar_added_callback_)
std::move(on_infobar_added_callback_).Run();
}
void OnInfoBarRemoved(infobars::InfoBar* infobar, bool animate) override {
if (on_infobar_removed_callback_)
std::move(on_infobar_removed_callback_).Run();
}
void set_on_infobar_added_callback(base::OnceClosure callback) {
on_infobar_added_callback_ = std::move(callback);
}
void set_on_infobar_removed_callback(base::OnceClosure callback) {
on_infobar_removed_callback_ = std::move(callback);
}
private:
base::OnceClosure on_infobar_added_callback_;
base::OnceClosure on_infobar_removed_callback_;
};
#endif // if defined(OS_ANDROID)
class TranslateBrowserTest : public WebLayerBrowserTest {
public:
TranslateBrowserTest() {
error_subscription_ =
translate::TranslateManager::RegisterTranslateErrorCallback(
base::BindRepeating(&TranslateBrowserTest::OnTranslateError,
base::Unretained(this)));
}
~TranslateBrowserTest() override = default;
void SetUpOnMainThread() override {
embedded_test_server()->RegisterRequestHandler(base::BindRepeating(
&TranslateBrowserTest::HandleRequest, base::Unretained(this)));
embedded_test_server()->StartAcceptingConnections();
// Translation will not be offered if NetworkChangeNotifier reports that the
// app is offline, which can occur on bots. Prevent this.
// NOTE: MockNetworkChangeNotifier cannot be instantiated earlier than this
// due to its dependence on browser state having been created.
mock_network_change_notifier_ =
std::make_unique<net::test::ScopedMockNetworkChangeNotifier>();
mock_network_change_notifier_->mock_network_change_notifier()
->SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI);
// By default, translation is not offered if the Google API key is not set.
GetTranslateClient(shell())
->GetTranslateManager()
->SetIgnoreMissingKeyForTesting(true);
GetTranslateClient(shell())->GetTranslatePrefs()->ResetToDefaults();
}
void TearDownOnMainThread() override {
mock_network_change_notifier_.reset();
}
void SetUpCommandLine(base::CommandLine* command_line) override {
ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
command_line->AppendSwitchASCII(
translate::switches::kTranslateScriptURL,
embedded_test_server()->GetURL("/mock_translate_script.js").spec());
}
protected:
translate::TranslateErrors::Type GetPageTranslatedResult() {
return error_type_;
}
void SetTranslateScript(const std::string& script) { script_ = script; }
private:
std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
const net::test_server::HttpRequest& request) {
if (request.GetURL().path() != "/mock_translate_script.js")
return nullptr;
auto http_response =
std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_code(net::HTTP_OK);
http_response->set_content(script_);
http_response->set_content_type("text/javascript");
return std::move(http_response);
}
void OnTranslateError(const translate::TranslateErrorDetails& details) {
error_type_ = details.error;
}
std::unique_ptr<net::test::ScopedMockNetworkChangeNotifier>
mock_network_change_notifier_;
translate::TranslateErrors::Type error_type_ =
translate::TranslateErrors::NONE;
std::unique_ptr<
translate::TranslateManager::TranslateErrorCallbackList::Subscription>
error_subscription_;
std::string script_;
};
// Tests that the CLD (Compact Language Detection) works properly.
IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, PageLanguageDetection) {
TranslateClientImpl* translate_client = GetTranslateClient(shell());
NavigateAndWaitForCompletion(GURL("about:blank"), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("und", translate_client->GetLanguageState().original_language());
// Go to a page in English.
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/english_page.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("en", translate_client->GetLanguageState().original_language());
// Now navigate to a page in French.
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/french_page.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("fr", translate_client->GetLanguageState().original_language());
}
// Test that the translation was successful.
IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, PageTranslationSuccess) {
SetTranslateScript(kTestValidScript);
TranslateClientImpl* translate_client = GetTranslateClient(shell());
NavigateAndWaitForCompletion(GURL("about:blank"), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("und", translate_client->GetLanguageState().original_language());
// Navigate to a page in French.
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/french_page.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("fr", translate_client->GetLanguageState().original_language());
// Translate the page through TranslateManager.
translate::TranslateManager* manager =
translate_client->GetTranslateManager();
manager->TranslatePage(
translate_client->GetLanguageState().original_language(), "en", true);
WaitUntilPageTranslated(shell());
EXPECT_FALSE(translate_client->GetLanguageState().translation_error());
EXPECT_EQ(translate::TranslateErrors::NONE, GetPageTranslatedResult());
}
class IncognitoTranslateBrowserTest : public TranslateBrowserTest {
public:
IncognitoTranslateBrowserTest() { SetShellStartsInIncognitoMode(); }
};
// Test that the translation infrastructure is set up properly when the user is
// in incognito mode.
IN_PROC_BROWSER_TEST_F(IncognitoTranslateBrowserTest,
PageTranslationSuccess_IncognitoMode) {
ASSERT_TRUE(GetProfile()->GetBrowserContext()->IsOffTheRecord());
SetTranslateScript(kTestValidScript);
TranslateClientImpl* translate_client = GetTranslateClient(shell());
NavigateAndWaitForCompletion(GURL("about:blank"), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("und", translate_client->GetLanguageState().original_language());
// Navigate to a page in French.
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/french_page.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("fr", translate_client->GetLanguageState().original_language());
// Translate the page through TranslateManager.
translate::TranslateManager* manager =
translate_client->GetTranslateManager();
manager->TranslatePage(
translate_client->GetLanguageState().original_language(), "en", true);
WaitUntilPageTranslated(shell());
EXPECT_FALSE(translate_client->GetLanguageState().translation_error());
EXPECT_EQ(translate::TranslateErrors::NONE, GetPageTranslatedResult());
}
// Test if there was an error during translation.
IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, PageTranslationError) {
#if defined(OS_ANDROID)
// TODO(crbug.com/1094903): Determine why this test times out on the M
// trybot.
if (base::android::BuildInfo::GetInstance()->sdk_int() <=
base::android::SDK_VERSION_MARSHMALLOW) {
return;
}
#endif
SetTranslateScript(kTestValidScript);
TranslateClientImpl* translate_client = GetTranslateClient(shell());
NavigateAndWaitForCompletion(GURL("about:blank"), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("und", translate_client->GetLanguageState().original_language());
// Translate the page through TranslateManager.
translate::TranslateManager* manager =
translate_client->GetTranslateManager();
manager->TranslatePage(
translate_client->GetLanguageState().original_language(), "en", true);
WaitUntilPageTranslated(shell());
EXPECT_TRUE(translate_client->GetLanguageState().translation_error());
EXPECT_EQ(translate::TranslateErrors::TRANSLATION_ERROR,
GetPageTranslatedResult());
}
// Test if there was an error during translate library initialization.
IN_PROC_BROWSER_TEST_F(TranslateBrowserTest,
PageTranslationInitializationError) {
SetTranslateScript(kTestScriptInitializationError);
TranslateClientImpl* translate_client = GetTranslateClient(shell());
NavigateAndWaitForCompletion(GURL("about:blank"), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("und", translate_client->GetLanguageState().original_language());
// Navigate to a page in French.
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/french_page.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("fr", translate_client->GetLanguageState().original_language());
// Translate the page through TranslateManager.
translate::TranslateManager* manager =
translate_client->GetTranslateManager();
manager->TranslatePage(
translate_client->GetLanguageState().original_language(), "en", true);
WaitUntilPageTranslated(shell());
EXPECT_TRUE(translate_client->GetLanguageState().translation_error());
EXPECT_EQ(translate::TranslateErrors::INITIALIZATION_ERROR,
GetPageTranslatedResult());
}
// Test the checks translate lib never gets ready and throws timeout.
IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, PageTranslationTimeoutError) {
SetTranslateScript(kTestScriptTimeout);
TranslateClientImpl* translate_client = GetTranslateClient(shell());
NavigateAndWaitForCompletion(GURL("about:blank"), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("und", translate_client->GetLanguageState().original_language());
// Navigate to a page in French.
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/french_page.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("fr", translate_client->GetLanguageState().original_language());
// Translate the page through TranslateManager.
translate::TranslateManager* manager =
translate_client->GetTranslateManager();
manager->TranslatePage(
translate_client->GetLanguageState().original_language(), "en", true);
WaitUntilPageTranslated(shell());
EXPECT_TRUE(translate_client->GetLanguageState().translation_error());
EXPECT_EQ(translate::TranslateErrors::TRANSLATION_TIMEOUT,
GetPageTranslatedResult());
}
// Test that autotranslation kicks in if configured via prefs.
IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, Autotranslation) {
#if defined(OS_ANDROID)
// TODO(crbug.com/1094903): Determine why this test times out on the M
// trybot.
if (base::android::BuildInfo::GetInstance()->sdk_int() <=
base::android::SDK_VERSION_MARSHMALLOW) {
return;
}
#endif
SetTranslateScript(kTestValidScript);
TranslateClientImpl* translate_client = GetTranslateClient(shell());
NavigateAndWaitForCompletion(GURL("about:blank"), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("und", translate_client->GetLanguageState().original_language());
// Before browsing, set autotranslate from French to Chinese.
translate_client->GetTranslatePrefs()->WhitelistLanguagePair("fr", "zh-CN");
// Navigate to a page in French.
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/french_page.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("fr", translate_client->GetLanguageState().original_language());
// Autotranslation should kick in.
WaitUntilPageTranslated(shell());
EXPECT_FALSE(translate_client->GetLanguageState().translation_error());
EXPECT_EQ(translate::TranslateErrors::NONE, GetPageTranslatedResult());
EXPECT_EQ("zh-CN", translate_client->GetLanguageState().current_language());
}
#if defined(OS_ANDROID)
// Test that the translation infobar is presented when visiting a page with a
// translation opportunity and removed when navigating away.
IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, TranslateInfoBarPresentation) {
auto* web_contents = static_cast<TabImpl*>(shell()->tab())->web_contents();
auto* infobar_service = InfoBarService::FromWebContents(web_contents);
SetTranslateScript(kTestValidScript);
TranslateClientImpl* translate_client = GetTranslateClient(shell());
NavigateAndWaitForCompletion(GURL("about:blank"), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("und", translate_client->GetLanguageState().original_language());
TestInfoBarManagerObserver infobar_observer;
infobar_service->AddObserver(&infobar_observer);
base::RunLoop run_loop;
infobar_observer.set_on_infobar_added_callback(run_loop.QuitClosure());
EXPECT_EQ(0u, infobar_service->infobar_count());
// Navigate to a page in French.
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/french_page.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("fr", translate_client->GetLanguageState().original_language());
// The translate infobar should be added.
run_loop.Run();
EXPECT_EQ(1u, infobar_service->infobar_count());
auto* infobar = static_cast<InfoBarAndroid*>(infobar_service->infobar_at(0));
EXPECT_TRUE(infobar->HasSetJavaInfoBar());
base::RunLoop run_loop2;
infobar_observer.set_on_infobar_removed_callback(run_loop2.QuitClosure());
NavigateAndWaitForCompletion(GURL("about:blank"), shell());
// The translate infobar should be removed.
run_loop2.Run();
EXPECT_EQ(0u, infobar_service->infobar_count());
infobar_service->RemoveObserver(&infobar_observer);
}
#endif
#if defined(OS_ANDROID)
// Test that the translation can be successfully initiated via infobar.
IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, TranslationViaInfoBar) {
// TODO(crbug.com/1094903): Determine why this test times out on the M
// trybot.
if (base::android::BuildInfo::GetInstance()->sdk_int() <=
base::android::SDK_VERSION_MARSHMALLOW) {
return;
}
auto* web_contents = static_cast<TabImpl*>(shell()->tab())->web_contents();
auto* infobar_service = InfoBarService::FromWebContents(web_contents);
SetTranslateScript(kTestValidScript);
TranslateClientImpl* translate_client = GetTranslateClient(shell());
NavigateAndWaitForCompletion(GURL("about:blank"), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("und", translate_client->GetLanguageState().original_language());
TestInfoBarManagerObserver infobar_observer;
infobar_service->AddObserver(&infobar_observer);
base::RunLoop run_loop;
infobar_observer.set_on_infobar_added_callback(run_loop.QuitClosure());
// Navigate to a page in French and wait for the infobar to be added.
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/french_page.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("fr", translate_client->GetLanguageState().original_language());
run_loop.Run();
// Select the target language via the Java infobar and ensure that translation
// occurs.
auto* infobar =
static_cast<TranslateCompactInfoBar*>(infobar_service->infobar_at(0));
infobar->SelectButtonForTesting(InfoBarAndroid::ActionType::ACTION_TRANSLATE);
WaitUntilPageTranslated(shell());
EXPECT_FALSE(translate_client->GetLanguageState().translation_error());
EXPECT_EQ(translate::TranslateErrors::NONE, GetPageTranslatedResult());
// The translate infobar should still be present.
EXPECT_EQ(1u, infobar_service->infobar_count());
// NOTE: The notification that the translate state of the page changed can
// occur synchronously once reversion is initiated, so it's necessary to start
// listening for that notification prior to initiating the reversion.
auto translate_reversion_waiter = CreateTranslateWaiter(
shell(), translate::TranslateWaiter::WaitEvent::kIsPageTranslatedChanged);
// Revert to the source language via the Java infobar and ensure that the
// translation is undone.
infobar->SelectButtonForTesting(
InfoBarAndroid::ActionType::ACTION_TRANSLATE_SHOW_ORIGINAL);
translate_reversion_waiter->Wait();
EXPECT_EQ("fr", translate_client->GetLanguageState().current_language());
// The translate infobar should still be present.
EXPECT_EQ(1u, infobar_service->infobar_count());
infobar_service->RemoveObserver(&infobar_observer);
}
#endif
#if defined(OS_ANDROID)
// Test that the translation infobar stays present when the "never translate
// language" item is clicked. Note that this behavior is intentionally different
// from that of Chrome, where the infobar is removed in this case and a snackbar
// is shown. As WebLayer has no snackbars, the UX decision was to simply leave
// the infobar open to allow the user to revert the decision if desired.
IN_PROC_BROWSER_TEST_F(TranslateBrowserTest,
TranslateInfoBarNeverTranslateLanguage) {
auto* web_contents = static_cast<TabImpl*>(shell()->tab())->web_contents();
auto* infobar_service = InfoBarService::FromWebContents(web_contents);
SetTranslateScript(kTestValidScript);
TranslateClientImpl* translate_client = GetTranslateClient(shell());
NavigateAndWaitForCompletion(GURL("about:blank"), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("und", translate_client->GetLanguageState().original_language());
TestInfoBarManagerObserver infobar_observer;
infobar_service->AddObserver(&infobar_observer);
base::RunLoop run_loop;
infobar_observer.set_on_infobar_added_callback(run_loop.QuitClosure());
// Navigate to a page in French and wait for the infobar to be added.
EXPECT_EQ(0u, infobar_service->infobar_count());
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/french_page.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("fr", translate_client->GetLanguageState().original_language());
run_loop.Run();
auto* infobar =
static_cast<TranslateCompactInfoBar*>(infobar_service->infobar_at(0));
infobar->ClickOverflowMenuItemForTesting(
TranslateCompactInfoBar::OverflowMenuItemId::NEVER_TRANSLATE_LANGUAGE);
// The translate infobar should still be present.
EXPECT_EQ(1u, infobar_service->infobar_count());
// However, the infobar should not be shown on a new navigation to a page in
// French.
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/french_page2.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("fr", translate_client->GetLanguageState().original_language());
// NOTE: There is no notification to wait for for the event of the infobar not
// showing. However, in practice the infobar is added synchronously, so if it
// were to be shown, this check would fail.
EXPECT_EQ(0u, infobar_service->infobar_count());
// The infobar *should* be shown on a navigation to this site if the page's
// language is detected as something other than French.
base::RunLoop run_loop2;
infobar_observer.set_on_infobar_added_callback(run_loop2.QuitClosure());
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/german_page.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("de", translate_client->GetLanguageState().original_language());
run_loop2.Run();
EXPECT_EQ(1u, infobar_service->infobar_count());
infobar_service->RemoveObserver(&infobar_observer);
}
// Test that the translation infobar stays present when the "never translate
// site" item is clicked. Note that this behavior is intentionally different
// from that of Chrome, where the infobar is removed in this case and a snackbar
// is shown. As WebLayer has no snackbars, the UX decision was to simply leave
// the infobar open to allow the user to revert the decision if desired.
IN_PROC_BROWSER_TEST_F(TranslateBrowserTest,
TranslateInfoBarNeverTranslateSite) {
auto* web_contents = static_cast<TabImpl*>(shell()->tab())->web_contents();
auto* infobar_service = InfoBarService::FromWebContents(web_contents);
SetTranslateScript(kTestValidScript);
TranslateClientImpl* translate_client = GetTranslateClient(shell());
NavigateAndWaitForCompletion(GURL("about:blank"), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("und", translate_client->GetLanguageState().original_language());
TestInfoBarManagerObserver infobar_observer;
infobar_service->AddObserver(&infobar_observer);
base::RunLoop run_loop;
infobar_observer.set_on_infobar_added_callback(run_loop.QuitClosure());
// Navigate to a page in French and wait for the infobar to be added.
EXPECT_EQ(0u, infobar_service->infobar_count());
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/french_page.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("fr", translate_client->GetLanguageState().original_language());
run_loop.Run();
auto* infobar =
static_cast<TranslateCompactInfoBar*>(infobar_service->infobar_at(0));
infobar->ClickOverflowMenuItemForTesting(
TranslateCompactInfoBar::OverflowMenuItemId::NEVER_TRANSLATE_SITE);
// The translate infobar should still be present.
EXPECT_EQ(1u, infobar_service->infobar_count());
// However, the infobar should not be shown on a new navigation to this site,
// independent of the detected language.
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/french_page2.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("fr", translate_client->GetLanguageState().original_language());
// NOTE: There is no notification to wait for for the event of the infobar not
// showing. However, in practice the infobar is added synchronously, so if it
// were to be shown, this check would fail.
EXPECT_EQ(0u, infobar_service->infobar_count());
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/german_page.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("de", translate_client->GetLanguageState().original_language());
EXPECT_EQ(0u, infobar_service->infobar_count());
infobar_service->RemoveObserver(&infobar_observer);
}
// Parameterized to run tests on the "never translate language" and "never
// translate site" menu items.
class NeverTranslateMenuItemTranslateBrowserTest
: public TranslateBrowserTest,
public testing::WithParamInterface<
TranslateCompactInfoBar::OverflowMenuItemId> {};
// Test that clicking and unclicking a never translate item ends up being a
// no-op.
IN_PROC_BROWSER_TEST_P(NeverTranslateMenuItemTranslateBrowserTest,
TranslateInfoBarToggleAndToggleBackNeverTranslateItem) {
auto* web_contents = static_cast<TabImpl*>(shell()->tab())->web_contents();
auto* infobar_service = InfoBarService::FromWebContents(web_contents);
SetTranslateScript(kTestValidScript);
TranslateClientImpl* translate_client = GetTranslateClient(shell());
NavigateAndWaitForCompletion(GURL("about:blank"), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("und", translate_client->GetLanguageState().original_language());
TestInfoBarManagerObserver infobar_observer;
infobar_service->AddObserver(&infobar_observer);
// Navigate to a page in French, wait for the infobar to be added, and click
// twice on the given overflow menu item.
{
base::RunLoop run_loop;
infobar_observer.set_on_infobar_added_callback(run_loop.QuitClosure());
EXPECT_EQ(0u, infobar_service->infobar_count());
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/french_page.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("fr", translate_client->GetLanguageState().original_language());
run_loop.Run();
auto* infobar =
static_cast<TranslateCompactInfoBar*>(infobar_service->infobar_at(0));
infobar->ClickOverflowMenuItemForTesting(GetParam());
// The translate infobar should still be present.
EXPECT_EQ(1u, infobar_service->infobar_count());
infobar->ClickOverflowMenuItemForTesting(GetParam());
}
// The infobar should be shown on a new navigation to a page in the same
// language.
{
base::RunLoop run_loop;
infobar_observer.set_on_infobar_added_callback(run_loop.QuitClosure());
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/french_page2.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("fr", translate_client->GetLanguageState().original_language());
run_loop.Run();
}
// The infobar should be shown on a new navigation to a page in a different
// language in the same site.
{
base::RunLoop run_loop;
infobar_observer.set_on_infobar_added_callback(run_loop.QuitClosure());
NavigateAndWaitForCompletion(
GURL(embedded_test_server()->GetURL("/german_page.html")), shell());
WaitUntilLanguageDetermined(shell());
EXPECT_EQ("de", translate_client->GetLanguageState().original_language());
run_loop.Run();
}
infobar_service->RemoveObserver(&infobar_observer);
}
INSTANTIATE_TEST_SUITE_P(
All,
NeverTranslateMenuItemTranslateBrowserTest,
::testing::Values(
TranslateCompactInfoBar::OverflowMenuItemId::NEVER_TRANSLATE_LANGUAGE,
TranslateCompactInfoBar::OverflowMenuItemId::NEVER_TRANSLATE_SITE));
#endif // #if defined(OS_ANDROID)
} // namespace weblayer
|