File: message_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 (125 lines) | stat: -rw-r--r-- 4,689 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
// Copyright 2024 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/message_center/views/message_view.h"

#include <memory>

#include "testing/gmock/include/gmock/gmock.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/events/test/event_generator.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/views/widget/widget_utils.h"

namespace message_center {

class NotificationControlButtonsView;

class TestMessageView : public MessageView {
 public:
  explicit TestMessageView(const Notification& notification)
      : MessageView(notification) {
    SetLayoutManager(std::make_unique<views::BoxLayout>());
    AddChildView(std::make_unique<views::Label>())
        ->SetText(notification.title());
    AddChildView((std::make_unique<views::Label>()))
        ->SetText(notification.message());
  }
  TestMessageView(const TestMessageView&) = delete;
  TestMessageView& operator=(const TestMessageView&) = delete;
  ~TestMessageView() override = default;

  // MessageView:
  MOCK_METHOD(void, UpdateControlButtonsVisibility, ());
  NotificationControlButtonsView* GetControlButtonsView() const override {
    return nullptr;
  }
};

class MessageViewTest : public views::ViewsTestBase {
 public:
  MessageViewTest() = default;
  MessageViewTest(const MessageViewTest&) = delete;
  MessageViewTest& operator=(const MessageViewTest&) = delete;
  ~MessageViewTest() override = default;

  // Overridden from ViewsTestBase:
  void SetUp() override {
    views::ViewsTestBase::SetUp();
    MessageCenter::Initialize();
    notification_ = std::make_unique<Notification>(
        NOTIFICATION_TYPE_SIMPLE, "id", u"title", u"test message",
        ui::ImageModel(), /*display_source=*/std::u16string(), GURL(),
        NotifierId(), RichNotificationData(), /*delegate=*/nullptr);

    // `widget_` owns `message_view_`.
    widget_ =
        CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
    message_view_ = widget_->SetContentsView(
        std::make_unique<TestMessageView>(*notification_.get()));
    widget_->Show();
  }

  void TearDown() override {
    message_view_ = nullptr;
    widget_.reset();
    MessageCenter::Shutdown();
    views::ViewsTestBase::TearDown();
  }

 protected:
  TestMessageView* message_view() { return message_view_.get(); }

 private:
  raw_ptr<TestMessageView> message_view_;
  std::unique_ptr<Notification> notification_;
  std::unique_ptr<views::Widget> widget_;
};

// Updating control buttons visibility is a ChromeOS only feature.
#if BUILDFLAG(IS_CHROMEOS)
// Make sure UpdateControlButtonsVisibility is called when their is a mouse
// enter or exit on a MessageView.
TEST_F(MessageViewTest, UpdateControlButtonsVisibilityCalled) {
  ui::test::EventGenerator event_generator(
      GetRootWindow(message_view()->GetWidget()));

  // Expect a call on the mouse entering the message view.
  EXPECT_CALL(*message_view(), UpdateControlButtonsVisibility);
  event_generator.MoveMouseTo(message_view()->GetBoundsInScreen().CenterPoint(),
                              10);

  // Expect a call on the mouse exiting the message view.
  EXPECT_CALL(*message_view(), UpdateControlButtonsVisibility);
  event_generator.MoveMouseTo(
      message_view()->GetBoundsInScreen().origin() - gfx::Vector2d(10, 10), 10);
}
#endif  // BUILDFLAG(IS_CHROMEOS)

TEST_F(MessageViewTest, AccessibleAttributes) {
  ui::AXNodeData data;
  message_view()->GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_EQ(data.role, ax::mojom::Role::kGenericContainer);

  auto notification = std::make_unique<Notification>(
      NOTIFICATION_TYPE_SIMPLE, "notification", u"", u"", ui::ImageModel(),
      /*display_source=*/std::u16string(), GURL(), NotifierId(),
      RichNotificationData(), /*delegate=*/nullptr);
  message_view()->UpdateWithNotification(*notification.get());
  data = ui::AXNodeData();
  message_view()->GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_EQ(data.GetString16Attribute(ax::mojom::StringAttribute::kName), u"");
  EXPECT_EQ(data.GetNameFrom(), ax::mojom::NameFrom::kAttributeExplicitlyEmpty);
  EXPECT_EQ(
      data.GetString16Attribute(ax::mojom::StringAttribute::kRoleDescription),
      l10n_util::GetStringUTF16(IDS_MESSAGE_NOTIFICATION_ACCESSIBLE_NAME));
}

}  // namespace message_center