File: request_system_proxy_credentials_view_unittest.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 (166 lines) | stat: -rw-r--r-- 6,557 bytes parent folder | download | duplicates (6)
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
// Copyright 2020 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/ash/notifications/request_system_proxy_credentials_view.h"

#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/textfield/textfield.h"

namespace {
constexpr char kProxy[] = "http://localserver";
constexpr char16_t kUsername[] = u"testuser";
constexpr char16_t kPassword[] = u"testpwd";
}  // namespace

namespace ash {

class RequestSystemProxyCredentialsViewTest : public BrowserWithTestWindowTest {
 public:
  RequestSystemProxyCredentialsViewTest()
      : BrowserWithTestWindowTest(Browser::TYPE_NORMAL) {}
  RequestSystemProxyCredentialsViewTest(
      const RequestSystemProxyCredentialsViewTest&) = delete;
  RequestSystemProxyCredentialsViewTest& operator=(
      const RequestSystemProxyCredentialsViewTest&) = delete;
  ~RequestSystemProxyCredentialsViewTest() override = default;

  void TearDown() override {
    active_widget_->CloseNow();
    BrowserWithTestWindowTest::TearDown();
  }

 protected:
  void CreateDialog(bool show_error) {
    system_proxy_dialog_ = new RequestSystemProxyCredentialsView(
        kProxy, show_error, base::DoNothing());

    system_proxy_dialog_->SetAcceptCallback(
        base::BindRepeating(&RequestSystemProxyCredentialsViewTest::OnAccept,
                            base::Unretained(this)));
    system_proxy_dialog_->SetCancelCallback(
        base::BindRepeating(&RequestSystemProxyCredentialsViewTest::OnCancel,
                            base::Unretained(this)));

    active_widget_ = views::DialogDelegate::CreateDialogWidget(
        system_proxy_dialog_, GetContext(), /*parent=*/nullptr);
    active_widget_->Show();
  }

  void OnAccept() { accepted_ = true; }

  void OnCancel() { canceled_ = true; }

  bool accepted_ = false;
  bool canceled_ = false;
  // Owned by |active_widget_|.
  raw_ptr<RequestSystemProxyCredentialsView, DanglingUntriaged>
      system_proxy_dialog_ = nullptr;

 private:
  // Owned by the UI code (NativeWidget).
  raw_ptr<views::Widget, DanglingUntriaged> active_widget_ = nullptr;
};

// Tests that clicking "OK" in the UI will result in calling the
// |system_proxy_dialog_.accept_callback_| with the user entered
// credentials as arguments.
TEST_F(RequestSystemProxyCredentialsViewTest, AcceptCallback) {
  CreateDialog(/*show_error=*/false);
  system_proxy_dialog_->username_textfield_for_testing()->SetText(kUsername);
  system_proxy_dialog_->password_textfield_for_testing()->SetText(kPassword);

  // Simulate pressing the "OK" button.
  system_proxy_dialog_->Accept();

  EXPECT_TRUE(accepted_);
  EXPECT_EQ(system_proxy_dialog_->GetUsername(), kUsername);
  EXPECT_EQ(system_proxy_dialog_->GetPassword(), kPassword);
}

TEST_F(RequestSystemProxyCredentialsViewTest, CancelCallback) {
  CreateDialog(/*show_error=*/false);
  system_proxy_dialog_->Cancel();

  EXPECT_TRUE(canceled_);
}

TEST_F(RequestSystemProxyCredentialsViewTest, GetProxyServer) {
  CreateDialog(/*show_error=*/false);
  EXPECT_EQ(system_proxy_dialog_->GetProxyServer(), kProxy);
}

TEST_F(RequestSystemProxyCredentialsViewTest, GetWindowTitle) {
  CreateDialog(/*show_error=*/false);
  EXPECT_EQ(system_proxy_dialog_->GetWindowTitle(), u"Sign in");
}

TEST_F(RequestSystemProxyCredentialsViewTest, ErrorLabelHidden) {
  CreateDialog(/*show_error=*/false);
  EXPECT_FALSE(system_proxy_dialog_->error_label_for_testing()->GetVisible());
}

TEST_F(RequestSystemProxyCredentialsViewTest, ErrorLabelVisible) {
  CreateDialog(/*show_error=*/true);
  EXPECT_TRUE(system_proxy_dialog_->error_label_for_testing()->GetVisible());
}

TEST_F(RequestSystemProxyCredentialsViewTest, TextfieldAccessibility) {
  CreateDialog(/*show_error=*/false);

  ui::AXNodeData username_data;
  auto* username_field = system_proxy_dialog_->username_textfield_for_testing();
  username_field->GetViewAccessibility().GetAccessibleNodeData(&username_data);
  EXPECT_EQ(username_data.role, ax::mojom::Role::kTextField);
  EXPECT_EQ(username_field->GetViewAccessibility().GetCachedRole(),
            ax::mojom::Role::kTextField);
  EXPECT_EQ(
      username_data.GetString16Attribute(ax::mojom::StringAttribute::kName),
      l10n_util::GetStringUTF16(IDS_SYSTEM_PROXY_AUTH_DIALOG_USERNAME_LABEL));
  EXPECT_EQ(
      username_field->GetViewAccessibility().GetCachedName(),
      l10n_util::GetStringUTF16(IDS_SYSTEM_PROXY_AUTH_DIALOG_USERNAME_LABEL));
  EXPECT_EQ(username_data.GetNameFrom(), ax::mojom::NameFrom::kRelatedElement);
  EXPECT_TRUE(username_data.HasIntListAttribute(
      ax::mojom::IntListAttribute::kLabelledbyIds));
  EXPECT_TRUE(username_data.HasState(ax::mojom::State::kEditable));
  EXPECT_FALSE(username_data.HasState(ax::mojom::State::kProtected));
  EXPECT_EQ(username_data.GetDefaultActionVerb(),
            ax::mojom::DefaultActionVerb::kActivate);

  ui::AXNodeData password_data;
  auto* password_field = system_proxy_dialog_->password_textfield_for_testing();
  password_field->GetViewAccessibility().GetAccessibleNodeData(&password_data);
  EXPECT_EQ(password_data.role, ax::mojom::Role::kTextField);
  EXPECT_EQ(password_field->GetViewAccessibility().GetCachedRole(),
            ax::mojom::Role::kTextField);
  EXPECT_EQ(
      password_data.GetString16Attribute(ax::mojom::StringAttribute::kName),
      l10n_util::GetStringUTF16(IDS_SYSTEM_PROXY_AUTH_DIALOG_PASSWORD_LABEL));
  EXPECT_EQ(
      password_field->GetViewAccessibility().GetCachedName(),
      l10n_util::GetStringUTF16(IDS_SYSTEM_PROXY_AUTH_DIALOG_PASSWORD_LABEL));
  EXPECT_EQ(password_data.GetNameFrom(), ax::mojom::NameFrom::kRelatedElement);
  EXPECT_TRUE(password_data.HasIntListAttribute(
      ax::mojom::IntListAttribute::kLabelledbyIds));
  EXPECT_TRUE(password_data.HasState(ax::mojom::State::kEditable));
  EXPECT_TRUE(password_data.HasState(ax::mojom::State::kProtected));
  EXPECT_EQ(password_data.GetDefaultActionVerb(),
            ax::mojom::DefaultActionVerb::kActivate);
}

}  // namespace ash