Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string> | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "chrome/app/chrome_command_ids.h" | |
| 9 #include "chrome/browser/chrome_notification_types.h" | |
| 10 #include "chrome/browser/tab_contents/render_view_context_menu.h" | |
| 11 #include "chrome/browser/translate/page_translated_details.h" | |
| 12 #include "chrome/browser/translate/translate_manager.h" | |
| 13 #include "chrome/browser/translate/translate_script.h" | |
| 14 #include "chrome/browser/translate/translate_tab_helper.h" | |
| 15 #include "chrome/browser/ui/browser_commands.h" | |
| 16 #include "chrome/browser/ui/browser_window.h" | |
| 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 18 #include "chrome/browser/ui/translate/translate_bubble_model.h" | |
| 19 #include "chrome/common/chrome_switches.h" | |
| 20 #include "chrome/common/render_messages.h" | |
| 21 #include "chrome/common/translate/language_detection_details.h" | |
| 22 #include "chrome/test/base/in_process_browser_test.h" | |
| 23 #include "chrome/test/base/ui_test_utils.h" | |
| 24 #include "content/public/browser/navigation_entry.h" | |
| 25 #include "content/public/test/browser_test_utils.h" | |
| 26 #include "net/http/http_status_code.h" | |
| 27 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 28 #include "net/url_request/test_url_fetcher_factory.h" | |
| 29 #include "net/url_request/url_fetcher_delegate.h" | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 const char kFrenchPageFilePath[] = "translate/fr_test.html"; | |
| 34 const char kUnknownLanguagePageFilePath[] = "translate/und_test.html"; | |
| 35 | |
| 36 std::string GetTranslateScript() { | |
| 37 return "var google = {\n" | |
| 38 " 'translate': {\n" | |
| 39 " 'TranslateService': function() {\n" | |
| 40 " return {\n" | |
| 41 " isAvailable: function() { return true; },\n" | |
| 42 " translatePage: function(sl, tl, cb) { cb(1, true); },\n" | |
| 43 " };\n" | |
| 44 " }\n" | |
| 45 " }\n" | |
| 46 "};\n" | |
| 47 "cr.googleTranslate.onTranslateElementLoad();\n"; | |
| 48 } | |
| 49 | |
| 50 class TestRenderViewContextMenu : public RenderViewContextMenu { | |
| 51 public: | |
| 52 static TestRenderViewContextMenu* CreateContextMenu( | |
| 53 content::WebContents* web_contents) { | |
| 54 content::ContextMenuParams params; | |
| 55 params.media_type = WebKit::WebContextMenuData::MediaTypeNone; | |
| 56 params.x = 0; | |
| 57 params.y = 0; | |
| 58 params.has_image_contents = true; | |
| 59 params.media_flags = 0; | |
| 60 params.spellcheck_enabled = false; | |
| 61 params.is_editable = false; | |
| 62 params.page_url = web_contents->GetController().GetActiveEntry()->GetURL(); | |
| 63 #if defined(OS_MACOSX) | |
| 64 params.writing_direction_default = 0; | |
| 65 params.writing_direction_left_to_right = 0; | |
| 66 params.writing_direction_right_to_left = 0; | |
| 67 #endif // OS_MACOSX | |
| 68 params.edit_flags = WebKit::WebContextMenuData::CanTranslate; | |
| 69 return new TestRenderViewContextMenu(web_contents, params); | |
| 70 } | |
| 71 | |
| 72 bool IsItemPresent(int id) { | |
| 73 return menu_model_.GetIndexOfCommandId(id) != -1; | |
| 74 } | |
| 75 | |
| 76 virtual void PlatformInit() OVERRIDE { } | |
| 77 virtual void PlatformCancel() OVERRIDE { } | |
| 78 virtual bool GetAcceleratorForCommandId( | |
| 79 int command_id, | |
| 80 ui::Accelerator* accelerator) OVERRIDE { return false; } | |
| 81 | |
| 82 private: | |
| 83 TestRenderViewContextMenu(content::WebContents* web_contents, | |
| 84 const content::ContextMenuParams& params) | |
| 85 : RenderViewContextMenu(web_contents, params) { | |
| 86 } | |
| 87 | |
| 88 DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu); | |
| 89 }; | |
| 90 | |
| 91 void TranslateFromContextMenu(content::WebContents* web_contents) { | |
| 92 scoped_ptr<TestRenderViewContextMenu> menu( | |
| 93 TestRenderViewContextMenu::CreateContextMenu(web_contents)); | |
| 94 menu->Init(); | |
| 95 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); | |
| 96 } | |
| 97 | |
| 98 } // namespace | |
| 99 | |
| 100 class TranslateBubbleBrowserTest : public InProcessBrowserTest { | |
| 101 public: | |
| 102 virtual void SetUp() OVERRIDE { | |
| 103 InProcessBrowserTest::SetUp(); | |
| 104 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
| 105 } | |
| 106 | |
| 107 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
| 108 command_line->AppendSwitch(switches::kEnableTranslateNewUX); | |
| 109 } | |
| 110 | |
| 111 protected: | |
| 112 void SimulateFetchingTranslateScript(bool success) { | |
| 113 net::TestURLFetcher* fetcher = | |
| 114 url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId); | |
| 115 ASSERT_TRUE(fetcher != NULL); | |
| 116 net::URLRequestStatus status; | |
| 117 status.set_status(success ? | |
| 118 net::URLRequestStatus::SUCCESS : | |
| 119 net::URLRequestStatus::FAILED); | |
| 120 fetcher->set_status(status); | |
| 121 fetcher->set_url(fetcher->GetOriginalURL()); | |
| 122 fetcher->set_response_code(success ? | |
| 123 net::HTTP_OK : | |
| 124 net::HTTP_INTERNAL_SERVER_ERROR); | |
| 125 fetcher->SetResponseString(GetTranslateScript()); | |
| 126 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 127 } | |
| 128 | |
| 129 void WaitForTranslateManagerProcess() { | |
| 130 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
| |
| 131 } | |
| 132 | |
| 133 private: | |
| 134 net::TestURLFetcherFactory url_fetcher_factory_; | |
| 135 }; | |
|
sky
2013/10/18 14:31:10
DISALLOW...
hajimehoshi
2013/10/21 10:29:12
Done.
| |
| 136 | |
| 137 #if defined(TOOLKIT_VIEWS) | |
| 138 | |
| 139 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
| |
| 140 content::WebContents* web_contents = | |
| 141 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 142 content::Source<content::WebContents> source(web_contents); | |
| 143 | |
| 144 // Start loading a French page. | |
| 145 ui_test_utils::WindowedNotificationObserverWithDetails< | |
| 146 LanguageDetectionDetails> | |
| 147 language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | |
| 148 source); | |
| 149 GURL french_url = ui_test_utils::GetTestUrl( | |
| 150 base::FilePath(), base::FilePath(FILE_PATH_LITERAL(kFrenchPageFilePath))); | |
| 151 ui_test_utils::NavigateToURL(browser(), french_url); | |
| 152 language_detected_signal.Wait(); | |
| 153 | |
| 154 WaitForTranslateManagerProcess(); | |
| 155 | |
| 156 // Check the bubble's initial state. | |
| 157 TranslateBubbleModel* bubble = | |
| 158 browser()->window()->GetBrowserWindowTesting()->GetTranslateBubbleModel(); | |
| 159 ASSERT_TRUE(bubble != NULL); | |
| 160 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE, | |
| 161 bubble->GetViewState()); | |
| 162 | |
| 163 // Start translating the page. | |
| 164 ui_test_utils::WindowedNotificationObserverWithDetails< | |
| 165 PageTranslatedDetails> | |
|
sky
2013/10/18 14:31:10
nit: indent 2 more.
hajimehoshi
2013/10/21 10:29:12
Done. (by template 'using')
| |
| 166 page_translated_signal(chrome::NOTIFICATION_PAGE_TRANSLATED, source); | |
| 167 bubble->Translate(); | |
| 168 | |
| 169 // Check the bubble during translating. | |
| 170 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, | |
| 171 bubble->GetViewState()); | |
| 172 | |
| 173 SimulateFetchingTranslateScript(true); | |
| 174 | |
| 175 // Wait for translation completed. | |
| 176 page_translated_signal.Wait(); | |
| 177 | |
| 178 WaitForTranslateManagerProcess(); | |
| 179 | |
| 180 // Check the bubble after translate. | |
| 181 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE, | |
| 182 bubble->GetViewState()); | |
| 183 } | |
| 184 | |
| 185 IN_PROC_BROWSER_TEST_F(TranslateBubbleBrowserTest, ScriptUnavailable) { | |
| 186 content::WebContents* web_contents = | |
| 187 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 188 content::Source<content::WebContents> source(web_contents); | |
| 189 | |
| 190 // Start loading a French page. | |
| 191 ui_test_utils::WindowedNotificationObserverWithDetails< | |
| 192 LanguageDetectionDetails> | |
| 193 language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | |
| 194 source); | |
| 195 GURL french_url = ui_test_utils::GetTestUrl( | |
| 196 base::FilePath(), base::FilePath(FILE_PATH_LITERAL(kFrenchPageFilePath))); | |
| 197 ui_test_utils::NavigateToURL(browser(), french_url); | |
| 198 language_detected_signal.Wait(); | |
| 199 | |
| 200 WaitForTranslateManagerProcess(); | |
| 201 | |
| 202 // Check the bubble's initial state. | |
| 203 TranslateBubbleModel* bubble = | |
| 204 browser()->window()->GetBrowserWindowTesting()->GetTranslateBubbleModel(); | |
| 205 ASSERT_TRUE(bubble != NULL); | |
| 206 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE, | |
| 207 bubble->GetViewState()); | |
| 208 | |
| 209 // Start translating the page. | |
| 210 bubble->Translate(); | |
| 211 | |
| 212 // Check the bubble during translating. | |
| 213 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, | |
| 214 bubble->GetViewState()); | |
| 215 | |
| 216 SimulateFetchingTranslateScript(false); | |
| 217 | |
| 218 WaitForTranslateManagerProcess(); | |
| 219 | |
| 220 // Check the error bubble. | |
| 221 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_ERROR, | |
| 222 bubble->GetViewState()); | |
| 223 } | |
| 224 | |
| 225 IN_PROC_BROWSER_TEST_F(TranslateBubbleBrowserTest, UnknownLanguage) { | |
| 226 content::WebContents* web_contents = | |
| 227 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 228 content::Source<content::WebContents> source(web_contents); | |
| 229 | |
| 230 // Start loading a French page. | |
| 231 ui_test_utils::WindowedNotificationObserverWithDetails< | |
| 232 LanguageDetectionDetails> | |
| 233 language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | |
| 234 source); | |
| 235 GURL und_url = ui_test_utils::GetTestUrl( | |
| 236 base::FilePath(), | |
| 237 base::FilePath(FILE_PATH_LITERAL(kUnknownLanguagePageFilePath))); | |
| 238 ui_test_utils::NavigateToURL(browser(), und_url); | |
| 239 language_detected_signal.Wait(); | |
| 240 | |
| 241 WaitForTranslateManagerProcess(); | |
| 242 | |
| 243 // Check that the bubble is not shown. | |
| 244 TranslateBubbleModel* bubble = | |
| 245 browser()->window()->GetBrowserWindowTesting()->GetTranslateBubbleModel(); | |
| 246 ASSERT_TRUE(bubble == NULL); | |
| 247 | |
| 248 // Start translating the page from the context menu. | |
| 249 ui_test_utils::WindowedNotificationObserverWithDetails< | |
| 250 PageTranslatedDetails> | |
| 251 page_translated_signal(chrome::NOTIFICATION_PAGE_TRANSLATED, source); | |
| 252 TranslateFromContextMenu(web_contents); | |
| 253 | |
| 254 // Check the bubble during translating. | |
| 255 bubble = | |
| 256 browser()->window()->GetBrowserWindowTesting()->GetTranslateBubbleModel(); | |
| 257 EXPECT_TRUE(bubble != NULL); | |
| 258 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, | |
| 259 bubble->GetViewState()); | |
| 260 | |
| 261 SimulateFetchingTranslateScript(true); | |
| 262 | |
| 263 // Wait for translation completed. | |
| 264 page_translated_signal.Wait(); | |
| 265 | |
| 266 WaitForTranslateManagerProcess(); | |
| 267 | |
| 268 // Check the bubble after translate. | |
| 269 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE, | |
| 270 bubble->GetViewState()); | |
| 271 } | |
| 272 | |
| 273 #endif // defined(TOOLKIT_VIEWS) | |
| OLD | NEW |