1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ui/message_center/message_center_impl.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/bind.h"
8ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "base/message_loop/message_loop.h"
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/run_loop.h"
107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ui/gfx/canvas.h"
134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ui/gfx/size.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ui/message_center/message_center.h"
15d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/message_center/message_center_types.h"
1658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "ui/message_center/notification_blocker.h"
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ui/message_center/notification_types.h"
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "ui/message_center/notifier_settings.h"
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using base::UTF8ToUTF16;
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace message_center {
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace {
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class MessageCenterImplTest : public testing::Test,
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                              public MessageCenterObserver {
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  MessageCenterImplTest() {}
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void SetUp() OVERRIDE {
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    MessageCenter::Initialize();
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    message_center_ = MessageCenter::Get();
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    loop_.reset(new base::MessageLoop);
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    run_loop_.reset(new base::RunLoop());
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    closure_ = run_loop_->QuitClosure();
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void TearDown() OVERRIDE {
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    run_loop_.reset();
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    loop_.reset();
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    message_center_ = NULL;
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    MessageCenter::Shutdown();
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  MessageCenter* message_center() const { return message_center_; }
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NotifierSettingsObserver* notifier_settings_observer() const {
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return static_cast<NotifierSettingsObserver*>(message_center_impl());
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  MessageCenterImpl* message_center_impl() const {
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return reinterpret_cast<MessageCenterImpl*>(message_center_);
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::RunLoop* run_loop() const { return run_loop_.get(); }
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::Closure closure() const { return closure_; }
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) protected:
5768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  Notification* CreateSimpleNotification(const std::string& id) {
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return CreateNotificationWithNotifierId(
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        id,
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        "app1",
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        NOTIFICATION_TYPE_SIMPLE);
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  Notification* CreateSimpleNotificationWithNotifierId(
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const std::string& id, const std::string& notifier_id) {
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return CreateNotificationWithNotifierId(
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        id,
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        notifier_id,
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        NOTIFICATION_TYPE_SIMPLE);
7068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
7168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
7268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  Notification* CreateNotification(const std::string& id,
7368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                                   message_center::NotificationType type) {
74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return CreateNotificationWithNotifierId(id, "app1", type);
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  Notification* CreateNotificationWithNotifierId(
78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const std::string& id,
79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const std::string& notifier_id,
80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      message_center::NotificationType type) {
814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    RichNotificationData optional_fields;
824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    optional_fields.buttons.push_back(ButtonInfo(UTF8ToUTF16("foo")));
834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    optional_fields.buttons.push_back(ButtonInfo(UTF8ToUTF16("foo")));
8468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    return new Notification(type,
8568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                            id,
8668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                            UTF8ToUTF16("title"),
8768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                            UTF8ToUTF16(id),
8868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                            gfx::Image() /* icon */,
8968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                            base::string16() /* display_source */,
90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                            NotifierId(NotifierId::APPLICATION, notifier_id),
914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                            optional_fields,
9268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                            NULL);
9368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
9468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  MessageCenter* message_center_;
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<base::MessageLoop> loop_;
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<base::RunLoop> run_loop_;
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::Closure closure_;
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(MessageCenterImplTest);
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
10558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)class ToggledNotificationBlocker : public NotificationBlocker {
10658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) public:
10758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  explicit ToggledNotificationBlocker(MessageCenter* message_center)
10858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      : NotificationBlocker(message_center),
10958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        notifications_enabled_(true) {}
11058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual ~ToggledNotificationBlocker() {}
11158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
11258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void SetNotificationsEnabled(bool enabled) {
11358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (notifications_enabled_ != enabled) {
11458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      notifications_enabled_ = enabled;
115a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      NotifyBlockingStateChanged();
11658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
11758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
11858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
11958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // NotificationBlocker overrides:
12058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual  bool ShouldShowNotificationAsPopup(
12158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const message_center::NotifierId& notifier_id) const OVERRIDE {
12258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return notifications_enabled_;
12358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
12458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
12558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) private:
12658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool notifications_enabled_;
12758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
12858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ToggledNotificationBlocker);
12958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)};
13058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
13158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)class PopupNotificationBlocker : public ToggledNotificationBlocker {
13258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) public:
13358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  PopupNotificationBlocker(MessageCenter* message_center,
13458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                           const NotifierId& allowed_notifier)
13558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      : ToggledNotificationBlocker(message_center),
13658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        allowed_notifier_(allowed_notifier) {}
13758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual ~PopupNotificationBlocker() {}
13858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
13958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // NotificationBlocker overrides:
14058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual bool ShouldShowNotificationAsPopup(
14158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const NotifierId& notifier_id) const OVERRIDE {
14258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return (notifier_id == allowed_notifier_) ||
14358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        ToggledNotificationBlocker::ShouldShowNotificationAsPopup(notifier_id);
14458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
14558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
14658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) private:
14758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  NotifierId allowed_notifier_;
14858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
14958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PopupNotificationBlocker);
15058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)};
15158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
152a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)class TotalNotificationBlocker : public PopupNotificationBlocker {
153a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) public:
154a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  TotalNotificationBlocker(MessageCenter* message_center,
155a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                           const NotifierId& allowed_notifier)
156a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      : PopupNotificationBlocker(message_center, allowed_notifier) {}
157a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual ~TotalNotificationBlocker() {}
158a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
159a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // NotificationBlocker overrides:
160a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual bool ShouldShowNotification(
161a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      const NotifierId& notifier_id) const OVERRIDE {
162a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return ShouldShowNotificationAsPopup(notifier_id);
163a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
164a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
165a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) private:
166a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TotalNotificationBlocker);
167a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)};
168a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
16958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool PopupNotificationsContain(
17058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const NotificationList::PopupNotifications& popups,
17158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const std::string& id) {
17258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  for (NotificationList::PopupNotifications::const_iterator iter =
17358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)           popups.begin(); iter != popups.end(); ++iter) {
17458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if ((*iter)->id() == id)
17558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      return true;
17658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
17758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return false;
17858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
17958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
180a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Right now, MessageCenter::HasNotification() returns regardless of blockers.
181a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)bool NotificationsContain(
182a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const NotificationList::Notifications& notifications,
183a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const std::string& id) {
184a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  for (NotificationList::Notifications::const_iterator iter =
185a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)           notifications.begin(); iter != notifications.end(); ++iter) {
186a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if ((*iter)->id() == id)
187a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      return true;
188a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
189a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return false;
190a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
191a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
192868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace
193868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
194868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace internal {
195868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
196868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class MockPopupTimersController : public PopupTimersController {
197868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
198868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  MockPopupTimersController(MessageCenter* message_center,
199868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                            base::Closure quit_closure)
200868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      : PopupTimersController(message_center),
201868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        timer_finished_(false),
202868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        quit_closure_(quit_closure) {}
203868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual ~MockPopupTimersController() {}
204868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
205868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void TimerFinished(const std::string& id) OVERRIDE {
206868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_);
207868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    timer_finished_ = true;
208868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    last_id_ = id;
209868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
210868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
211868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool timer_finished() const { return timer_finished_; }
212868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const std::string& last_id() const { return last_id_; }
213868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
214868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
215868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool timer_finished_;
216868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::string last_id_;
217868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::Closure quit_closure_;
218868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
219868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
220868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(MessageCenterImplTest, PopupTimersEmptyController) {
221868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<PopupTimersController> popup_timers_controller =
222868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      make_scoped_ptr(new PopupTimersController(message_center()));
223868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
224868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Test that all functions succed without any timers created.
225868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->PauseAll();
226868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->StartAll();
227868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->CancelAll();
228868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->TimerFinished("unknown");
229868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->PauseTimer("unknown");
230868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->CancelTimer("unknown");
231868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
232868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
233868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(MessageCenterImplTest, PopupTimersControllerStartTimer) {
234868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<MockPopupTimersController> popup_timers_controller =
235868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      make_scoped_ptr(
236868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new MockPopupTimersController(message_center(), closure()));
237868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->StartTimer("test",
238868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      base::TimeDelta::FromMilliseconds(1));
239868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  run_loop()->Run();
240868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(popup_timers_controller->timer_finished());
241868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
242868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
243868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(MessageCenterImplTest, PopupTimersControllerPauseTimer) {
244868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<MockPopupTimersController> popup_timers_controller =
245868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      make_scoped_ptr(
246868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new MockPopupTimersController(message_center(), closure()));
247868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->StartTimer("test",
248868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      base::TimeDelta::FromMilliseconds(1));
249868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->PauseTimer("test");
250868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  run_loop()->RunUntilIdle();
251868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
252868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(popup_timers_controller->timer_finished());
253868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
254868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
255868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(MessageCenterImplTest, PopupTimersControllerCancelTimer) {
256868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<MockPopupTimersController> popup_timers_controller =
257868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      make_scoped_ptr(
258868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new MockPopupTimersController(message_center(), closure()));
259868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->StartTimer("test",
260868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      base::TimeDelta::FromMilliseconds(1));
261868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->CancelTimer("test");
262868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  run_loop()->RunUntilIdle();
263868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
264868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(popup_timers_controller->timer_finished());
265868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
266868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
267868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(MessageCenterImplTest, PopupTimersControllerPauseAllTimers) {
268868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<MockPopupTimersController> popup_timers_controller =
269868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      make_scoped_ptr(
270868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new MockPopupTimersController(message_center(), closure()));
271868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->StartTimer("test",
272868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      base::TimeDelta::FromMilliseconds(1));
273868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->PauseAll();
274868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  run_loop()->RunUntilIdle();
275868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
276868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(popup_timers_controller->timer_finished());
277868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
278868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
279868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(MessageCenterImplTest, PopupTimersControllerStartAllTimers) {
280868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<MockPopupTimersController> popup_timers_controller =
281868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      make_scoped_ptr(
282868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new MockPopupTimersController(message_center(), closure()));
283868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->StartTimer("test",
284868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      base::TimeDelta::FromMilliseconds(1));
285868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->PauseAll();
286868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->StartAll();
287868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  run_loop()->Run();
288868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
289868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(popup_timers_controller->timer_finished());
290868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
291868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
292868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(MessageCenterImplTest, PopupTimersControllerStartMultipleTimers) {
293868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<MockPopupTimersController> popup_timers_controller =
294868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      make_scoped_ptr(
295868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new MockPopupTimersController(message_center(), closure()));
296868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->StartTimer("test",
297868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      base::TimeDelta::FromMilliseconds(5));
298868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->StartTimer("test2",
299868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      base::TimeDelta::FromMilliseconds(1));
300868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->StartTimer("test3",
301868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      base::TimeDelta::FromMilliseconds(3));
302868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->PauseAll();
303868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->StartAll();
304868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  run_loop()->Run();
305868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
306868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(popup_timers_controller->last_id(), "test2");
307868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(popup_timers_controller->timer_finished());
308868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
309868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
310868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(MessageCenterImplTest, PopupTimersControllerStartMultipleTimersPause) {
311868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<MockPopupTimersController> popup_timers_controller =
312868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      make_scoped_ptr(
313868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new MockPopupTimersController(message_center(), closure()));
314868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->StartTimer("test",
315868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      base::TimeDelta::FromMilliseconds(5));
316868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->StartTimer("test2",
317868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      base::TimeDelta::FromMilliseconds(1));
318868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->StartTimer("test3",
319868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      base::TimeDelta::FromMilliseconds(3));
320868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->PauseTimer("test2");
321868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
322868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  run_loop()->Run();
323868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
324868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(popup_timers_controller->last_id(), "test3");
325868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(popup_timers_controller->timer_finished());
326868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
327868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
328868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(MessageCenterImplTest, PopupTimersControllerResetTimer) {
329868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<MockPopupTimersController> popup_timers_controller =
330868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      make_scoped_ptr(
331868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new MockPopupTimersController(message_center(), closure()));
332868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->StartTimer("test",
333868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      base::TimeDelta::FromMilliseconds(5));
334868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->StartTimer("test2",
335868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      base::TimeDelta::FromMilliseconds(1));
336868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->StartTimer("test3",
337868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      base::TimeDelta::FromMilliseconds(3));
338868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->PauseTimer("test2");
339868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  popup_timers_controller->ResetTimer("test",
340868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                      base::TimeDelta::FromMilliseconds(2));
341868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
342868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  run_loop()->Run();
343868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
344868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(popup_timers_controller->last_id(), "test");
345868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(popup_timers_controller->timer_finished());
346868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
347868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
34858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(MessageCenterImplTest, NotificationBlocker) {
34958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  NotifierId notifier_id(NotifierId::APPLICATION, "app1");
35058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Multiple blockers to verify the case that one blocker blocks but another
35158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // doesn't.
35258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ToggledNotificationBlocker blocker1(message_center());
35358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ToggledNotificationBlocker blocker2(message_center());
35458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
35558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
35658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NOTIFICATION_TYPE_SIMPLE,
35758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "id1",
35858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      UTF8ToUTF16("title"),
35958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      UTF8ToUTF16("message"),
36058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      gfx::Image() /* icon */,
36158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::string16() /* display_source */,
36258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      notifier_id,
36358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      RichNotificationData(),
36458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NULL)));
36558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
36658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NOTIFICATION_TYPE_SIMPLE,
36758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "id2",
36858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      UTF8ToUTF16("title"),
36958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      UTF8ToUTF16("message"),
37058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      gfx::Image() /* icon */,
37158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::string16() /* display_source */,
37258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      notifier_id,
37358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      RichNotificationData(),
37458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NULL)));
37558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_EQ(2u, message_center()->GetPopupNotifications().size());
37668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
37758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
37858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Block all notifications. All popups are gone and message center should be
37958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // hidden.
38058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blocker1.SetNotificationsEnabled(false);
38158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
38268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
38358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
38458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Updates |blocker2| state, which doesn't affect the global state.
38558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blocker2.SetNotificationsEnabled(false);
38658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
38768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
38858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
38958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blocker2.SetNotificationsEnabled(true);
39058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
39168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
39258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
39358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // If |blocker2| blocks, then unblocking blocker1 doesn't change the global
39458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // state.
39558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blocker2.SetNotificationsEnabled(false);
39658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blocker1.SetNotificationsEnabled(true);
39758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
39868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
39958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
40058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Unblock both blockers, which recovers the global state, but the popups
40158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // aren't shown.
40258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blocker2.SetNotificationsEnabled(true);
40358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
40468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
40558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
40658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
40758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(MessageCenterImplTest, NotificationsDuringBlocked) {
40858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  NotifierId notifier_id(NotifierId::APPLICATION, "app1");
40958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ToggledNotificationBlocker blocker(message_center());
41058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
41158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
41258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NOTIFICATION_TYPE_SIMPLE,
41358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "id1",
41458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      UTF8ToUTF16("title"),
41558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      UTF8ToUTF16("message"),
41658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      gfx::Image() /* icon */,
41758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::string16() /* display_source */,
41858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      notifier_id,
41958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      RichNotificationData(),
42058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NULL)));
42158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_EQ(1u, message_center()->GetPopupNotifications().size());
42268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  EXPECT_EQ(1u, message_center()->GetVisibleNotifications().size());
42358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
42458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Create a notification during blocked. Still no popups.
42558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blocker.SetNotificationsEnabled(false);
42658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
42758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NOTIFICATION_TYPE_SIMPLE,
42858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "id2",
42958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      UTF8ToUTF16("title"),
43058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      UTF8ToUTF16("message"),
43158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      gfx::Image() /* icon */,
43258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::string16() /* display_source */,
43358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      notifier_id,
43458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      RichNotificationData(),
43558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NULL)));
43658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
43768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
43858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
43958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Unblock notifications, the id1 should appear as a popup.
44058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blocker.SetNotificationsEnabled(true);
44158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  NotificationList::PopupNotifications popups =
44258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      message_center()->GetPopupNotifications();
44358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_EQ(1u, popups.size());
44458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(PopupNotificationsContain(popups, "id2"));
44568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
44658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
447868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
44858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Similar to other blocker cases but this test case allows |notifier_id2| even
44958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// in blocked.
45058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(MessageCenterImplTest, NotificationBlockerAllowsPopups) {
45158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  NotifierId notifier_id1(NotifierId::APPLICATION, "app1");
45258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  NotifierId notifier_id2(NotifierId::APPLICATION, "app2");
45358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  PopupNotificationBlocker blocker(message_center(), notifier_id2);
45458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
45558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
45658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NOTIFICATION_TYPE_SIMPLE,
45758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "id1",
45858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      UTF8ToUTF16("title"),
45958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      UTF8ToUTF16("message"),
46058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      gfx::Image() /* icon */,
46158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::string16() /* display_source */,
46258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      notifier_id1,
46358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      RichNotificationData(),
46458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NULL)));
46558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
46658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NOTIFICATION_TYPE_SIMPLE,
46758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "id2",
46858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      UTF8ToUTF16("title"),
46958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      UTF8ToUTF16("message"),
47058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      gfx::Image() /* icon */,
47158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::string16() /* display_source */,
47258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      notifier_id2,
47358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      RichNotificationData(),
47458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NULL)));
47558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
47658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // "id1" is closed but "id2" is still visible as a popup.
47758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blocker.SetNotificationsEnabled(false);
47858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  NotificationList::PopupNotifications popups =
47958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      message_center()->GetPopupNotifications();
48058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_EQ(1u, popups.size());
48158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(PopupNotificationsContain(popups, "id2"));
48268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size());
48358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
48458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
48558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NOTIFICATION_TYPE_SIMPLE,
48658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "id3",
48758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      UTF8ToUTF16("title"),
48858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      UTF8ToUTF16("message"),
48958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      gfx::Image() /* icon */,
49058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::string16() /* display_source */,
49158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      notifier_id1,
49258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      RichNotificationData(),
49358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NULL)));
49458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
49558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NOTIFICATION_TYPE_SIMPLE,
49658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "id4",
49758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      UTF8ToUTF16("title"),
49858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      UTF8ToUTF16("message"),
49958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      gfx::Image() /* icon */,
50058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      base::string16() /* display_source */,
50158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      notifier_id2,
50258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      RichNotificationData(),
50358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NULL)));
50458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  popups = message_center()->GetPopupNotifications();
50558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_EQ(2u, popups.size());
50658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(PopupNotificationsContain(popups, "id2"));
50758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(PopupNotificationsContain(popups, "id4"));
50868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  EXPECT_EQ(4u, message_center()->GetVisibleNotifications().size());
50958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
51058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blocker.SetNotificationsEnabled(true);
51158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  popups = message_center()->GetPopupNotifications();
51258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_EQ(3u, popups.size());
51358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(PopupNotificationsContain(popups, "id2"));
51458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(PopupNotificationsContain(popups, "id3"));
51558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(PopupNotificationsContain(popups, "id4"));
51668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  EXPECT_EQ(4u, message_center()->GetVisibleNotifications().size());
51758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
51858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
519a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// TotalNotificationBlocker suppresses showing notifications even from the list.
520a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// This would provide the feature to 'separated' message centers per-profile for
521a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// ChromeOS multi-login.
522a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)TEST_F(MessageCenterImplTest, TotalNotificationBlocker) {
523a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  NotifierId notifier_id1(NotifierId::APPLICATION, "app1");
524a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  NotifierId notifier_id2(NotifierId::APPLICATION, "app2");
525a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  TotalNotificationBlocker blocker(message_center(), notifier_id2);
526a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
527a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
528a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      NOTIFICATION_TYPE_SIMPLE,
529a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "id1",
530a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      UTF8ToUTF16("title"),
531a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      UTF8ToUTF16("message"),
532a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      gfx::Image() /* icon */,
533a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      base::string16() /* display_source */,
534a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      notifier_id1,
535a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      RichNotificationData(),
536a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      NULL)));
537a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
538a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      NOTIFICATION_TYPE_SIMPLE,
539a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "id2",
540a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      UTF8ToUTF16("title"),
541a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      UTF8ToUTF16("message"),
542a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      gfx::Image() /* icon */,
543a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      base::string16() /* display_source */,
544a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      notifier_id2,
545a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      RichNotificationData(),
546a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      NULL)));
547a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
548a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // "id1" becomes invisible while "id2" is still visible.
549a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  blocker.SetNotificationsEnabled(false);
550a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_EQ(1u, message_center()->NotificationCount());
551a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  NotificationList::Notifications notifications =
552a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      message_center()->GetVisibleNotifications();
553a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_FALSE(NotificationsContain(notifications, "id1"));
554a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_TRUE(NotificationsContain(notifications, "id2"));
555a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
556a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
557a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      NOTIFICATION_TYPE_SIMPLE,
558a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "id3",
559a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      UTF8ToUTF16("title"),
560a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      UTF8ToUTF16("message"),
561a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      gfx::Image() /* icon */,
562a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      base::string16() /* display_source */,
563a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      notifier_id1,
564a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      RichNotificationData(),
565a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      NULL)));
566a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
567a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      NOTIFICATION_TYPE_SIMPLE,
568a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      "id4",
569a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      UTF8ToUTF16("title"),
570a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      UTF8ToUTF16("message"),
571a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      gfx::Image() /* icon */,
572a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      base::string16() /* display_source */,
573a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      notifier_id2,
574a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      RichNotificationData(),
575a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      NULL)));
576a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_EQ(2u, message_center()->NotificationCount());
577a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  notifications = message_center()->GetVisibleNotifications();
578a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_FALSE(NotificationsContain(notifications, "id1"));
579a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_TRUE(NotificationsContain(notifications, "id2"));
580a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_FALSE(NotificationsContain(notifications, "id3"));
581a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_TRUE(NotificationsContain(notifications, "id4"));
582a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
583a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  blocker.SetNotificationsEnabled(true);
584a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_EQ(4u, message_center()->NotificationCount());
585a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  notifications = message_center()->GetVisibleNotifications();
586a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_TRUE(NotificationsContain(notifications, "id1"));
587a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_TRUE(NotificationsContain(notifications, "id2"));
588a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_TRUE(NotificationsContain(notifications, "id3"));
589a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_TRUE(NotificationsContain(notifications, "id4"));
590a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
591a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // RemoveAllVisibleNotifications should remove just visible notifications.
592a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  blocker.SetNotificationsEnabled(false);
593a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  message_center()->RemoveAllVisibleNotifications(false /* by_user */);
594a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_EQ(0u, message_center()->NotificationCount());
595a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  blocker.SetNotificationsEnabled(true);
596a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_EQ(2u, message_center()->NotificationCount());
597a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  notifications = message_center()->GetVisibleNotifications();
598a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_TRUE(NotificationsContain(notifications, "id1"));
599a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_FALSE(NotificationsContain(notifications, "id2"));
600a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_TRUE(NotificationsContain(notifications, "id3"));
601a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_FALSE(NotificationsContain(notifications, "id4"));
602a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
603a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // And RemoveAllNotifications should remove all.
604a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  blocker.SetNotificationsEnabled(false);
605a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  message_center()->RemoveAllNotifications(false /* by_user */);
606a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_EQ(0u, message_center()->NotificationCount());
607a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
608a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
609d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(MessageCenterImplTest, QueueUpdatesWithCenterVisible) {
610d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  std::string id("id1");
611d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  std::string id2("id2");
612d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  NotifierId notifier_id1(NotifierId::APPLICATION, "app1");
613d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
61468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // First, add and update a notification to ensure updates happen
61568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // normally.
61668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<Notification> notification(CreateSimpleNotification(id));
617d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  message_center()->AddNotification(notification.Pass());
61868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  notification.reset(CreateSimpleNotification(id2));
619d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  message_center()->UpdateNotification(id, notification.Pass());
620f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(message_center()->FindVisibleNotificationById(id2));
621f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_FALSE(message_center()->FindVisibleNotificationById(id));
62268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
62368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Then open the message center.
624d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER);
62568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
62668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Then update a notification; nothing should have happened.
62768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  notification.reset(CreateSimpleNotification(id));
628d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  message_center()->UpdateNotification(id2, notification.Pass());
629f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(message_center()->FindVisibleNotificationById(id2));
630f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_FALSE(message_center()->FindVisibleNotificationById(id));
63168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
63268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Close the message center; then the update should have propagated.
633d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  message_center()->SetVisibility(VISIBILITY_TRANSIENT);
634f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_FALSE(message_center()->FindVisibleNotificationById(id2));
635f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(message_center()->FindVisibleNotificationById(id));
636d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
637d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
63868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)TEST_F(MessageCenterImplTest, ComplexQueueing) {
63968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  std::string ids[5] = {"0", "1", "2", "3", "4p"};
64068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  NotifierId notifier_id1(NotifierId::APPLICATION, "app1");
64168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
64268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<Notification> notification;
64368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Add some notifications
64468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int i = 0;
64568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  for (; i < 3; i++) {
64668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    notification.reset(CreateSimpleNotification(ids[i]));
64768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    message_center()->AddNotification(notification.Pass());
64868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
64968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  for (i = 0; i < 3; i++) {
650f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    EXPECT_TRUE(message_center()->FindVisibleNotificationById(ids[i]));
65168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
65268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  for (; i < 5; i++) {
653f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    EXPECT_FALSE(message_center()->FindVisibleNotificationById(ids[i]));
65468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
65568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
65668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  notification.reset(CreateNotification(ids[4], NOTIFICATION_TYPE_PROGRESS));
65768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  message_center()->AddNotification(notification.Pass());
65868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
65968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Now start queueing.
66068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // NL: ["0", "1", "2", "4p"]
66168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER);
66268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
66368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // This should update notification "1" to have id "3".
66468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  notification.reset(CreateSimpleNotification(ids[3]));
66568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  message_center()->UpdateNotification(ids[1], notification.Pass());
66668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
66768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  notification.reset(CreateSimpleNotification(ids[4]));
66868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  message_center()->UpdateNotification(ids[4], notification.Pass());
66968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
67068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  notification.reset(CreateNotification(ids[4], NOTIFICATION_TYPE_PROGRESS));
67168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  message_center()->UpdateNotification(ids[4], notification.Pass());
67268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
67368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // This should update notification "3" to a new ID after we go TRANSIENT.
67468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  notification.reset(CreateSimpleNotification("New id"));
67568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  message_center()->UpdateNotification(ids[3], notification.Pass());
67668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
67768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // This should create a new "3", that doesn't overwrite the update to 3
67868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // before.
67968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  notification.reset(CreateSimpleNotification(ids[3]));
68068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  message_center()->AddNotification(notification.Pass());
68168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
68268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // The NL should still be the same: ["0", "1", "2", "4p"]
683f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(message_center()->FindVisibleNotificationById(ids[0]));
684f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(message_center()->FindVisibleNotificationById(ids[1]));
685f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(message_center()->FindVisibleNotificationById(ids[2]));
686f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_FALSE(message_center()->FindVisibleNotificationById(ids[3]));
687f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(message_center()->FindVisibleNotificationById(ids[4]));
68868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  EXPECT_EQ(message_center()->GetVisibleNotifications().size(), 4u);
68968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  message_center()->SetVisibility(VISIBILITY_TRANSIENT);
69068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
691f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(message_center()->FindVisibleNotificationById(ids[0]));
692f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_FALSE(message_center()->FindVisibleNotificationById(ids[1]));
693f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(message_center()->FindVisibleNotificationById(ids[2]));
694f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(message_center()->FindVisibleNotificationById(ids[3]));
695f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(message_center()->FindVisibleNotificationById(ids[4]));
696f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(message_center()->FindVisibleNotificationById("New id"));
69768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  EXPECT_EQ(message_center()->GetVisibleNotifications().size(), 5u);
69868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
69968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
7004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)TEST_F(MessageCenterImplTest, QueuedDirectUpdates) {
7014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::string id("id1");
7024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::string id2("id2");
7034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  NotifierId notifier_id1(NotifierId::APPLICATION, "app1");
7044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
7054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  gfx::Size original_size(0, 0);
7064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Open the message center to prevent adding notifications
7074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER);
7084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
7094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Create new notification to be added to the queue; images all have the same
7104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // original size.
7114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  scoped_ptr<Notification> notification(CreateSimpleNotification(id));
7124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
7134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Double-check that sizes all match.
7144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const std::vector<ButtonInfo>& original_buttons = notification->buttons();
7154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ASSERT_EQ(2u, original_buttons.size());
7164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
7174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ(original_size, notification->icon().Size());
7184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ(original_size, notification->image().Size());
7194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ(original_size, original_buttons[0].icon.Size());
7204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ(original_size, original_buttons[1].icon.Size());
7214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
7224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  message_center()->AddNotification(notification.Pass());
7234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
7244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // The notification should be in the queue.
725f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_FALSE(message_center()->FindVisibleNotificationById(id));
7264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
7274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Now try setting the icon to a different size.
7284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  gfx::Size new_size(16, 16);
7294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_NE(original_size, new_size);
7304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
7314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  gfx::Canvas canvas(new_size, 1.0f, true);
7324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  canvas.DrawColor(SK_ColorBLUE);
7334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  gfx::Image testImage(gfx::Image(gfx::ImageSkia(canvas.ExtractImageRep())));
7344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  message_center()->SetNotificationIcon(id, testImage);
7354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  message_center()->SetNotificationImage(id, testImage);
7364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  message_center()->SetNotificationButtonIcon(id, 0, testImage);
7374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  message_center()->SetNotificationButtonIcon(id, 1, testImage);
7384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
7394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // The notification should be in the queue.
740f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_FALSE(message_center()->FindVisibleNotificationById(id));
7414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
7424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Close the message center; then the update should have propagated.
7434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  message_center()->SetVisibility(VISIBILITY_TRANSIENT);
7444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // The notification should no longer be in the queue.
745f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(message_center()->FindVisibleNotificationById(id));
7464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
7474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  Notification* mc_notification =
7484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      *(message_center()->GetVisibleNotifications().begin());
7494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const std::vector<ButtonInfo>& buttons = mc_notification->buttons();
7504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ASSERT_EQ(2u, buttons.size());
7514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
7524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ(new_size, mc_notification->icon().Size());
7534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ(new_size, mc_notification->image().Size());
7544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ(new_size, buttons[0].icon.Size());
7554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ(new_size, buttons[1].icon.Size());
7564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
7574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
7585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(MessageCenterImplTest, CachedUnreadCount) {
7595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  message_center()->AddNotification(
7605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<Notification>(CreateSimpleNotification("id1")));
7615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  message_center()->AddNotification(
7625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<Notification>(CreateSimpleNotification("id2")));
7635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  message_center()->AddNotification(
7645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<Notification>(CreateSimpleNotification("id3")));
7655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(3u, message_center()->UnreadNotificationCount());
7665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Mark 'displayed' on all notifications by using for-loop. This shouldn't
7685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // recreate |notifications| inside of the loop.
7695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const NotificationList::Notifications& notifications =
7705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      message_center()->GetVisibleNotifications();
7715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (NotificationList::Notifications::const_iterator iter =
7725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)           notifications.begin(); iter != notifications.end(); ++iter) {
773010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    message_center()->DisplayedNotification(
774010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        (*iter)->id(), message_center::DISPLAY_SOURCE_MESSAGE_CENTER);
7755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
7765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(0u, message_center()->UnreadNotificationCount());
7775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Imitate the timeout, which recovers the unread count. Again, this shouldn't
7795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // recreate |notifications| inside of the loop.
7805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (NotificationList::Notifications::const_iterator iter =
7815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)           notifications.begin(); iter != notifications.end(); ++iter) {
7825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    message_center()->MarkSinglePopupAsShown((*iter)->id(), false);
7835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
7845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(3u, message_center()->UnreadNotificationCount());
7855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Opening the message center will reset the unread count.
7875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER);
7885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(0u, message_center()->UnreadNotificationCount());
7895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
7905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
791cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(MessageCenterImplTest, DisableNotificationsByNotifier) {
792cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_EQ(0u, message_center()->NotificationCount());
793cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message_center()->AddNotification(
794cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      scoped_ptr<Notification>(
795cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          CreateSimpleNotificationWithNotifierId("id1-1", "app1")));
796cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message_center()->AddNotification(
797cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      scoped_ptr<Notification>(
798cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          CreateSimpleNotificationWithNotifierId("id1-2", "app1")));
799cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message_center()->AddNotification(
800cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      scoped_ptr<Notification>(
801cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          CreateSimpleNotificationWithNotifierId("id2-1", "app2")));
802cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message_center()->AddNotification(
803cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      scoped_ptr<Notification>(
804cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          CreateSimpleNotificationWithNotifierId("id2-2", "app2")));
805cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message_center()->AddNotification(
806cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      scoped_ptr<Notification>(
807cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          CreateSimpleNotificationWithNotifierId("id2-3", "app2")));
808cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_EQ(5u, message_center()->NotificationCount());
809cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
810cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Removing all of app1's notifications should only leave app2's.
811cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message_center()->DisableNotificationsByNotifier(
812cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      NotifierId(NotifierId::APPLICATION, "app1"));
813cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_EQ(3u, message_center()->NotificationCount());
814cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
815cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Now we remove the remaining notifications.
816cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message_center()->DisableNotificationsByNotifier(
817cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      NotifierId(NotifierId::APPLICATION, "app2"));
818cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_EQ(0u, message_center()->NotificationCount());
819cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
820cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
821cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(MessageCenterImplTest, NotifierEnabledChanged) {
822cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_EQ(0u, message_center()->NotificationCount());
823cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message_center()->AddNotification(
824cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      scoped_ptr<Notification>(
825cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          CreateSimpleNotificationWithNotifierId("id1-1", "app1")));
826cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message_center()->AddNotification(
827cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      scoped_ptr<Notification>(
828cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          CreateSimpleNotificationWithNotifierId("id1-2", "app1")));
829cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message_center()->AddNotification(
830cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      scoped_ptr<Notification>(
831cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          CreateSimpleNotificationWithNotifierId("id1-3", "app1")));
832cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message_center()->AddNotification(
833cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      scoped_ptr<Notification>(
834cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          CreateSimpleNotificationWithNotifierId("id2-1", "app2")));
835cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message_center()->AddNotification(
836cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      scoped_ptr<Notification>(
837cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          CreateSimpleNotificationWithNotifierId("id2-2", "app2")));
838cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message_center()->AddNotification(
839cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      scoped_ptr<Notification>(
840cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          CreateSimpleNotificationWithNotifierId("id2-3", "app2")));
841cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message_center()->AddNotification(
842cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      scoped_ptr<Notification>(
843cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          CreateSimpleNotificationWithNotifierId("id2-4", "app2")));
844cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message_center()->AddNotification(
845cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      scoped_ptr<Notification>(
846cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          CreateSimpleNotificationWithNotifierId("id2-5", "app2")));
847cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_EQ(8u, message_center()->NotificationCount());
848cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
849cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Enabling an extension should have no effect on the count.
850cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  notifier_settings_observer()->NotifierEnabledChanged(
851cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      NotifierId(NotifierId::APPLICATION, "app1"), true);
852cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_EQ(8u, message_center()->NotificationCount());
853cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
854cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Removing all of app2's notifications should only leave app1's.
855cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  notifier_settings_observer()->NotifierEnabledChanged(
856cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      NotifierId(NotifierId::APPLICATION, "app2"), false);
857cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_EQ(3u, message_center()->NotificationCount());
858cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
859cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Removal operations should be idempotent.
860cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  notifier_settings_observer()->NotifierEnabledChanged(
861cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      NotifierId(NotifierId::APPLICATION, "app2"), false);
862cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_EQ(3u, message_center()->NotificationCount());
863cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
864cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Now we remove the remaining notifications.
865cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  notifier_settings_observer()->NotifierEnabledChanged(
866cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      NotifierId(NotifierId::APPLICATION, "app1"), false);
867cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_EQ(0u, message_center()->NotificationCount());
868cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
869cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
87058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}  // namespace internal
871868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace message_center
872