message_center_ui_controller_unittest.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright (c) 2013 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. #include "ash/system/message_center/message_center_ui_controller.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "ash/constants/ash_features.h"
  8. #include "ash/test/ash_test_base.h"
  9. #include "base/strings/utf_string_conversions.h"
  10. #include "base/test/scoped_feature_list.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. #include "ui/base/models/menu_model.h"
  13. #include "ui/message_center/message_center.h"
  14. #include "ui/message_center/public/cpp/notification.h"
  15. #include "ui/message_center/public/cpp/notification_types.h"
  16. using base::ASCIIToUTF16;
  17. namespace ash {
  18. namespace {
  19. class TestNotificationDelegate : public message_center::NotificationDelegate {
  20. public:
  21. TestNotificationDelegate() = default;
  22. TestNotificationDelegate(const TestNotificationDelegate&) = delete;
  23. TestNotificationDelegate& operator=(const TestNotificationDelegate&) = delete;
  24. private:
  25. ~TestNotificationDelegate() override = default;
  26. };
  27. class MockDelegate : public MessageCenterUiDelegate {
  28. public:
  29. MockDelegate() {}
  30. MockDelegate(const MockDelegate&) = delete;
  31. MockDelegate& operator=(const MockDelegate&) = delete;
  32. ~MockDelegate() override {}
  33. void OnMessageCenterContentsChanged() override {}
  34. bool ShowPopups() override {
  35. if (!show_message_center_success_)
  36. return false;
  37. EXPECT_FALSE(popups_visible_);
  38. popups_visible_ = true;
  39. return true;
  40. }
  41. void HidePopups() override {
  42. EXPECT_TRUE(popups_visible_);
  43. popups_visible_ = false;
  44. }
  45. bool ShowMessageCenter() override {
  46. EXPECT_FALSE(popups_visible_);
  47. return show_popups_success_;
  48. }
  49. void HideMessageCenter() override { EXPECT_FALSE(popups_visible_); }
  50. bool popups_visible_ = false;
  51. bool show_popups_success_ = true;
  52. bool show_message_center_success_ = true;
  53. };
  54. } // namespace
  55. class MessageCenterUiControllerTest : public AshTestBase,
  56. public testing::WithParamInterface<bool> {
  57. public:
  58. MessageCenterUiControllerTest() {}
  59. MessageCenterUiControllerTest(const MessageCenterUiControllerTest&) = delete;
  60. MessageCenterUiControllerTest& operator=(
  61. const MessageCenterUiControllerTest&) = delete;
  62. ~MessageCenterUiControllerTest() override {}
  63. void SetUp() override {
  64. scoped_feature_list_ = std::make_unique<base::test::ScopedFeatureList>();
  65. scoped_feature_list_->InitWithFeatureState(features::kNotificationsRefresh,
  66. IsNotificationsRefreshEnabled());
  67. AshTestBase::SetUp();
  68. delegate_ = std::make_unique<MockDelegate>();
  69. message_center_ = message_center::MessageCenter::Get();
  70. ui_controller_ =
  71. std::make_unique<MessageCenterUiController>(delegate_.get());
  72. }
  73. bool IsNotificationsRefreshEnabled() const { return GetParam(); }
  74. void TearDown() override {
  75. ui_controller_.reset();
  76. delegate_.reset();
  77. message_center_ = nullptr;
  78. AshTestBase::TearDown();
  79. }
  80. protected:
  81. message_center::NotifierId DummyNotifierId() {
  82. return message_center::NotifierId();
  83. }
  84. message_center::Notification* AddNotification(const std::string& id) {
  85. return AddNotification(id, DummyNotifierId());
  86. }
  87. message_center::Notification* AddNotification(
  88. const std::string& id,
  89. message_center::NotifierId notifier_id) {
  90. std::unique_ptr<message_center::Notification> notification(
  91. new message_center::Notification(
  92. message_center::NOTIFICATION_TYPE_SIMPLE, id,
  93. u"Test Web Notification", u"Notification message body.",
  94. ui::ImageModel(), u"www.test.org", GURL(), notifier_id,
  95. message_center::RichNotificationData(),
  96. new TestNotificationDelegate()));
  97. message_center::Notification* notification_ptr = notification.get();
  98. message_center_->AddNotification(std::move(notification));
  99. return notification_ptr;
  100. }
  101. std::unique_ptr<MockDelegate> delegate_;
  102. std::unique_ptr<MessageCenterUiController> ui_controller_;
  103. message_center::MessageCenter* message_center_;
  104. std::unique_ptr<base::test::ScopedFeatureList> scoped_feature_list_;
  105. };
  106. INSTANTIATE_TEST_SUITE_P(All,
  107. MessageCenterUiControllerTest,
  108. testing::Bool() /* IsNotificationsRefreshEnabled() */);
  109. TEST_P(MessageCenterUiControllerTest, BasicMessageCenter) {
  110. ASSERT_FALSE(ui_controller_->popups_visible());
  111. ASSERT_FALSE(ui_controller_->message_center_visible());
  112. bool shown = ui_controller_->ShowMessageCenterBubble();
  113. EXPECT_TRUE(shown);
  114. ASSERT_FALSE(ui_controller_->popups_visible());
  115. ASSERT_TRUE(ui_controller_->message_center_visible());
  116. ui_controller_->HideMessageCenterBubble();
  117. ASSERT_FALSE(ui_controller_->popups_visible());
  118. ASSERT_FALSE(ui_controller_->message_center_visible());
  119. ui_controller_->ShowMessageCenterBubble();
  120. ASSERT_FALSE(ui_controller_->popups_visible());
  121. ASSERT_TRUE(ui_controller_->message_center_visible());
  122. ui_controller_->HideMessageCenterBubble();
  123. ASSERT_FALSE(ui_controller_->popups_visible());
  124. ASSERT_FALSE(ui_controller_->message_center_visible());
  125. }
  126. TEST_P(MessageCenterUiControllerTest, BasicPopup) {
  127. ASSERT_FALSE(ui_controller_->popups_visible());
  128. ASSERT_FALSE(ui_controller_->message_center_visible());
  129. ui_controller_->ShowPopupBubble();
  130. ASSERT_FALSE(ui_controller_->popups_visible());
  131. ASSERT_FALSE(ui_controller_->message_center_visible());
  132. AddNotification("BasicPopup");
  133. ASSERT_TRUE(ui_controller_->popups_visible());
  134. ASSERT_FALSE(ui_controller_->message_center_visible());
  135. ui_controller_->HidePopupBubble();
  136. ASSERT_FALSE(ui_controller_->popups_visible());
  137. ASSERT_FALSE(ui_controller_->message_center_visible());
  138. }
  139. TEST_P(MessageCenterUiControllerTest, MessageCenterClosesPopups) {
  140. ASSERT_FALSE(ui_controller_->popups_visible());
  141. ASSERT_FALSE(ui_controller_->message_center_visible());
  142. AddNotification("MessageCenterClosesPopups");
  143. ASSERT_TRUE(ui_controller_->popups_visible());
  144. ASSERT_FALSE(ui_controller_->message_center_visible());
  145. bool shown = ui_controller_->ShowMessageCenterBubble();
  146. EXPECT_TRUE(shown);
  147. ASSERT_FALSE(ui_controller_->popups_visible());
  148. ASSERT_TRUE(ui_controller_->message_center_visible());
  149. // The notification is queued if it's added when message center is visible.
  150. AddNotification("MessageCenterClosesPopups2");
  151. ui_controller_->ShowPopupBubble();
  152. ASSERT_FALSE(ui_controller_->popups_visible());
  153. ASSERT_TRUE(ui_controller_->message_center_visible());
  154. ui_controller_->HideMessageCenterBubble();
  155. // There is no queued notification.
  156. ASSERT_FALSE(ui_controller_->popups_visible());
  157. ASSERT_FALSE(ui_controller_->message_center_visible());
  158. ui_controller_->ShowMessageCenterBubble();
  159. ui_controller_->HideMessageCenterBubble();
  160. ASSERT_FALSE(ui_controller_->popups_visible());
  161. ASSERT_FALSE(ui_controller_->message_center_visible());
  162. }
  163. TEST_P(MessageCenterUiControllerTest, ShowBubbleFails) {
  164. // Now the delegate will signal that it was unable to show a bubble.
  165. delegate_->show_popups_success_ = false;
  166. delegate_->show_message_center_success_ = false;
  167. ASSERT_FALSE(ui_controller_->popups_visible());
  168. ASSERT_FALSE(ui_controller_->message_center_visible());
  169. AddNotification("ShowBubbleFails");
  170. ui_controller_->ShowPopupBubble();
  171. ASSERT_FALSE(ui_controller_->popups_visible());
  172. ASSERT_FALSE(ui_controller_->message_center_visible());
  173. bool shown = ui_controller_->ShowMessageCenterBubble();
  174. EXPECT_FALSE(shown);
  175. ASSERT_FALSE(ui_controller_->popups_visible());
  176. ASSERT_FALSE(ui_controller_->message_center_visible());
  177. ui_controller_->HideMessageCenterBubble();
  178. ASSERT_FALSE(ui_controller_->popups_visible());
  179. ASSERT_FALSE(ui_controller_->message_center_visible());
  180. ui_controller_->ShowMessageCenterBubble();
  181. ASSERT_FALSE(ui_controller_->popups_visible());
  182. ASSERT_FALSE(ui_controller_->message_center_visible());
  183. ui_controller_->HidePopupBubble();
  184. ASSERT_FALSE(ui_controller_->popups_visible());
  185. ASSERT_FALSE(ui_controller_->message_center_visible());
  186. }
  187. } // namespace ash