File: popups_only_ui_controller_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; 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 (128 lines) | stat: -rw-r--r-- 4,741 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
// Copyright 2013 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/notifications/popups_only_ui_controller.h"

#include <stddef.h>

#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "content/public/test/test_utils.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/notification_list.h"
#include "ui/message_center/public/cpp/message_center_constants.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_delegate.h"
#include "ui/message_center/public/cpp/notification_types.h"
#include "ui/message_center/views/message_popup_collection.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

using message_center::MessageCenter;
using message_center::Notification;
using message_center::NotifierId;

namespace {

class PopupsOnlyUiControllerTest : public views::test::WidgetTest {
 public:
  PopupsOnlyUiControllerTest() = default;
  PopupsOnlyUiControllerTest(const PopupsOnlyUiControllerTest&) = delete;
  PopupsOnlyUiControllerTest& operator=(const PopupsOnlyUiControllerTest&) =
      delete;
  ~PopupsOnlyUiControllerTest() override = default;

  void SetUp() override {
    set_native_widget_type(NativeWidgetType::kDesktop);
    views::test::WidgetTest::SetUp();
    MessageCenter::Initialize();
  }

  void TearDown() override {
    MessageCenter::Get()->RemoveAllNotifications(
        false, MessageCenter::RemoveType::ALL);

    for (views::Widget* widget : GetAllWidgets())
      widget->CloseNow();
    MessageCenter::Shutdown();
    views::test::WidgetTest::TearDown();
  }

 protected:
  void AddNotification(const std::string& id) {
    auto notification = std::make_unique<Notification>(
        message_center::NOTIFICATION_TYPE_SIMPLE, id, u"Test Web Notification",
        u"Notification message body.", ui::ImageModel(),
        u"Some Chrome extension", GURL("chrome-extension://abbccedd"),
        NotifierId(message_center::NotifierType::APPLICATION, id),
        message_center::RichNotificationData(), nullptr);

    MessageCenter::Get()->AddNotification(std::move(notification));
  }

  void UpdateNotification(const std::string& id) {
    auto notification = std::make_unique<Notification>(
        message_center::NOTIFICATION_TYPE_SIMPLE, id,
        u"Updated Test Web Notification", u"Notification message body.",
        ui::ImageModel(), u"Some Chrome extension",
        GURL("chrome-extension://abbccedd"),
        NotifierId(message_center::NotifierType::APPLICATION, id),
        message_center::RichNotificationData(), nullptr);

    MessageCenter::Get()->UpdateNotification(id, std::move(notification));
  }

  void RemoveNotification(const std::string& id) {
    MessageCenter::Get()->RemoveNotification(id, false);
  }

  bool HasNotification(const std::string& id) {
    return !!MessageCenter::Get()->FindVisibleNotificationById(id);
  }
};

TEST_F(PopupsOnlyUiControllerTest, WebNotificationPopupBubble) {
  auto ui_controller = std::make_unique<PopupsOnlyUiController>();

  // Adding a notification should show the popup bubble.
  AddNotification("id1");
  EXPECT_TRUE(ui_controller->popups_visible());

  // Updating a notification should not hide the popup bubble.
  AddNotification("id2");
  UpdateNotification("id2");
  EXPECT_TRUE(ui_controller->popups_visible());

  // Removing the first notification should not hide the popup bubble.
  RemoveNotification("id1");
  EXPECT_TRUE(ui_controller->popups_visible());

  // Removing the visible notification should hide the popup bubble.
  RemoveNotification("id2");
  EXPECT_FALSE(ui_controller->popups_visible());
}

TEST_F(PopupsOnlyUiControllerTest, ManyPopupNotifications) {
  auto ui_controller = std::make_unique<PopupsOnlyUiController>();

  // Add the max visible popup notifications +1, ensure the correct num visible.
  size_t notifications_to_add =
      message_center::kMaxVisiblePopupNotifications + 1;
  for (size_t i = 0; i < notifications_to_add; ++i) {
    std::string id = base::StringPrintf("id%d", static_cast<int>(i));
    AddNotification(id);
  }
  EXPECT_TRUE(ui_controller->popups_visible());
  MessageCenter* message_center = MessageCenter::Get();
  EXPECT_EQ(notifications_to_add, message_center->NotificationCount());
  message_center::NotificationList::PopupNotifications popups =
      message_center->GetPopupNotifications();
  EXPECT_EQ(message_center::kMaxVisiblePopupNotifications, popups.size());
}

}  // namespace