File: password_generation_popup_view_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (298 lines) | stat: -rw-r--r-- 13,551 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/passwords/password_generation_popup_view.h"

#include <string>

#include "base/memory/weak_ptr.h"
#include "chrome/browser/password_manager/chrome_password_manager_client.h"
#include "chrome/browser/password_manager/password_manager_test_base.h"
#include "chrome/browser/password_manager/password_manager_uitest_util.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/passwords/password_generation_popup_controller_impl.h"
#include "chrome/browser/ui/views/passwords/password_generation_popup_view_views.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/password_manager/content/browser/content_password_manager_driver.h"
#include "components/password_manager/content/browser/content_password_manager_driver_factory.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/scoped_accessibility_mode_override.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/accessibility/ax_enums.mojom-shared.h"
#include "ui/accessibility/ax_mode.h"
#include "ui/accessibility/platform/ax_platform_node.h"
#include "ui/accessibility/platform/ax_platform_node_delegate.h"
#include "ui/accessibility/platform/child_iterator_base.h"
#include "ui/events/event.h"
#include "ui/events/event_utils.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/widget/any_widget_observer.h"
#include "ui/views/widget/widget.h"

namespace autofill {
namespace {

using ::testing::NotNull;

const ui::AXPlatformNodeDelegate* FindNode(
    const ui::AXPlatformNodeDelegate* root,
    const std::string& class_name) {
  if (!root) {
    return nullptr;
  }

  if (root->GetStringAttribute(ax::mojom::StringAttribute::kClassName) ==
      class_name) {
    return root;
  }

  for (auto it = root->ChildrenBegin(); *it != *root->ChildrenEnd(); ++(*it)) {
    const ui::AXPlatformNodeDelegate* child_found =
        FindNode(it->get(), class_name);
    if (child_found) {
      return child_found;
    }
  }

  return nullptr;
}

void CheckViewAccessibilitySelected(
    const views::ViewAccessibility& view_accessibility,
    bool selected) {
  ui::AXNodeData node_data;
  view_accessibility.GetAccessibleNodeData(&node_data);
  EXPECT_EQ(node_data.GetBoolAttribute(ax::mojom::BoolAttribute::kSelected),
            selected);
}

}  // namespace

class PasswordGenerationPopupViewTest : public PasswordManagerBrowserTestBase {
 public:
  void SetUpOnMainThread() override {
    PasswordManagerBrowserTestBase::SetUpOnMainThread();
    NavigateToFile("/password/signup_form_new_password.html");
  }

  const views::ViewAccessibility& GetPasswordViewAccessibility(
      PasswordGenerationPopupViewViews* popup_view) {
    return popup_view->GetPasswordViewViewAccessibilityForTest();
  }
};

// Regression test for crbug.com/400543. Verifying that moving the mouse in the
// editing dialog doesn't crash.
IN_PROC_BROWSER_TEST_F(PasswordGenerationPopupViewTest,
                       MouseMovementInEditingPopup) {
  auto* client = ChromePasswordManagerClient::FromWebContents(WebContents());
  client->SetCurrentTargetFrameForTesting(WebContents()->GetPrimaryMainFrame());
  client->ShowPasswordEditingPopup(gfx::RectF(0, 0, 10, 10), FormData(),
                                   FieldRendererId(100), u"password123");
  // Avoid dangling pointers on shutdown.
  client->SetCurrentTargetFrameForTesting(nullptr);
  base::WeakPtr<PasswordGenerationPopupControllerImpl> controller =
      client->generation_popup_controller();
  ASSERT_TRUE(controller);
  ASSERT_TRUE(controller->IsVisible());

  static_cast<PasswordGenerationPopupViewViews*>(controller->view())
      ->OnMouseMoved(ui::MouseEvent(
          ui::EventType::kMouseMoved,
          /*location=*/gfx::Point(1, 1), /*root_location=*/gfx::Point(0, 0),
          ui::EventTimeForNow(), /*flags=*/0, /*changed_button_flags=*/0));
}

// Verify that destroying web contents with visible popup does not crash.
IN_PROC_BROWSER_TEST_F(PasswordGenerationPopupViewTest,
                       CloseWebContentsWithVisiblePopup) {
  auto* client = ChromePasswordManagerClient::FromWebContents(WebContents());
  client->SetCurrentTargetFrameForTesting(WebContents()->GetPrimaryMainFrame());
  client->ShowPasswordEditingPopup(gfx::RectF(0, 0, 10, 10), FormData(),
                                   FieldRendererId(100), u"password123");
  // Avoid dangling pointers on shutdown.
  client->SetCurrentTargetFrameForTesting(nullptr);
  base::WeakPtr<PasswordGenerationPopupControllerImpl> controller =
      client->generation_popup_controller();
  ASSERT_TRUE(controller);
  ASSERT_TRUE(controller->IsVisible());

  content::WebContents* web_contents = WebContents();
  ClearWebContentsPtr();
  web_contents->Close();
}

// Verify that controller is not crashed in case of insufficient vertical space
// for showing popup.
IN_PROC_BROWSER_TEST_F(PasswordGenerationPopupViewTest,
                       DoNotCrashInCaseOfInsuffucientVerticalSpace) {
  auto* client = ChromePasswordManagerClient::FromWebContents(WebContents());
  client->SetCurrentTargetFrameForTesting(WebContents()->GetPrimaryMainFrame());
  client->ShowPasswordEditingPopup(gfx::RectF(0, -20, 10, 10), FormData(),
                                   FieldRendererId(100), u"password123");
  EXPECT_FALSE(client->generation_popup_controller());
  // Avoid dangling pointers on shutdown.
  client->SetCurrentTargetFrameForTesting(nullptr);
}

IN_PROC_BROWSER_TEST_F(PasswordGenerationPopupViewTest,
                       GeneratedPasswordBoxAccessibleProperties) {
  auto* client = ChromePasswordManagerClient::FromWebContents(WebContents());
  client->SetCurrentTargetFrameForTesting(WebContents()->GetPrimaryMainFrame());
  client->ShowPasswordEditingPopup(gfx::RectF(0, 0, 10, 10), FormData(),
                                   FieldRendererId(100), u"password123");
  // Avoid dangling pointers on shutdown.
  client->SetCurrentTargetFrameForTesting(nullptr);
  base::WeakPtr<PasswordGenerationPopupControllerImpl> controller =
      client->generation_popup_controller();
  PasswordGenerationPopupViewViews* popup_view =
      static_cast<PasswordGenerationPopupViewViews*>(controller->view());
  ui::AXNodeData node_data;

  GetPasswordViewAccessibility(popup_view).GetAccessibleNodeData(&node_data);
  EXPECT_EQ(node_data.role, ax::mojom::Role::kListBoxOption);
}

IN_PROC_BROWSER_TEST_F(PasswordGenerationPopupViewTest,
                       ExpandedCollapsedAccessibleState) {
  auto* client = ChromePasswordManagerClient::FromWebContents(WebContents());
  client->SetCurrentTargetFrameForTesting(WebContents()->GetPrimaryMainFrame());
  client->ShowPasswordEditingPopup(gfx::RectF(0, 0, 10, 10), FormData(),
                                   FieldRendererId(100), u"password123");
  // Avoid dangling pointers on shutdown.
  client->SetCurrentTargetFrameForTesting(nullptr);
  base::WeakPtr<PasswordGenerationPopupControllerImpl> controller =
      client->generation_popup_controller();

  PasswordGenerationPopupViewViews* popup_view =
      static_cast<PasswordGenerationPopupViewViews*>(controller->view());
  ui::AXNodeData node_data;
  popup_view->GetViewAccessibility().GetAccessibleNodeData(&node_data);
  EXPECT_TRUE(node_data.HasState(ax::mojom::State::kExpanded));
  EXPECT_FALSE(node_data.HasState(ax::mojom::State::kCollapsed));
}

IN_PROC_BROWSER_TEST_F(PasswordGenerationPopupViewTest,
                       PasswordViewSelectionAccessibleState) {
  views::NamedWidgetShownWaiter waiter(views::test::AnyWidgetTestPasskey{},
                                       "PasswordGenerationPopupViewViews");
  ASSERT_TRUE(content::ExecJs(
      WebContents(), "document.getElementById('password_field').focus()"));
  auto* client = ChromePasswordManagerClient::FromWebContents(WebContents());
  client->GeneratePassword(
      autofill::password_generation::PasswordGenerationType::kManual);
  waiter.WaitIfNeededAndGet();

  base::WeakPtr<PasswordGenerationPopupControllerImpl> controller =
      client->generation_popup_controller();
  PasswordGenerationPopupViewViews* popup_view =
      static_cast<PasswordGenerationPopupViewViews*>(controller->view());

  controller->Show(PasswordGenerationPopupController::kOfferGeneration);
  controller->SelectAcceptButtonForTesting();
  const views::ViewAccessibility& accept_button =
      popup_view->GetAcceptButtonViewAccessibilityForTest();
  const views::ViewAccessibility& cancel_button =
      popup_view->GetCancelButtonViewAccessibilityForTest();
  CheckViewAccessibilitySelected(accept_button, /*selected=*/true);
  CheckViewAccessibilitySelected(cancel_button, /*selected=*/false);

  controller->SelectCancelButtonForTesting();
  CheckViewAccessibilitySelected(accept_button, /*selected=*/false);
  CheckViewAccessibilitySelected(cancel_button, /*selected=*/true);
}

IN_PROC_BROWSER_TEST_F(PasswordGenerationPopupViewTest,
                       HeaderAccessibilityProperties) {
  auto* client = ChromePasswordManagerClient::FromWebContents(WebContents());
  client->SetCurrentTargetFrameForTesting(WebContents()->GetPrimaryMainFrame());
  client->ShowPasswordEditingPopup(gfx::RectF(0, 0, 10, 10), FormData(),
                                   FieldRendererId(100), u"password123");
  // Avoid dangling pointers on shutdown.
  client->SetCurrentTargetFrameForTesting(nullptr);
  base::WeakPtr<PasswordGenerationPopupControllerImpl> controller =
      client->generation_popup_controller();

  PasswordGenerationPopupViewViews* popup_view =
      static_cast<PasswordGenerationPopupViewViews*>(controller->view());

  const std::u16string expected_cached_description = l10n_util::GetStringFUTF16(
      IDS_PASSWORD_GENERATION_PROMPT_GOOGLE_PASSWORD_MANAGER,
      l10n_util::GetStringUTF16(
          IDS_PASSWORD_BUBBLES_PASSWORD_MANAGER_LINK_TEXT_SYNCED_TO_ACCOUNT),
      u"");
  EXPECT_EQ(GetPasswordViewAccessibility(popup_view).GetCachedDescription(),
            expected_cached_description);
}

IN_PROC_BROWSER_TEST_F(PasswordGenerationPopupViewTest,
                       PasswordGenerationPopupViewAccessibilityProperties) {
  auto* client = ChromePasswordManagerClient::FromWebContents(WebContents());
  client->SetCurrentTargetFrameForTesting(WebContents()->GetPrimaryMainFrame());
  client->ShowPasswordEditingPopup(gfx::RectF(0, 0, 10, 10), FormData(),
                                   FieldRendererId(100), u"password123");
  // Avoid dangling pointers on shutdown.
  client->SetCurrentTargetFrameForTesting(nullptr);
  base::WeakPtr<PasswordGenerationPopupControllerImpl> controller =
      client->generation_popup_controller();

  PasswordGenerationPopupViewViews* popup_view =
      static_cast<PasswordGenerationPopupViewViews*>(controller->view());

  ui::AXNodeData node_data;
  popup_view->GetViewAccessibility().GetAccessibleNodeData(&node_data);
  EXPECT_EQ(node_data.role, ax::mojom::Role::kListBox);
  EXPECT_FALSE(node_data.HasState(ax::mojom::State::kInvisible));
}

#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
IN_PROC_BROWSER_TEST_F(PasswordGenerationPopupViewTest, PopupInAxTree) {
  content::ScopedAccessibilityModeOverride mode_override(ui::kAXModeComplete);

  views::NamedWidgetShownWaiter waiter(views::test::AnyWidgetTestPasskey{},
                                       "PasswordGenerationPopupViewViews");
  ASSERT_TRUE(content::ExecJs(
      WebContents(), "document.getElementById('password_field').focus()"));
  auto* client = ChromePasswordManagerClient::FromWebContents(WebContents());
  client->GeneratePassword(
      autofill::password_generation::PasswordGenerationType::kManual);
  gfx::NativeWindow window;
#if BUILDFLAG(IS_MAC)
  // On Mac the whole ax tree grows from the main root windows and the popup
  // node can be found there. This gives more confidence that it is in
  // the right place than on Linux or Windows (see below) where the popup
  // subtree lives separately.
  waiter.WaitIfNeededAndGet();
  window = chrome::FindLastActive()->window()->GetNativeWindow();
#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_WIN)
  views::Widget* dialog_widget = waiter.WaitIfNeededAndGet();
  window = dialog_widget->GetNativeWindow();
#endif
  ASSERT_TRUE(client->generation_popup_controller());

  ui::AXPlatformNode* root_node = ui::AXPlatformNode::FromNativeWindow(window);
  ui::AXPlatformNodeDelegate* root_node_delegate = root_node->GetDelegate();
  const ui::AXPlatformNodeDelegate* node_delegate =
      FindNode(root_node_delegate, "MdTextButton");

  ASSERT_THAT(node_delegate, NotNull());
  EXPECT_FALSE(
      node_delegate->GetBoolAttribute(ax::mojom::BoolAttribute::kSelected));

  // Set the screen reader focus by calling a method on the controller directly,
  // it normally is triggered by UI events when the screen reader is on,
  // screen reader presence is hard/expensive to emulate.
  client->generation_popup_controller()->SelectCancelButtonForTesting();
  EXPECT_TRUE(
      node_delegate->GetBoolAttribute(ax::mojom::BoolAttribute::kSelected));
}
#endif  // BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)

}  // namespace autofill