| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import <XCTest/XCTest.h> | 5 #import <XCTest/XCTest.h> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/mac/bind_objc_block.h" | 8 #include "base/mac/bind_objc_block.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| (...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 // Start the HTTP server. | 495 // Start the HTTP server. |
| 496 std::unique_ptr<web::DataResponseProvider> provider(new TestResponseProvider); | 496 std::unique_ptr<web::DataResponseProvider> provider(new TestResponseProvider); |
| 497 web::test::SetUpHttpServer(std::move(provider)); | 497 web::test::SetUpHttpServer(std::move(provider)); |
| 498 LanguageDetectionController::DetectionDetails expectedLanguageDetails; | 498 LanguageDetectionController::DetectionDetails expectedLanguageDetails; |
| 499 | 499 |
| 500 // Detection works when clicking on a link. | 500 // Detection works when clicking on a link. |
| 501 GURL URL = web::test::HttpServer::MakeUrl(std::string("http://") + kLinkPath); | 501 GURL URL = web::test::HttpServer::MakeUrl(std::string("http://") + kLinkPath); |
| 502 GURL someLanguageURL = web::test::HttpServer::MakeUrl(kSomeLanguageUrl); | 502 GURL someLanguageURL = web::test::HttpServer::MakeUrl(kSomeLanguageUrl); |
| 503 [ChromeEarlGrey loadURL:URL]; | 503 [ChromeEarlGrey loadURL:URL]; |
| 504 chrome_test_util::TapWebViewElementWithId("click"); | 504 chrome_test_util::TapWebViewElementWithId("click"); |
| 505 [[EarlGrey selectElementWithMatcher:chrome_test_util::omniboxText( | 505 [[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText( |
| 506 someLanguageURL.GetContent())] | 506 someLanguageURL.GetContent())] |
| 507 assertWithMatcher:grey_notNil()]; | 507 assertWithMatcher:grey_notNil()]; |
| 508 expectedLanguageDetails.content_language = "es"; | 508 expectedLanguageDetails.content_language = "es"; |
| 509 expectedLanguageDetails.adopted_language = "es"; | 509 expectedLanguageDetails.adopted_language = "es"; |
| 510 [self assertLanguageDetails:expectedLanguageDetails]; | 510 [self assertLanguageDetails:expectedLanguageDetails]; |
| 511 } | 511 } |
| 512 | 512 |
| 513 // Tests that language detection still happens when a very large quantity of | 513 // Tests that language detection still happens when a very large quantity of |
| 514 // text is present on the page. | 514 // text is present on the page. |
| 515 - (void)testLanguageDetectionLargePage { | 515 - (void)testLanguageDetectionLargePage { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 571 const GURL URL = | 571 const GURL URL = |
| 572 web::test::HttpServer::MakeUrl("http://scenarioLanguageDetectionInfobar"); | 572 web::test::HttpServer::MakeUrl("http://scenarioLanguageDetectionInfobar"); |
| 573 std::map<GURL, std::string> responses; | 573 std::map<GURL, std::string> responses; |
| 574 // A page with French text. | 574 // A page with French text. |
| 575 responses[URL] = GetFrenchPageHtml(kHtmlAttribute, ""); | 575 responses[URL] = GetFrenchPageHtml(kHtmlAttribute, ""); |
| 576 web::test::SetUpSimpleHttpServer(responses); | 576 web::test::SetUpSimpleHttpServer(responses); |
| 577 [ChromeEarlGrey loadURL:URL]; | 577 [ChromeEarlGrey loadURL:URL]; |
| 578 | 578 |
| 579 // Check that the "Before Translate" infobar is displayed. | 579 // Check that the "Before Translate" infobar is displayed. |
| 580 [[EarlGrey | 580 [[EarlGrey |
| 581 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabel( | 581 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabel( |
| 582 @"English")] | 582 @"English")] |
| 583 assertWithMatcher:grey_notNil()]; | 583 assertWithMatcher:grey_notNil()]; |
| 584 [[EarlGrey | 584 [[EarlGrey |
| 585 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabelId( | 585 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabelId( |
| 586 IDS_TRANSLATE_INFOBAR_ACCEPT)] | 586 IDS_TRANSLATE_INFOBAR_ACCEPT)] |
| 587 assertWithMatcher:grey_notNil()]; | 587 assertWithMatcher:grey_notNil()]; |
| 588 [[EarlGrey | 588 [[EarlGrey |
| 589 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabelId( | 589 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabelId( |
| 590 IDS_CLOSE)] assertWithMatcher:grey_notNil()]; | 590 IDS_CLOSE)] assertWithMatcher:grey_notNil()]; |
| 591 | 591 |
| 592 // Open the language picker. | 592 // Open the language picker. |
| 593 NSString* kFrench = @"French"; | 593 NSString* kFrench = @"French"; |
| 594 [[EarlGrey | 594 [[EarlGrey |
| 595 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabel( | 595 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabel( |
| 596 kFrench)] performAction:grey_tap()]; | 596 kFrench)] performAction:grey_tap()]; |
| 597 | 597 |
| 598 // The language picker uses the system accessibility labels (thus no | 598 // The language picker uses the system accessibility labels (thus no |
| 599 // IDS_CANCEL here). | 599 // IDS_CANCEL here). |
| 600 [[EarlGrey | 600 [[EarlGrey |
| 601 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabel( | 601 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabel( |
| 602 @"Cancel")] assertWithMatcher:grey_notNil()]; | 602 @"Cancel")] assertWithMatcher:grey_notNil()]; |
| 603 | 603 |
| 604 // Change the language using the picker. | 604 // Change the language using the picker. |
| 605 NSString* const kPickedLanguage = @"Finnish"; | 605 NSString* const kPickedLanguage = @"Finnish"; |
| 606 id<GREYMatcher> languageMatcher = grey_allOf( | 606 id<GREYMatcher> languageMatcher = grey_allOf( |
| 607 chrome_test_util::staticTextWithAccessibilityLabel(kPickedLanguage), | 607 chrome_test_util::StaticTextWithAccessibilityLabel(kPickedLanguage), |
| 608 grey_sufficientlyVisible(), nil); | 608 grey_sufficientlyVisible(), nil); |
| 609 [[EarlGrey selectElementWithMatcher:languageMatcher] | 609 [[EarlGrey selectElementWithMatcher:languageMatcher] |
| 610 performAction:grey_tap()]; | 610 performAction:grey_tap()]; |
| 611 [[EarlGrey | 611 [[EarlGrey |
| 612 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabel( | 612 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabel( |
| 613 @"Done")] performAction:grey_tap()]; | 613 @"Done")] performAction:grey_tap()]; |
| 614 [[EarlGrey | 614 [[EarlGrey |
| 615 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabel( | 615 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabel( |
| 616 kPickedLanguage)] | 616 kPickedLanguage)] |
| 617 assertWithMatcher:grey_notNil()]; | 617 assertWithMatcher:grey_notNil()]; |
| 618 [[EarlGrey | 618 [[EarlGrey |
| 619 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabel( | 619 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabel( |
| 620 kFrench)] assertWithMatcher:grey_nil()]; | 620 kFrench)] assertWithMatcher:grey_nil()]; |
| 621 | 621 |
| 622 // Deny the translation, and check that the infobar is dismissed. | 622 // Deny the translation, and check that the infobar is dismissed. |
| 623 [[EarlGrey | 623 [[EarlGrey |
| 624 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabelId( | 624 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabelId( |
| 625 IDS_TRANSLATE_INFOBAR_DENY)] | 625 IDS_TRANSLATE_INFOBAR_DENY)] |
| 626 performAction:grey_tap()]; | 626 performAction:grey_tap()]; |
| 627 [[EarlGrey | 627 [[EarlGrey |
| 628 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabel( | 628 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabel( |
| 629 kPickedLanguage)] | 629 kPickedLanguage)] |
| 630 assertWithMatcher:grey_nil()]; | 630 assertWithMatcher:grey_nil()]; |
| 631 } | 631 } |
| 632 | 632 |
| 633 // Tests that the Translate infobar is displayed after translation. | 633 // Tests that the Translate infobar is displayed after translation. |
| 634 - (void)testTranslateInfobar { | 634 - (void)testTranslateInfobar { |
| 635 const GURL URL = | 635 const GURL URL = |
| 636 web::test::HttpServer::MakeUrl("http://scenarioTranslateInfobar"); | 636 web::test::HttpServer::MakeUrl("http://scenarioTranslateInfobar"); |
| 637 std::map<GURL, std::string> responses; | 637 std::map<GURL, std::string> responses; |
| 638 // A page with some text. | 638 // A page with some text. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 653 translatePrefs->IncrementTranslationAcceptedCount("es"); | 653 translatePrefs->IncrementTranslationAcceptedCount("es"); |
| 654 } | 654 } |
| 655 | 655 |
| 656 // Open a new webpage. | 656 // Open a new webpage. |
| 657 [ChromeEarlGrey loadURL:URL]; | 657 [ChromeEarlGrey loadURL:URL]; |
| 658 [self simulateTranslationFromSpanishToEnglish]; | 658 [self simulateTranslationFromSpanishToEnglish]; |
| 659 | 659 |
| 660 // Check that the "Always Translate" switch is displayed in the infobar. | 660 // Check that the "Always Translate" switch is displayed in the infobar. |
| 661 NSString* switchLabel = GetTranslateInfobarSwitchLabel("Spanish"); | 661 NSString* switchLabel = GetTranslateInfobarSwitchLabel("Spanish"); |
| 662 [[EarlGrey | 662 [[EarlGrey |
| 663 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabel( | 663 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabel( |
| 664 switchLabel)] | 664 switchLabel)] |
| 665 assertWithMatcher:grey_notNil()]; | 665 assertWithMatcher:grey_notNil()]; |
| 666 | 666 |
| 667 // Toggle "Always Translate" and check the preference. | 667 // Toggle "Always Translate" and check the preference. |
| 668 [[EarlGrey | 668 [[EarlGrey |
| 669 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabel( | 669 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabel( |
| 670 switchLabel)] performAction:grey_tap()]; | 670 switchLabel)] performAction:grey_tap()]; |
| 671 id<GREYMatcher> switchOn = | 671 id<GREYMatcher> switchOn = |
| 672 grey_allOf(chrome_test_util::buttonWithAccessibilityLabel(switchLabel), | 672 grey_allOf(chrome_test_util::ButtonWithAccessibilityLabel(switchLabel), |
| 673 grey_accessibilityValue(@"1"), nil); | 673 grey_accessibilityValue(@"1"), nil); |
| 674 [[EarlGrey selectElementWithMatcher:switchOn] | 674 [[EarlGrey selectElementWithMatcher:switchOn] |
| 675 assertWithMatcher:grey_notNil()]; | 675 assertWithMatcher:grey_notNil()]; |
| 676 | 676 |
| 677 // Assert that Spanish to English translation is enabled. | 677 // Assert that Spanish to English translation is enabled. |
| 678 GREYAssert(translatePrefs->IsLanguagePairWhitelisted("es", "en"), | 678 GREYAssert(translatePrefs->IsLanguagePairWhitelisted("es", "en"), |
| 679 @"Translate Spanish is disabled"); | 679 @"Translate Spanish is disabled"); |
| 680 } | 680 } |
| 681 | 681 |
| 682 // Tests that the "Always Translate" switch is not shown in incognito mode. | 682 // Tests that the "Always Translate" switch is not shown in incognito mode. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 698 translatePrefs->IncrementTranslationAcceptedCount("es"); | 698 translatePrefs->IncrementTranslationAcceptedCount("es"); |
| 699 } | 699 } |
| 700 | 700 |
| 701 // Do a translation in incognito | 701 // Do a translation in incognito |
| 702 chrome_test_util::OpenNewIncognitoTab(); | 702 chrome_test_util::OpenNewIncognitoTab(); |
| 703 [ChromeEarlGrey loadURL:URL]; | 703 [ChromeEarlGrey loadURL:URL]; |
| 704 [self simulateTranslationFromSpanishToEnglish]; | 704 [self simulateTranslationFromSpanishToEnglish]; |
| 705 // Check that the infobar does not contain the "Always Translate" switch. | 705 // Check that the infobar does not contain the "Always Translate" switch. |
| 706 NSString* switchLabel = GetTranslateInfobarSwitchLabel("Spanish"); | 706 NSString* switchLabel = GetTranslateInfobarSwitchLabel("Spanish"); |
| 707 [[EarlGrey | 707 [[EarlGrey |
| 708 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabel( | 708 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabel( |
| 709 switchLabel)] assertWithMatcher:grey_nil()]; | 709 switchLabel)] assertWithMatcher:grey_nil()]; |
| 710 } | 710 } |
| 711 | 711 |
| 712 // Tests that translation occurs automatically on second navigation to an | 712 // Tests that translation occurs automatically on second navigation to an |
| 713 // already translated page. | 713 // already translated page. |
| 714 - (void)testAutoTranslate { | 714 - (void)testAutoTranslate { |
| 715 // The translate machinery will not auto-fire without API keys, unless that | 715 // The translate machinery will not auto-fire without API keys, unless that |
| 716 // behavior is overridden for testing. | 716 // behavior is overridden for testing. |
| 717 translate::TranslateManager::SetIgnoreMissingKeyForTesting(true); | 717 translate::TranslateManager::SetIgnoreMissingKeyForTesting(true); |
| 718 | 718 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 741 GURL translateScriptURL = | 741 GURL translateScriptURL = |
| 742 web::test::HttpServer::MakeUrl("http://translatescript"); | 742 web::test::HttpServer::MakeUrl("http://translatescript"); |
| 743 command_line.AppendSwitchASCII(translate::switches::kTranslateScriptURL, | 743 command_line.AppendSwitchASCII(translate::switches::kTranslateScriptURL, |
| 744 translateScriptURL.spec().c_str()); | 744 translateScriptURL.spec().c_str()); |
| 745 | 745 |
| 746 // Translate the page with the link. | 746 // Translate the page with the link. |
| 747 GURL frenchPageURL = | 747 GURL frenchPageURL = |
| 748 web::test::HttpServer::MakeUrl("http://frenchpagewithlink"); | 748 web::test::HttpServer::MakeUrl("http://frenchpagewithlink"); |
| 749 [ChromeEarlGrey loadURL:frenchPageURL]; | 749 [ChromeEarlGrey loadURL:frenchPageURL]; |
| 750 [[EarlGrey | 750 [[EarlGrey |
| 751 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabelId( | 751 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabelId( |
| 752 IDS_TRANSLATE_INFOBAR_ACCEPT)] | 752 IDS_TRANSLATE_INFOBAR_ACCEPT)] |
| 753 performAction:grey_tap()]; | 753 performAction:grey_tap()]; |
| 754 | 754 |
| 755 // Wait for all callbacks. | 755 // Wait for all callbacks. |
| 756 GREYAssert(testing::WaitUntilConditionOrTimeout( | 756 GREYAssert(testing::WaitUntilConditionOrTimeout( |
| 757 testing::kWaitForJSCompletionTimeout, | 757 testing::kWaitForJSCompletionTimeout, |
| 758 ^{ | 758 ^{ |
| 759 return jsTranslateManager.get().translateStatusChecked; | 759 return jsTranslateManager.get().translateStatusChecked; |
| 760 }), | 760 }), |
| 761 @"Did not receive all translate status callbacks"); | 761 @"Did not receive all translate status callbacks"); |
| 762 | 762 |
| 763 // Check that the translation happened. | 763 // Check that the translation happened. |
| 764 [[EarlGrey selectElementWithMatcher:chrome_test_util::webViewContainingText( | 764 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( |
| 765 "Translated")] | 765 "Translated")] |
| 766 assertWithMatcher:grey_notNil()]; | 766 assertWithMatcher:grey_notNil()]; |
| 767 | 767 |
| 768 // Click on the link. | 768 // Click on the link. |
| 769 [ChromeEarlGrey tapWebViewElementWithID:@"link"]; | 769 [ChromeEarlGrey tapWebViewElementWithID:@"link"]; |
| 770 GURL frenchPagePathURL = web::test::HttpServer::MakeUrl( | 770 GURL frenchPagePathURL = web::test::HttpServer::MakeUrl( |
| 771 base::StringPrintf("http://%s", kFrenchPagePath)); | 771 base::StringPrintf("http://%s", kFrenchPagePath)); |
| 772 [[EarlGrey | 772 [[EarlGrey |
| 773 selectElementWithMatcher:chrome_test_util::webViewContainingText("link")] | 773 selectElementWithMatcher:chrome_test_util::WebViewContainingText("link")] |
| 774 assertWithMatcher:grey_nil()]; | 774 assertWithMatcher:grey_nil()]; |
| 775 [[EarlGrey selectElementWithMatcher:chrome_test_util::omniboxText( | 775 [[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText( |
| 776 frenchPagePathURL.GetContent())] | 776 frenchPagePathURL.GetContent())] |
| 777 assertWithMatcher:grey_notNil()]; | 777 assertWithMatcher:grey_notNil()]; |
| 778 | 778 |
| 779 // Check that the auto-translation happened. | 779 // Check that the auto-translation happened. |
| 780 [[EarlGrey selectElementWithMatcher:chrome_test_util::webViewContainingText( | 780 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( |
| 781 "Translated")] | 781 "Translated")] |
| 782 assertWithMatcher:grey_notNil()]; | 782 assertWithMatcher:grey_notNil()]; |
| 783 } | 783 } |
| 784 | 784 |
| 785 #pragma mark - Utility methods | 785 #pragma mark - Utility methods |
| 786 | 786 |
| 787 // Waits until a language has been detected and checks the language details. | 787 // Waits until a language has been detected and checks the language details. |
| 788 - (void)assertLanguageDetails: | 788 - (void)assertLanguageDetails: |
| 789 (const LanguageDetectionController::DetectionDetails&)expectedDetails { | 789 (const LanguageDetectionController::DetectionDetails&)expectedDetails { |
| 790 GREYAssert(testing::WaitUntilConditionOrTimeout( | 790 GREYAssert(testing::WaitUntilConditionOrTimeout( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 824 // InfoBar is shown. | 824 // InfoBar is shown. |
| 825 - (void)simulateTranslationFromSpanishToEnglish { | 825 - (void)simulateTranslationFromSpanishToEnglish { |
| 826 // Simulate translation. | 826 // Simulate translation. |
| 827 ChromeIOSTranslateClient* client = ChromeIOSTranslateClient::FromWebState( | 827 ChromeIOSTranslateClient* client = ChromeIOSTranslateClient::FromWebState( |
| 828 chrome_test_util::GetCurrentWebState()); | 828 chrome_test_util::GetCurrentWebState()); |
| 829 client->GetTranslateManager()->PageTranslated( | 829 client->GetTranslateManager()->PageTranslated( |
| 830 "es", "en", translate::TranslateErrors::NONE); | 830 "es", "en", translate::TranslateErrors::NONE); |
| 831 | 831 |
| 832 // Assert that the infobar is visible. | 832 // Assert that the infobar is visible. |
| 833 [[EarlGrey | 833 [[EarlGrey |
| 834 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabelId( | 834 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabelId( |
| 835 IDS_DONE)] assertWithMatcher:grey_notNil()]; | 835 IDS_DONE)] assertWithMatcher:grey_notNil()]; |
| 836 [[EarlGrey | 836 [[EarlGrey |
| 837 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabelId( | 837 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabelId( |
| 838 IDS_TRANSLATE_INFOBAR_REVERT)] | 838 IDS_TRANSLATE_INFOBAR_REVERT)] |
| 839 assertWithMatcher:grey_notNil()]; | 839 assertWithMatcher:grey_notNil()]; |
| 840 [[EarlGrey | 840 [[EarlGrey |
| 841 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabelId( | 841 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabelId( |
| 842 IDS_CLOSE)] assertWithMatcher:grey_notNil()]; | 842 IDS_CLOSE)] assertWithMatcher:grey_notNil()]; |
| 843 } | 843 } |
| 844 | 844 |
| 845 @end | 845 @end |
| OLD | NEW |