Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Unified Diff: chrome/browser/signin/easy_unlock_notification_controller_chromeos_unittest.cc

Issue 2968323002: [EasyUnlock] Introduce EasyUnlockNotificationController (Closed)
Patch Set: Notification test. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/signin/easy_unlock_notification_controller_chromeos_unittest.cc
diff --git a/chrome/browser/signin/easy_unlock_notification_controller_chromeos_unittest.cc b/chrome/browser/signin/easy_unlock_notification_controller_chromeos_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e5cc4502afa0ee2699502f6b70813da3058d3aae
--- /dev/null
+++ b/chrome/browser/signin/easy_unlock_notification_controller_chromeos_unittest.cc
@@ -0,0 +1,203 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/signin/easy_unlock_notification_controller_chromeos.h"
+
+#include "base/strings/utf_string_conversions.h"
+#include "chrome/test/base/testing_profile.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_types.h"
+
+namespace {
+
+const char kPhoneName[] = "Nexus 6";
+
+class TestMessageCenter : public message_center::FakeMessageCenter {
+ public:
+ TestMessageCenter() : message_center::FakeMessageCenter() {}
+ ~TestMessageCenter() override {}
+
+ void NotifyNotificationTapped(const std::string& notification_id) {
+ for (auto& observer : observer_list_) {
+ observer.OnNotificationClicked(notification_id);
+ }
+ }
+
+ void NotifyNotificationButtonTapped(const std::string& notification_id,
+ int button_index) {
+ for (auto& observer : observer_list_) {
+ observer.OnNotificationButtonClicked(notification_id, button_index);
+ }
+ }
+
+ size_t GetNumNotifications() { return notifications_.size(); }
+
+ // message_center::FakeMessageCenter:
+ void AddObserver(message_center::MessageCenterObserver* observer) override {
+ observer_list_.AddObserver(observer);
+ }
+
+ void RemoveObserver(
+ message_center::MessageCenterObserver* observer) override {
+ observer_list_.RemoveObserver(observer);
+ }
+
+ 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_;
+ base::ObserverList<message_center::MessageCenterObserver> observer_list_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestMessageCenter);
+};
+
+class TestableNotificationController
+ : public EasyUnlockNotificationControllerChromeOS {
+ public:
+ TestableNotificationController(Profile* profile,
+ message_center::MessageCenter* message_center)
+ : EasyUnlockNotificationControllerChromeOS(profile, message_center) {}
+
+ ~TestableNotificationController() override {}
+
+ // EasyUnlockNotificationControllerChromeOS:
+ MOCK_METHOD0(LaunchEasyUnlockSettings, void());
+ MOCK_METHOD0(LockScreen, void());
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(TestableNotificationController);
+};
+
+} // namespace
+
+class EasyUnlockNotificationControllerChromeOSTest : public ::testing::Test {
+ protected:
+ EasyUnlockNotificationControllerChromeOSTest()
+ : notification_controller_(&profile_, &message_center_) {}
+
+ ~EasyUnlockNotificationControllerChromeOSTest() override {}
+
+ TestingProfile profile_;
+ TestMessageCenter message_center_;
+ testing::StrictMock<TestableNotificationController> notification_controller_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(EasyUnlockNotificationControllerChromeOSTest);
+};
+
+TEST_F(EasyUnlockNotificationControllerChromeOSTest,
+ TestShowChromebookAddedNotification) {
+ const char kNotificationId[] = "easyunlock_notification_ids.chromebook_added";
+
+ notification_controller_.ShowChromebookAddedNotification();
+ message_center::Notification* notification =
+ message_center_.FindVisibleNotificationById(kNotificationId);
+ ASSERT_TRUE(notification);
+ ASSERT_EQ(1u, notification->buttons().size());
+ EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
+
+ // Clicking notification button should launch settings.
+ EXPECT_CALL(notification_controller_, LaunchEasyUnlockSettings());
+ message_center_.NotifyNotificationButtonTapped(kNotificationId, 0);
+
+ // Clicking the notification itself should also launch settings.
+ EXPECT_CALL(notification_controller_, LaunchEasyUnlockSettings());
+ message_center_.NotifyNotificationTapped(kNotificationId);
+}
+
+TEST_F(EasyUnlockNotificationControllerChromeOSTest,
+ TestShowPairingChangeNotification) {
+ const char kNotificationId[] = "easyunlock_notification_ids.pairing_change";
+
+ notification_controller_.ShowPairingChangeNotification();
+ message_center::Notification* notification =
+ message_center_.FindVisibleNotificationById(kNotificationId);
+ ASSERT_TRUE(notification);
+ ASSERT_EQ(2u, notification->buttons().size());
+ EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
+
+ // Clicking 1st notification button should lock screen settings.
+ EXPECT_CALL(notification_controller_, LockScreen());
+ message_center_.NotifyNotificationButtonTapped(kNotificationId, 0);
+
+ // Clicking 2nd notification button should launch settings.
+ EXPECT_CALL(notification_controller_, LaunchEasyUnlockSettings());
+ message_center_.NotifyNotificationButtonTapped(kNotificationId, 1);
+
+ // Clicking the notification itself should do nothing.
+ message_center_.NotifyNotificationTapped(kNotificationId);
+}
+
+TEST_F(EasyUnlockNotificationControllerChromeOSTest,
+ TestShowPairingChangeAppliedNotification) {
+ const char kNotificationId[] =
+ "easyunlock_notification_ids.pairing_change_applied";
+
+ notification_controller_.ShowPairingChangeAppliedNotification(kPhoneName);
+ message_center::Notification* notification =
+ message_center_.FindVisibleNotificationById(kNotificationId);
+ ASSERT_TRUE(notification);
+ ASSERT_EQ(1u, notification->buttons().size());
+ EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
+
+ // Check that the phone name is in the notification message.
+ EXPECT_NE(std::string::npos,
+ notification->message().find(base::UTF8ToUTF16(kPhoneName)));
+
+ // Clicking notification button should launch settings.
+ EXPECT_CALL(notification_controller_, LaunchEasyUnlockSettings());
+ message_center_.NotifyNotificationButtonTapped(kNotificationId, 0);
+
+ // Clicking the notification itself should also launch settings.
+ EXPECT_CALL(notification_controller_, LaunchEasyUnlockSettings());
+ message_center_.NotifyNotificationTapped(kNotificationId);
+}
+
+TEST_F(EasyUnlockNotificationControllerChromeOSTest,
+ PairingAppliedRemovesPairingChange) {
+ const char kPairingChangeId[] = "easyunlock_notification_ids.pairing_change";
+ const char kPairingAppliedId[] =
+ "easyunlock_notification_ids.pairing_change_applied";
+
+ notification_controller_.ShowPairingChangeNotification();
+ EXPECT_TRUE(message_center_.FindVisibleNotificationById(kPairingChangeId));
+
+ notification_controller_.ShowPairingChangeAppliedNotification(kPhoneName);
+ EXPECT_FALSE(message_center_.FindVisibleNotificationById(kPairingChangeId));
+ EXPECT_TRUE(message_center_.FindVisibleNotificationById(kPairingAppliedId));
+}

Powered by Google App Engine
This is Rietveld 408576698