Skip to content

Commit

Permalink
Do not allow updates to notifications while center is open.
Browse files Browse the repository at this point in the history
This will enable a stable interface while the user is interacting
with the message center.

BUG=246637

Review URL: https://chromiumcodereview.appspot.com/16502003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@204722 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
dewittj@chromium.org committed Jun 7, 2013
1 parent afc6652 commit d560d4d
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,26 @@ void MessageCenterNotificationManager::CancelAll() {

bool MessageCenterNotificationManager::ShowNotification(
const Notification& notification, Profile* profile) {
// There is always space in MessageCenter, it never rejects Notifications.
if (message_center_->IsMessageCenterVisible())
return false;

if (UpdateNotification(notification, profile))
return true;

AddProfileNotification(
new ProfileNotification(profile, notification, message_center_));
return true;
}

bool MessageCenterNotificationManager::UpdateNotification(
const Notification& notification, Profile* profile) {
if (message_center_->IsMessageCenterVisible())
return false;

const string16& replace_id = notification.replace_id();
DCHECK(!replace_id.empty());
if (replace_id.empty())
return false;

const GURL origin_url = notification.origin_url();
DCHECK(origin_url.is_valid());

Expand Down Expand Up @@ -266,6 +276,12 @@ void MessageCenterNotificationManager::OnNotificationRemoved(
RemoveProfileNotification(iter->second, by_user);
}

void MessageCenterNotificationManager::OnNotificationCenterClosed() {
// When the center is open it halts all notifications, so we need to listen
// for events indicating it's been closed.
CheckAndShowNotifications();
}

////////////////////////////////////////////////////////////////////////////////
// ImageDownloads

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class MessageCenterNotificationManager
// MessageCenterObserver
virtual void OnNotificationRemoved(const std::string& notification_id,
bool by_user) OVERRIDE;
virtual void OnNotificationCenterClosed() OVERRIDE;

private:
class ImageDownloadsObserver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <string>

#include "base/command_line.h"
#include "base/message_loop.h"
#include "base/run_loop.h"
#include "base/string_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/utf_string_conversions.h"
Expand All @@ -19,6 +21,34 @@
#include "ui/message_center/message_center_switches.h"
#include "ui/message_center/message_center_util.h"

class TestAddObserver : public message_center::MessageCenterObserver {
public:
TestAddObserver(const std::string& id,
message_center::MessageCenter* message_center)
: id_(id), message_center_(message_center) {
quit_closure_ = run_loop_.QuitClosure();
message_center_->AddObserver(this);
}

virtual ~TestAddObserver() { message_center_->RemoveObserver(this); }

virtual void OnNotificationAdded(const std::string& id) OVERRIDE {
log_ += "_" + id;
if (id == id_)
base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_);
}

void Run() { run_loop_.Run(); }
const std::string log() const { return log_; }

private:
std::string id_;
std::string log_;
message_center::MessageCenter* message_center_;
base::RunLoop run_loop_;
base::Closure quit_closure_;
};

class MessageCenterNotificationsTest : public InProcessBrowserTest {
public:
MessageCenterNotificationsTest() {}
Expand Down Expand Up @@ -199,4 +229,34 @@ IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest,
delegate2->Release();
}

// MessaceCenter-specific test.
#if defined(RUN_MESSAGE_CENTER_TESTS)
#define MAYBE_QueueWhenCenterVisible QueueWhenCenterVisible
#else
#define MAYBE_QueueWhenCenterVisible DISABLED_QueueWhenCenterVisible
#endif

IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest,
MAYBE_QueueWhenCenterVisible) {
EXPECT_TRUE(NotificationUIManager::DelegatesToMessageCenter());
TestAddObserver observer("n2", message_center());

TestDelegate* delegate;
TestDelegate* delegate2;

manager()->Add(CreateTestNotification("n", &delegate), profile());
message_center()->SetMessageCenterVisible(true);
manager()->Add(CreateTestNotification("n2", &delegate2), profile());

EXPECT_EQ("_n", observer.log());

message_center()->SetMessageCenterVisible(false);
observer.Run();

EXPECT_EQ("_n_n2", observer.log());

delegate->Release();
delegate2->Release();
}

#endif // !defined(OS_MACOSX)
4 changes: 4 additions & 0 deletions ui/message_center/fake_message_center.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,8 @@ void FakeMessageCenter::EnterQuietModeWithExpire(
void FakeMessageCenter::SetMessageCenterVisible(bool visible) {
}

bool FakeMessageCenter::IsMessageCenterVisible() {
return false;
}

} // namespace message_center
1 change: 1 addition & 0 deletions ui/message_center/fake_message_center.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class FakeMessageCenter : public MessageCenter {
virtual void EnterQuietModeWithExpire(
const base::TimeDelta& expires_in) OVERRIDE;
virtual void SetMessageCenterVisible(bool visible) OVERRIDE;
virtual bool IsMessageCenterVisible() OVERRIDE;

private:
const NotificationList::Notifications empty_notifications_;
Expand Down
3 changes: 3 additions & 0 deletions ui/message_center/message_center.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ class MESSAGE_CENTER_EXPORT MessageCenter {
// This affects whether or not a message has been "read".
virtual void SetMessageCenterVisible(bool visible) = 0;

// Allows querying the visibility of the center.
virtual bool IsMessageCenterVisible() = 0;

protected:
MessageCenter();
virtual ~MessageCenter();
Expand Down
9 changes: 9 additions & 0 deletions ui/message_center/message_center_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ void MessageCenterImpl::SetMessageCenterVisible(bool visible) {
FOR_EACH_OBSERVER(MessageCenterObserver, observer_list_,
OnNotificationUpdated(*iter));
}

if (!visible) {
FOR_EACH_OBSERVER(
MessageCenterObserver, observer_list_, OnNotificationCenterClosed());
}
}

bool MessageCenterImpl::IsMessageCenterVisible() {
return notification_list_->is_message_center_visible();
}

size_t MessageCenterImpl::NotificationCount() const {
Expand Down
1 change: 1 addition & 0 deletions ui/message_center/message_center_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class MessageCenterImpl : public MessageCenter {
virtual void AddObserver(MessageCenterObserver* observer) OVERRIDE;
virtual void RemoveObserver(MessageCenterObserver* observer) OVERRIDE;
virtual void SetMessageCenterVisible(bool visible) OVERRIDE;
virtual bool IsMessageCenterVisible() OVERRIDE;
virtual size_t NotificationCount() const OVERRIDE;
virtual size_t UnreadNotificationCount() const OVERRIDE;
virtual bool HasPopupNotifications() const OVERRIDE;
Expand Down
4 changes: 4 additions & 0 deletions ui/message_center/message_center_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class MESSAGE_CENTER_EXPORT MessageCenterObserver {
// Called when the notification associated with |notification_id| is actually
// displayed.
virtual void OnNotificationDisplayed(const std::string& notification_id) {}

// Called when the notification list is no longer being displayed as a
// notification center.
virtual void OnNotificationCenterClosed() {}
};

} // namespace message_center
Expand Down
1 change: 1 addition & 0 deletions ui/message_center/notification_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class MESSAGE_CENTER_EXPORT NotificationList {
const Notifications& GetNotifications();
size_t NotificationCount() const;
size_t unread_count() const { return unread_count_; }
bool is_message_center_visible() const { return message_center_visible_; }

private:
friend class test::NotificationListTest;
Expand Down

0 comments on commit d560d4d

Please sign in to comment.