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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/app_menu/notification_menu_view.h"
#include "ash/app_menu/notification_item_view.h"
#include "ash/app_menu/notification_menu_view_test_api.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/geometry/transform.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/views/controls/label.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/test/views_test_utils.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/views/widget/widget_utils.h"
namespace ash {
namespace {
// The app id used in tests.
constexpr char kTestAppId[] = "test-app-id";
class MockNotificationMenuController : public views::SlideOutControllerDelegate,
public NotificationMenuView::Delegate {
public:
MockNotificationMenuController() = default;
MockNotificationMenuController(const MockNotificationMenuController&) =
delete;
MockNotificationMenuController& operator=(
const MockNotificationMenuController&) = delete;
~MockNotificationMenuController() override = default;
void ActivateNotificationAndClose(
const std::string& notification_id) override {
activation_count_++;
}
void OnOverflowAddedOrRemoved() override {
overflow_added_or_removed_count_++;
}
ui::Layer* GetSlideOutLayer() override {
return notification_menu_view_->GetSlideOutLayer();
}
void OnSlideChanged(bool in_progress) override {}
void OnSlideOut() override { slide_out_count_++; }
void set_notification_menu_view(
NotificationMenuView* notification_menu_view) {
notification_menu_view_ = notification_menu_view;
}
int slide_out_count_ = 0;
int activation_count_ = 0;
int overflow_added_or_removed_count_ = 0;
// Owned by NotificationMenuViewTest.
raw_ptr<NotificationMenuView, DanglingUntriaged> notification_menu_view_ =
nullptr;
};
} // namespace
class NotificationMenuViewTest : public views::ViewsTestBase {
public:
NotificationMenuViewTest() {}
NotificationMenuViewTest(const NotificationMenuViewTest&) = delete;
NotificationMenuViewTest& operator=(const NotificationMenuViewTest&) = delete;
~NotificationMenuViewTest() override = default;
// views::ViewsTestBase:
void SetUp() override {
views::ViewsTestBase::SetUp();
zero_duration_scope_ =
std::make_unique<ui::ScopedAnimationDurationScaleMode>(
ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
mock_notification_menu_controller_ =
std::make_unique<MockNotificationMenuController>();
auto notification_menu_view = std::make_unique<NotificationMenuView>(
mock_notification_menu_controller_.get(),
mock_notification_menu_controller_.get(), kTestAppId);
// Set the NotificationMenuView so |mock_notification_menu_controller_|
// can get the slide out layer. In production NotificationMenuController is
// the NotificationItemViewDelegate, and it gets a reference to
// NotificationMenuView when it is created.
mock_notification_menu_controller_->set_notification_menu_view(
notification_menu_view.get());
test_api_ = std::make_unique<NotificationMenuViewTestAPI>(
notification_menu_view.get());
widget_ = std::make_unique<views::Widget>();
views::Widget::InitParams init_params(
CreateParams(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET,
views::Widget::InitParams::TYPE_POPUP));
init_params.activatable = views::Widget::InitParams::Activatable::kYes;
widget_->Init(std::move(init_params));
notification_menu_view_ =
widget_->SetContentsView(std::move(notification_menu_view));
widget_->SetSize(notification_menu_view_->GetPreferredSize());
widget_->Show();
widget_->Activate();
}
void TearDown() override {
widget_->Close();
views::ViewsTestBase::TearDown();
}
message_center::Notification AddNotification(
const std::string& notification_id,
const std::u16string& title,
const std::u16string& message) {
const message_center::NotifierId notifier_id(
message_center::NotifierType::APPLICATION, kTestAppId);
message_center::Notification notification(
message_center::NOTIFICATION_TYPE_SIMPLE, notification_id, title,
message, ui::ImageModel(), u"www.test.org", GURL(), notifier_id,
message_center::RichNotificationData(), nullptr /* delegate */);
notification_menu_view_->AddNotificationItemView(notification);
views::test::RunScheduledLayout(notification_menu_view_);
return notification;
}
message_center::Notification UpdateNotification(
const std::string& notification_id,
const std::u16string& title,
const std::u16string& message) {
const message_center::NotifierId notifier_id(
message_center::NotifierType::APPLICATION, kTestAppId);
message_center::Notification notification(
message_center::NOTIFICATION_TYPE_SIMPLE, notification_id, title,
message, ui::ImageModel(), u"www.test.org", GURL(), notifier_id,
message_center::RichNotificationData(), nullptr /* delegate */);
notification_menu_view_->UpdateNotificationItemView(notification);
return notification;
}
void CheckDisplayedNotification(
const message_center::Notification& notification) {
// Check whether the notification and view contents match.
const auto* item =
notification_menu_view_->GetDisplayedNotificationItemView();
ASSERT_TRUE(item);
EXPECT_EQ(item->notification_id(), notification.id());
EXPECT_EQ(item->title(), notification.title());
EXPECT_EQ(item->message(), notification.message());
}
void BeginScroll() {
DispatchGesture(
ui::GestureEventDetails(ui::EventType::kGestureScrollBegin));
}
void EndScroll() {
DispatchGesture(ui::GestureEventDetails(ui::EventType::kGestureScrollEnd));
}
void ScrollBy(int dx) {
DispatchGesture(
ui::GestureEventDetails(ui::EventType::kGestureScrollUpdate, dx, 0));
}
void DispatchGesture(const ui::GestureEventDetails& details) {
ui::test::EventGenerator generator(
GetRootWindow(notification_menu_view_->GetWidget()));
const auto* item =
notification_menu_view_->GetDisplayedNotificationItemView();
ui::GestureEvent event(0, item->GetBoundsInScreen().y(), 0,
ui::EventTimeForNow(), details);
generator.Dispatch(&event);
}
float GetSlideAmount() const {
return notification_menu_view_->GetSlideOutLayer()
->transform()
.To2dTranslation()
.x();
}
NotificationMenuView* notification_menu_view() {
return notification_menu_view_;
}
NotificationMenuViewTestAPI* test_api() { return test_api_.get(); }
MockNotificationMenuController* mock_notification_menu_controller() {
return mock_notification_menu_controller_.get();
}
private:
std::unique_ptr<MockNotificationMenuController>
mock_notification_menu_controller_;
raw_ptr<NotificationMenuView, DanglingUntriaged> notification_menu_view_;
std::unique_ptr<NotificationMenuViewTestAPI> test_api_;
std::unique_ptr<views::Widget> widget_;
std::unique_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_scope_;
};
// Tests that the correct NotificationItemView is shown when notifications come
// and go.
TEST_F(NotificationMenuViewTest, Basic) {
// Add a notification to the view.
const message_center::Notification notification_0 =
AddNotification("notification_id_0", u"title_0", u"message_0");
// The counter should update to 1, and the displayed NotificationItemView
// should match the notification.
EXPECT_EQ(base::NumberToString16(1), test_api()->GetCounterViewContents());
EXPECT_EQ(1, test_api()->GetItemViewCount());
CheckDisplayedNotification(notification_0);
// Add a second notification to the view, the counter view and displayed
// NotificationItemView should change.
const message_center::Notification notification_1 =
AddNotification("notification_id_1", u"title_1", u"message_1");
EXPECT_EQ(base::NumberToString16(2), test_api()->GetCounterViewContents());
EXPECT_EQ(2, test_api()->GetItemViewCount());
CheckDisplayedNotification(notification_1);
// Remove |notification_1|, |notification_0| should be shown.
notification_menu_view()->OnNotificationRemoved(notification_1.id());
EXPECT_EQ(base::NumberToString16(1), test_api()->GetCounterViewContents());
EXPECT_EQ(1, test_api()->GetItemViewCount());
CheckDisplayedNotification(notification_0);
}
TEST_F(NotificationMenuViewTest, MultipleNotificationsBasic) {
// Add multiple notifications to the view.
const message_center::Notification notification_0 =
AddNotification("notification_id_0", u"title_0", u"message_0");
// Overflow should not be created until there are two notifications.
EXPECT_FALSE(test_api()->GetOverflowView());
EXPECT_EQ(
0, mock_notification_menu_controller()->overflow_added_or_removed_count_);
// Add a second notification, this will push |notification_0| into overflow.
const message_center::Notification notification_1 =
AddNotification("notification_id_1", u"title_1", u"message_1");
CheckDisplayedNotification(notification_1);
EXPECT_TRUE(test_api()->GetOverflowView());
EXPECT_EQ(
1, mock_notification_menu_controller()->overflow_added_or_removed_count_);
// Remove the notification that is in overflow.
notification_menu_view()->OnNotificationRemoved(notification_0.id());
// The displayed notification should not have changed, and the overflow view
// should be deleted.
CheckDisplayedNotification(notification_1);
EXPECT_FALSE(test_api()->GetOverflowView());
}
// Tests that when the displayed NotificationItemView is removed, the
// notification from the overflow view becomes the displayed view.
TEST_F(NotificationMenuViewTest, ShowNotificationFromOverflow) {
// Add multiple notifications to the view.
const message_center::Notification notification_0 =
AddNotification("notification_id_0", u"title_0", u"message_0");
EXPECT_FALSE(test_api()->GetOverflowView());
const message_center::Notification notification_1 =
AddNotification("notification_id_1", u"title_1", u"message_1");
// |notification_1| should be the displayed NotificationItemView.
CheckDisplayedNotification(notification_1);
EXPECT_TRUE(test_api()->GetOverflowView());
// Remove the displayed NotificationItemView, the overflow notification should
// take its place and overflow should be removed.
notification_menu_view()->OnNotificationRemoved(notification_1.id());
CheckDisplayedNotification(notification_0);
EXPECT_FALSE(test_api()->GetOverflowView());
}
// Tests that removing a notification that is not being shown only updates the
// counter.
TEST_F(NotificationMenuViewTest, RemoveOlderNotification) {
// Add two notifications.
const message_center::Notification notification_0 =
AddNotification("notification_id_0", u"title_0", u"message_0");
const message_center::Notification notification_1 =
AddNotification("notification_id_1", u"title_1", u"message_1");
// The latest notification should be shown.
EXPECT_EQ(base::NumberToString16(2), test_api()->GetCounterViewContents());
EXPECT_EQ(2, test_api()->GetItemViewCount());
CheckDisplayedNotification(notification_1);
// Remove the older notification, |notification_0|.
notification_menu_view()->OnNotificationRemoved(notification_0.id());
// The latest notification, |notification_1|, should be shown.
EXPECT_EQ(base::NumberToString16(1), test_api()->GetCounterViewContents());
EXPECT_EQ(1, test_api()->GetItemViewCount());
CheckDisplayedNotification(notification_1);
}
// Tests that the displayed NotificationItemView is only dismissed when dragged
// beyond the threshold.
// TODO(crbug.com/401481106): This test is very flaky on Linux under Asan+Lsan.
#if BUILDFLAG(IS_LINUX) && defined(ADDRESS_SANITIZER)
#define MAYBE_SlideOut DISABLED_SlideOut
#else
#define MAYBE_SlideOut SlideOut
#endif
TEST_F(NotificationMenuViewTest, MAYBE_SlideOut) {
AddNotification("notification_id", u"title", u"message");
EXPECT_EQ(0, mock_notification_menu_controller()->slide_out_count_);
BeginScroll();
// Scroll by a small amount, the notification should move but not slide out.
ScrollBy(-10);
EXPECT_EQ(0, mock_notification_menu_controller()->slide_out_count_);
EXPECT_EQ(-10.f, GetSlideAmount());
// End the scroll gesture, the notifications should return to its resting
// place.
EndScroll();
EXPECT_EQ(0, mock_notification_menu_controller()->slide_out_count_);
EXPECT_EQ(0.f, GetSlideAmount());
BeginScroll();
// Scroll beyond the threshold but do not release the gesture scroll.
ScrollBy(-200);
EXPECT_EQ(-200.f, GetSlideAmount());
// Release the gesture, the notification should slide out.
EndScroll();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, mock_notification_menu_controller()->slide_out_count_);
EXPECT_EQ(0, mock_notification_menu_controller()->activation_count_);
}
// Tests that tapping a notification activates it.
TEST_F(NotificationMenuViewTest, TapNotification) {
AddNotification("notification_id", u"title", u"message");
EXPECT_EQ(0, mock_notification_menu_controller()->activation_count_);
DispatchGesture(ui::GestureEventDetails(ui::EventType::kGestureTap));
EXPECT_EQ(1, mock_notification_menu_controller()->activation_count_);
}
// Tests that an in bounds mouse release activates a notification.
TEST_F(NotificationMenuViewTest, ClickNotification) {
AddNotification("notification_id", u"title", u"message");
EXPECT_EQ(0, mock_notification_menu_controller()->activation_count_);
const auto* item =
notification_menu_view()->GetDisplayedNotificationItemView();
const gfx::Point cursor_location = item->GetBoundsInScreen().origin();
ui::MouseEvent press(ui::EventType::kMousePressed, cursor_location,
cursor_location, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_NONE);
notification_menu_view()->GetWidget()->OnMouseEvent(&press);
EXPECT_EQ(0, mock_notification_menu_controller()->activation_count_);
ui::MouseEvent release(ui::EventType::kMouseReleased, cursor_location,
cursor_location, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_NONE);
notification_menu_view()->GetWidget()->OnMouseEvent(&release);
EXPECT_EQ(1, mock_notification_menu_controller()->activation_count_);
}
// Tests that an out of bounds mouse release does not activate a notification.
TEST_F(NotificationMenuViewTest, OutOfBoundsClick) {
AddNotification("notification_id", u"title", u"message");
EXPECT_EQ(0, mock_notification_menu_controller()->activation_count_);
const auto* item =
notification_menu_view()->GetDisplayedNotificationItemView();
const gfx::Point cursor_location = item->GetBoundsInScreen().origin();
ui::MouseEvent press(ui::EventType::kMousePressed, cursor_location,
cursor_location, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_NONE);
notification_menu_view()->GetWidget()->OnMouseEvent(&press);
EXPECT_EQ(0, mock_notification_menu_controller()->activation_count_);
const gfx::Point out_of_bounds;
ui::MouseEvent out_of_bounds_release(
ui::EventType::kMouseReleased, out_of_bounds, out_of_bounds,
ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, ui::EF_NONE);
notification_menu_view()->GetWidget()->OnMouseEvent(&out_of_bounds_release);
EXPECT_EQ(0, mock_notification_menu_controller()->activation_count_);
}
// Tests updating notifications that do and do not exist.
TEST_F(NotificationMenuViewTest, UpdateNotification) {
// Add a notification.
const std::string notification_id = "notification_id";
AddNotification(notification_id, u"title", u"message");
// Send an updated notification with a matching |notification_id|.
const message_center::Notification updated_notification =
UpdateNotification(notification_id, u"new_title", u"new_message");
// The displayed notification's contents should have changed to match the
// updated notification.
EXPECT_EQ(base::NumberToString16(1), test_api()->GetCounterViewContents());
EXPECT_EQ(1, test_api()->GetItemViewCount());
CheckDisplayedNotification(updated_notification);
// Send an updated notification for a notification which doesn't yet exist.
UpdateNotification("Bad notification", u"Bad Title", u"Bad Message");
// Test that the displayed notification has not been changed.
EXPECT_EQ(base::NumberToString16(1), test_api()->GetCounterViewContents());
EXPECT_EQ(1, test_api()->GetItemViewCount());
CheckDisplayedNotification(updated_notification);
}
} // namespace ash
|