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 "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 password_visible_(false) {} | |
| 29 virtual ~TestPopupObserver() {} | |
| 30 | |
| 31 virtual void OnPopupShown(bool password_visible) OVERRIDE { | |
| 32 popup_showing_ = true; | |
| 33 password_visible_ = password_visible; | |
| 34 } | |
| 35 | |
| 36 virtual void OnPopupHidden() OVERRIDE { | |
| 37 popup_showing_ = false; | |
| 38 } | |
| 39 | |
| 40 bool popup_showing() { return popup_showing_; } | |
| 41 bool password_visible() { return password_visible_; } | |
| 42 | |
| 43 private: | |
| 44 bool popup_showing_; | |
| 45 bool password_visible_; | |
| 46 }; | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 class PasswordGenerationInteractiveTest : public InProcessBrowserTest { | |
| 51 public: | |
| 52 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
| 53 // Make sure the feature is enabled. | |
| 54 command_line->AppendSwitch(autofill::switches::kEnablePasswordGeneration); | |
| 55 | |
| 56 // Don't require ping from autofill or blacklist checking. | |
| 57 command_line->AppendSwitch( | |
| 58 autofill::switches::kLocalHeuristicsOnlyForPasswordGeneration); | |
| 59 } | |
| 60 | |
| 61 virtual void SetUpOnMainThread() OVERRIDE { | |
| 62 // Disable Autofill requesting access to AddressBook data. This will cause | |
| 63 // test tests to hang on Mac. | |
|
Evan Stade
2014/02/03 18:49:15
test tests?
Garrett Casto
2014/02/04 17:54:57
Done.
| |
| 64 autofill::test::DisableSystemServices(browser()->profile()); | |
| 65 | |
| 66 // Set observer for popup. | |
| 67 PasswordGenerationManager* generation_manager = | |
| 68 PasswordGenerationManager::FromWebContents(GetWebContents()); | |
| 69 generation_manager->SetTestObserver(&observer_); | |
| 70 | |
| 71 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
| 72 GURL url = embedded_test_server()->GetURL("/password/signup_form.html"); | |
| 73 ui_test_utils::NavigateToURL(browser(), url); | |
| 74 } | |
| 75 | |
| 76 virtual void CleanUpOnMainThread() OVERRIDE { | |
| 77 // Cleanup UI. | |
|
Evan Stade
2014/02/03 18:49:15
nit: Clean up
Garrett Casto
2014/02/04 17:54:57
Done.
| |
| 78 PasswordGenerationManager* generation_manager = | |
| 79 PasswordGenerationManager::FromWebContents(GetWebContents()); | |
| 80 generation_manager->HidePopup(); | |
| 81 } | |
| 82 | |
| 83 content::WebContents* GetWebContents() { | |
| 84 return browser()->tab_strip_model()->GetActiveWebContents(); | |
| 85 } | |
| 86 | |
| 87 content::RenderViewHost* GetRenderViewHost() { | |
| 88 return GetWebContents()->GetRenderViewHost(); | |
| 89 } | |
| 90 | |
| 91 std::string GetFieldValue(const std::string& field_id) { | |
| 92 std::string value; | |
| 93 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
| 94 GetRenderViewHost(), | |
| 95 "window.domAutomationController.send(" | |
| 96 " document.getElementById('" + field_id + "').value);", | |
| 97 &value)); | |
| 98 return value; | |
| 99 } | |
| 100 | |
| 101 std::string GetFocusedElement() { | |
| 102 std::string focused_element; | |
| 103 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
| 104 GetRenderViewHost(), | |
| 105 "window.domAutomationController.send(" | |
| 106 " document.activeElement.id)", | |
| 107 &focused_element)); | |
| 108 return focused_element; | |
| 109 } | |
| 110 | |
| 111 void FocusPasswordField() { | |
| 112 ASSERT_TRUE(content::ExecuteScript( | |
| 113 GetRenderViewHost(), | |
| 114 "document.getElementById('password_field').focus()")); | |
| 115 } | |
| 116 | |
| 117 void SendKeyToPopup(ui::KeyboardCode key) { | |
| 118 content::NativeWebKeyboardEvent event; | |
| 119 event.windowsKeyCode = key; | |
| 120 event.type = blink::WebKeyboardEvent::RawKeyDown; | |
| 121 GetRenderViewHost()->ForwardKeyboardEvent(event); | |
| 122 } | |
| 123 | |
| 124 bool GenerationPopupShowing() { | |
| 125 return observer_.popup_showing() && observer_.password_visible(); | |
| 126 } | |
| 127 | |
| 128 bool EditingPopupShowing() { | |
| 129 return observer_.popup_showing() && !observer_.password_visible(); | |
| 130 } | |
| 131 | |
| 132 private: | |
| 133 TestPopupObserver observer_; | |
| 134 }; | |
| 135 | |
| 136 #if defined(USE_AURA) | |
| 137 // Enabled on these platforms | |
|
Evan Stade
2014/02/03 18:49:15
nit: final punctuation.
| |
| 138 #define MAYBE_PopupShownAndPasswordSelected PopupShownAndPasswordSelected | |
| 139 #define MAYBE_PopupShownAndDismissed PopupShownAndDismissed | |
| 140 #else | |
| 141 // Popup not enabled for these platforms yet. | |
| 142 #define MAYBE_PopupShownAndPasswordSelected DISABLED_PopupShownAndPasswordSelect ed | |
| 143 #define MAYBE_PopupShownAndDismissed DISABLED_PopupShownAndDismissed | |
| 144 #endif | |
| 145 | |
| 146 IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest, | |
| 147 MAYBE_PopupShownAndPasswordSelected) { | |
| 148 FocusPasswordField(); | |
| 149 EXPECT_TRUE(GenerationPopupShowing()); | |
| 150 SendKeyToPopup(ui::VKEY_DOWN); | |
| 151 SendKeyToPopup(ui::VKEY_RETURN); | |
| 152 | |
| 153 // Selecting the password should fill the field and move focus to the | |
| 154 // submit button. | |
| 155 EXPECT_FALSE(GetFieldValue("password_field").empty()); | |
| 156 EXPECT_FALSE(GenerationPopupShowing()); | |
| 157 EXPECT_FALSE(EditingPopupShowing()); | |
| 158 EXPECT_EQ("input_submit_button", GetFocusedElement()); | |
| 159 | |
| 160 // Re-focusing the password field should show the editing popup. | |
| 161 FocusPasswordField(); | |
| 162 EXPECT_TRUE(EditingPopupShowing()); | |
| 163 } | |
| 164 | |
| 165 IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest, | |
| 166 MAYBE_PopupShownAndDismissed) { | |
| 167 FocusPasswordField(); | |
| 168 EXPECT_TRUE(GenerationPopupShowing()); | |
| 169 | |
| 170 SendKeyToPopup(ui::VKEY_ESCAPE); | |
| 171 | |
| 172 // Popup is dismissed. | |
| 173 EXPECT_FALSE(GenerationPopupShowing()); | |
| 174 } | |
| OLD | NEW |