Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / password_manager / password_generation_interactive_uitest.cc
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/chrome_password_manager_client.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 "components/password_manager/core/browser/password_generation_manager.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"
22
23 namespace {
24
25 class TestPopupObserver : public autofill::PasswordGenerationPopupObserver {
26  public:
27   TestPopupObserver()
28       : popup_showing_(false),
29         password_visible_(false) {}
30   virtual ~TestPopupObserver() {}
31
32   void OnPopupShown(bool password_visible) override {
33     popup_showing_ = true;
34     password_visible_ = password_visible;
35   }
36
37   void OnPopupHidden() override { popup_showing_ = false; }
38
39   bool popup_showing() { return popup_showing_; }
40   bool password_visible() { return password_visible_; }
41
42  private:
43   bool popup_showing_;
44   bool password_visible_;
45 };
46
47 }  // namespace
48
49 class PasswordGenerationInteractiveTest : public InProcessBrowserTest {
50  public:
51   void SetUpCommandLine(CommandLine* command_line) override {
52     // Make sure the feature is enabled.
53     command_line->AppendSwitch(autofill::switches::kEnablePasswordGeneration);
54
55     // Don't require ping from autofill or blacklist checking.
56     command_line->AppendSwitch(
57         autofill::switches::kLocalHeuristicsOnlyForPasswordGeneration);
58   }
59
60   void SetUpOnMainThread() override {
61     // Disable Autofill requesting access to AddressBook data. This will cause
62     // the tests to hang on Mac.
63     autofill::test::DisableSystemServices(browser()->profile()->GetPrefs());
64
65     // Set observer for popup.
66     ChromePasswordManagerClient* client =
67         ChromePasswordManagerClient::FromWebContents(GetWebContents());
68     client->SetTestObserver(&observer_);
69
70     ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
71     GURL url = embedded_test_server()->GetURL("/password/signup_form.html");
72     ui_test_utils::NavigateToURL(browser(), url);
73   }
74
75   void TearDownOnMainThread() override {
76     // Clean up UI.
77     ChromePasswordManagerClient* client =
78         ChromePasswordManagerClient::FromWebContents(GetWebContents());
79     client->HidePasswordGenerationPopup();
80   }
81
82   content::WebContents* GetWebContents() {
83     return browser()->tab_strip_model()->GetActiveWebContents();
84   }
85
86   content::RenderViewHost* GetRenderViewHost() {
87     return GetWebContents()->GetRenderViewHost();
88   }
89
90   std::string GetFieldValue(const std::string& field_id) {
91     std::string value;
92     EXPECT_TRUE(content::ExecuteScriptAndExtractString(
93         GetRenderViewHost(),
94         "window.domAutomationController.send("
95         "    document.getElementById('" + field_id + "').value);",
96         &value));
97     return value;
98   }
99
100   std::string GetFocusedElement() {
101     std::string focused_element;
102     EXPECT_TRUE(content::ExecuteScriptAndExtractString(
103         GetRenderViewHost(),
104         "window.domAutomationController.send("
105         "    document.activeElement.id)",
106         &focused_element));
107     return focused_element;
108   }
109
110   void FocusPasswordField() {
111     ASSERT_TRUE(content::ExecuteScript(
112         GetRenderViewHost(),
113         "document.getElementById('password_field').focus()"));
114   }
115
116   void SendKeyToPopup(ui::KeyboardCode key) {
117     content::NativeWebKeyboardEvent event;
118     event.windowsKeyCode = key;
119     event.type = blink::WebKeyboardEvent::RawKeyDown;
120     GetRenderViewHost()->ForwardKeyboardEvent(event);
121   }
122
123   bool GenerationPopupShowing() {
124     return observer_.popup_showing() && observer_.password_visible();
125   }
126
127   bool EditingPopupShowing() {
128     return observer_.popup_showing() && !observer_.password_visible();
129   }
130
131  private:
132   TestPopupObserver observer_;
133 };
134
135 #if defined(USE_AURA)
136 // Enabled on these platforms.
137 // Disabled due to flakiness, see http://crbug.com/407998
138 #define MAYBE_PopupShownAndPasswordSelected \
139   DISABLED_PopupShownAndPasswordSelected
140 #define MAYBE_PopupShownAndDismissed DISABLED_PopupShownAndDismissed
141 #define MAYBE_PopupShownAndDismissedByScrolling \
142   DISABLED_PopupShownAndDismissedByScrolling
143 #else
144 // Popup not enabled for these platforms yet.
145 #define MAYBE_PopupShownAndPasswordSelected \
146   DISABLED_PopupShownAndPasswordSelected
147 #define MAYBE_PopupShownAndDismissed DISABLED_PopupShownAndDismissed
148 #define MAYBE_PopupShownAndDismissedByScrolling \
149   DISABLED_PopupShownAndDismissedByScrolling
150 #endif
151
152 IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest,
153                        MAYBE_PopupShownAndPasswordSelected) {
154   FocusPasswordField();
155   EXPECT_TRUE(GenerationPopupShowing());
156   SendKeyToPopup(ui::VKEY_DOWN);
157   SendKeyToPopup(ui::VKEY_RETURN);
158
159   // Selecting the password should fill the field and move focus to the
160   // submit button.
161   EXPECT_FALSE(GetFieldValue("password_field").empty());
162   EXPECT_FALSE(GenerationPopupShowing());
163   EXPECT_FALSE(EditingPopupShowing());
164   EXPECT_EQ("input_submit_button", GetFocusedElement());
165
166   // Re-focusing the password field should show the editing popup.
167   FocusPasswordField();
168   EXPECT_TRUE(EditingPopupShowing());
169 }
170
171 IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest,
172                        MAYBE_PopupShownAndDismissed) {
173   FocusPasswordField();
174   EXPECT_TRUE(GenerationPopupShowing());
175
176   SendKeyToPopup(ui::VKEY_ESCAPE);
177
178   // Popup is dismissed.
179   EXPECT_FALSE(GenerationPopupShowing());
180 }
181
182 IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest,
183                        MAYBE_PopupShownAndDismissedByScrolling) {
184   FocusPasswordField();
185   EXPECT_TRUE(GenerationPopupShowing());
186
187   ASSERT_TRUE(content::ExecuteScript(GetRenderViewHost(),
188                                      "window.scrollTo(100, 0);"));
189
190   EXPECT_FALSE(GenerationPopupShowing());
191 }