OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/signin/easy_unlock_notification_controller_chromeos.h" |
| 6 |
| 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/test/base/testing_profile.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "ui/message_center/fake_message_center.h" |
| 12 #include "ui/message_center/notification.h" |
| 13 #include "ui/message_center/notification_types.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 const char kPhoneName[] = "Nexus 6"; |
| 18 |
| 19 class TestMessageCenter : public message_center::FakeMessageCenter { |
| 20 public: |
| 21 TestMessageCenter() : message_center::FakeMessageCenter() {} |
| 22 ~TestMessageCenter() override {} |
| 23 |
| 24 void NotifyNotificationTapped(const std::string& notification_id) { |
| 25 for (auto& observer : observer_list_) { |
| 26 observer.OnNotificationClicked(notification_id); |
| 27 } |
| 28 } |
| 29 |
| 30 void NotifyNotificationButtonTapped(const std::string& notification_id, |
| 31 int button_index) { |
| 32 for (auto& observer : observer_list_) { |
| 33 observer.OnNotificationButtonClicked(notification_id, button_index); |
| 34 } |
| 35 } |
| 36 |
| 37 size_t GetNumNotifications() { return notifications_.size(); } |
| 38 |
| 39 // message_center::FakeMessageCenter: |
| 40 void AddObserver(message_center::MessageCenterObserver* observer) override { |
| 41 observer_list_.AddObserver(observer); |
| 42 } |
| 43 |
| 44 void RemoveObserver( |
| 45 message_center::MessageCenterObserver* observer) override { |
| 46 observer_list_.RemoveObserver(observer); |
| 47 } |
| 48 |
| 49 message_center::Notification* FindVisibleNotificationById( |
| 50 const std::string& id) override { |
| 51 auto iter = std::find_if( |
| 52 notifications_.begin(), notifications_.end(), |
| 53 [id](const std::shared_ptr<message_center::Notification> notification) { |
| 54 return notification->id() == id; |
| 55 }); |
| 56 return iter != notifications_.end() ? iter->get() : nullptr; |
| 57 } |
| 58 |
| 59 void AddNotification( |
| 60 std::unique_ptr<message_center::Notification> notification) override { |
| 61 notifications_.push_back(std::move(notification)); |
| 62 } |
| 63 |
| 64 void UpdateNotification( |
| 65 const std::string& old_id, |
| 66 std::unique_ptr<message_center::Notification> new_notification) override { |
| 67 RemoveNotification(old_id, false /* by_user */); |
| 68 AddNotification(std::move(new_notification)); |
| 69 } |
| 70 |
| 71 void RemoveNotification(const std::string& id, bool by_user) override { |
| 72 if (!FindVisibleNotificationById(id)) |
| 73 return; |
| 74 |
| 75 notifications_.erase(std::find_if( |
| 76 notifications_.begin(), notifications_.end(), |
| 77 [id](const std::shared_ptr<message_center::Notification> notification) { |
| 78 return notification->id() == id; |
| 79 })); |
| 80 } |
| 81 |
| 82 private: |
| 83 std::vector<std::shared_ptr<message_center::Notification>> notifications_; |
| 84 base::ObserverList<message_center::MessageCenterObserver> observer_list_; |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(TestMessageCenter); |
| 87 }; |
| 88 |
| 89 class TestableNotificationController |
| 90 : public EasyUnlockNotificationControllerChromeOS { |
| 91 public: |
| 92 TestableNotificationController(Profile* profile, |
| 93 message_center::MessageCenter* message_center) |
| 94 : EasyUnlockNotificationControllerChromeOS(profile, message_center) {} |
| 95 |
| 96 ~TestableNotificationController() override {} |
| 97 |
| 98 // EasyUnlockNotificationControllerChromeOS: |
| 99 MOCK_METHOD0(LaunchEasyUnlockSettings, void()); |
| 100 MOCK_METHOD0(LockScreen, void()); |
| 101 |
| 102 private: |
| 103 DISALLOW_COPY_AND_ASSIGN(TestableNotificationController); |
| 104 }; |
| 105 |
| 106 } // namespace |
| 107 |
| 108 class EasyUnlockNotificationControllerChromeOSTest : public ::testing::Test { |
| 109 protected: |
| 110 EasyUnlockNotificationControllerChromeOSTest() |
| 111 : notification_controller_(&profile_, &message_center_) {} |
| 112 |
| 113 ~EasyUnlockNotificationControllerChromeOSTest() override {} |
| 114 |
| 115 TestingProfile profile_; |
| 116 TestMessageCenter message_center_; |
| 117 testing::StrictMock<TestableNotificationController> notification_controller_; |
| 118 |
| 119 private: |
| 120 DISALLOW_COPY_AND_ASSIGN(EasyUnlockNotificationControllerChromeOSTest); |
| 121 }; |
| 122 |
| 123 TEST_F(EasyUnlockNotificationControllerChromeOSTest, |
| 124 TestShowChromebookAddedNotification) { |
| 125 const char kNotificationId[] = "easyunlock_notification_ids.chromebook_added"; |
| 126 |
| 127 notification_controller_.ShowChromebookAddedNotification(); |
| 128 message_center::Notification* notification = |
| 129 message_center_.FindVisibleNotificationById(kNotificationId); |
| 130 ASSERT_TRUE(notification); |
| 131 ASSERT_EQ(1u, notification->buttons().size()); |
| 132 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); |
| 133 |
| 134 // Clicking notification button should launch settings. |
| 135 EXPECT_CALL(notification_controller_, LaunchEasyUnlockSettings()); |
| 136 message_center_.NotifyNotificationButtonTapped(kNotificationId, 0); |
| 137 |
| 138 // Clicking the notification itself should also launch settings. |
| 139 EXPECT_CALL(notification_controller_, LaunchEasyUnlockSettings()); |
| 140 message_center_.NotifyNotificationTapped(kNotificationId); |
| 141 } |
| 142 |
| 143 TEST_F(EasyUnlockNotificationControllerChromeOSTest, |
| 144 TestShowPairingChangeNotification) { |
| 145 const char kNotificationId[] = "easyunlock_notification_ids.pairing_change"; |
| 146 |
| 147 notification_controller_.ShowPairingChangeNotification(); |
| 148 message_center::Notification* notification = |
| 149 message_center_.FindVisibleNotificationById(kNotificationId); |
| 150 ASSERT_TRUE(notification); |
| 151 ASSERT_EQ(2u, notification->buttons().size()); |
| 152 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); |
| 153 |
| 154 // Clicking 1st notification button should lock screen settings. |
| 155 EXPECT_CALL(notification_controller_, LockScreen()); |
| 156 message_center_.NotifyNotificationButtonTapped(kNotificationId, 0); |
| 157 |
| 158 // Clicking 2nd notification button should launch settings. |
| 159 EXPECT_CALL(notification_controller_, LaunchEasyUnlockSettings()); |
| 160 message_center_.NotifyNotificationButtonTapped(kNotificationId, 1); |
| 161 |
| 162 // Clicking the notification itself should do nothing. |
| 163 message_center_.NotifyNotificationTapped(kNotificationId); |
| 164 } |
| 165 |
| 166 TEST_F(EasyUnlockNotificationControllerChromeOSTest, |
| 167 TestShowPairingChangeAppliedNotification) { |
| 168 const char kNotificationId[] = |
| 169 "easyunlock_notification_ids.pairing_change_applied"; |
| 170 |
| 171 notification_controller_.ShowPairingChangeAppliedNotification(kPhoneName); |
| 172 message_center::Notification* notification = |
| 173 message_center_.FindVisibleNotificationById(kNotificationId); |
| 174 ASSERT_TRUE(notification); |
| 175 ASSERT_EQ(1u, notification->buttons().size()); |
| 176 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); |
| 177 |
| 178 // Check that the phone name is in the notification message. |
| 179 EXPECT_NE(std::string::npos, |
| 180 notification->message().find(base::UTF8ToUTF16(kPhoneName))); |
| 181 |
| 182 // Clicking notification button should launch settings. |
| 183 EXPECT_CALL(notification_controller_, LaunchEasyUnlockSettings()); |
| 184 message_center_.NotifyNotificationButtonTapped(kNotificationId, 0); |
| 185 |
| 186 // Clicking the notification itself should also launch settings. |
| 187 EXPECT_CALL(notification_controller_, LaunchEasyUnlockSettings()); |
| 188 message_center_.NotifyNotificationTapped(kNotificationId); |
| 189 } |
| 190 |
| 191 TEST_F(EasyUnlockNotificationControllerChromeOSTest, |
| 192 PairingAppliedRemovesPairingChange) { |
| 193 const char kPairingChangeId[] = "easyunlock_notification_ids.pairing_change"; |
| 194 const char kPairingAppliedId[] = |
| 195 "easyunlock_notification_ids.pairing_change_applied"; |
| 196 |
| 197 notification_controller_.ShowPairingChangeNotification(); |
| 198 EXPECT_TRUE(message_center_.FindVisibleNotificationById(kPairingChangeId)); |
| 199 |
| 200 notification_controller_.ShowPairingChangeAppliedNotification(kPhoneName); |
| 201 EXPECT_FALSE(message_center_.FindVisibleNotificationById(kPairingChangeId)); |
| 202 EXPECT_TRUE(message_center_.FindVisibleNotificationById(kPairingAppliedId)); |
| 203 } |
OLD | NEW |