Commit 288e2479 authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

Make EasyUnlockNotification on Chrome OS use NotificationDisplayService.

Bug: 783018
Change-Id: Ic1340627bacde0109e92afd45b2cb3c164dd9594
Reviewed-on: https://chromium-review.googlesource.com/783661Reviewed-by: default avatarTim Song <tengs@chromium.org>
Commit-Queue: Evan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521493}
parent 10cdae32
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
#include "chrome/browser/signin/easy_unlock_notification_controller_chromeos.h" #include "chrome/browser/signin/easy_unlock_notification_controller_chromeos.h"
#include "ui/message_center/message_center.h"
#endif #endif
namespace { namespace {
...@@ -38,8 +37,7 @@ class EasyUnlockNotificationControllerStub ...@@ -38,8 +37,7 @@ class EasyUnlockNotificationControllerStub
std::unique_ptr<EasyUnlockNotificationController> std::unique_ptr<EasyUnlockNotificationController>
EasyUnlockNotificationController::Create(Profile* profile) { EasyUnlockNotificationController::Create(Profile* profile) {
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
return base::MakeUnique<EasyUnlockNotificationControllerChromeOS>( return base::MakeUnique<EasyUnlockNotificationControllerChromeOS>(profile);
profile, message_center::MessageCenter::Get());
#else #else
return base::MakeUnique<EasyUnlockNotificationControllerStub>(); return base::MakeUnique<EasyUnlockNotificationControllerStub>();
#endif #endif
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/guid.h" #include "base/guid.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/ui/chrome_pages.h" #include "chrome/browser/ui/chrome_pages.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "chrome/grit/theme_resources.h" #include "chrome/grit/theme_resources.h"
...@@ -53,12 +54,8 @@ std::unique_ptr<message_center::Notification> CreateNotification( ...@@ -53,12 +54,8 @@ std::unique_ptr<message_center::Notification> CreateNotification(
} // namespace } // namespace
EasyUnlockNotificationControllerChromeOS:: EasyUnlockNotificationControllerChromeOS::
EasyUnlockNotificationControllerChromeOS( EasyUnlockNotificationControllerChromeOS(Profile* profile)
Profile* profile, : profile_(profile), weak_ptr_factory_(this) {}
message_center::MessageCenter* message_center)
: profile_(profile),
message_center_(message_center),
weak_ptr_factory_(this) {}
EasyUnlockNotificationControllerChromeOS:: EasyUnlockNotificationControllerChromeOS::
~EasyUnlockNotificationControllerChromeOS() {} ~EasyUnlockNotificationControllerChromeOS() {}
...@@ -110,8 +107,8 @@ void EasyUnlockNotificationControllerChromeOS::ShowPairingChangeNotification() { ...@@ -110,8 +107,8 @@ void EasyUnlockNotificationControllerChromeOS::ShowPairingChangeNotification() {
void EasyUnlockNotificationControllerChromeOS:: void EasyUnlockNotificationControllerChromeOS::
ShowPairingChangeAppliedNotification(const std::string& phone_name) { ShowPairingChangeAppliedNotification(const std::string& phone_name) {
// Remove the pairing change notification if it is still being shown. // Remove the pairing change notification if it is still being shown.
message_center_->RemoveNotification(kEasyUnlockPairingChangeNotifierId, NotificationDisplayService::GetForProfile(profile_)->Close(
false /* by_user */); NotificationHandler::Type::TRANSIENT, kEasyUnlockPairingChangeNotifierId);
message_center::RichNotificationData rich_notification_data; message_center::RichNotificationData rich_notification_data;
rich_notification_data.buttons.push_back( rich_notification_data.buttons.push_back(
...@@ -154,13 +151,8 @@ void EasyUnlockNotificationControllerChromeOS::ShowPromotionNotification() { ...@@ -154,13 +151,8 @@ void EasyUnlockNotificationControllerChromeOS::ShowPromotionNotification() {
void EasyUnlockNotificationControllerChromeOS::ShowNotification( void EasyUnlockNotificationControllerChromeOS::ShowNotification(
std::unique_ptr<message_center::Notification> notification) { std::unique_ptr<message_center::Notification> notification) {
notification->SetSystemPriority(); notification->SetSystemPriority();
std::string notification_id = notification->id(); NotificationDisplayService::GetForProfile(profile_)->Display(
if (message_center_->FindVisibleNotificationById(notification_id)) { NotificationHandler::Type::TRANSIENT, *notification);
message_center_->UpdateNotification(notification_id,
std::move(notification));
} else {
message_center_->AddNotification(std::move(notification));
}
} }
void EasyUnlockNotificationControllerChromeOS::LaunchEasyUnlockSettings() { void EasyUnlockNotificationControllerChromeOS::LaunchEasyUnlockSettings() {
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "chrome/browser/signin/easy_unlock_notification_controller.h" #include "chrome/browser/signin/easy_unlock_notification_controller.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/notification.h" #include "ui/message_center/notification.h"
#include "ui/message_center/notification_delegate.h" #include "ui/message_center/notification_delegate.h"
...@@ -22,9 +21,7 @@ class Profile; ...@@ -22,9 +21,7 @@ class Profile;
class EasyUnlockNotificationControllerChromeOS class EasyUnlockNotificationControllerChromeOS
: public EasyUnlockNotificationController { : public EasyUnlockNotificationController {
public: public:
EasyUnlockNotificationControllerChromeOS( explicit EasyUnlockNotificationControllerChromeOS(Profile* profile);
Profile* profile,
message_center::MessageCenter* message_center);
~EasyUnlockNotificationControllerChromeOS() override; ~EasyUnlockNotificationControllerChromeOS() override;
// EasyUnlockNotificationController: // EasyUnlockNotificationController:
...@@ -68,8 +65,6 @@ class EasyUnlockNotificationControllerChromeOS ...@@ -68,8 +65,6 @@ class EasyUnlockNotificationControllerChromeOS
Profile* profile_; Profile* profile_;
message_center::MessageCenter* message_center_;
base::WeakPtrFactory<EasyUnlockNotificationControllerChromeOS> base::WeakPtrFactory<EasyUnlockNotificationControllerChromeOS>
weak_ptr_factory_; weak_ptr_factory_;
......
...@@ -5,11 +5,9 @@ ...@@ -5,11 +5,9 @@
#include "chrome/browser/signin/easy_unlock_notification_controller_chromeos.h" #include "chrome/browser/signin/easy_unlock_notification_controller_chromeos.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/browser/notifications/notification_display_service_tester.h"
#include "content/public/test/test_browser_thread_bundle.h" #include "chrome/test/base/browser_with_test_window_test.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/message_center/fake_message_center.h"
#include "ui/message_center/notification.h" #include "ui/message_center/notification.h"
#include "ui/message_center/notification_types.h" #include "ui/message_center/notification_types.h"
...@@ -17,57 +15,11 @@ namespace { ...@@ -17,57 +15,11 @@ namespace {
const char kPhoneName[] = "Nexus 6"; const char kPhoneName[] = "Nexus 6";
class TestMessageCenter : public message_center::FakeMessageCenter {
public:
TestMessageCenter() : message_center::FakeMessageCenter() {}
~TestMessageCenter() override {}
// message_center::FakeMessageCenter:
message_center::Notification* FindVisibleNotificationById(
const std::string& id) override {
auto iter = std::find_if(
notifications_.begin(), notifications_.end(),
[id](const std::shared_ptr<message_center::Notification> notification) {
return notification->id() == id;
});
return iter != notifications_.end() ? iter->get() : nullptr;
}
void AddNotification(
std::unique_ptr<message_center::Notification> notification) override {
notifications_.push_back(std::move(notification));
}
void UpdateNotification(
const std::string& old_id,
std::unique_ptr<message_center::Notification> new_notification) override {
RemoveNotification(old_id, false /* by_user */);
AddNotification(std::move(new_notification));
}
void RemoveNotification(const std::string& id, bool by_user) override {
if (!FindVisibleNotificationById(id))
return;
notifications_.erase(std::find_if(
notifications_.begin(), notifications_.end(),
[id](const std::shared_ptr<message_center::Notification> notification) {
return notification->id() == id;
}));
}
private:
std::vector<std::shared_ptr<message_center::Notification>> notifications_;
DISALLOW_COPY_AND_ASSIGN(TestMessageCenter);
};
class TestableNotificationController class TestableNotificationController
: public EasyUnlockNotificationControllerChromeOS { : public EasyUnlockNotificationControllerChromeOS {
public: public:
TestableNotificationController(Profile* profile, explicit TestableNotificationController(Profile* profile)
message_center::MessageCenter* message_center) : EasyUnlockNotificationControllerChromeOS(profile) {}
: EasyUnlockNotificationControllerChromeOS(profile, message_center) {}
~TestableNotificationController() override {} ~TestableNotificationController() override {}
...@@ -81,17 +33,28 @@ class TestableNotificationController ...@@ -81,17 +33,28 @@ class TestableNotificationController
} // namespace } // namespace
class EasyUnlockNotificationControllerChromeOSTest : public ::testing::Test { class EasyUnlockNotificationControllerChromeOSTest
: public BrowserWithTestWindowTest {
protected: protected:
EasyUnlockNotificationControllerChromeOSTest() EasyUnlockNotificationControllerChromeOSTest() {}
: notification_controller_(&profile_, &message_center_) {}
~EasyUnlockNotificationControllerChromeOSTest() override {} ~EasyUnlockNotificationControllerChromeOSTest() override {}
const content::TestBrowserThreadBundle thread_bundle_; void SetUp() override {
TestingProfile profile_; BrowserWithTestWindowTest::SetUp();
TestMessageCenter message_center_;
testing::StrictMock<TestableNotificationController> notification_controller_; display_service_ =
std::make_unique<NotificationDisplayServiceTester>(profile());
notification_controller_ =
std::make_unique<testing::StrictMock<TestableNotificationController>>(
profile());
}
// const content::TestBrowserThreadBundle thread_bundle_;
// TestMessageCenter message_center_;
std::unique_ptr<testing::StrictMock<TestableNotificationController>>
notification_controller_;
std::unique_ptr<NotificationDisplayServiceTester> display_service_;
private: private:
DISALLOW_COPY_AND_ASSIGN(EasyUnlockNotificationControllerChromeOSTest); DISALLOW_COPY_AND_ASSIGN(EasyUnlockNotificationControllerChromeOSTest);
...@@ -101,19 +64,19 @@ TEST_F(EasyUnlockNotificationControllerChromeOSTest, ...@@ -101,19 +64,19 @@ TEST_F(EasyUnlockNotificationControllerChromeOSTest,
TestShowChromebookAddedNotification) { TestShowChromebookAddedNotification) {
const char kNotificationId[] = "easyunlock_notification_ids.chromebook_added"; const char kNotificationId[] = "easyunlock_notification_ids.chromebook_added";
notification_controller_.ShowChromebookAddedNotification(); notification_controller_->ShowChromebookAddedNotification();
message_center::Notification* notification = base::Optional<message_center::Notification> notification =
message_center_.FindVisibleNotificationById(kNotificationId); display_service_->GetNotification(kNotificationId);
ASSERT_TRUE(notification); ASSERT_TRUE(notification);
ASSERT_EQ(1u, notification->buttons().size()); ASSERT_EQ(1u, notification->buttons().size());
EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
// Clicking notification button should launch settings. // Clicking notification button should launch settings.
EXPECT_CALL(notification_controller_, LaunchEasyUnlockSettings()); EXPECT_CALL(*notification_controller_, LaunchEasyUnlockSettings());
notification->ButtonClick(0); notification->ButtonClick(0);
// Clicking the notification itself should also launch settings. // Clicking the notification itself should also launch settings.
EXPECT_CALL(notification_controller_, LaunchEasyUnlockSettings()); EXPECT_CALL(*notification_controller_, LaunchEasyUnlockSettings());
notification->Click(); notification->Click();
} }
...@@ -121,19 +84,19 @@ TEST_F(EasyUnlockNotificationControllerChromeOSTest, ...@@ -121,19 +84,19 @@ TEST_F(EasyUnlockNotificationControllerChromeOSTest,
TestShowPairingChangeNotification) { TestShowPairingChangeNotification) {
const char kNotificationId[] = "easyunlock_notification_ids.pairing_change"; const char kNotificationId[] = "easyunlock_notification_ids.pairing_change";
notification_controller_.ShowPairingChangeNotification(); notification_controller_->ShowPairingChangeNotification();
message_center::Notification* notification = base::Optional<message_center::Notification> notification =
message_center_.FindVisibleNotificationById(kNotificationId); display_service_->GetNotification(kNotificationId);
ASSERT_TRUE(notification); ASSERT_TRUE(notification);
ASSERT_EQ(2u, notification->buttons().size()); ASSERT_EQ(2u, notification->buttons().size());
EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
// Clicking 1st notification button should lock screen settings. // Clicking 1st notification button should lock screen settings.
EXPECT_CALL(notification_controller_, LockScreen()); EXPECT_CALL(*notification_controller_, LockScreen());
notification->ButtonClick(0); notification->ButtonClick(0);
// Clicking 2nd notification button should launch settings. // Clicking 2nd notification button should launch settings.
EXPECT_CALL(notification_controller_, LaunchEasyUnlockSettings()); EXPECT_CALL(*notification_controller_, LaunchEasyUnlockSettings());
notification->ButtonClick(1); notification->ButtonClick(1);
// Clicking the notification itself should do nothing. // Clicking the notification itself should do nothing.
...@@ -145,9 +108,9 @@ TEST_F(EasyUnlockNotificationControllerChromeOSTest, ...@@ -145,9 +108,9 @@ TEST_F(EasyUnlockNotificationControllerChromeOSTest,
const char kNotificationId[] = const char kNotificationId[] =
"easyunlock_notification_ids.pairing_change_applied"; "easyunlock_notification_ids.pairing_change_applied";
notification_controller_.ShowPairingChangeAppliedNotification(kPhoneName); notification_controller_->ShowPairingChangeAppliedNotification(kPhoneName);
message_center::Notification* notification = base::Optional<message_center::Notification> notification =
message_center_.FindVisibleNotificationById(kNotificationId); display_service_->GetNotification(kNotificationId);
ASSERT_TRUE(notification); ASSERT_TRUE(notification);
ASSERT_EQ(1u, notification->buttons().size()); ASSERT_EQ(1u, notification->buttons().size());
EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
...@@ -157,11 +120,11 @@ TEST_F(EasyUnlockNotificationControllerChromeOSTest, ...@@ -157,11 +120,11 @@ TEST_F(EasyUnlockNotificationControllerChromeOSTest,
notification->message().find(base::UTF8ToUTF16(kPhoneName))); notification->message().find(base::UTF8ToUTF16(kPhoneName)));
// Clicking notification button should launch settings. // Clicking notification button should launch settings.
EXPECT_CALL(notification_controller_, LaunchEasyUnlockSettings()); EXPECT_CALL(*notification_controller_, LaunchEasyUnlockSettings());
notification->ButtonClick(0); notification->ButtonClick(0);
// Clicking the notification itself should also launch settings. // Clicking the notification itself should also launch settings.
EXPECT_CALL(notification_controller_, LaunchEasyUnlockSettings()); EXPECT_CALL(*notification_controller_, LaunchEasyUnlockSettings());
notification->Click(); notification->Click();
} }
...@@ -171,30 +134,30 @@ TEST_F(EasyUnlockNotificationControllerChromeOSTest, ...@@ -171,30 +134,30 @@ TEST_F(EasyUnlockNotificationControllerChromeOSTest,
const char kPairingAppliedId[] = const char kPairingAppliedId[] =
"easyunlock_notification_ids.pairing_change_applied"; "easyunlock_notification_ids.pairing_change_applied";
notification_controller_.ShowPairingChangeNotification(); notification_controller_->ShowPairingChangeNotification();
EXPECT_TRUE(message_center_.FindVisibleNotificationById(kPairingChangeId)); EXPECT_TRUE(display_service_->GetNotification(kPairingChangeId));
notification_controller_.ShowPairingChangeAppliedNotification(kPhoneName); notification_controller_->ShowPairingChangeAppliedNotification(kPhoneName);
EXPECT_FALSE(message_center_.FindVisibleNotificationById(kPairingChangeId)); EXPECT_FALSE(display_service_->GetNotification(kPairingChangeId));
EXPECT_TRUE(message_center_.FindVisibleNotificationById(kPairingAppliedId)); EXPECT_TRUE(display_service_->GetNotification(kPairingAppliedId));
} }
TEST_F(EasyUnlockNotificationControllerChromeOSTest, TEST_F(EasyUnlockNotificationControllerChromeOSTest,
TestShowPromotionNotification) { TestShowPromotionNotification) {
const char kNotificationId[] = "easyunlock_notification_ids.promotion"; const char kNotificationId[] = "easyunlock_notification_ids.promotion";
notification_controller_.ShowPromotionNotification(); notification_controller_->ShowPromotionNotification();
message_center::Notification* notification = base::Optional<message_center::Notification> notification =
message_center_.FindVisibleNotificationById(kNotificationId); display_service_->GetNotification(kNotificationId);
ASSERT_TRUE(notification); ASSERT_TRUE(notification);
ASSERT_EQ(1u, notification->buttons().size()); ASSERT_EQ(1u, notification->buttons().size());
EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
// Clicking notification button should launch settings. // Clicking notification button should launch settings.
EXPECT_CALL(notification_controller_, LaunchEasyUnlockSettings()); EXPECT_CALL(*notification_controller_, LaunchEasyUnlockSettings());
notification->ButtonClick(0); notification->ButtonClick(0);
// Clicking the notification itself should also launch settings. // Clicking the notification itself should also launch settings.
EXPECT_CALL(notification_controller_, LaunchEasyUnlockSettings()); EXPECT_CALL(*notification_controller_, LaunchEasyUnlockSettings());
notification->Click(); notification->Click();
} }
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