Home | History | Annotate | Download | only in message_center
      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 
      5 #include "ui/message_center/message_center_tray.h"
      6 
      7 #include "base/strings/utf_string_conversions.h"
      8 #include "testing/gtest/include/gtest/gtest.h"
      9 #include "ui/base/models/menu_model.h"
     10 #include "ui/message_center/message_center.h"
     11 #include "ui/message_center/notification.h"
     12 #include "ui/message_center/notification_types.h"
     13 
     14 using base::ASCIIToUTF16;
     15 
     16 namespace message_center {
     17 namespace {
     18 
     19 class MockDelegate : public MessageCenterTrayDelegate {
     20  public:
     21   MockDelegate()
     22       : show_popups_success_(true),
     23         show_message_center_success_(true),
     24         enable_context_menu_(true) {}
     25   virtual ~MockDelegate() {}
     26   virtual void OnMessageCenterTrayChanged() OVERRIDE {}
     27   virtual bool ShowPopups() OVERRIDE {
     28     return show_message_center_success_;
     29   }
     30   virtual void HidePopups() OVERRIDE {}
     31   virtual bool ShowMessageCenter() OVERRIDE {
     32     return show_popups_success_;
     33   }
     34   virtual void HideMessageCenter() OVERRIDE {}
     35   virtual bool ShowNotifierSettings() OVERRIDE {
     36     return true;
     37   }
     38   virtual bool IsContextMenuEnabled() const OVERRIDE {
     39     return enable_context_menu_;
     40   }
     41 
     42   virtual MessageCenterTray* GetMessageCenterTray() OVERRIDE {
     43     return NULL;
     44   }
     45 
     46   bool show_popups_success_;
     47   bool show_message_center_success_;
     48   bool enable_context_menu_;
     49 
     50  private:
     51   DISALLOW_COPY_AND_ASSIGN(MockDelegate);
     52 };
     53 
     54 }  // namespace
     55 
     56 class MessageCenterTrayTest : public testing::Test {
     57  public:
     58   MessageCenterTrayTest() {}
     59   virtual ~MessageCenterTrayTest() {}
     60 
     61   virtual void SetUp() {
     62     MessageCenter::Initialize();
     63     delegate_.reset(new MockDelegate);
     64     message_center_ = MessageCenter::Get();
     65     message_center_tray_.reset(
     66         new MessageCenterTray(delegate_.get(), message_center_));
     67   }
     68 
     69   virtual void TearDown() {
     70     message_center_tray_.reset();
     71     delegate_.reset();
     72     message_center_ = NULL;
     73     MessageCenter::Shutdown();
     74   }
     75 
     76  protected:
     77   NotifierId DummyNotifierId() {
     78     return NotifierId();
     79   }
     80 
     81   void AddNotification(const std::string& id) {
     82     scoped_ptr<Notification> notification(
     83         new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
     84                          id,
     85                          ASCIIToUTF16("Test Web Notification"),
     86                          ASCIIToUTF16("Notification message body."),
     87                          gfx::Image(),
     88                          ASCIIToUTF16("www.test.org"),
     89                          DummyNotifierId(),
     90                          message_center::RichNotificationData(),
     91                          NULL /* delegate */));
     92     message_center_->AddNotification(notification.Pass());
     93   }
     94   scoped_ptr<MockDelegate> delegate_;
     95   scoped_ptr<MessageCenterTray> message_center_tray_;
     96   MessageCenter* message_center_;
     97 
     98  private:
     99   DISALLOW_COPY_AND_ASSIGN(MessageCenterTrayTest);
    100 };
    101 
    102 TEST_F(MessageCenterTrayTest, BasicMessageCenter) {
    103   ASSERT_FALSE(message_center_tray_->popups_visible());
    104   ASSERT_FALSE(message_center_tray_->message_center_visible());
    105 
    106   bool shown = message_center_tray_->ShowMessageCenterBubble();
    107   EXPECT_TRUE(shown);
    108 
    109   ASSERT_FALSE(message_center_tray_->popups_visible());
    110   ASSERT_TRUE(message_center_tray_->message_center_visible());
    111 
    112   message_center_tray_->HideMessageCenterBubble();
    113 
    114   ASSERT_FALSE(message_center_tray_->popups_visible());
    115   ASSERT_FALSE(message_center_tray_->message_center_visible());
    116 
    117   message_center_tray_->ToggleMessageCenterBubble();
    118 
    119   ASSERT_FALSE(message_center_tray_->popups_visible());
    120   ASSERT_TRUE(message_center_tray_->message_center_visible());
    121 
    122   message_center_tray_->ToggleMessageCenterBubble();
    123 
    124   ASSERT_FALSE(message_center_tray_->popups_visible());
    125   ASSERT_FALSE(message_center_tray_->message_center_visible());
    126 }
    127 
    128 TEST_F(MessageCenterTrayTest, BasicPopup) {
    129   ASSERT_FALSE(message_center_tray_->popups_visible());
    130   ASSERT_FALSE(message_center_tray_->message_center_visible());
    131 
    132   message_center_tray_->ShowPopupBubble();
    133 
    134   ASSERT_FALSE(message_center_tray_->popups_visible());
    135   ASSERT_FALSE(message_center_tray_->message_center_visible());
    136 
    137   AddNotification("BasicPopup");
    138 
    139   ASSERT_TRUE(message_center_tray_->popups_visible());
    140   ASSERT_FALSE(message_center_tray_->message_center_visible());
    141 
    142   message_center_tray_->HidePopupBubble();
    143 
    144   ASSERT_FALSE(message_center_tray_->popups_visible());
    145   ASSERT_FALSE(message_center_tray_->message_center_visible());
    146 }
    147 
    148 TEST_F(MessageCenterTrayTest, MessageCenterClosesPopups) {
    149   ASSERT_FALSE(message_center_tray_->popups_visible());
    150   ASSERT_FALSE(message_center_tray_->message_center_visible());
    151 
    152   AddNotification("MessageCenterClosesPopups");
    153 
    154   ASSERT_TRUE(message_center_tray_->popups_visible());
    155   ASSERT_FALSE(message_center_tray_->message_center_visible());
    156 
    157   bool shown = message_center_tray_->ShowMessageCenterBubble();
    158   EXPECT_TRUE(shown);
    159 
    160   ASSERT_FALSE(message_center_tray_->popups_visible());
    161   ASSERT_TRUE(message_center_tray_->message_center_visible());
    162 
    163   // The notification is queued if it's added when message center is visible.
    164   AddNotification("MessageCenterClosesPopups2");
    165 
    166   message_center_tray_->ShowPopupBubble();
    167 
    168   ASSERT_FALSE(message_center_tray_->popups_visible());
    169   ASSERT_TRUE(message_center_tray_->message_center_visible());
    170 
    171   message_center_tray_->HideMessageCenterBubble();
    172 
    173   // The queued notification appears as a popup.
    174   ASSERT_TRUE(message_center_tray_->popups_visible());
    175   ASSERT_FALSE(message_center_tray_->message_center_visible());
    176 
    177   message_center_tray_->ShowMessageCenterBubble();
    178   message_center_tray_->HideMessageCenterBubble();
    179   ASSERT_FALSE(message_center_tray_->popups_visible());
    180   ASSERT_FALSE(message_center_tray_->message_center_visible());
    181 }
    182 
    183 TEST_F(MessageCenterTrayTest, MessageCenterReopenPopupsForSystemPriority) {
    184   ASSERT_FALSE(message_center_tray_->popups_visible());
    185   ASSERT_FALSE(message_center_tray_->message_center_visible());
    186 
    187   scoped_ptr<Notification> notification(
    188       new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
    189                        "MessageCenterReopnPopupsForSystemPriority",
    190                        ASCIIToUTF16("Test Web Notification"),
    191                        ASCIIToUTF16("Notification message body."),
    192                        gfx::Image(),
    193                        ASCIIToUTF16("www.test.org"),
    194                        DummyNotifierId(),
    195                        message_center::RichNotificationData(),
    196                        NULL /* delegate */));
    197   notification->SetSystemPriority();
    198   message_center_->AddNotification(notification.Pass());
    199 
    200   ASSERT_TRUE(message_center_tray_->popups_visible());
    201   ASSERT_FALSE(message_center_tray_->message_center_visible());
    202 
    203   bool shown = message_center_tray_->ShowMessageCenterBubble();
    204   EXPECT_TRUE(shown);
    205 
    206   ASSERT_FALSE(message_center_tray_->popups_visible());
    207   ASSERT_TRUE(message_center_tray_->message_center_visible());
    208 
    209   message_center_tray_->HideMessageCenterBubble();
    210 
    211   ASSERT_TRUE(message_center_tray_->popups_visible());
    212   ASSERT_FALSE(message_center_tray_->message_center_visible());
    213 }
    214 
    215 TEST_F(MessageCenterTrayTest, ShowBubbleFails) {
    216   // Now the delegate will signal that it was unable to show a bubble.
    217   delegate_->show_popups_success_ = false;
    218   delegate_->show_message_center_success_ = false;
    219 
    220   ASSERT_FALSE(message_center_tray_->popups_visible());
    221   ASSERT_FALSE(message_center_tray_->message_center_visible());
    222 
    223   AddNotification("ShowBubbleFails");
    224 
    225   message_center_tray_->ShowPopupBubble();
    226 
    227   ASSERT_FALSE(message_center_tray_->popups_visible());
    228   ASSERT_FALSE(message_center_tray_->message_center_visible());
    229 
    230   bool shown = message_center_tray_->ShowMessageCenterBubble();
    231   EXPECT_FALSE(shown);
    232 
    233   ASSERT_FALSE(message_center_tray_->popups_visible());
    234   ASSERT_FALSE(message_center_tray_->message_center_visible());
    235 
    236   message_center_tray_->HideMessageCenterBubble();
    237 
    238   ASSERT_FALSE(message_center_tray_->popups_visible());
    239   ASSERT_FALSE(message_center_tray_->message_center_visible());
    240 
    241   message_center_tray_->ToggleMessageCenterBubble();
    242 
    243   ASSERT_FALSE(message_center_tray_->popups_visible());
    244   ASSERT_FALSE(message_center_tray_->message_center_visible());
    245 
    246   message_center_tray_->HidePopupBubble();
    247 
    248   ASSERT_FALSE(message_center_tray_->popups_visible());
    249   ASSERT_FALSE(message_center_tray_->message_center_visible());
    250 }
    251 
    252 TEST_F(MessageCenterTrayTest, ContextMenuTest) {
    253   const std::string id1 = "id1";
    254   const std::string id2 = "id2";
    255   const std::string id3 = "id3";
    256   AddNotification(id1);
    257 
    258   base::string16 display_source = ASCIIToUTF16("www.test.org");
    259   NotifierId notifier_id = DummyNotifierId();
    260 
    261   NotifierId notifier_id2(NotifierId::APPLICATION, "sample-app");
    262   scoped_ptr<Notification> notification(
    263       new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
    264                        id2,
    265                        ASCIIToUTF16("Test Web Notification"),
    266                        ASCIIToUTF16("Notification message body."),
    267                        gfx::Image(),
    268                        base::string16() /* empty display source */,
    269                        notifier_id2,
    270                        message_center::RichNotificationData(),
    271                        NULL /* delegate */));
    272   message_center_->AddNotification(notification.Pass());
    273 
    274   AddNotification(id3);
    275 
    276   scoped_ptr<ui::MenuModel> model(
    277       message_center_tray_->CreateNotificationMenuModel(
    278           notifier_id, display_source));
    279   EXPECT_EQ(2, model->GetItemCount());
    280   const int second_command = model->GetCommandIdAt(1);
    281 
    282   // The second item is to open the settings.
    283   EXPECT_TRUE(model->IsEnabledAt(0));
    284   EXPECT_TRUE(model->IsEnabledAt(1));
    285   model->ActivatedAt(1);
    286   EXPECT_TRUE(message_center_tray_->message_center_visible());
    287 
    288   message_center_tray_->HideMessageCenterBubble();
    289 
    290   // The first item is to disable notifications from the notifier id. It also
    291   // removes all notifications from the same notifier, i.e. id1 and id3.
    292   model->ActivatedAt(0);
    293   NotificationList::Notifications notifications =
    294       message_center_->GetVisibleNotifications();
    295   EXPECT_EQ(1u, notifications.size());
    296   EXPECT_EQ(id2, (*notifications.begin())->id());
    297 
    298   // Disables the context menu.
    299   delegate_->enable_context_menu_ = false;
    300 
    301   // id2 doesn't have the display source, so it don't have the menu item for
    302   // disabling notifications.
    303   model = message_center_tray_->CreateNotificationMenuModel(
    304       notifier_id2, base::string16());
    305   EXPECT_EQ(1, model->GetItemCount());
    306   EXPECT_EQ(second_command, model->GetCommandIdAt(0));
    307 
    308   // The command itself is disabled because delegate disables context menu.
    309   EXPECT_FALSE(model->IsEnabledAt(0));
    310 }
    311 
    312 }  // namespace message_center
    313