Commit faee226f authored by James Cook's avatar James Cook Committed by Commit Bot

lacros: Add GetDisplayedNotifications to MessageCenter crosapi

This method returns a list of the ids of displayed notifications. Later
CLs will use this for two things:

- To re-synchronize lacros-chrome's cached view of existing
  notifications after a crash (see C++ NotificationPlatformBridge
  GetDisplayed() method).
- To integration-test the MessageCenter crosapi.

Bug: 1134389
Test: added to unit_tests
Change-Id: Iad877a20216c813b72c265d1b23deb1cfeed1353
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2445849Reviewed-by: default avatarJun Mukai <mukai@chromium.org>
Reviewed-by: default avatarErik Chen <erikchen@chromium.org>
Reviewed-by: default avatarJorge Lucangeli Obes <jorgelo@chromium.org>
Commit-Queue: James Cook <jamescook@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813243}
parent 2fd5742a
......@@ -4,7 +4,9 @@
#include "chrome/browser/chromeos/crosapi/message_center_ash.h"
#include <string>
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/check.h"
......@@ -177,4 +179,14 @@ void MessageCenterAsh::CloseNotification(const std::string& id) {
mc::MessageCenter::Get()->RemoveNotification(id, /*by_user=*/false);
}
void MessageCenterAsh::GetDisplayedNotifications(
GetDisplayedNotificationsCallback callback) {
mc::NotificationList::Notifications notifications =
mc::MessageCenter::Get()->GetNotifications();
std::vector<std::string> ids;
for (mc::Notification* notification : notifications)
ids.push_back(notification->id());
std::move(callback).Run(ids);
}
} // namespace crosapi
......@@ -27,6 +27,8 @@ class MessageCenterAsh : public mojom::MessageCenter {
mojom::NotificationPtr notification,
mojo::PendingRemote<mojom::NotificationDelegate> delegate) override;
void CloseNotification(const std::string& id) override;
void GetDisplayedNotifications(
GetDisplayedNotificationsCallback callback) override;
private:
mojo::Receiver<mojom::MessageCenter> receiver_;
......
......@@ -4,21 +4,28 @@
#include "chrome/browser/chromeos/crosapi/message_center_ash.h"
#include <memory>
#include <string>
#include "base/optional.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "chromeos/crosapi/mojom/message_center.mojom-test-utils.h"
#include "chromeos/crosapi/mojom/message_center.mojom.h"
#include "chromeos/crosapi/mojom/notification.mojom.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_delegate.h"
#include "ui/message_center/public/cpp/notifier_id.h"
#include "url/gurl.h"
using base::ASCIIToUTF16;
......@@ -28,6 +35,16 @@ using gfx::test::AreImagesEqual;
namespace crosapi {
namespace {
// Creates an ash message center notification.
std::unique_ptr<message_center::Notification> CreateNotificationWithId(
const std::string& id) {
return std::make_unique<message_center::Notification>(
message_center::NOTIFICATION_TYPE_SIMPLE, id, ASCIIToUTF16("title"),
ASCIIToUTF16("message"), /*icon=*/gfx::Image(),
/*display_source=*/base::string16(), GURL(), message_center::NotifierId(),
message_center::RichNotificationData(), /*delegate=*/nullptr);
}
class MojoDelegate : public mojom::NotificationDelegate {
public:
MojoDelegate() = default;
......@@ -325,5 +342,20 @@ TEST_F(MessageCenterAshTest, UserActions) {
EXPECT_EQ(1, mojo_delegate.closed_count_);
}
TEST_F(MessageCenterAshTest, GetDisplayedNotifications) {
// Create an ash-side notification.
auto* message_center = message_center::MessageCenter::Get();
message_center->AddNotification(CreateNotificationWithId("id0"));
message_center->AddNotification(CreateNotificationWithId("id1"));
// Get the list of notifications.
mojom::MessageCenterAsyncWaiter waiter(message_center_remote_.get());
std::vector<std::string> ids;
waiter.GetDisplayedNotifications(&ids);
// The notifications ids are returned. No particular order is specified.
EXPECT_THAT(ids, testing::UnorderedElementsAre("id0", "id1"));
}
} // namespace
} // namespace crosapi
......@@ -95,6 +95,9 @@ class TestMessageCenter : public crosapi::mojom::MessageCenter {
last_close_id_ = id;
}
void GetDisplayedNotifications(
GetDisplayedNotificationsCallback callback) override {}
int display_count_ = 0;
std::string last_display_id_;
crosapi::mojom::NotificationPtr last_notification_;
......
......@@ -20,6 +20,11 @@ interface MessageCenter {
// Closes a notification by the ID provided in |notification| above.
CloseNotification@1(string id);
// Returns the IDs of the currently-displayed notifications. Order within the
// array is not guaranteed. Mojo does not support sets.
[MinVersion=1]
GetDisplayedNotifications@2() => (array<string> ids);
};
// Handles responses to user actions on notifications. Multiple actions may
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment