NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / password_manager / password_generation_interactive_uitest.cc
blobdbd948d6bf90138a87db7b339de3805533f76cfc
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.
5 #include "base/command_line.h"
6 #include "chrome/browser/password_manager/chrome_password_manager_client.h"
7 #include "chrome/browser/password_manager/password_generation_manager.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/autofill/password_generation_popup_observer.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "components/autofill/core/browser/autofill_test_utils.h"
15 #include "components/autofill/core/common/autofill_switches.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/test/browser_test_utils.h"
19 #include "net/test/embedded_test_server/embedded_test_server.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "ui/events/keycodes/keyboard_codes.h"
23 namespace {
25 class TestPopupObserver : public autofill::PasswordGenerationPopupObserver {
26 public:
27 TestPopupObserver()
28 : popup_showing_(false),
29 password_visible_(false) {}
30 virtual ~TestPopupObserver() {}
32 virtual void OnPopupShown(bool password_visible) OVERRIDE {
33 popup_showing_ = true;
34 password_visible_ = password_visible;
37 virtual void OnPopupHidden() OVERRIDE {
38 popup_showing_ = false;
41 bool popup_showing() { return popup_showing_; }
42 bool password_visible() { return password_visible_; }
44 private:
45 bool popup_showing_;
46 bool password_visible_;
49 } // namespace
51 class PasswordGenerationInteractiveTest : public InProcessBrowserTest {
52 public:
53 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
54 // Make sure the feature is enabled.
55 command_line->AppendSwitch(autofill::switches::kEnablePasswordGeneration);
57 // Don't require ping from autofill or blacklist checking.
58 command_line->AppendSwitch(
59 autofill::switches::kLocalHeuristicsOnlyForPasswordGeneration);
62 virtual void SetUpOnMainThread() OVERRIDE {
63 // Disable Autofill requesting access to AddressBook data. This will cause
64 // the tests to hang on Mac.
65 autofill::test::DisableSystemServices(browser()->profile());
67 // Set observer for popup.
68 PasswordGenerationManager* generation_manager =
69 ChromePasswordManagerClient::GetGenerationManagerFromWebContents(
70 GetWebContents());
71 generation_manager->SetTestObserver(&observer_);
73 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
74 GURL url = embedded_test_server()->GetURL("/password/signup_form.html");
75 ui_test_utils::NavigateToURL(browser(), url);
78 virtual void CleanUpOnMainThread() OVERRIDE {
79 // Clean up UI.
80 PasswordGenerationManager* generation_manager =
81 ChromePasswordManagerClient::GetGenerationManagerFromWebContents(
82 GetWebContents());
83 generation_manager->HidePopup();
86 content::WebContents* GetWebContents() {
87 return browser()->tab_strip_model()->GetActiveWebContents();
90 content::RenderViewHost* GetRenderViewHost() {
91 return GetWebContents()->GetRenderViewHost();
94 std::string GetFieldValue(const std::string& field_id) {
95 std::string value;
96 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
97 GetRenderViewHost(),
98 "window.domAutomationController.send("
99 " document.getElementById('" + field_id + "').value);",
100 &value));
101 return value;
104 std::string GetFocusedElement() {
105 std::string focused_element;
106 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
107 GetRenderViewHost(),
108 "window.domAutomationController.send("
109 " document.activeElement.id)",
110 &focused_element));
111 return focused_element;
114 void FocusPasswordField() {
115 ASSERT_TRUE(content::ExecuteScript(
116 GetRenderViewHost(),
117 "document.getElementById('password_field').focus()"));
120 void SendKeyToPopup(ui::KeyboardCode key) {
121 content::NativeWebKeyboardEvent event;
122 event.windowsKeyCode = key;
123 event.type = blink::WebKeyboardEvent::RawKeyDown;
124 GetRenderViewHost()->ForwardKeyboardEvent(event);
127 bool GenerationPopupShowing() {
128 return observer_.popup_showing() && observer_.password_visible();
131 bool EditingPopupShowing() {
132 return observer_.popup_showing() && !observer_.password_visible();
135 private:
136 TestPopupObserver observer_;
139 #if defined(USE_AURA)
140 // Enabled on these platforms.
141 #define MAYBE_PopupShownAndPasswordSelected PopupShownAndPasswordSelected
142 #define MAYBE_PopupShownAndDismissed PopupShownAndDismissed
143 #else
144 // Popup not enabled for these platforms yet.
145 #define MAYBE_PopupShownAndPasswordSelected DISABLED_PopupShownAndPasswordSelected
146 #define MAYBE_PopupShownAndDismissed DISABLED_PopupShownAndDismissed
147 #endif
149 IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest,
150 MAYBE_PopupShownAndPasswordSelected) {
151 FocusPasswordField();
152 EXPECT_TRUE(GenerationPopupShowing());
153 SendKeyToPopup(ui::VKEY_DOWN);
154 SendKeyToPopup(ui::VKEY_RETURN);
156 // Selecting the password should fill the field and move focus to the
157 // submit button.
158 EXPECT_FALSE(GetFieldValue("password_field").empty());
159 EXPECT_FALSE(GenerationPopupShowing());
160 EXPECT_FALSE(EditingPopupShowing());
161 EXPECT_EQ("input_submit_button", GetFocusedElement());
163 // Re-focusing the password field should show the editing popup.
164 FocusPasswordField();
165 EXPECT_TRUE(EditingPopupShowing());
168 IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest,
169 MAYBE_PopupShownAndDismissed) {
170 FocusPasswordField();
171 EXPECT_TRUE(GenerationPopupShowing());
173 SendKeyToPopup(ui::VKEY_ESCAPE);
175 // Popup is dismissed.
176 EXPECT_FALSE(GenerationPopupShowing());