Index: chrome/browser/autofill/autofill_browsertest.cc |
diff --git a/chrome/browser/autofill/autofill_browsertest.cc b/chrome/browser/autofill/autofill_browsertest.cc |
index a14f5616277ef13285fd89d66240644d1c10dafa..d8a8c7f36c11ed5c56a1700197fd9843aea15eb4 100644 |
--- a/chrome/browser/autofill/autofill_browsertest.cc |
+++ b/chrome/browser/autofill/autofill_browsertest.cc |
@@ -4,6 +4,7 @@ |
#include <string> |
+#include "base/utf_string_conversions.h" |
#include "app/keyboard_code_conversion.h" |
#include "base/basictypes.h" |
#include "base/ref_counted.h" |
@@ -14,15 +15,55 @@ |
#include "chrome/browser/autofill/personal_data_manager.h" |
#include "chrome/browser/net/predictor_api.h" |
#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/renderer_host/mock_render_process_host.h" |
#include "chrome/browser/tab_contents/tab_contents.h" |
+#include "chrome/browser/translate/translate_infobar_delegate.h" |
+#include "chrome/browser/translate/translate_manager.h" |
#include "chrome/browser/ui/browser.h" |
#include "chrome/browser/ui/browser_window.h" |
+#include "chrome/browser/renderer_host/render_view_host.h" |
dhollowa
2010/12/08 17:33:41
This needs to be moved up.
|
+#include "chrome/common/net/test_url_fetcher_factory.h" |
#include "chrome/common/pref_names.h" |
+#include "chrome/common/render_messages.h" |
+#include "chrome/renderer/translate_helper.h" |
#include "chrome/test/in_process_browser_test.h" |
#include "chrome/test/ui_test_utils.h" |
#include "testing/gtest/include/gtest/gtest.h" |
+static const std::string kTestFormString = |
dhollowa
2010/12/08 17:33:41
Static globals must not be of class-type. POD (pl
|
+ "<form action=\"http://www.google.com/\" method=\"POST\">" |
+ "<label for=\"firstname\">First name:</label>" |
+ " <input type=\"text\" id=\"firstname\"" |
+ " onFocus=\"domAutomationController.send(true)\"" |
+ " /><br />" |
+ "<label for=\"lastname\">Last name:</label>" |
+ " <input type=\"text\" id=\"lastname\" /><br />" |
+ "<label for=\"address1\">Address line 1:</label>" |
+ " <input type=\"text\" id=\"address1\" /><br />" |
+ "<label for=\"address2\">Address line 2:</label>" |
+ " <input type=\"text\" id=\"address2\" /><br />" |
+ "<label for=\"city\">City:</label>" |
+ " <input type=\"text\" id=\"city\" /><br />" |
+ "<label for=\"state\">State:</label>" |
+ " <select id=\"state\">" |
+ " <option value=\"\" selected=\"yes\">--</option>" |
+ " <option value=\"CA\">California</option>" |
+ " <option value=\"TX\">Texas</option>" |
+ " </select><br />" |
+ "<label for=\"zip\">ZIP code:</label>" |
+ " <input type=\"text\" id=\"zip\" /><br />" |
+ "<label for=\"country\">Country:</label>" |
+ " <select id=\"country\">" |
+ " <option value=\"\" selected=\"yes\">--</option>" |
+ " <option value=\"CA\">Canada</option>" |
+ " <option value=\"US\">United States</option>" |
+ " </select><br />" |
+ "<label for=\"phone\">Phone number:</label>" |
+ " <input type=\"text\" id=\"phone\" /><br />" |
+ "</form>"; |
+ |
class AutoFillTest : public InProcessBrowserTest { |
+ TestURLFetcherFactory url_fetcher_factory_; |
dhollowa
2010/12/08 17:33:41
Data members should come last in declaration order
|
protected: |
AutoFillTest() { |
set_show_window(true); |
@@ -56,6 +97,110 @@ class AutoFillTest : public InProcessBrowserTest { |
L"document.getElementById('" + field_name + L"').value);", &value)); |
EXPECT_EQ(expected_value, value); |
} |
+ |
+ RenderViewHost* rvh() { |
+ return browser()->GetSelectedTabContents()->render_view_host(); |
+ } |
+ |
+ virtual void SetUp() { |
+ URLFetcher::set_factory(&url_fetcher_factory_); |
+ InProcessBrowserTest::SetUp(); |
+ } |
+ |
+ void SimulateURLFetch(bool success) { |
+ TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
+ ASSERT_TRUE(fetcher); |
+ URLRequestStatus status; |
+ status.set_status(success ? URLRequestStatus::SUCCESS : |
+ URLRequestStatus::FAILED); |
+ |
+ std::string script = " var google = {};" |
+ "google.translate = (function() {" |
+ " return {" |
+ " TranslateService: function() {" |
+ " return {" |
+ " isAvailable : function() {" |
+ " return true;" |
+ " }," |
+ " restore : function() {" |
+ " return;" |
+ " }," |
+ " getDetectedLanguage : function() {" |
+ " return \"ja\";" |
+ " }," |
+ " translatePage : function(originalLang, targetLang," |
+ " onTranslateProgress) {" |
+ " document.getElementsByTagName(\"body\")[0].innerHTML = '" + |
+ kTestFormString + |
+ " ';" |
+ " onTranslateProgress(100, true, false);" |
+ " }" |
+ " };" |
+ " }" |
+ " };" |
+ "})();"; |
+ |
+ fetcher->delegate()->OnURLFetchComplete(fetcher, fetcher->original_url(), |
+ status, success ? 200 : 500, |
+ ResponseCookies(), |
+ script); |
+ } |
+ |
+ void TryBasicFormFillWithMKey() { |
+ ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(), |
+ VIEW_ID_TAB_CONTAINER)); |
+ ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), |
+ VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
+ |
+ bool result = false; |
+ ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
+ rvh(), L"", L"document.getElementById('firstname').focus();", &result)); |
+ ASSERT_TRUE(result); |
+ // Start filling the first name field with "M" and wait for the popup to be |
+ // shown. |
+ ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
+ browser(), app::VKEY_M, false, true, false, false, |
+ NotificationType::AUTOFILL_DID_SHOW_SUGGESTIONS, |
+ Source<RenderViewHost>(rvh()))); |
+ |
+ // Press the down arrow to select the suggestion and preview the autofilled |
+ // form. |
+ ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
+ browser(), app::VKEY_DOWN, false, false, false, false, |
+ NotificationType::AUTOFILL_DID_FILL_FORM_DATA, |
+ Source<RenderViewHost>(rvh()))); |
+ |
+ // The previewed values should not be accessible to JavaScript. |
+ ExpectFieldValue(L"firstname", "M"); |
+ ExpectFieldValue(L"lastname", ""); |
+ ExpectFieldValue(L"address1", ""); |
+ ExpectFieldValue(L"address2", ""); |
+ ExpectFieldValue(L"city", ""); |
+ ExpectFieldValue(L"state", ""); |
+ ExpectFieldValue(L"zip", ""); |
+ ExpectFieldValue(L"country", ""); |
+ ExpectFieldValue(L"phone", ""); |
+ // TODO(isherman): It would be nice to test that the previewed values are |
+ // displayed: http://crbug.com/57220 |
+ |
+ // Press Enter to accept the autofill suggestions. |
+ ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
+ browser(), app::VKEY_RETURN, false, false, false, false, |
+ NotificationType::AUTOFILL_DID_FILL_FORM_DATA, |
+ Source<RenderViewHost>(rvh()))); |
+ |
+ // The form should be filled. |
+ ExpectFieldValue(L"firstname", "Milton"); |
+ ExpectFieldValue(L"lastname", "Waddams"); |
+ ExpectFieldValue(L"address1", "4120 Freidrich Lane"); |
+ ExpectFieldValue(L"address2", "Basement"); |
+ ExpectFieldValue(L"city", "Austin"); |
+ ExpectFieldValue(L"state", "TX"); |
+ ExpectFieldValue(L"zip", "78744"); |
+ ExpectFieldValue(L"country", "US"); |
+ ExpectFieldValue(L"phone", "5125551234"); |
+ |
+ } |
}; |
// Test that basic form fill is working. |
@@ -64,92 +209,77 @@ IN_PROC_BROWSER_TEST_F(AutoFillTest, BasicFormFill) { |
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
- browser(), GURL("data:text/html;charset=utf-8," |
- "<form action=\"http://www.google.com/\" method=\"POST\">" |
- "<label for=\"firstname\">First name:</label>" |
- " <input type=\"text\" id=\"firstname\"" |
- " onFocus=\"domAutomationController.send(true)\"" |
- " /><br />" |
- "<label for=\"lastname\">Last name:</label>" |
- " <input type=\"text\" id=\"lastname\" /><br />" |
- "<label for=\"address1\">Address line 1:</label>" |
- " <input type=\"text\" id=\"address1\" /><br />" |
- "<label for=\"address2\">Address line 2:</label>" |
- " <input type=\"text\" id=\"address2\" /><br />" |
- "<label for=\"city\">City:</label>" |
- " <input type=\"text\" id=\"city\" /><br />" |
- "<label for=\"state\">State:</label>" |
- " <select id=\"state\">" |
- " <option value=\"\" selected=\"yes\">--</option>" |
- " <option value=\"CA\">California</option>" |
- " <option value=\"TX\">Texas</option>" |
- " </select><br />" |
- "<label for=\"zip\">ZIP code:</label>" |
- " <input type=\"text\" id=\"zip\" /><br />" |
- "<label for=\"country\">Country:</label>" |
- " <select id=\"country\">" |
- " <option value=\"\" selected=\"yes\">--</option>" |
- " <option value=\"CA\">Canada</option>" |
- " <option value=\"US\">United States</option>" |
- " </select><br />" |
- "<label for=\"phone\">Phone number:</label>" |
- " <input type=\"text\" id=\"phone\" /><br />" |
- "</form>"))); |
- |
- ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(), |
- VIEW_ID_TAB_CONTAINER)); |
- ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), |
- VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
- |
- RenderViewHost* render_view_host = |
- browser()->GetSelectedTabContents()->render_view_host(); |
- bool result; |
- ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
- render_view_host, L"", L"document.getElementById('firstname').focus();", |
- &result)); |
- ASSERT_TRUE(result); |
- |
- // Start filling the first name field with "M" and wait for the popup to be |
- // shown. |
- ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
- browser(), app::VKEY_M, false, true, false, false, |
- NotificationType::AUTOFILL_DID_SHOW_SUGGESTIONS, |
- Source<RenderViewHost>(render_view_host))); |
- |
- // Press the down arrow to select the suggestion and preview the autofilled |
- // form. |
- ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
- browser(), app::VKEY_DOWN, false, false, false, false, |
- NotificationType::AUTOFILL_DID_FILL_FORM_DATA, |
- Source<RenderViewHost>(render_view_host))); |
- |
- // The previewed values should not be accessible to JavaScript. |
- ExpectFieldValue(L"firstname", "M"); |
- ExpectFieldValue(L"lastname", ""); |
- ExpectFieldValue(L"address1", ""); |
- ExpectFieldValue(L"address2", ""); |
- ExpectFieldValue(L"city", ""); |
- ExpectFieldValue(L"state", ""); |
- ExpectFieldValue(L"zip", ""); |
- ExpectFieldValue(L"country", ""); |
- ExpectFieldValue(L"phone", ""); |
- // TODO(isherman): It would be nice to test that the previewed values are |
- // displayed: http://crbug.com/57220 |
- |
- // Press Enter to accept the autofill suggestions. |
- ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
- browser(), app::VKEY_RETURN, false, false, false, false, |
- NotificationType::AUTOFILL_DID_FILL_FORM_DATA, |
- Source<RenderViewHost>(render_view_host))); |
- |
- // The form should be filled. |
- ExpectFieldValue(L"firstname", "Milton"); |
- ExpectFieldValue(L"lastname", "Waddams"); |
- ExpectFieldValue(L"address1", "4120 Freidrich Lane"); |
- ExpectFieldValue(L"address2", "Basement"); |
- ExpectFieldValue(L"city", "Austin"); |
- ExpectFieldValue(L"state", "TX"); |
- ExpectFieldValue(L"zip", "78744"); |
- ExpectFieldValue(L"country", "US"); |
- ExpectFieldValue(L"phone", "5125551234"); |
+ browser(), GURL("data:text/html;charset=utf-8," + kTestFormString))); |
+ |
+ TryBasicFormFillWithMKey(); |
+} |
+ |
+// Test that basic form fill is working. |
+IN_PROC_BROWSER_TEST_F(AutoFillTest, TranslateAndFormFill) { |
+ SetUpProfile(); |
+ |
+ GURL url("data:text/html;charset=utf-8," |
+ "<form action=\"http://www.google.com/\" method=\"POST\">" |
+ "<label for=\"firstname\">なまえ</label>" |
+ " <input type=\"text\" id=\"firstname\"" |
+ " onFocus=\"domAutomationController.send(true)\"" |
+ " /><br />" |
+ "<label for=\"lastname\">みょうじ</label>" |
+ " <input type=\"text\" id=\"lastname\" /><br />" |
+ "<label for=\"address1\">Address line 1:</label>" |
+ " <input type=\"text\" id=\"address1\" /><br />" |
+ "<label for=\"address2\">Address line 2:</label>" |
+ " <input type=\"text\" id=\"address2\" /><br />" |
+ "<label for=\"city\">City:</label>" |
+ " <input type=\"text\" id=\"city\" /><br />" |
+ "<label for=\"state\">State:</label>" |
+ " <select id=\"state\">" |
+ " <option value=\"\" selected=\"yes\">--</option>" |
+ " <option value=\"CA\">California</option>" |
+ " <option value=\"TX\">Texas</option>" |
+ " </select><br />" |
+ "<label for=\"zip\">ZIP code:</label>" |
+ " <input type=\"text\" id=\"zip\" /><br />" |
+ "<label for=\"country\">Country:</label>" |
+ " <select id=\"country\">" |
+ " <option value=\"\" selected=\"yes\">--</option>" |
+ " <option value=\"CA\">Canada</option>" |
+ " <option value=\"US\">United States</option>" |
+ " </select><br />" |
+ "<label for=\"phone\">Phone number:</label>" |
+ " <input type=\"text\" id=\"phone\" /><br />" |
+ "</form>"); |
+ ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
+ ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
+ browser(), url)); |
+ |
+ // Get translation bar. |
+ int page_id = browser()->GetSelectedTabContents()->controller(). |
+ GetLastCommittedEntry()->page_id(); |
+ rvh()->OnMessageReceived(ViewHostMsg_PageContents(0, url, page_id, |
+ UTF8ToUTF16("test"), "ja", true)); |
+ TranslateInfoBarDelegate* infobar = browser()->GetSelectedTabContents()-> |
+ GetInfoBarDelegateAt(0)->AsTranslateInfoBarDelegate(); |
+ |
+ ASSERT_TRUE(infobar != NULL); |
+ EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, infobar->type()); |
+ |
+ // Simulate press translation button. |
+ infobar->Translate(); |
+ |
+ // Simulate the translate script being retrieved. |
+ // Pass fake google.translate lib as the translate script. |
+ SimulateURLFetch(true); |
+ |
+ // Simulate translation to kick onTranslateElementLoad. |
+ // But right now, the call stucks here. |
+ // Once click the text field, it starts again. |
+ ASSERT_TRUE(ui_test_utils::ExecuteJavaScript( |
+ rvh(), L"", L"cr.googleTranslate.onTranslateElementLoad();")); |
+ |
+ // Simulate the render notifying the translation has been done. |
+ ui_test_utils::WaitForNotification(NotificationType::PAGE_TRANSLATED); |
+ |
+ TryBasicFormFillWithMKey(); |
+ |
dhollowa
2010/12/08 17:33:41
nit: empty newline is not needed.
|
} |