Commit 42058800 authored by toyoshim@chromium.org's avatar toyoshim@chromium.org

Translate: introduce unittest for TranslateHelper

Currently all tests for TranslateHelper are implemented as browser tests.
But some small tests can run as unit tests. This change introduce new unittest
file for TranslateHelper, then replace an existing test into it.

BUG=175673
TEST=unit_tests --gtest_filter='TranslateHelperTest.*'

Review URL: https://codereview.chromium.org/12209114

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@182412 0039d316-1c4b-4281-b951-d872f2087c98
parent 0057884b
......@@ -1602,6 +1602,7 @@
'renderer/spellchecker/spellcheck_provider_test.h',
'renderer/spellchecker/spellcheck_unittest.cc',
'renderer/spellchecker/spellcheck_worditerator_unittest.cc',
'renderer/translate_helper_unittest.cc',
'service/cloud_print/cloud_print_helpers_unittest.cc',
'service/cloud_print/cloud_print_token_store_unittest.cc',
'service/cloud_print/cloud_print_url_fetcher_unittest.cc',
......
......@@ -142,26 +142,6 @@ void TranslateHelper::CancelPendingTranslation() {
target_lang_.clear();
}
// static
bool TranslateHelper::IsPageTranslatable(WebDocument* document) {
std::vector<WebElement> meta_elements;
webkit_glue::GetMetaElementsWithAttribute(document,
ASCIIToUTF16("name"),
ASCIIToUTF16("google"),
&meta_elements);
std::vector<WebElement>::const_iterator iter;
for (iter = meta_elements.begin(); iter != meta_elements.end(); ++iter) {
WebString attribute = iter->getAttribute("value");
if (attribute.isNull()) // We support both 'value' and 'content'.
attribute = iter->getAttribute("content");
if (attribute.isNull())
continue;
if (LowerCaseEqualsASCII(attribute, "notranslate"))
return false;
}
return true;
}
#if defined(ENABLE_LANGUAGE_DETECTION)
// static
std::string TranslateHelper::DetermineTextLanguage(const string16& text) {
......@@ -194,17 +174,6 @@ std::string TranslateHelper::DetermineTextLanguage(const string16& text) {
////////////////////////////////////////////////////////////////////////////////
// TranslateHelper, protected:
//
// static
void TranslateHelper::ConvertLanguageCodeSynonym(std::string* code) {
// Apply liner search here because number of items in the list is just four.
for (size_t i = 0; i < arraysize(kLanguageCodeSynonyms); ++i) {
if (code->compare(kLanguageCodeSynonyms[i].from) == 0) {
*code = std::string(kLanguageCodeSynonyms[i].to);
break;
}
}
}
bool TranslateHelper::IsTranslateLibAvailable() {
bool lib_available = false;
if (!ExecuteScriptAndGetBoolResult(
......@@ -272,6 +241,37 @@ bool TranslateHelper::DontDelayTasks() {
////////////////////////////////////////////////////////////////////////////////
// TranslateHelper, private:
//
// static
void TranslateHelper::ConvertLanguageCodeSynonym(std::string* code) {
// Apply liner search here because number of items in the list is just four.
for (size_t i = 0; i < arraysize(kLanguageCodeSynonyms); ++i) {
if (code->compare(kLanguageCodeSynonyms[i].from) == 0) {
*code = std::string(kLanguageCodeSynonyms[i].to);
break;
}
}
}
// static
bool TranslateHelper::IsPageTranslatable(WebDocument* document) {
std::vector<WebElement> meta_elements;
webkit_glue::GetMetaElementsWithAttribute(document,
ASCIIToUTF16("name"),
ASCIIToUTF16("google"),
&meta_elements);
std::vector<WebElement>::const_iterator iter;
for (iter = meta_elements.begin(); iter != meta_elements.end(); ++iter) {
WebString attribute = iter->getAttribute("value");
if (attribute.isNull()) // We support both 'value' and 'content'.
attribute = iter->getAttribute("content");
if (attribute.isNull())
continue;
if (LowerCaseEqualsASCII(attribute, "notranslate"))
return false;
}
return true;
}
bool TranslateHelper::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(TranslateHelper, message)
......
......@@ -7,6 +7,7 @@
#include <string>
#include "base/gtest_prod_util.h"
#include "base/memory/weak_ptr.h"
#include "chrome/common/translate_errors.h"
#include "content/public/renderer/render_view_observer.h"
......@@ -28,9 +29,6 @@ class TranslateHelper : public content::RenderViewObserver {
void PageCaptured(const string16& contents);
protected:
// Convert language code to the one used in server supporting list.
static void ConvertLanguageCodeSynonym(std::string* code);
// The following methods are protected so they can be overridden in
// unit-tests.
void OnTranslatePage(int page_id,
......@@ -68,6 +66,11 @@ class TranslateHelper : public content::RenderViewObserver {
virtual bool DontDelayTasks();
private:
FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, LanguageCodeSynonyms);
// Convert language code to the one used in server supporting list.
static void ConvertLanguageCodeSynonym(std::string* code);
// Returns whether the page associated with |document| is a candidate for
// translation. Some pages can explictly specify (via a meta-tag) that they
// should not be translated.
......
......@@ -29,10 +29,6 @@ class TestTranslateHelper : public TranslateHelper {
OnTranslatePage(page_id, translate_script, source_lang, target_lang);
}
void ConvertLanguageCodeSynonym(std::string* code) {
TranslateHelper::ConvertLanguageCodeSynonym(code);
}
MOCK_METHOD0(IsTranslateLibAvailable, bool());
MOCK_METHOD0(IsTranslateLibReady, bool());
MOCK_METHOD0(HasTranslationFinished, bool());
......@@ -44,9 +40,9 @@ class TestTranslateHelper : public TranslateHelper {
DISALLOW_COPY_AND_ASSIGN(TestTranslateHelper);
};
class TranslateHelperTest : public ChromeRenderViewTest {
class TranslateHelperBrowserTest : public ChromeRenderViewTest {
public:
TranslateHelperTest() : translate_helper_(NULL) {}
TranslateHelperBrowserTest() : translate_helper_(NULL) {}
protected:
virtual void SetUp() {
......@@ -86,7 +82,7 @@ class TranslateHelperTest : public ChromeRenderViewTest {
// Tests that the browser gets notified of the translation failure if the
// translate library fails/times-out during initialization.
TEST_F(TranslateHelperTest, TranslateLibNeverReady) {
TEST_F(TranslateHelperBrowserTest, TranslateLibNeverReady) {
// We make IsTranslateLibAvailable true so we don't attempt to inject the
// library.
EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable())
......@@ -111,7 +107,7 @@ TEST_F(TranslateHelperTest, TranslateLibNeverReady) {
// Tests that the browser gets notified of the translation success when the
// translation succeeds.
TEST_F(TranslateHelperTest, TranslateSuccess) {
TEST_F(TranslateHelperBrowserTest, TranslateSuccess) {
// We make IsTranslateLibAvailable true so we don't attempt to inject the
// library.
EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable())
......@@ -154,7 +150,7 @@ TEST_F(TranslateHelperTest, TranslateSuccess) {
// Tests that the browser gets notified of the translation failure when the
// translation fails.
TEST_F(TranslateHelperTest, TranslateFailure) {
TEST_F(TranslateHelperBrowserTest, TranslateFailure) {
// We make IsTranslateLibAvailable true so we don't attempt to inject the
// library.
EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable())
......@@ -190,7 +186,7 @@ TEST_F(TranslateHelperTest, TranslateFailure) {
// Tests that when the browser translate a page for which the language is
// undefined we query the translate element to get the language.
TEST_F(TranslateHelperTest, UndefinedSourceLang) {
TEST_F(TranslateHelperBrowserTest, UndefinedSourceLang) {
// We make IsTranslateLibAvailable true so we don't attempt to inject the
// library.
EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable())
......@@ -229,7 +225,7 @@ TEST_F(TranslateHelperTest, UndefinedSourceLang) {
// Tests that starting a translation while a similar one is pending does not
// break anything.
TEST_F(TranslateHelperTest, MultipleSimilarTranslations) {
TEST_F(TranslateHelperBrowserTest, MultipleSimilarTranslations) {
// We make IsTranslateLibAvailable true so we don't attempt to inject the
// library.
EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable())
......@@ -270,7 +266,7 @@ TEST_F(TranslateHelperTest, MultipleSimilarTranslations) {
}
// Tests that starting a translation while a different one is pending works.
TEST_F(TranslateHelperTest, MultipleDifferentTranslations) {
TEST_F(TranslateHelperBrowserTest, MultipleDifferentTranslations) {
EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable())
.Times(AtLeast(1))
.WillRepeatedly(Return(true));
......@@ -307,27 +303,6 @@ TEST_F(TranslateHelperTest, MultipleDifferentTranslations) {
EXPECT_EQ(TranslateErrors::NONE, error);
}
// Tests that synonym language code is converted to one used in supporting list.
TEST_F(TranslateHelperTest, LanguageCodeSynonyms) {
std::string language;
language = std::string("nb");
translate_helper_->ConvertLanguageCodeSynonym(&language);
EXPECT_EQ(0, language.compare("no"));
language = std::string("he");
translate_helper_->ConvertLanguageCodeSynonym(&language);
EXPECT_EQ(0, language.compare("iw"));
language = std::string("jv");
translate_helper_->ConvertLanguageCodeSynonym(&language);
EXPECT_EQ(0, language.compare("jw"));
language = std::string("fil");
translate_helper_->ConvertLanguageCodeSynonym(&language);
EXPECT_EQ(0, language.compare("tl"));
}
// Tests that we send the right translate language message for a page and that
// we respect the "no translate" meta-tag.
TEST_F(ChromeRenderViewTest, TranslatablePage) {
......
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/renderer/translate_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
typedef testing::Test TranslateHelperTest;
// Tests that synonym language code is converted to one used in supporting list.
TEST_F(TranslateHelperTest, LanguageCodeSynonyms) {
std::string language;
language = std::string("nb");
TranslateHelper::ConvertLanguageCodeSynonym(&language);
EXPECT_EQ("no", language);
language = std::string("he");
TranslateHelper::ConvertLanguageCodeSynonym(&language);
EXPECT_EQ("iw", language);
language = std::string("jv");
TranslateHelper::ConvertLanguageCodeSynonym(&language);
EXPECT_EQ("jw", language);
language = std::string("fil");
TranslateHelper::ConvertLanguageCodeSynonym(&language);
EXPECT_EQ("tl", language);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment