This source file includes following definitions.
- Observe
- load_committed_details
- SimulateNavigation
- SimulateOnTranslateLanguageDetermined
- SimulateOnPageTranslated
- SimulateOnPageTranslated
- GetTranslateMessage
- infobar_service
- infobar_manager
- GetTranslateInfoBar
- CloseTranslateInfoBar
- CheckInfoBarRemovedAndReset
- ExpireTranslateScriptImmediately
- DenyTranslation
- ReloadAndWait
- CreateContextMenu
- Observe
- SetUp
- TearDown
- SetApplicationLocale
- SimulateTranslateScriptURLFetch
- SimulateSupportedLanguagesURLFetch
- SetPrefObserverExpectation
- ShowImplementation
- model
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <algorithm>
#include <set>
#include <vector>
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_change_registrar.h"
#include "base/prefs/pref_service.h"
#include "base/strings/stringprintf.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/infobars/infobar.h"
#include "chrome/browser/infobars/infobar_manager.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
#include "chrome/browser/translate/translate_infobar_delegate.h"
#include "chrome/browser/translate/translate_manager.h"
#include "chrome/browser/translate/translate_service.h"
#include "chrome/browser/translate/translate_tab_helper.h"
#include "chrome/browser/ui/translate/translate_bubble_factory.h"
#include "chrome/browser/ui/translate/translate_bubble_model.h"
#include "chrome/browser/ui/translate/translate_bubble_model_impl.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "components/translate/content/common/translate_messages.h"
#include "components/translate/core/browser/translate_accept_languages.h"
#include "components/translate/core/browser/translate_download_manager.h"
#include "components/translate/core/browser/translate_language_list.h"
#include "components/translate/core/browser/translate_prefs.h"
#include "components/translate/core/browser/translate_script.h"
#include "components/translate/core/common/language_detection_details.h"
#include "components/translate/core/common/translate_pref_names.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_renderer_host.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/WebKit/public/web/WebContextMenuData.h"
#include "url/gurl.h"
class NavEntryCommittedObserver : public content::NotificationObserver {
public:
explicit NavEntryCommittedObserver(content::WebContents* web_contents) {
registrar_.Add(this,
content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<content::NavigationController>(
&web_contents->GetController()));
}
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE {
DCHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED);
details_ =
*(content::Details<content::LoadCommittedDetails>(details).ptr());
}
const content::LoadCommittedDetails& load_committed_details() const {
return details_;
}
private:
content::LoadCommittedDetails details_;
content::NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(NavEntryCommittedObserver);
};
class TranslateManagerRenderViewHostTest
: public ChromeRenderViewHostTestHarness,
public content::NotificationObserver {
public:
TranslateManagerRenderViewHostTest()
: pref_callback_(
base::Bind(&TranslateManagerRenderViewHostTest::OnPreferenceChanged,
base::Unretained(this))) {}
void SimulateNavigation(const GURL& url,
const std::string& lang,
bool page_translatable) {
NavigateAndCommit(url);
SimulateOnTranslateLanguageDetermined(lang, page_translatable);
}
void SimulateOnTranslateLanguageDetermined(const std::string& lang,
bool page_translatable) {
LanguageDetectionDetails details;
details.adopted_language = lang;
content::RenderViewHostTester::TestOnMessageReceived(
rvh(),
ChromeViewHostMsg_TranslateLanguageDetermined(
0, details, page_translatable));
}
void SimulateOnPageTranslated(int routing_id,
const std::string& source_lang,
const std::string& target_lang,
TranslateErrors::Type error) {
content::RenderViewHostTester::TestOnMessageReceived(
rvh(),
ChromeViewHostMsg_PageTranslated(
routing_id, 0, source_lang, target_lang, error));
}
void SimulateOnPageTranslated(const std::string& source_lang,
const std::string& target_lang) {
SimulateOnPageTranslated(
0, source_lang, target_lang, TranslateErrors::NONE);
}
bool GetTranslateMessage(int* page_id,
std::string* original_lang,
std::string* target_lang) {
const IPC::Message* message = process()->sink().GetFirstMessageMatching(
ChromeViewMsg_TranslatePage::ID);
if (!message)
return false;
Tuple4<int, std::string, std::string, std::string> translate_param;
ChromeViewMsg_TranslatePage::Read(message, &translate_param);
if (page_id)
*page_id = translate_param.a;
if (original_lang)
*original_lang = translate_param.c;
if (target_lang)
*target_lang = translate_param.d;
return true;
}
InfoBarService* infobar_service() {
return InfoBarService::FromWebContents(web_contents());
}
InfoBarManager* infobar_manager() {
return infobar_service()->infobar_manager();
}
TranslateInfoBarDelegate* GetTranslateInfoBar() {
return (infobar_manager()->infobar_count() == 1)
? infobar_manager()
->infobar_at(0)
->delegate()
->AsTranslateInfoBarDelegate()
: NULL;
}
bool CloseTranslateInfoBar() {
InfoBarDelegate* infobar = GetTranslateInfoBar();
if (!infobar)
return false;
infobar->InfoBarDismissed();
infobar_manager()->RemoveInfoBar(infobar_manager()->infobar_at(0));
return true;
}
bool CheckInfoBarRemovedAndReset(InfoBarDelegate* delegate) {
bool found = removed_infobars_.count(delegate) != 0;
removed_infobars_.clear();
return found;
}
void ExpireTranslateScriptImmediately() {
TranslateDownloadManager::GetInstance()->SetTranslateScriptExpirationDelay(
0);
}
bool DenyTranslation() {
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
if (!infobar)
return false;
infobar->TranslationDeclined();
infobar_manager()->RemoveInfoBar(infobar_manager()->infobar_at(0));
return true;
}
void ReloadAndWait(bool successful_reload) {
NavEntryCommittedObserver nav_observer(web_contents());
if (successful_reload)
Reload();
else
FailedReload();
const content::LoadCommittedDetails& nav_details =
nav_observer.load_committed_details();
EXPECT_TRUE(nav_details.entry != NULL);
EXPECT_EQ(content::NAVIGATION_TYPE_EXISTING_PAGE, nav_details.type);
base::MessageLoop::current()->RunUntilIdle();
}
TestRenderViewContextMenu* CreateContextMenu() {
content::ContextMenuParams params;
params.media_type = blink::WebContextMenuData::MediaTypeNone;
params.x = 0;
params.y = 0;
params.has_image_contents = true;
params.media_flags = 0;
params.spellcheck_enabled = false;
params.is_editable = false;
params.page_url =
web_contents()->GetController().GetActiveEntry()->GetURL();
#if defined(OS_MACOSX)
params.writing_direction_default = 0;
params.writing_direction_left_to_right = 0;
params.writing_direction_right_to_left = 0;
#endif
params.edit_flags = blink::WebContextMenuData::CanTranslate;
return new TestRenderViewContextMenu(web_contents()->GetMainFrame(),
params);
}
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type);
removed_infobars_.insert(
content::Details<InfoBar::RemovedDetails>(details)->first->delegate());
}
MOCK_METHOD1(OnPreferenceChanged, void(const std::string&));
protected:
virtual void SetUp() {
TranslateService::InitializeForTesting();
TranslateService::SetUseInfobar(true);
TranslateDownloadManager* download_manager =
TranslateDownloadManager::GetInstance();
download_manager->ClearTranslateScriptForTesting();
download_manager->SetTranslateScriptExpirationDelay(60 * 60 * 1000);
ChromeRenderViewHostTestHarness::SetUp();
InfoBarService::CreateForWebContents(web_contents());
TranslateTabHelper::CreateForWebContents(web_contents());
TranslateManager* manager =
TranslateTabHelper::GetManagerFromWebContents(web_contents());
manager->set_translate_max_reload_attemps(0);
notification_registrar_.Add(
this,
chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
content::Source<InfoBarService>(infobar_service()));
}
virtual void TearDown() {
process()->sink().ClearMessages();
notification_registrar_.Remove(
this,
chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
content::Source<InfoBarService>(infobar_service()));
ChromeRenderViewHostTestHarness::TearDown();
TranslateService::ShutdownForTesting();
}
void SetApplicationLocale(const std::string& locale) {
g_browser_process->SetApplicationLocale(locale);
TranslateDownloadManager::GetInstance()->set_application_locale(
g_browser_process->GetApplicationLocale());
}
void SimulateTranslateScriptURLFetch(bool success) {
net::TestURLFetcher* fetcher =
url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId);
ASSERT_TRUE(fetcher);
net::URLRequestStatus status;
status.set_status(success ? net::URLRequestStatus::SUCCESS
: net::URLRequestStatus::FAILED);
fetcher->set_url(fetcher->GetOriginalURL());
fetcher->set_status(status);
fetcher->set_response_code(success ? 200 : 500);
fetcher->delegate()->OnURLFetchComplete(fetcher);
}
void SimulateSupportedLanguagesURLFetch(
bool success,
const std::vector<std::string>& languages,
bool use_alpha_languages,
const std::vector<std::string>& alpha_languages) {
net::URLRequestStatus status;
status.set_status(success ? net::URLRequestStatus::SUCCESS
: net::URLRequestStatus::FAILED);
std::string data;
if (success) {
data =
base::StringPrintf("%s{\"sl\": {\"bla\": \"bla\"}, \"%s\": {",
TranslateLanguageList::kLanguageListCallbackName,
TranslateLanguageList::kTargetLanguagesKey);
const char* comma = "";
for (size_t i = 0; i < languages.size(); ++i) {
data += base::StringPrintf(
"%s\"%s\": \"UnusedFullName\"", comma, languages[i].c_str());
if (i == 0)
comma = ",";
}
if (use_alpha_languages) {
data += base::StringPrintf("},\"%s\": {",
TranslateLanguageList::kAlphaLanguagesKey);
comma = "";
for (size_t i = 0; i < alpha_languages.size(); ++i) {
data += base::StringPrintf(
"%s\"%s\": 1", comma, alpha_languages[i].c_str());
if (i == 0)
comma = ",";
}
}
data += "}})";
}
net::TestURLFetcher* fetcher =
url_fetcher_factory_.GetFetcherByID(TranslateLanguageList::kFetcherId);
ASSERT_TRUE(fetcher != NULL);
fetcher->set_url(fetcher->GetOriginalURL());
fetcher->set_status(status);
fetcher->set_response_code(success ? 200 : 500);
fetcher->SetResponseString(data);
fetcher->delegate()->OnURLFetchComplete(fetcher);
}
void SetPrefObserverExpectation(const char* path) {
EXPECT_CALL(*this, OnPreferenceChanged(std::string(path)));
}
PrefChangeRegistrar::NamedChangeCallback pref_callback_;
private:
content::NotificationRegistrar notification_registrar_;
net::TestURLFetcherFactory url_fetcher_factory_;
std::set<InfoBarDelegate*> removed_infobars_;
DISALLOW_COPY_AND_ASSIGN(TranslateManagerRenderViewHostTest);
};
class MockTranslateBubbleFactory : public TranslateBubbleFactory {
public:
MockTranslateBubbleFactory() {}
virtual void ShowImplementation(BrowserWindow* window,
content::WebContents* web_contents,
TranslateTabHelper::TranslateStep step,
TranslateErrors::Type error_type) OVERRIDE {
if (model_) {
model_->SetViewState(
TranslateBubbleModelImpl::TranslateStepToViewState(step));
return;
}
TranslateTabHelper* translate_tab_helper =
TranslateTabHelper::FromWebContents(web_contents);
std::string source_language =
translate_tab_helper->GetLanguageState().original_language();
std::string target_language = TranslateDownloadManager::GetLanguageCode(
g_browser_process->GetApplicationLocale());
scoped_ptr<TranslateUIDelegate> ui_delegate(new TranslateUIDelegate(
web_contents, source_language, target_language));
model_.reset(new TranslateBubbleModelImpl(step, ui_delegate.Pass()));
}
TranslateBubbleModel* model() { return model_.get(); }
private:
scoped_ptr<TranslateBubbleModel> model_;
DISALLOW_COPY_AND_ASSIGN(MockTranslateBubbleFactory);
};
TEST_F(TranslateManagerRenderViewHostTest, NormalTranslate) {
SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step());
process()->sink().ClearMessages();
infobar->Translate();
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateTabHelper::TRANSLATING, infobar->translate_step());
SimulateTranslateScriptURLFetch(true);
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);
SimulateOnPageTranslated("fr", "en");
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateTabHelper::AFTER_TRANSLATE, infobar->translate_step());
process()->sink().ClearMessages();
std::string new_original_lang = infobar->language_code_at(0);
infobar->UpdateOriginalLanguageIndex(0);
infobar->Translate();
EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
EXPECT_EQ(new_original_lang, original_lang);
EXPECT_EQ("en", target_lang);
SimulateOnPageTranslated(new_original_lang, "en");
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
process()->sink().ClearMessages();
std::string new_target_lang = infobar->language_code_at(1);
infobar->UpdateTargetLanguageIndex(1);
infobar->Translate();
EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
EXPECT_EQ(new_original_lang, original_lang);
EXPECT_EQ(new_target_lang, target_lang);
SimulateOnPageTranslated(new_original_lang, new_target_lang);
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(new_target_lang, infobar->target_language_code());
ReloadAndWait(true);
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step());
infobar->UpdateTargetLanguageIndex(1);
infobar->ToggleAlwaysTranslate();
ReloadAndWait(true);
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateTabHelper::TRANSLATING, infobar->translate_step());
EXPECT_EQ(new_target_lang, infobar->target_language_code());
}
TEST_F(TranslateManagerRenderViewHostTest, TranslateScriptNotAvailable) {
SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step());
process()->sink().ClearMessages();
infobar->Translate();
SimulateTranslateScriptURLFetch(false);
EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL));
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step());
}
TEST_F(TranslateManagerRenderViewHostTest, TranslateUnknownLanguage) {
SimulateNavigation(GURL("http://www.google.mys"), "und", true);
ASSERT_TRUE(GetTranslateInfoBar() == NULL);
scoped_ptr<TestRenderViewContextMenu> menu(CreateContextMenu());
menu->Init();
menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
SimulateTranslateScriptURLFetch(false);
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step());
EXPECT_TRUE(infobar->is_error());
infobar->MessageInfoBarButtonPressed();
SimulateTranslateScriptURLFetch(true);
SimulateOnPageTranslated("fr", "en");
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateTabHelper::AFTER_TRANSLATE, infobar->translate_step());
EXPECT_EQ("fr", infobar->original_language_code());
EXPECT_EQ("en", infobar->target_language_code());
SimulateNavigation(GURL("http://www.google.com"), "und", true);
menu.reset(CreateContextMenu());
menu->Init();
menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
SimulateOnPageTranslated(1, "en", "en", TranslateErrors::IDENTICAL_LANGUAGES);
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step());
EXPECT_EQ(TranslateErrors::IDENTICAL_LANGUAGES, infobar->error_type());
SimulateNavigation(GURL("http://www.google.com"), "und", true);
menu.reset(CreateContextMenu());
menu->Init();
menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
SimulateOnPageTranslated(
2, std::string(), "en", TranslateErrors::UNKNOWN_LANGUAGE);
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step());
EXPECT_EQ(TranslateErrors::UNKNOWN_LANGUAGE, infobar->error_type());
}
TEST_F(TranslateManagerRenderViewHostTest, TestLanguages) {
std::vector<std::string> languages;
languages.push_back("en");
languages.push_back("ja");
languages.push_back("fr");
languages.push_back("ht");
languages.push_back("xx");
languages.push_back("zh");
languages.push_back("zh-CN");
languages.push_back("und");
GURL url("http://www.google.com");
for (size_t i = 0; i < languages.size(); ++i) {
std::string lang = languages[i];
SCOPED_TRACE(::testing::Message() << "Iteration " << i
<< " language=" << lang);
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar == NULL);
SimulateNavigation(url, lang, true);
infobar = GetTranslateInfoBar();
bool expected =
TranslateDownloadManager::IsSupportedLanguage(lang) && lang != "en";
EXPECT_EQ(expected, infobar != NULL);
if (infobar != NULL)
EXPECT_TRUE(CloseTranslateInfoBar());
}
}
TEST_F(TranslateManagerRenderViewHostTest, FetchLanguagesFromTranslateServer) {
std::vector<std::string> server_languages;
server_languages.push_back("aa");
server_languages.push_back("ak");
server_languages.push_back("ab");
server_languages.push_back("en-CA");
server_languages.push_back("zh");
server_languages.push_back("yi");
server_languages.push_back("fr-FR");
server_languages.push_back("xx");
std::vector<std::string> alpha_languages;
alpha_languages.push_back("aa");
alpha_languages.push_back("yi");
std::vector<std::string> default_supported_languages;
TranslateDownloadManager::GetSupportedLanguages(&default_supported_languages);
ASSERT_NE(default_supported_languages.size(), server_languages.size());
std::vector<std::string> current_supported_languages;
TranslateDownloadManager::GetSupportedLanguages(¤t_supported_languages);
EXPECT_EQ(default_supported_languages, current_supported_languages);
SimulateSupportedLanguagesURLFetch(
false, std::vector<std::string>(), true, std::vector<std::string>());
current_supported_languages.clear();
TranslateDownloadManager::GetSupportedLanguages(¤t_supported_languages);
EXPECT_EQ(default_supported_languages, current_supported_languages);
SimulateSupportedLanguagesURLFetch(
true, server_languages, true, alpha_languages);
current_supported_languages.clear();
TranslateDownloadManager::GetSupportedLanguages(¤t_supported_languages);
EXPECT_EQ(server_languages.size() - 1, current_supported_languages.size());
for (size_t i = 0; i < server_languages.size(); ++i) {
const std::string& lang = server_languages[i];
if (lang == "xx")
continue;
EXPECT_NE(current_supported_languages.end(),
std::find(current_supported_languages.begin(),
current_supported_languages.end(),
lang));
bool is_alpha =
std::find(alpha_languages.begin(), alpha_languages.end(), lang) !=
alpha_languages.end();
EXPECT_EQ(TranslateDownloadManager::IsAlphaLanguage(lang), is_alpha);
}
}
TEST_F(TranslateManagerRenderViewHostTest,
FetchLanguagesFromTranslateServerWithoutAlpha) {
std::vector<std::string> server_languages;
server_languages.push_back("aa");
server_languages.push_back("ak");
server_languages.push_back("ab");
server_languages.push_back("en-CA");
server_languages.push_back("zh");
server_languages.push_back("yi");
server_languages.push_back("fr-FR");
server_languages.push_back("xx");
std::vector<std::string> alpha_languages;
alpha_languages.push_back("aa");
alpha_languages.push_back("yi");
std::vector<std::string> default_supported_languages;
TranslateDownloadManager::GetSupportedLanguages(&default_supported_languages);
SimulateSupportedLanguagesURLFetch(
true, server_languages, false, alpha_languages);
std::vector<std::string> current_supported_languages;
TranslateDownloadManager::GetSupportedLanguages(¤t_supported_languages);
EXPECT_EQ(server_languages.size() - 1, current_supported_languages.size());
for (size_t i = 0; i < server_languages.size(); ++i) {
const std::string& lang = server_languages[i];
if (lang == "xx")
continue;
EXPECT_NE(current_supported_languages.end(),
std::find(current_supported_languages.begin(),
current_supported_languages.end(),
lang));
EXPECT_FALSE(TranslateDownloadManager::IsAlphaLanguage(lang));
}
}
TEST_F(TranslateManagerRenderViewHostTest, AutoTranslateOnNavigate) {
SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
infobar->Translate();
SimulateTranslateScriptURLFetch(true);
SimulateOnPageTranslated("fr", "en");
process()->sink().ClearMessages();
SimulateNavigation(GURL("http://news.google.fr"), "fr", true);
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);
process()->sink().ClearMessages();
SimulateNavigation(GURL("http://news.google.es"), "es", true);
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
}
TEST_F(TranslateManagerRenderViewHostTest, MultipleOnPageContents) {
SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
EXPECT_TRUE(DenyTranslation());
EXPECT_EQ(0U, infobar_manager()->infobar_count());
SimulateOnTranslateLanguageDetermined("fr", true);
EXPECT_EQ(0U, infobar_manager()->infobar_count());
SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true);
EXPECT_TRUE(CloseTranslateInfoBar());
EXPECT_EQ(0U, infobar_manager()->infobar_count());
SimulateOnTranslateLanguageDetermined("fr", true);
EXPECT_EQ(0U, infobar_manager()->infobar_count());
}
TEST_F(TranslateManagerRenderViewHostTest, Reload) {
SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
EXPECT_TRUE(CloseTranslateInfoBar());
ReloadAndWait(true);
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
EXPECT_TRUE(CloseTranslateInfoBar());
ReloadAndWait(false);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
TranslateManager* manager =
TranslateTabHelper::GetManagerFromWebContents(web_contents());
manager->set_translate_max_reload_attemps(100);
ReloadAndWait(true);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
}
TEST_F(TranslateManagerRenderViewHostTest, ReloadFromLocationBar) {
GURL url("http://www.google.fr");
SimulateNavigation(url, "fr", true);
EXPECT_TRUE(CloseTranslateInfoBar());
NavEntryCommittedObserver nav_observer(web_contents());
web_contents()->GetController().LoadURL(
url, content::Referrer(), content::PAGE_TRANSITION_TYPED, std::string());
rvh_tester()->SendNavigate(0, url);
const content::LoadCommittedDetails& nav_details =
nav_observer.load_committed_details();
EXPECT_TRUE(nav_details.entry != NULL);
EXPECT_EQ(content::NAVIGATION_TYPE_SAME_PAGE, nav_details.type);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
TEST_F(TranslateManagerRenderViewHostTest, CloseInfoBarInPageNavigation) {
SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
EXPECT_TRUE(CloseTranslateInfoBar());
SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true);
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
TEST_F(TranslateManagerRenderViewHostTest, CloseInfoBarInSubframeNavigation) {
SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
EXPECT_TRUE(CloseTranslateInfoBar());
rvh_tester()->SendNavigateWithTransition(
1, GURL("http://pub.com"), content::PAGE_TRANSITION_AUTO_SUBFRAME);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
rvh_tester()->SendNavigateWithTransition(
2, GURL("http://pub.com"), content::PAGE_TRANSITION_MANUAL_SUBFRAME);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true);
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
TEST_F(TranslateManagerRenderViewHostTest, DenyTranslateInPageNavigation) {
SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
EXPECT_TRUE(DenyTranslation());
SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true);
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
TEST_F(TranslateManagerRenderViewHostTest,
TranslateCloseInfoBarInPageNavigation) {
SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
infobar->Translate();
SimulateTranslateScriptURLFetch(true);
SimulateOnPageTranslated("fr", "en");
EXPECT_TRUE(CloseTranslateInfoBar());
SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
SimulateNavigation(GURL("http://www.google.de"), "de", true);
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
TEST_F(TranslateManagerRenderViewHostTest, TranslateInPageNavigation) {
SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
infobar->Translate();
SimulateTranslateScriptURLFetch(true);
SimulateOnPageTranslated("fr", "en");
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
SimulateNavigation(GURL("http://www.google.de"), "de", true);
EXPECT_TRUE(CheckInfoBarRemovedAndReset(infobar));
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
TEST_F(TranslateManagerRenderViewHostTest, CLDReportsUnsupportedPageLanguage) {
SimulateNavigation(GURL("http://www.google.com"), "qbz", true);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
}
TEST_F(TranslateManagerRenderViewHostTest, ServerReportsUnsupportedLanguage) {
SimulateNavigation(GURL("http://mail.google.fr"), "fr", true);
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
process()->sink().ClearMessages();
infobar->Translate();
SimulateTranslateScriptURLFetch(true);
SimulateOnPageTranslated("qbz", "en");
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step());
ASSERT_FALSE(infobar->GetMessageInfoBarButtonText().empty());
process()->sink().ClearMessages();
infobar->MessageInfoBarButtonPressed();
const IPC::Message* message = process()->sink().GetFirstMessageMatching(
ChromeViewMsg_RevertTranslation::ID);
EXPECT_TRUE(message != NULL);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
}
TEST_F(TranslateManagerRenderViewHostTest, UnsupportedUILanguage) {
std::string original_lang = g_browser_process->GetApplicationLocale();
SetApplicationLocale("qbz");
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
PrefService* prefs = profile->GetPrefs();
prefs->SetString(prefs::kAcceptLanguages, "qbz");
SimulateNavigation(GURL("http://www.google.com"), "en", true);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
scoped_ptr<TestRenderViewContextMenu> menu(CreateContextMenu());
menu->Init();
EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
SetApplicationLocale(original_lang);
}
TEST_F(TranslateManagerRenderViewHostTest, TranslateAcceptLanguage) {
std::string original_lang = g_browser_process->GetApplicationLocale();
SetApplicationLocale("qbz");
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
PrefService* prefs = profile->GetPrefs();
prefs->SetString(prefs::kAcceptLanguages, "qbz,fr");
SimulateNavigation(GURL("http://google.de"), "de", true);
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
prefs->SetString(prefs::kAcceptLanguages, "qbz,en-us");
SimulateNavigation(GURL("http://google.de"), "de", true);
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
TEST_F(TranslateManagerRenderViewHostTest, TranslateEnabledPref) {
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
PrefService* prefs = profile->GetPrefs();
prefs->SetBoolean(prefs::kEnableTranslate, true);
SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
EXPECT_TRUE(infobar != NULL);
prefs->SetBoolean(prefs::kEnableTranslate, false);
GURL url("http://www.youtube.fr");
NavigateAndCommit(url);
infobar = GetTranslateInfoBar();
EXPECT_TRUE(infobar == NULL);
SimulateOnTranslateLanguageDetermined("fr", true);
infobar = GetTranslateInfoBar();
EXPECT_TRUE(infobar == NULL);
}
TEST_F(TranslateManagerRenderViewHostTest, NeverTranslateLanguagePref) {
GURL url("http://www.google.fr");
SimulateNavigation(url, "fr", true);
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
PrefService* prefs = profile->GetPrefs();
PrefChangeRegistrar registrar;
registrar.Init(prefs);
registrar.Add(TranslatePrefs::kPrefTranslateBlockedLanguages, pref_callback_);
scoped_ptr<TranslatePrefs> translate_prefs(
TranslateTabHelper::CreateTranslatePrefs(prefs));
EXPECT_FALSE(translate_prefs->IsBlockedLanguage("fr"));
TranslateAcceptLanguages* accept_languages =
TranslateTabHelper::GetTranslateAcceptLanguages(profile);
EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr"));
SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateBlockedLanguages);
translate_prefs->BlockLanguage("fr");
EXPECT_TRUE(translate_prefs->IsBlockedLanguage("fr"));
EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(url.host()));
EXPECT_FALSE(translate_prefs->CanTranslateLanguage(accept_languages, "fr"));
EXPECT_TRUE(CloseTranslateInfoBar());
SimulateNavigation(GURL("http://wwww.youtube.fr"), "fr", true);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateBlockedLanguages);
translate_prefs->UnblockLanguage("fr");
EXPECT_FALSE(translate_prefs->IsBlockedLanguage("fr"));
EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(url.host()));
EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr"));
SimulateNavigation(url, "fr", true);
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
TEST_F(TranslateManagerRenderViewHostTest, NeverTranslateSitePref) {
GURL url("http://www.google.fr");
std::string host(url.host());
SimulateNavigation(url, "fr", true);
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
PrefService* prefs = profile->GetPrefs();
PrefChangeRegistrar registrar;
registrar.Init(prefs);
registrar.Add(TranslatePrefs::kPrefTranslateSiteBlacklist, pref_callback_);
scoped_ptr<TranslatePrefs> translate_prefs(
TranslateTabHelper::CreateTranslatePrefs(prefs));
EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(host));
TranslateAcceptLanguages* accept_languages =
TranslateTabHelper::GetTranslateAcceptLanguages(profile);
EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr"));
SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist);
translate_prefs->BlacklistSite(host);
EXPECT_TRUE(translate_prefs->IsSiteBlacklisted(host));
EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr"));
EXPECT_TRUE(CloseTranslateInfoBar());
SimulateNavigation(GURL("http://www.google.fr/hello"), "fr", true);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist);
translate_prefs->RemoveSiteFromBlacklist(host);
EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(host));
EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr"));
SimulateNavigation(url, "fr", true);
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
TEST_F(TranslateManagerRenderViewHostTest, AlwaysTranslateLanguagePref) {
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
PrefService* prefs = profile->GetPrefs();
PrefChangeRegistrar registrar;
registrar.Init(prefs);
registrar.Add(TranslatePrefs::kPrefTranslateWhitelists, pref_callback_);
scoped_ptr<TranslatePrefs> translate_prefs(
TranslateTabHelper::CreateTranslatePrefs(prefs));
SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists);
translate_prefs->WhitelistLanguagePair("fr", "en");
SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateTabHelper::TRANSLATING, infobar->translate_step());
SimulateTranslateScriptURLFetch(true);
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();
SimulateNavigation(GURL("http://www.google.es"), "es", true);
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
EXPECT_TRUE(CloseTranslateInfoBar());
TestingProfile* test_profile =
static_cast<TestingProfile*>(web_contents()->GetBrowserContext());
test_profile->ForceIncognito(true);
SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true);
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
EXPECT_TRUE(CloseTranslateInfoBar());
test_profile->ForceIncognito(false);
SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists);
translate_prefs->RemoveLanguagePairFromWhitelist("fr", "en");
SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step());
}
TEST_F(TranslateManagerRenderViewHostTest, ContextMenu) {
GURL url("http://www.google.fr");
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
scoped_ptr<TranslatePrefs> translate_prefs(
TranslateTabHelper::CreateTranslatePrefs(profile->GetPrefs()));
translate_prefs->BlockLanguage("fr");
translate_prefs->BlacklistSite(url.host());
EXPECT_TRUE(translate_prefs->IsBlockedLanguage("fr"));
EXPECT_TRUE(translate_prefs->IsSiteBlacklisted(url.host()));
NavigateAndCommit(url);
scoped_ptr<TestRenderViewContextMenu> menu(CreateContextMenu());
menu->Init();
EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
SimulateOnTranslateLanguageDetermined("fr", true);
menu.reset(CreateContextMenu());
menu->Init();
EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateTabHelper::TRANSLATING, infobar->translate_step());
SimulateTranslateScriptURLFetch(true);
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();
EXPECT_FALSE(translate_prefs->IsBlockedLanguage("fr"));
EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(url.host()));
SimulateOnPageTranslated("fr", "en");
menu.reset(CreateContextMenu());
menu->Init();
EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
SimulateNavigation(GURL("http://www.google.es"), "es", true);
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
infobar->Translate();
EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
process()->sink().ClearMessages();
menu.reset(CreateContextMenu());
menu->Init();
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
SimulateNavigation(GURL("http://www.google.de"), "de", true);
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
infobar->Translate();
EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
process()->sink().ClearMessages();
menu.reset(CreateContextMenu());
menu->Init();
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
SimulateOnPageTranslated("de", "en");
menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
SimulateNavigation(url, "und", true);
menu.reset(CreateContextMenu());
menu->Init();
EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
SimulateNavigation(url, "qbz", true);
menu.reset(CreateContextMenu());
menu->Init();
EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
}
TEST_F(TranslateManagerRenderViewHostTest, BeforeTranslateExtraButtons) {
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
scoped_ptr<TranslatePrefs> translate_prefs(
TranslateTabHelper::CreateTranslatePrefs(profile->GetPrefs()));
translate_prefs->ResetTranslationAcceptedCount("fr");
translate_prefs->ResetTranslationDeniedCount("fr");
translate_prefs->ResetTranslationAcceptedCount("de");
translate_prefs->ResetTranslationDeniedCount("de");
TranslateInfoBarDelegate* infobar;
TestingProfile* test_profile =
static_cast<TestingProfile*>(web_contents()->GetBrowserContext());
static_cast<extensions::TestExtensionSystem*>(
extensions::ExtensionSystem::Get(test_profile))->CreateProcessManager();
test_profile->ForceIncognito(true);
for (int i = 0; i < 8; ++i) {
SCOPED_TRACE(::testing::Message() << "Iteration " << i << " incognito mode="
<< test_profile->IsOffTheRecord());
SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step());
if (i < 7) {
EXPECT_FALSE(infobar->ShouldShowAlwaysTranslateShortcut());
infobar->Translate();
process()->sink().ClearMessages();
} else {
EXPECT_TRUE(infobar->ShouldShowAlwaysTranslateShortcut());
}
if (i == 3)
test_profile->ForceIncognito(false);
}
infobar->AlwaysTranslatePageLanguage();
EXPECT_TRUE(translate_prefs->IsLanguagePairWhitelisted("fr", "en"));
SimulateTranslateScriptURLFetch(true);
int page_id = 0;
std::string original_lang, target_lang;
EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
process()->sink().ClearMessages();
test_profile->ForceIncognito(true);
for (int i = 0; i < 8; ++i) {
SCOPED_TRACE(::testing::Message() << "Iteration " << i << " incognito mode="
<< test_profile->IsOffTheRecord());
SimulateNavigation(GURL("http://www.google.de"), "de", true);
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step());
if (i < 7) {
EXPECT_FALSE(infobar->ShouldShowNeverTranslateShortcut());
infobar->TranslationDeclined();
} else {
EXPECT_TRUE(infobar->ShouldShowNeverTranslateShortcut());
}
if (i == 3)
test_profile->ForceIncognito(false);
}
infobar->NeverTranslatePageLanguage();
EXPECT_TRUE(translate_prefs->IsBlockedLanguage("de"));
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
process()->sink().ClearMessages();
ASSERT_TRUE(GetTranslateInfoBar() == NULL);
}
TEST_F(TranslateManagerRenderViewHostTest, NonTranslatablePage) {
SimulateNavigation(GURL("http://mail.google.fr"), "fr", false);
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
scoped_ptr<TestRenderViewContextMenu> menu(CreateContextMenu());
menu->Init();
EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
}
TEST_F(TranslateManagerRenderViewHostTest, ScriptExpires) {
ExpireTranslateScriptImmediately();
SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
process()->sink().ClearMessages();
infobar->Translate();
SimulateTranslateScriptURLFetch(true);
SimulateOnPageTranslated("fr", "en");
base::MessageLoop::current()->RunUntilIdle();
SimulateNavigation(GURL("http://www.google.es"), "es", true);
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
process()->sink().ClearMessages();
infobar->Translate();
EXPECT_TRUE(process()->sink().GetFirstMessageMatching(
ChromeViewMsg_TranslatePage::ID) == NULL);
SimulateTranslateScriptURLFetch(true);
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);
}
TEST_F(TranslateManagerRenderViewHostTest, DownloadsAndHistoryNotTranslated) {
ASSERT_FALSE(
TranslateManager::IsTranslatableURL(GURL(chrome::kChromeUIDownloadsURL)));
ASSERT_FALSE(
TranslateManager::IsTranslatableURL(GURL(chrome::kChromeUIHistoryURL)));
}
#if defined(USE_AURA)
TEST_F(TranslateManagerRenderViewHostTest, BubbleNormalTranslate) {
TranslateService::SetUseInfobar(false);
MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
TranslateBubbleFactory::SetFactory(factory);
SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar == NULL);
TranslateBubbleModel* bubble = factory->model();
ASSERT_TRUE(bubble != NULL);
EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE,
bubble->GetViewState());
process()->sink().ClearMessages();
bubble->Translate();
bubble = factory->model();
ASSERT_TRUE(bubble != NULL);
EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING,
bubble->GetViewState());
SimulateTranslateScriptURLFetch(true);
SimulateOnPageTranslated("fr", "en");
bubble = factory->model();
ASSERT_TRUE(bubble != NULL);
EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE,
bubble->GetViewState());
}
TEST_F(TranslateManagerRenderViewHostTest, BubbleTranslateScriptNotAvailable) {
TranslateService::SetUseInfobar(false);
MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
TranslateBubbleFactory::SetFactory(factory);
SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar == NULL);
TranslateBubbleModel* bubble = factory->model();
ASSERT_TRUE(bubble != NULL);
EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE,
bubble->GetViewState());
process()->sink().ClearMessages();
bubble->Translate();
SimulateTranslateScriptURLFetch(false);
EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL));
bubble = factory->model();
ASSERT_TRUE(bubble != NULL);
EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_ERROR, bubble->GetViewState());
}
TEST_F(TranslateManagerRenderViewHostTest, BubbleUnknownLanguage) {
TranslateService::SetUseInfobar(false);
MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
TranslateBubbleFactory::SetFactory(factory);
SimulateNavigation(GURL("http://www.google.mys"), "und", true);
ASSERT_TRUE(factory->model() == NULL);
scoped_ptr<TestRenderViewContextMenu> menu(CreateContextMenu());
menu->Init();
menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar == NULL);
TranslateBubbleModel* bubble = factory->model();
ASSERT_TRUE(bubble != NULL);
EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING,
bubble->GetViewState());
}
#endif