diff options
author | jcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-12 21:53:55 +0000 |
---|---|---|
committer | jcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-12 21:53:55 +0000 |
commit | 3b1c18191d41eac0a551b3009269a259757ca070 (patch) | |
tree | 5f466b716abc81ac3bb67262522ba1af0d699588 /chrome/browser/translate/translate_manager_unittest.cc | |
parent | 7d9ad0b30c0775a7345e965294c9f86bb0d543e5 (diff) | |
download | chromium_src-3b1c18191d41eac0a551b3009269a259757ca070.zip chromium_src-3b1c18191d41eac0a551b3009269a259757ca070.tar.gz chromium_src-3b1c18191d41eac0a551b3009269a259757ca070.tar.bz2 |
Implements the auto-translate on click: if you have translated
a page and are navigating to a new page in the same language by
clicking a link, the new page is automatically translated.
In order to do that I moved the language state from the navigation
entry to some dedicated class that each TabContents owns.
Also added some basic unit-testing for good measure.
BUG=35477
TEST=See bug steps. Run unit-tests.
Review URL: http://codereview.chromium.org/596092
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@38961 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/translate/translate_manager_unittest.cc')
-rw-r--r-- | chrome/browser/translate/translate_manager_unittest.cc | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/chrome/browser/translate/translate_manager_unittest.cc b/chrome/browser/translate/translate_manager_unittest.cc new file mode 100644 index 0000000..75420c5 --- /dev/null +++ b/chrome/browser/translate/translate_manager_unittest.cc @@ -0,0 +1,142 @@ +// 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/browser/renderer_host/test/test_render_view_host.h" + +#include "chrome/browser/renderer_host/mock_render_process_host.h" +#include "chrome/browser/translate/translate_infobars_delegates.h" +#include "chrome/browser/translate/translate_manager.h" +#include "chrome/common/ipc_test_sink.h" +#include "chrome/common/render_messages.h" + +class TestTranslateManager : public TranslateManager { + public: + TestTranslateManager() {} + + protected: + virtual bool TestEnabled() { return true; } +}; + +class TranslateManagerTest : public RenderViewHostTestHarness { + public: + // Simluates navigating to a page and getting teh page contents and language + // for that navigation. + void SimulateNavigation(const GURL& url, int page_id, + const std::wstring& contents, + const std::string& lang) { + NavigateAndCommit(url); + rvh()->TestOnMessageReceived(ViewHostMsg_PageContents(0, url, page_id, + contents, lang)); + } + + bool GetTranslateMessage(int* page_id, + std::string* original_lang, + std::string* target_lang) { + const IPC::Message* message = + process()->sink().GetFirstMessageMatching(ViewMsg_TranslatePage::ID); + if (!message) + return false; + Tuple3<int, std::string, std::string> translate_param; + ViewMsg_TranslatePage::Read(message, &translate_param); + *page_id = translate_param.a; + *original_lang = translate_param.b; + *target_lang = translate_param.c; + return true; + } + + private: + TestTranslateManager translate_manager_; +}; + +TEST_F(TranslateManagerTest, NormalTranslate) { + // Simulate navigating to a page. + SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); + + // We should have an info-bar. + ASSERT_EQ(1, contents()->infobar_delegate_count()); + TranslateInfoBarDelegate* infobar = + contents()->GetInfoBarDelegateAt(0)->AsTranslateInfoBarDelegate(); + ASSERT_TRUE(infobar != NULL); + EXPECT_EQ(TranslateInfoBarDelegate::kBeforeTranslate, infobar->state()); + + // Simulate clicking translate. + process()->sink().ClearMessages(); + infobar->Translate(); + + // Test that we sent the right message to the renderer. + int page_id = 0; + std::string original_lang, target_lang; + EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); + EXPECT_EQ(0, page_id); + EXPECT_EQ("fr", original_lang); + EXPECT_EQ("en", target_lang); + + // The infobar should now be in the translating state. + ASSERT_EQ(1, contents()->infobar_delegate_count()); + ASSERT_EQ(infobar, contents()->GetInfoBarDelegateAt(0)); // Same instance. + // TODO(jcampan): the state is not set if the button is not clicked. + // Refactor the infobar code so we can simulate the click. + // EXPECT_EQ(TranslateInfoBarDelegate::kTranslating, infobar->state()); + + // Simulate the render notifying the translation has been done. + rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en")); + + // The infobar should have changed to the after state. + ASSERT_EQ(1, contents()->infobar_delegate_count()); + ASSERT_EQ(infobar, contents()->GetInfoBarDelegateAt(0)); + // TODO(jcampan): the TranslateInfoBar is listening for the PAGE_TRANSLATED + // notification. Since in unit-test, no actual info-bar is + // created, it does not get the notification and does not + // update its state. Ideally the delegate (or rather model) + // would be the one listening for notifications and updating + // states. That would make this test work. + // EXPECT_EQ(TranslateInfoBarDelegate::kAfterTranslate, infobar->state()); + + // Simulate translating again from there but 2 different languages. + infobar->ModifyOriginalLanguage(0); + infobar->ModifyTargetLanguage(1); + std::string new_original_lang = infobar->original_lang_code(); + std::string new_target_lang = infobar->target_lang_code(); + process()->sink().ClearMessages(); + infobar->Translate(); + + // Test that we sent the right message to the renderer. + EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); + EXPECT_EQ(0, page_id); + EXPECT_EQ(new_original_lang, original_lang); + EXPECT_EQ(new_target_lang, target_lang); +} + +// Test auto-translate on page. +TEST_F(TranslateManagerTest, AutoTranslateOnNavigate) { + // Simulate navigating to a page and gettings its language. + SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr"); + + // Simulate the user translating. + ASSERT_EQ(1, contents()->infobar_delegate_count()); + TranslateInfoBarDelegate* infobar = + contents()->GetInfoBarDelegateAt(0)->AsTranslateInfoBarDelegate(); + ASSERT_TRUE(infobar != NULL); + infobar->Translate(); + rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en")); + + // Now navigate to a new page in the same language. + process()->sink().ClearMessages(); + SimulateNavigation(GURL("http://news.google.fr"), 1, L"Les news", "fr"); + + // This should have automatically triggered a translation. + 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); + + // Now navigate to a page in a different language. + process()->sink().ClearMessages(); + SimulateNavigation(GURL("http://news.google.es"), 1, L"Las news", "es"); + + // This should not have triggered a translate. + EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); +} |