Index: chrome/browser/translate/translate_manager_browsertest.cc |
=================================================================== |
--- chrome/browser/translate/translate_manager_browsertest.cc (revision 89207) |
+++ chrome/browser/translate/translate_manager_browsertest.cc (working copy) |
@@ -3,9 +3,12 @@ |
// found in the LICENSE file. |
+#include <algorithm> |
#include <set> |
+#include <vector> |
#include "base/utf_string_conversions.h" |
+#include "base/stringprintf.h" |
#include "chrome/app/chrome_command_ids.h" |
#include "chrome/browser/prefs/pref_change_registrar.h" |
#include "chrome/browser/prefs/pref_service.h" |
@@ -177,7 +180,7 @@ |
URLFetcher::set_factory(NULL); |
} |
- void SimulateURLFetch(bool success) { |
+ void SimulateTranslateScriptURLFetch(bool success) { |
TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
ASSERT_TRUE(fetcher); |
net::URLRequestStatus status; |
@@ -189,6 +192,34 @@ |
std::string()); |
} |
+ void SimulateSupportedLanguagesURLFetch( |
+ bool success, const std::vector<std::string>& languages) { |
+ TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(1); |
+ ASSERT_TRUE(fetcher); |
+ 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': {", |
+ TranslateManager::kLanguageListCallbackName, |
+ TranslateManager::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 = ","; |
+ } |
+ data += "}})"; |
+ } |
+ fetcher->delegate()->OnURLFetchComplete(fetcher, fetcher->original_url(), |
+ status, success ? 200 : 500, |
+ net::ResponseCookies(), |
+ data); |
+ } |
+ |
void SetPrefObserverExpectation(const char* path) { |
EXPECT_CALL( |
pref_observer_, |
@@ -300,7 +331,7 @@ |
// Simulate the translate script being retrieved (it only needs to be done |
// once in the test as it is cached). |
- SimulateURLFetch(true); |
+ SimulateTranslateScriptURLFetch(true); |
// Test that we sent the right message to the renderer. |
int page_id = 0; |
@@ -361,7 +392,7 @@ |
process()->sink().ClearMessages(); |
infobar->Translate(); |
// Simulate a failure retrieving the translate script. |
- SimulateURLFetch(false); |
+ SimulateTranslateScriptURLFetch(false); |
// We should not have sent any message to translate to the renderer. |
EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL)); |
@@ -389,13 +420,14 @@ |
menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE); |
// To test that bug #49018 if fixed, make sure we deal correctly with errors. |
- SimulateURLFetch(false); // Simulate a failure to fetch the translate script. |
+ // Simulate a failure to fetch the translate script. |
+ SimulateTranslateScriptURLFetch(false); |
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
ASSERT_TRUE(infobar != NULL); |
EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, infobar->type()); |
EXPECT_TRUE(infobar->IsError()); |
infobar->MessageInfoBarButtonPressed(); |
- SimulateURLFetch(true); // This time succeed. |
+ SimulateTranslateScriptURLFetch(true); // This time succeed. |
// Simulate the render notifying the translation has been done, the server |
// having detected the page was in a known and supported language. |
@@ -506,6 +538,59 @@ |
} |
} |
+// Test the fetching of languages from the translate server |
+TEST_F(TranslateManagerTest, FetchLanguagesFromTranslateServer) { |
+ std::vector<std::string> server_languages; |
+ // A list of languages to fake being returned by the translate server. |
+ server_languages.push_back("aa"); |
+ server_languages.push_back("bb"); |
+ server_languages.push_back("ab"); |
+ server_languages.push_back("en-CA"); |
+ server_languages.push_back("zz"); |
+ server_languages.push_back("yy"); |
+ server_languages.push_back("fr-FR"); |
+ |
+ // First, get the default languages list: |
+ std::vector<std::string> default_supported_languages; |
+ TranslateManager::GetSupportedLanguages(&default_supported_languages); |
+ // To make sure we got the defaults and don't confuse them with the mocks. |
+ ASSERT_NE(default_supported_languages.size(), server_languages.size()); |
+ |
+ TranslateManager::GetInstance()->FetchLanguageListFromTranslateServer( |
+ contents()->profile()->GetPrefs()); |
+ |
+ // Check that we still get the defaults until the URLFetch has completed. |
+ std::vector<std::string> current_supported_languages; |
+ TranslateManager::GetSupportedLanguages(¤t_supported_languages); |
+ EXPECT_EQ(default_supported_languages, current_supported_languages); |
+ |
+ // Also check that it didn't change if we failed the URL fetch. |
+ SimulateSupportedLanguagesURLFetch(false, std::vector<std::string>()); |
+ current_supported_languages.clear(); |
+ TranslateManager::GetSupportedLanguages(¤t_supported_languages); |
+ EXPECT_EQ(default_supported_languages, current_supported_languages); |
+ |
+ // Now check that we got the appropriate set of languages from the server. |
+ TranslateManager::GetInstance()->FetchLanguageListFromTranslateServer( |
+ contents()->profile()->GetPrefs()); |
+ SimulateSupportedLanguagesURLFetch(true, server_languages); |
+ current_supported_languages.clear(); |
+ TranslateManager::GetSupportedLanguages(¤t_supported_languages); |
+ // Not sure we need to guarantee the order of languages, so we find them. |
+ EXPECT_EQ(server_languages.size(), current_supported_languages.size()); |
+ for (size_t i = 0; i < server_languages.size(); ++i) { |
+ EXPECT_NE(current_supported_languages.end(), |
+ std::find(current_supported_languages.begin(), |
+ current_supported_languages.end(), |
+ server_languages[i])); |
+ } |
+ |
+ // Reset to original state. |
+ TranslateManager::GetInstance()->FetchLanguageListFromTranslateServer( |
+ contents()->profile()->GetPrefs()); |
+ SimulateSupportedLanguagesURLFetch(true, default_supported_languages); |
+} |
+ |
// Tests auto-translate on page. |
TEST_F(TranslateManagerTest, AutoTranslateOnNavigate) { |
// Simulate navigating to a page and getting its language. |
@@ -515,7 +600,8 @@ |
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
ASSERT_TRUE(infobar != NULL); |
infobar->Translate(); |
- SimulateURLFetch(true); // Simulate the translate script being retrieved. |
+ // Simulate the translate script being retrieved. |
+ SimulateTranslateScriptURLFetch(true); |
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
TranslateErrors::NONE)); |
@@ -687,7 +773,8 @@ |
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
ASSERT_TRUE(infobar != NULL); |
infobar->Translate(); |
- SimulateURLFetch(true); // Simulate the translate script being retrieved. |
+ // Simulate the translate script being retrieved. |
+ SimulateTranslateScriptURLFetch(true); |
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
TranslateErrors::NONE)); |
@@ -716,7 +803,8 @@ |
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
ASSERT_TRUE(infobar != NULL); |
infobar->Translate(); |
- SimulateURLFetch(true); // Simulate the translate script being retrieved. |
+ // Simulate the translate script being retrieved. |
+ SimulateTranslateScriptURLFetch(true); |
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
TranslateErrors::NONE)); |
// The after translate infobar is showing. |
@@ -753,7 +841,7 @@ |
ASSERT_TRUE(infobar != NULL); |
process()->sink().ClearMessages(); |
infobar->Translate(); |
- SimulateURLFetch(true); |
+ SimulateTranslateScriptURLFetch(true); |
// Simulate the render notifying the translation has been done, but it |
// reports a language we don't support. |
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "qbz", "en", |
@@ -936,8 +1024,8 @@ |
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
ASSERT_TRUE(infobar != NULL); |
EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->type()); |
- |
- SimulateURLFetch(true); // Simulate the translate script being retrieved. |
+ // Simulate the translate script being retrieved. |
+ SimulateTranslateScriptURLFetch(true); |
int page_id = 0; |
std::string original_lang, target_lang; |
EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
@@ -1007,7 +1095,8 @@ |
TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
ASSERT_TRUE(infobar != NULL); |
EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->type()); |
- SimulateURLFetch(true); // Simulate the translate script being retrieved. |
+ // Simulate the translate script being retrieved. |
+ SimulateTranslateScriptURLFetch(true); |
int page_id = 0; |
std::string original_lang, target_lang; |
EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
@@ -1117,7 +1206,7 @@ |
EXPECT_TRUE(translate_prefs.IsLanguagePairWhitelisted("fr", "en")); |
// Simulate the translate script being retrieved (it only needs to be done |
// once in the test as it is cached). |
- SimulateURLFetch(true); |
+ SimulateTranslateScriptURLFetch(true); |
// That should have triggered a page translate. |
int page_id = 0; |
std::string original_lang, target_lang; |
@@ -1179,7 +1268,7 @@ |
ASSERT_TRUE(infobar != NULL); |
process()->sink().ClearMessages(); |
infobar->Translate(); |
- SimulateURLFetch(true); |
+ SimulateTranslateScriptURLFetch(true); |
rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en", |
TranslateErrors::NONE)); |
@@ -1198,7 +1287,7 @@ |
process()->sink().GetFirstMessageMatching(ViewMsg_TranslatePage::ID) == |
NULL); |
// Now simulate the URL fetch. |
- SimulateURLFetch(true); |
+ SimulateTranslateScriptURLFetch(true); |
// Now the message should have been sent. |
int page_id = 0; |
std::string original_lang, target_lang; |