Chromium Code Reviews| Index: chrome/browser/translate/translate_bubble_browsertest.cc |
| diff --git a/chrome/browser/translate/translate_bubble_browsertest.cc b/chrome/browser/translate/translate_bubble_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..37beb7f19324f8ad4fa2df3bd711cc8af073396d |
| --- /dev/null |
| +++ b/chrome/browser/translate/translate_bubble_browsertest.cc |
| @@ -0,0 +1,273 @@ |
| +// Copyright 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 <string> |
| + |
| +#include "base/command_line.h" |
| +#include "chrome/app/chrome_command_ids.h" |
| +#include "chrome/browser/chrome_notification_types.h" |
| +#include "chrome/browser/tab_contents/render_view_context_menu.h" |
| +#include "chrome/browser/translate/page_translated_details.h" |
| +#include "chrome/browser/translate/translate_manager.h" |
| +#include "chrome/browser/translate/translate_script.h" |
| +#include "chrome/browser/translate/translate_tab_helper.h" |
| +#include "chrome/browser/ui/browser_commands.h" |
| +#include "chrome/browser/ui/browser_window.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "chrome/browser/ui/translate/translate_bubble_model.h" |
| +#include "chrome/common/chrome_switches.h" |
| +#include "chrome/common/render_messages.h" |
| +#include "chrome/common/translate/language_detection_details.h" |
| +#include "chrome/test/base/in_process_browser_test.h" |
| +#include "chrome/test/base/ui_test_utils.h" |
| +#include "content/public/browser/navigation_entry.h" |
| +#include "content/public/test/browser_test_utils.h" |
| +#include "net/http/http_status_code.h" |
| +#include "net/test/embedded_test_server/embedded_test_server.h" |
| +#include "net/url_request/test_url_fetcher_factory.h" |
| +#include "net/url_request/url_fetcher_delegate.h" |
| + |
| +namespace { |
| + |
| +const char kFrenchPageFilePath[] = "translate/fr_test.html"; |
| +const char kUnknownLanguagePageFilePath[] = "translate/und_test.html"; |
| + |
| +std::string GetTranslateScript() { |
| + return "var google = {\n" |
| + " 'translate': {\n" |
| + " 'TranslateService': function() {\n" |
| + " return {\n" |
| + " isAvailable: function() { return true; },\n" |
| + " translatePage: function(sl, tl, cb) { cb(1, true); },\n" |
| + " };\n" |
| + " }\n" |
| + " }\n" |
| + "};\n" |
| + "cr.googleTranslate.onTranslateElementLoad();\n"; |
| +} |
| + |
| +class TestRenderViewContextMenu : public RenderViewContextMenu { |
| + public: |
| + static TestRenderViewContextMenu* CreateContextMenu( |
| + content::WebContents* web_contents) { |
| + content::ContextMenuParams params; |
| + params.media_type = WebKit::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 // OS_MACOSX |
| + params.edit_flags = WebKit::WebContextMenuData::CanTranslate; |
| + return new TestRenderViewContextMenu(web_contents, params); |
| + } |
| + |
| + bool IsItemPresent(int id) { |
| + return menu_model_.GetIndexOfCommandId(id) != -1; |
| + } |
| + |
| + virtual void PlatformInit() OVERRIDE { } |
| + virtual void PlatformCancel() OVERRIDE { } |
| + virtual bool GetAcceleratorForCommandId( |
| + int command_id, |
| + ui::Accelerator* accelerator) OVERRIDE { return false; } |
| + |
| + private: |
| + TestRenderViewContextMenu(content::WebContents* web_contents, |
| + const content::ContextMenuParams& params) |
| + : RenderViewContextMenu(web_contents, params) { |
| + } |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu); |
| +}; |
| + |
| +void TranslateFromContextMenu(content::WebContents* web_contents) { |
| + scoped_ptr<TestRenderViewContextMenu> menu( |
| + TestRenderViewContextMenu::CreateContextMenu(web_contents)); |
| + menu->Init(); |
| + menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); |
| +} |
| + |
| +} // namespace |
| + |
| +class TranslateBubbleBrowserTest : public InProcessBrowserTest { |
| + public: |
| + virtual void SetUp() OVERRIDE { |
| + InProcessBrowserTest::SetUp(); |
| + ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| + } |
| + |
| + virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| + command_line->AppendSwitch(switches::kEnableTranslateNewUX); |
| + } |
| + |
| + protected: |
| + void SimulateFetchingTranslateScript(bool success) { |
| + net::TestURLFetcher* fetcher = |
| + url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId); |
| + ASSERT_TRUE(fetcher != NULL); |
| + net::URLRequestStatus status; |
| + status.set_status(success ? |
| + net::URLRequestStatus::SUCCESS : |
| + net::URLRequestStatus::FAILED); |
| + fetcher->set_status(status); |
| + fetcher->set_url(fetcher->GetOriginalURL()); |
| + fetcher->set_response_code(success ? |
| + net::HTTP_OK : |
| + net::HTTP_INTERNAL_SERVER_ERROR); |
| + fetcher->SetResponseString(GetTranslateScript()); |
| + fetcher->delegate()->OnURLFetchComplete(fetcher); |
| + } |
| + |
| + void WaitForTranslateManagerProcess() { |
| + base::MessageLoop::current()->RunUntilIdle(); |
|
sky
2013/10/18 14:31:10
How do you know this is good enough for the transl
hajimehoshi
2013/10/21 10:29:12
From TranslateManagerBrowserTest. I'll add a comme
|
| + } |
| + |
| + private: |
| + net::TestURLFetcherFactory url_fetcher_factory_; |
| +}; |
|
sky
2013/10/18 14:31:10
DISALLOW...
hajimehoshi
2013/10/21 10:29:12
Done.
|
| + |
| +#if defined(TOOLKIT_VIEWS) |
| + |
| +IN_PROC_BROWSER_TEST_F(TranslateBubbleBrowserTest, TranslateSucceeded) { |
|
sky
2013/10/18 14:31:10
Is it possible to write these as unit tests?
hajimehoshi
2013/10/18 15:19:59
I tried to write a unit test, but I think this is
hajimehoshi
2013/10/22 10:30:25
I moved these tests to translate_manager_browserte
|
| + content::WebContents* web_contents = |
| + browser()->tab_strip_model()->GetActiveWebContents(); |
| + content::Source<content::WebContents> source(web_contents); |
| + |
| + // Start loading a French page. |
| + ui_test_utils::WindowedNotificationObserverWithDetails< |
| + LanguageDetectionDetails> |
| + language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| + source); |
| + GURL french_url = ui_test_utils::GetTestUrl( |
| + base::FilePath(), base::FilePath(FILE_PATH_LITERAL(kFrenchPageFilePath))); |
| + ui_test_utils::NavigateToURL(browser(), french_url); |
| + language_detected_signal.Wait(); |
| + |
| + WaitForTranslateManagerProcess(); |
| + |
| + // Check the bubble's initial state. |
| + TranslateBubbleModel* bubble = |
| + browser()->window()->GetBrowserWindowTesting()->GetTranslateBubbleModel(); |
| + ASSERT_TRUE(bubble != NULL); |
| + EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE, |
| + bubble->GetViewState()); |
| + |
| + // Start translating the page. |
| + ui_test_utils::WindowedNotificationObserverWithDetails< |
| + PageTranslatedDetails> |
|
sky
2013/10/18 14:31:10
nit: indent 2 more.
hajimehoshi
2013/10/21 10:29:12
Done. (by template 'using')
|
| + page_translated_signal(chrome::NOTIFICATION_PAGE_TRANSLATED, source); |
| + bubble->Translate(); |
| + |
| + // Check the bubble during translating. |
| + EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, |
| + bubble->GetViewState()); |
| + |
| + SimulateFetchingTranslateScript(true); |
| + |
| + // Wait for translation completed. |
| + page_translated_signal.Wait(); |
| + |
| + WaitForTranslateManagerProcess(); |
| + |
| + // Check the bubble after translate. |
| + EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE, |
| + bubble->GetViewState()); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(TranslateBubbleBrowserTest, ScriptUnavailable) { |
| + content::WebContents* web_contents = |
| + browser()->tab_strip_model()->GetActiveWebContents(); |
| + content::Source<content::WebContents> source(web_contents); |
| + |
| + // Start loading a French page. |
| + ui_test_utils::WindowedNotificationObserverWithDetails< |
| + LanguageDetectionDetails> |
| + language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| + source); |
| + GURL french_url = ui_test_utils::GetTestUrl( |
| + base::FilePath(), base::FilePath(FILE_PATH_LITERAL(kFrenchPageFilePath))); |
| + ui_test_utils::NavigateToURL(browser(), french_url); |
| + language_detected_signal.Wait(); |
| + |
| + WaitForTranslateManagerProcess(); |
| + |
| + // Check the bubble's initial state. |
| + TranslateBubbleModel* bubble = |
| + browser()->window()->GetBrowserWindowTesting()->GetTranslateBubbleModel(); |
| + ASSERT_TRUE(bubble != NULL); |
| + EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE, |
| + bubble->GetViewState()); |
| + |
| + // Start translating the page. |
| + bubble->Translate(); |
| + |
| + // Check the bubble during translating. |
| + EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, |
| + bubble->GetViewState()); |
| + |
| + SimulateFetchingTranslateScript(false); |
| + |
| + WaitForTranslateManagerProcess(); |
| + |
| + // Check the error bubble. |
| + EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_ERROR, |
| + bubble->GetViewState()); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(TranslateBubbleBrowserTest, UnknownLanguage) { |
| + content::WebContents* web_contents = |
| + browser()->tab_strip_model()->GetActiveWebContents(); |
| + content::Source<content::WebContents> source(web_contents); |
| + |
| + // Start loading a French page. |
| + ui_test_utils::WindowedNotificationObserverWithDetails< |
| + LanguageDetectionDetails> |
| + language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| + source); |
| + GURL und_url = ui_test_utils::GetTestUrl( |
| + base::FilePath(), |
| + base::FilePath(FILE_PATH_LITERAL(kUnknownLanguagePageFilePath))); |
| + ui_test_utils::NavigateToURL(browser(), und_url); |
| + language_detected_signal.Wait(); |
| + |
| + WaitForTranslateManagerProcess(); |
| + |
| + // Check that the bubble is not shown. |
| + TranslateBubbleModel* bubble = |
| + browser()->window()->GetBrowserWindowTesting()->GetTranslateBubbleModel(); |
| + ASSERT_TRUE(bubble == NULL); |
| + |
| + // Start translating the page from the context menu. |
| + ui_test_utils::WindowedNotificationObserverWithDetails< |
| + PageTranslatedDetails> |
| + page_translated_signal(chrome::NOTIFICATION_PAGE_TRANSLATED, source); |
| + TranslateFromContextMenu(web_contents); |
| + |
| + // Check the bubble during translating. |
| + bubble = |
| + browser()->window()->GetBrowserWindowTesting()->GetTranslateBubbleModel(); |
| + EXPECT_TRUE(bubble != NULL); |
| + EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, |
| + bubble->GetViewState()); |
| + |
| + SimulateFetchingTranslateScript(true); |
| + |
| + // Wait for translation completed. |
| + page_translated_signal.Wait(); |
| + |
| + WaitForTranslateManagerProcess(); |
| + |
| + // Check the bubble after translate. |
| + EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE, |
| + bubble->GetViewState()); |
| +} |
| + |
| +#endif // defined(TOOLKIT_VIEWS) |