File: web_dialog_view_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (230 lines) | stat: -rw-r--r-- 8,410 bytes parent folder | download | duplicates (5)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/controls/webview/web_dialog_view.h"

#include <memory>
#include <utility>

#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/common/content_client.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_renderer_host.h"
#include "content/test/test_content_browser_client.h"
#include "content/test/test_web_contents.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/test/view_metadata_test_utils.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/window/dialog_delegate.h"
#include "ui/web_dialogs/test/test_web_contents_handler.h"
#include "ui/web_dialogs/test/test_web_dialog_delegate.h"
#include "ui/web_dialogs/web_dialog_web_contents_delegate.h"
#include "url/gurl.h"

namespace views {

// Testing delegate configured for use in this test.
class TestWebDialogViewWebDialogDelegate
    : public ui::test::TestWebDialogDelegate {
 public:
  TestWebDialogViewWebDialogDelegate()
      : ui::test::TestWebDialogDelegate(GURL()) {}
  void set_close_on_escape(bool close_on_escape) {
    close_on_escape_ = close_on_escape;
  }

  // ui::WebDialogDelegate
  bool OnDialogCloseRequested() override { return true; }
  bool ShouldCloseDialogOnEscape() const override { return close_on_escape_; }
  ui::mojom::ModalType GetDialogModalType() const override {
    return ui::mojom::ModalType::kWindow;
  }

 private:
  bool close_on_escape_ = true;
};

// Provides functionality to test a WebDialogView.
class WebDialogViewUnitTest : public views::test::WidgetTest {
 public:
  WebDialogViewUnitTest()
      : views::test::WidgetTest(std::unique_ptr<base::test::TaskEnvironment>(
            std::make_unique<content::BrowserTaskEnvironment>())) {}

  WebDialogViewUnitTest(const WebDialogViewUnitTest&) = delete;
  WebDialogViewUnitTest& operator=(const WebDialogViewUnitTest&) = delete;

  ~WebDialogViewUnitTest() override = default;

  // testing::Test
  void SetUp() override {
    views::test::WidgetTest::SetUp();

    // Set the test content browser client to avoid pulling in needless
    // dependencies from content.
    SetBrowserClientForTesting(&test_browser_client_);

    browser_context_ = std::make_unique<content::TestBrowserContext>();

    web_dialog_delegate_ =
        std::make_unique<TestWebDialogViewWebDialogDelegate>();
    web_contents_ = CreateWebContents();
    web_dialog_view_ = new WebDialogView(
        web_contents_->GetBrowserContext(), web_dialog_delegate_.get(),
        std::make_unique<ui::test::TestWebContentsHandler>());

    // This prevents the initialization of the dialog from navigating
    // to the URL in the WebUI.  This is needed because the WebUI
    // loading code that would otherwise attempt to use our TestBrowserContext,
    // but will fail because TestBrowserContext not an instance of
    // Profile (i.e. TestingProfile).  We cannot, however, create an instance of
    // TestingProfile in this test due to dependency restrictions between the
    // views code and the location of TestingProfile.
    web_dialog_view_->disable_url_load_for_test_ = true;

    widget_ = views::DialogDelegate::CreateDialogWidget(
        web_dialog_view_, GetContext(), gfx::NativeView());
    widget_->Show();
    EXPECT_FALSE(widget_is_closed());
  }

  void TearDown() override {
    web_dialog_view_ = nullptr;
    widget_.ExtractAsDangling()->CloseNow();
    views::test::WidgetTest::TearDown();
  }

  bool widget_is_closed() { return widget_->IsClosed(); }

  WebDialogView* web_dialog_view() { return web_dialog_view_; }

  views::WebView* web_view() {
    return web_dialog_view_ ? web_dialog_view_->web_view_.get() : nullptr;
  }

  ui::WebDialogDelegate* web_view_delegate() {
    return web_dialog_view_ ? web_dialog_view_->delegate_.get() : nullptr;
  }

  TestWebDialogViewWebDialogDelegate* web_dialog_delegate() {
    return web_dialog_delegate_.get();
  }

  void ResetWebDialogDelegate() { web_dialog_delegate_.reset(); }

 protected:
  std::unique_ptr<content::TestWebContents> CreateWebContents() const {
    return base::WrapUnique<content::TestWebContents>(
        content::TestWebContents::Create(
            content::WebContents::CreateParams(browser_context_.get())));
  }

  void SimulateKeyEvent(const ui::KeyEvent& event) {
    ASSERT_TRUE(web_dialog_view_->GetFocusManager() != nullptr);
    ASSERT_TRUE(widget_ != nullptr);
    ui::KeyEvent event_copy = event;
    if (web_dialog_view_->GetFocusManager()->OnKeyEvent(event_copy)) {
      widget_->OnKeyEvent(&event_copy);
    }
  }

 private:
  content::RenderViewHostTestEnabler test_render_host_factories_;
  content::TestContentBrowserClient test_browser_client_;
  std::unique_ptr<content::TestBrowserContext> browser_context_;
  // These are raw pointers (vs unique pointers) because the views
  // system does its own internal memory management.
  raw_ptr<views::Widget> widget_ = nullptr;
  raw_ptr<WebDialogView> web_dialog_view_ = nullptr;
  base::RepeatingClosure quit_closure_;

  std::unique_ptr<TestWebDialogViewWebDialogDelegate> web_dialog_delegate_;
  std::unique_ptr<content::TestWebContents> web_contents_;
};

TEST_F(WebDialogViewUnitTest, WebDialogViewClosedOnEscape) {
  web_dialog_delegate()->set_close_on_escape(true);
  const ui::KeyEvent escape_event(ui::EventType::kKeyPressed, ui::VKEY_ESCAPE,
                                  ui::EF_NONE);
  SimulateKeyEvent(escape_event);

  // The Dialog Widget should be closed when escape is pressed.
  EXPECT_TRUE(widget_is_closed());
}

TEST_F(WebDialogViewUnitTest, WebDialogViewNotClosedOnEscape) {
  web_dialog_delegate()->set_close_on_escape(false);
  const ui::KeyEvent escape_event(ui::EventType::kKeyPressed, ui::VKEY_ESCAPE,
                                  ui::EF_NONE);
  SimulateKeyEvent(escape_event);

  // The Dialog Widget should not be closed when escape is pressed.
  EXPECT_FALSE(widget_is_closed());
}

TEST_F(WebDialogViewUnitTest, ObservableWebViewOnWebDialogViewClosed) {
  // Close the widget by pressing ESC key.
  web_dialog_delegate()->set_close_on_escape(true);
  const ui::KeyEvent escape_event(ui::EventType::kKeyPressed, ui::VKEY_ESCAPE,
                                  ui::EF_NONE);
  SimulateKeyEvent(escape_event);

  // The Dialog Widget should be closed .
  ASSERT_TRUE(widget_is_closed());
  // The delegate should be nullified so no further communication with it.
  EXPECT_FALSE(web_view_delegate());

  ResetWebDialogDelegate();
}

TEST_F(WebDialogViewUnitTest, RootViewAccessibleName) {
  ui::AXNodeData root_view_data;
  web_dialog_view()
      ->GetWidget()
      ->GetRootView()
      ->GetViewAccessibility()
      .GetAccessibleNodeData(&root_view_data);
  EXPECT_EQ(
      root_view_data.GetString16Attribute(ax::mojom::StringAttribute::kName),
      web_dialog_view()->GetAccessibleWindowTitle());

  root_view_data = ui::AXNodeData();
  web_dialog_view()
      ->GetWidget()
      ->GetRootView()
      ->GetViewAccessibility()
      .GetAccessibleNodeData(&root_view_data);
  EXPECT_EQ(u"Test", web_dialog_view()->GetAccessibleWindowTitle());
  EXPECT_EQ(
      root_view_data.GetString16Attribute(ax::mojom::StringAttribute::kName),
      web_dialog_view()->GetAccessibleWindowTitle());

  web_dialog_delegate()->set_accessible_dialog_title(u"Acessible Dialog Title");
  root_view_data = ui::AXNodeData();
  web_dialog_view()
      ->GetWidget()
      ->GetRootView()
      ->GetViewAccessibility()
      .GetAccessibleNodeData(&root_view_data);
  EXPECT_EQ(u"Acessible Dialog Title",
            web_dialog_view()->GetAccessibleWindowTitle());
  EXPECT_EQ(
      root_view_data.GetString16Attribute(ax::mojom::StringAttribute::kName),
      web_dialog_view()->GetAccessibleWindowTitle());
}

TEST_F(WebDialogViewUnitTest, MetadataTest) {
  test::TestViewMetadata(web_dialog_view());
}

}  // namespace views