#include "ui/message_center/notification_blocker.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/message_center/fake_message_center.h"
#include "ui/message_center/lock_screen/fake_lock_screen_controller.h"
#include "ui/message_center/message_center.h"
namespace message_center {
namespace {
class TestNotificationBlocker : public NotificationBlocker {
public:
explicit TestNotificationBlocker(MessageCenter* message_center)
: NotificationBlocker(message_center) {}
bool ShouldShowNotificationAsPopup(
const Notification& notification) const override {
return true;
}
};
class TestMessageCenter : public FakeMessageCenter {
public:
TestMessageCenter() = default;
TestMessageCenter(const TestMessageCenter&) = delete;
TestMessageCenter& operator=(const TestMessageCenter&) = delete;
~TestMessageCenter() override = default;
void AddNotificationBlocker(NotificationBlocker* blocker) override {
add_notification_blocker_called_count_++;
}
size_t add_notification_blocker_called_count() {
return add_notification_blocker_called_count_;
}
private:
size_t add_notification_blocker_called_count_ = 0;
};
}
class NotificationBlockerTest : public testing::Test {
public:
NotificationBlockerTest() = default;
NotificationBlockerTest(const NotificationBlockerTest&) = delete;
NotificationBlockerTest& operator=(const NotificationBlockerTest&) = delete;
~NotificationBlockerTest() override = default;
void SetUp() override {
MessageCenter::Initialize(std::make_unique<FakeLockScreenController>());
}
void TearDown() override { MessageCenter::Shutdown(); }
};
TEST_F(NotificationBlockerTest, OnlyRegisteredAfterInit) {
TestMessageCenter message_center;
size_t initial_count = message_center.add_notification_blocker_called_count();
TestNotificationBlocker blocker(&message_center);
EXPECT_EQ(initial_count,
message_center.add_notification_blocker_called_count());
blocker.Init();
EXPECT_EQ(initial_count + 1,
message_center.add_notification_blocker_called_count());
}
using NotificationBlockerDeathTest = NotificationBlockerTest;
TEST_F(NotificationBlockerDeathTest, MaxOneInit) {
TestNotificationBlocker blocker(MessageCenter::Get());
blocker.Init();
std::string log;
#if CHECK_WILL_STREAM()
log = "Do not initialize a NotificationBlocker more than once\\.";
#else
log = ".*";
#endif
EXPECT_DEATH({ blocker.Init(); }, log);
}
}