OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "base/command_line.h" |
| 6 #include "chrome/browser/password_manager/password_generation_manager.h" |
| 7 #include "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/browser/ui/autofill/password_generation_popup_observer.h" |
| 9 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 11 #include "chrome/test/base/in_process_browser_test.h" |
| 12 #include "chrome/test/base/ui_test_utils.h" |
| 13 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 14 #include "components/autofill/core/common/autofill_switches.h" |
| 15 #include "content/public/browser/render_view_host.h" |
| 16 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/test/browser_test_utils.h" |
| 18 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "ui/events/keycodes/keyboard_codes.h" |
| 21 |
| 22 namespace { |
| 23 |
| 24 class TestPopupObserver : public autofill::PasswordGenerationPopupObserver { |
| 25 public: |
| 26 TestPopupObserver() |
| 27 : popup_showing_(false) {} |
| 28 virtual ~TestPopupObserver() {} |
| 29 |
| 30 virtual void OnPopupShown() OVERRIDE { |
| 31 popup_showing_ = true; |
| 32 } |
| 33 |
| 34 virtual void OnPopupHidden() OVERRIDE { |
| 35 popup_showing_ = false; |
| 36 } |
| 37 |
| 38 bool popup_showing() { return popup_showing_; } |
| 39 |
| 40 private: |
| 41 bool popup_showing_; |
| 42 }; |
| 43 |
| 44 } // namespace |
| 45 |
| 46 class PasswordGenerationInteractiveTest : public InProcessBrowserTest { |
| 47 public: |
| 48 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 49 // Make sure the feature is enabled. |
| 50 command_line->AppendSwitch(autofill::switches::kEnablePasswordGeneration); |
| 51 |
| 52 // Don't require ping from autofill or blacklist checking. |
| 53 command_line->AppendSwitch( |
| 54 autofill::switches::kLocalHeuristicsOnlyForPasswordGeneration); |
| 55 } |
| 56 |
| 57 virtual void SetUpOnMainThread() OVERRIDE { |
| 58 // Disable Autofill requesting access to AddressBook data. This will cause |
| 59 // test tests to hang on Mac. |
| 60 autofill::test::DisableSystemServices(browser()->profile()); |
| 61 |
| 62 // Set observer for popup. |
| 63 PasswordGenerationManager* generation_manager = |
| 64 PasswordGenerationManager::FromWebContents(GetWebContents()); |
| 65 generation_manager->SetTestObserver(&observer_); |
| 66 |
| 67 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 68 GURL url = embedded_test_server()->GetURL("/password/signup_form.html"); |
| 69 ui_test_utils::NavigateToURL(browser(), url); |
| 70 } |
| 71 |
| 72 virtual void CleanUpOnMainThread() OVERRIDE { |
| 73 // Cleanup UI. |
| 74 PasswordGenerationManager* generation_manager = |
| 75 PasswordGenerationManager::FromWebContents(GetWebContents()); |
| 76 generation_manager->HidePopup(); |
| 77 } |
| 78 |
| 79 content::WebContents* GetWebContents() { |
| 80 return browser()->tab_strip_model()->GetActiveWebContents(); |
| 81 } |
| 82 |
| 83 content::RenderViewHost* GetRenderViewHost() { |
| 84 return GetWebContents()->GetRenderViewHost(); |
| 85 } |
| 86 |
| 87 std::string GetFieldValue(const std::string& field_id) { |
| 88 std::string value; |
| 89 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 90 GetRenderViewHost(), |
| 91 "window.domAutomationController.send(" |
| 92 " document.getElementById('" + field_id + "').value);", |
| 93 &value)); |
| 94 return value; |
| 95 } |
| 96 |
| 97 void FocusPasswordField() { |
| 98 ASSERT_TRUE(content::ExecuteScript( |
| 99 GetRenderViewHost(), |
| 100 "document.getElementById('password_field').focus()")); |
| 101 } |
| 102 |
| 103 void SendKeyToPopup(ui::KeyboardCode key) { |
| 104 content::NativeWebKeyboardEvent event; |
| 105 event.windowsKeyCode = key; |
| 106 event.type = blink::WebKeyboardEvent::RawKeyDown; |
| 107 GetRenderViewHost()->ForwardKeyboardEvent(event); |
| 108 } |
| 109 |
| 110 bool popup_showing() { |
| 111 return observer_.popup_showing(); |
| 112 } |
| 113 |
| 114 private: |
| 115 TestPopupObserver observer_; |
| 116 }; |
| 117 |
| 118 #if defined(USE_AURA) |
| 119 // Enabled on these platforms |
| 120 #define MAYBE_PopupShownAndPasswordSelected PopupShownAndPasswordSelected |
| 121 #define MAYBE_PopupShownAndDismissed PopupShownAndDismissed |
| 122 #else |
| 123 // Popup not enabled for these platforms yet. |
| 124 #define MAYBE_PopupShownAndPasswordSelected DISABLED_PopupShownAndPasswordSelect
ed |
| 125 #define MAYBE_PopupShownAndDismissed DISABLED_PopupShownAndDismissed |
| 126 #endif |
| 127 |
| 128 IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest, |
| 129 MAYBE_PopupShownAndPasswordSelected) { |
| 130 FocusPasswordField(); |
| 131 EXPECT_TRUE(popup_showing()); |
| 132 SendKeyToPopup(ui::VKEY_DOWN); |
| 133 SendKeyToPopup(ui::VKEY_RETURN); |
| 134 |
| 135 EXPECT_FALSE(GetFieldValue("password_field").empty()); |
| 136 } |
| 137 |
| 138 IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest, |
| 139 MAYBE_PopupShownAndDismissed) { |
| 140 FocusPasswordField(); |
| 141 EXPECT_TRUE(popup_showing()); |
| 142 |
| 143 SendKeyToPopup(ui::VKEY_ESCAPE); |
| 144 |
| 145 // Popup is dismissed. |
| 146 EXPECT_FALSE(popup_showing()); |
| 147 } |
OLD | NEW |