Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. | |
|
groby-ooo-7-16
2017/05/31 19:51:44
Why did you name this interactive_uitest instead o
Gaja
2017/06/01 12:24:02
I noticed that few bubble_view/icon_view related t
| |
| 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/macros.h" | |
| 8 #include "chrome/app/chrome_command_ids.h" | |
| 9 #include "chrome/browser/chrome_notification_types.h" | |
| 10 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti l.h" | |
| 11 #include "chrome/browser/translate/chrome_translate_client.h" | |
| 12 #include "chrome/browser/translate/translate_service.h" | |
| 13 #include "chrome/browser/ui/browser_command_controller.h" | |
| 14 #include "chrome/browser/ui/browser_window.h" | |
| 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 16 #include "chrome/browser/ui/translate/translate_bubble_test_utils.h" | |
| 17 #include "chrome/test/base/in_process_browser_test.h" | |
| 18 #include "chrome/test/base/interactive_test_utils.h" | |
| 19 #include "chrome/test/base/ui_test_utils.h" | |
| 20 #include "components/translate/core/browser/translate_manager.h" | |
| 21 #include "content/public/browser/notification_service.h" | |
| 22 #include "content/public/browser/web_contents.h" | |
| 23 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 24 #include "net/url_request/test_url_fetcher_factory.h" | |
| 25 #include "net/url_request/url_request_status.h" | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 static const char kDataURIPrefix[] = "data:text/html;charset=utf-8,"; | |
| 30 static const char kTestLanguageString[] = | |
| 31 "我々は重要な、興味深いものになるが、時折状況が発生するため苦労や痛みは" | |
| 32 "彼にいくつかの素晴らしいを調達することができます。それから、いくつかの利"; | |
| 33 } | |
| 34 | |
| 35 class TranslateBubbleViewInteractiveTest : public InProcessBrowserTest { | |
| 36 public: | |
| 37 TranslateBubbleViewInteractiveTest() {} | |
| 38 ~TranslateBubbleViewInteractiveTest() override {} | |
| 39 void SetUpOnMainThread() override { | |
| 40 InProcessBrowserTest::SetUpOnMainThread(); | |
| 41 } | |
| 42 | |
| 43 content::WebContents* GetWebContents() { | |
| 44 return browser()->tab_strip_model()->GetActiveWebContents(); | |
| 45 } | |
| 46 | |
| 47 void SimulateURLFetch(bool success) { | |
|
groby-ooo-7-16
2017/05/31 19:51:44
It's probably worth collecting those test function
Gaja
2017/06/01 12:24:02
OK, I will try moving it to separate file.
Yes, mo
| |
| 48 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | |
| 49 ASSERT_TRUE(fetcher); | |
| 50 net::Error error = success ? net::OK : net::ERR_FAILED; | |
| 51 | |
| 52 std::string script = | |
| 53 " var google = {};" | |
| 54 "google.translate = (function() {" | |
| 55 " return {" | |
| 56 " TranslateService: function() {" | |
| 57 " return {" | |
| 58 " isAvailable : function() {" | |
| 59 " return true;" | |
| 60 " }," | |
| 61 " restore : function() {" | |
| 62 " return;" | |
| 63 " }," | |
| 64 " getDetectedLanguage : function() {" | |
| 65 " return \"ja\";" | |
| 66 " }," | |
| 67 " translatePage : function(originalLang, targetLang," | |
| 68 " onTranslateProgress) {" | |
| 69 " document.getElementsByTagName(\"body\")[0].innerHTML = '" + | |
| 70 std::string(kTestLanguageString) + | |
| 71 " ';" | |
| 72 " onTranslateProgress(100, true, false);" | |
| 73 " }" | |
| 74 " };" | |
| 75 " }" | |
| 76 " };" | |
| 77 "})();" | |
| 78 "cr.googleTranslate.onTranslateElementLoad();"; | |
| 79 | |
| 80 fetcher->set_url(fetcher->GetOriginalURL()); | |
| 81 fetcher->set_status(net::URLRequestStatus::FromError(error)); | |
| 82 fetcher->set_response_code(success ? 200 : 500); | |
| 83 fetcher->SetResponseString(script); | |
| 84 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 85 } | |
| 86 | |
| 87 private: | |
| 88 net::TestURLFetcherFactory url_fetcher_factory_; | |
| 89 | |
| 90 DISALLOW_COPY_AND_ASSIGN(TranslateBubbleViewInteractiveTest); | |
| 91 }; | |
| 92 | |
| 93 IN_PROC_BROWSER_TEST_F(TranslateBubbleViewInteractiveTest, TranslateSuccess) { | |
| 94 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 95 ASSERT_TRUE(TranslateService::IsTranslateBubbleEnabled()); | |
| 96 | |
| 97 translate::TranslateManager::SetIgnoreMissingKeyForTesting(true); | |
|
groby-ooo-7-16
2017/05/31 19:51:44
You probably want to do that in the SetUp code - i
Gaja
2017/06/01 12:24:02
Acknowledged.
| |
| 98 | |
| 99 GURL url( | |
| 100 std::string(kDataURIPrefix) + | |
| 101 // Add Japanese characters to ensure the translate bar | |
| 102 // will appear. | |
| 103 "我々は重要な、興味深いものになるが、時折状況が発生するため苦労や痛みは" | |
| 104 "彼にいくつかの素晴らしいを調達することができます。それから、いくつかの" | |
| 105 "利"); | |
| 106 | |
| 107 // Set up an observer to be able to wait for the bubble to be shown. | |
|
groby-ooo-7-16
2017/05/31 19:51:44
Since this is shared across tests, might belong on
Gaja
2017/06/01 12:24:02
Acknowledged.
| |
| 108 content::Source<content::WebContents> source(GetWebContents()); | |
| 109 content::WindowedNotificationObserver language_detected_signal( | |
| 110 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, source); | |
| 111 | |
| 112 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), url)); | |
| 113 | |
| 114 // Wait for the translate bubble to appear. | |
| 115 language_detected_signal.Wait(); | |
| 116 | |
| 117 // Verify current translate step. | |
| 118 const TranslateBubbleModel* model = | |
| 119 translate::test_utils::GetCurrentModel(browser()); | |
| 120 ASSERT_NE(nullptr, model); | |
| 121 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE, | |
| 122 model->GetViewState()); | |
| 123 | |
| 124 translate::test_utils::PressTranslate(browser()); | |
| 125 | |
| 126 // Verify current translate step after pressing translate button. | |
| 127 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, | |
| 128 model->GetViewState()); | |
| 129 | |
| 130 // Wait for translation. | |
| 131 content::WindowedNotificationObserver translation_observer( | |
|
groby-ooo-7-16
2017/05/31 19:51:44
This is probably sharable too
Gaja
2017/06/01 12:24:02
Acknowledged.
| |
| 132 chrome::NOTIFICATION_PAGE_TRANSLATED, | |
| 133 content::NotificationService::AllSources()); | |
| 134 | |
| 135 // Simulate the translate script being retrieved. | |
| 136 // Pass fake google.translate lib as the translate script. | |
| 137 SimulateURLFetch(true); | |
| 138 | |
| 139 // Simulate the render notifying the translation has been done. | |
| 140 translation_observer.Wait(); | |
| 141 | |
| 142 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE, | |
| 143 model->GetViewState()); | |
| 144 } | |
| 145 | |
| 146 IN_PROC_BROWSER_TEST_F(TranslateBubbleViewInteractiveTest, TranslateError) { | |
| 147 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 148 ASSERT_TRUE(TranslateService::IsTranslateBubbleEnabled()); | |
| 149 | |
| 150 translate::TranslateManager::SetIgnoreMissingKeyForTesting(true); | |
| 151 | |
| 152 // Set up an observer to be able to wait for the bubble to be shown. | |
| 153 content::Source<content::WebContents> source(GetWebContents()); | |
| 154 content::WindowedNotificationObserver language_detected_signal( | |
| 155 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, source); | |
| 156 | |
| 157 ASSERT_NO_FATAL_FAILURE( | |
| 158 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"))); | |
| 159 | |
| 160 // Wait for the language detection. | |
| 161 language_detected_signal.Wait(); | |
| 162 | |
| 163 content::ContextMenuParams params; | |
|
groby-ooo-7-16
2017/05/31 19:51:44
You might want to use translate::test_utils::Press
Gaja
2017/06/01 12:24:02
As I observed, for about:blank case, the translate
| |
| 164 TestRenderViewContextMenu menu(GetWebContents()->GetMainFrame(), params); | |
| 165 menu.Init(); | |
| 166 menu.ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); | |
| 167 | |
| 168 // Verify current translate step. | |
| 169 const TranslateBubbleModel* model = | |
| 170 translate::test_utils::GetCurrentModel(browser()); | |
| 171 ASSERT_NE(nullptr, model); | |
| 172 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, | |
| 173 model->GetViewState()); | |
| 174 | |
| 175 // Wait for translation. | |
| 176 content::WindowedNotificationObserver translation_observer( | |
| 177 chrome::NOTIFICATION_PAGE_TRANSLATED, | |
| 178 content::NotificationService::AllSources()); | |
| 179 | |
| 180 // Simulate the translate script being retrieved. | |
| 181 // Pass fake google.translate lib as the translate script. | |
| 182 SimulateURLFetch(true); | |
| 183 | |
| 184 // Simulate the render notifying the translation has been done. | |
| 185 translation_observer.Wait(); | |
| 186 | |
| 187 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_ERROR, model->GetViewState()); | |
| 188 } | |
| OLD | NEW |