diff options
author | jcivelli@google.com <jcivelli@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-06 22:21:02 +0000 |
---|---|---|
committer | jcivelli@google.com <jcivelli@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-06 22:21:02 +0000 |
commit | 85d252e22b6bec161873a0b5656d59c8ebe04e30 (patch) | |
tree | 937f8add8eac344f93d6a8ec6604796f371c761c /chrome/renderer/translate_helper_unittest.cc | |
parent | d41661b5e24c8d774acea07c05ef2e5896587e56 (diff) | |
download | chromium_src-85d252e22b6bec161873a0b5656d59c8ebe04e30.zip chromium_src-85d252e22b6bec161873a0b5656d59c8ebe04e30.tar.gz chromium_src-85d252e22b6bec161873a0b5656d59c8ebe04e30.tar.bz2 |
Changing the translate back-end to use the Google Translate element.
When the user indicates that a page should be translated, the browser first fetches the Google Translate Element JS code.
It then sends it to the renderer, which injects the script in the page, waits for the Translate element to be initialized and then calls the translate method on it.
The TranslationService class previously used to translate text chunks is now unused and has been removed. Some of its static methods that are still used have been moved to the TranslateManager class.
This CL also implements the "revert" translation behavior.
BUG=35474,37778,35553,39375
TEST=Test the translation feature extensively.
Review URL: http://codereview.chromium.org/1599016
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@43768 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer/translate_helper_unittest.cc')
-rw-r--r-- | chrome/renderer/translate_helper_unittest.cc | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/chrome/renderer/translate_helper_unittest.cc b/chrome/renderer/translate_helper_unittest.cc new file mode 100644 index 0000000..c0f0e5e --- /dev/null +++ b/chrome/renderer/translate_helper_unittest.cc @@ -0,0 +1,169 @@ +// Copyright (c) 2010 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 "chrome/test/render_view_test.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" + +using testing::AtLeast; +using testing::Return; + +class TestTranslateHelper : public TranslateHelper { + public: + explicit TestTranslateHelper(RenderView* render_view) + : TranslateHelper(render_view) { + } + + virtual bool DontDelayTasks() { return true; } + + MOCK_METHOD0(IsTranslateLibAvailable, bool()); + MOCK_METHOD0(IsTranslateLibReady, bool()); + MOCK_METHOD0(HasTranslationFinished, bool()); + MOCK_METHOD0(HasTranslationFailed, bool()); + MOCK_METHOD2(StartTranslation, bool(const std::string& source_lang, + const std::string& target_lang)); + + private: + DISALLOW_COPY_AND_ASSIGN(TestTranslateHelper); +}; + +class TranslateHelperTest : public RenderViewTest { + public: + TranslateHelperTest() {} + + protected: + virtual void SetUp() { + RenderViewTest::SetUp(); + translate_helper_.reset(new TestTranslateHelper(view_)); + } + + bool GetPageTranslatedMessage(int* page_id, + std::string* original_lang, + std::string* target_lang, + TranslateErrors::Type* error) { + const IPC::Message* message = render_thread_.sink(). + GetUniqueMessageMatching(ViewHostMsg_PageTranslated::ID); + if (!message) + return false; + Tuple4<int, std::string, std::string, TranslateErrors::Type> + translate_param; + ViewHostMsg_PageTranslated::Read(message, &translate_param); + if (page_id) + *page_id = translate_param.a; + if (original_lang) + *original_lang = translate_param.b; + if (target_lang) + *target_lang = translate_param.c; + if (error) + *error = translate_param.d; + return true; + } + + scoped_ptr<TestTranslateHelper> translate_helper_; +}; + +// Tests that the browser gets notified of the translation failure if the +// translate library fails/times-out during initialization. +TEST_F(TranslateHelperTest, TranslateLibNeverReady) { + // We make IsTranslateLibAvailable true so we don't attempt to inject the + // library. + EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable()) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + + EXPECT_CALL(*translate_helper_, IsTranslateLibReady()) + .Times(AtLeast(5)) // See kMaxTranslateInitCheckAttempts in + // translate_helper.cc + .WillRepeatedly(Return(false)); + + translate_helper_->TranslatePage(view_->page_id(), "en", "fr", std::string()); + MessageLoop::current()->RunAllPending(); + + int page_id; + TranslateErrors::Type error; + ASSERT_TRUE(GetPageTranslatedMessage(&page_id, NULL, NULL, &error)); + EXPECT_EQ(view_->page_id(), page_id); + EXPECT_EQ(TranslateErrors::INITIALIZATION_ERROR, error); +} + +// Tests that the browser gets notified of the translation success when the +// translation succeeds. +TEST_F(TranslateHelperTest, TranslateSuccess) { + // We make IsTranslateLibAvailable true so we don't attempt to inject the + // library. + EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable()) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + + EXPECT_CALL(*translate_helper_, IsTranslateLibReady()) + .WillOnce(Return(false)) + .WillOnce(Return(true)); + + EXPECT_CALL(*translate_helper_, StartTranslation("en", "fr")) + .WillOnce(Return(true)); + + // Succeed after few checks. + EXPECT_CALL(*translate_helper_, HasTranslationFailed()) + .WillRepeatedly(Return(false)); + EXPECT_CALL(*translate_helper_, HasTranslationFinished()) + .WillOnce(Return(false)) + .WillOnce(Return(false)) + .WillOnce(Return(true)); + + std::string original_lang("en"); + std::string target_lang("fr"); + translate_helper_->TranslatePage(view_->page_id(), original_lang, target_lang, + std::string()); + MessageLoop::current()->RunAllPending(); + + int page_id; + std::string received_original_lang; + std::string received_target_lang; + TranslateErrors::Type error; + ASSERT_TRUE(GetPageTranslatedMessage(&page_id, + &received_original_lang, + &received_target_lang, + &error)); + EXPECT_EQ(view_->page_id(), page_id); + EXPECT_EQ(original_lang, received_original_lang); + EXPECT_EQ(target_lang, received_target_lang); + EXPECT_EQ(TranslateErrors::NONE, error); +} + +// Tests that the browser gets notified of the translation failure when the +// translation fails. +TEST_F(TranslateHelperTest, TranslateFailure) { + // We make IsTranslateLibAvailable true so we don't attempt to inject the + // library. + EXPECT_CALL(*translate_helper_, IsTranslateLibAvailable()) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + + EXPECT_CALL(*translate_helper_, IsTranslateLibReady()) + .WillOnce(Return(true)); + + EXPECT_CALL(*translate_helper_, StartTranslation("en", "fr")) + .WillOnce(Return(true)); + + // Fail after few checks. + EXPECT_CALL(*translate_helper_, HasTranslationFailed()) + .WillOnce(Return(false)) + .WillOnce(Return(false)) + .WillOnce(Return(false)) + .WillOnce(Return(true)); + + EXPECT_CALL(*translate_helper_, HasTranslationFinished()) + .Times(AtLeast(1)) + .WillRepeatedly(Return(false)); + + translate_helper_->TranslatePage(view_->page_id(), "en", "fr", std::string()); + MessageLoop::current()->RunAllPending(); + + int page_id; + TranslateErrors::Type error; + ASSERT_TRUE(GetPageTranslatedMessage(&page_id, NULL, NULL, &error)); + EXPECT_EQ(view_->page_id(), page_id); + EXPECT_EQ(TranslateErrors::TRANSLATION_ERROR, error); +} |